Skip to content

Commit d85b583

Browse files
committed
[1.1] seccomp: patchbpf: rename nativeArch -> linuxAuditArch
(This is a backport of b288abe.) Calling the Linux AUDIT_* architecture constants "native" leads to confusing code when we are getting the actual native architecture of the running system. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 6223a65 commit d85b583

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

libcontainer/seccomp/patchbpf/enosys_linux.go

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -164,60 +164,60 @@ func disassembleFilter(filter *libseccomp.ScmpFilter) ([]bpf.Instruction, error)
164164
return program, nil
165165
}
166166

167-
type nativeArch uint32
167+
type linuxAuditArch uint32
168168

169-
const invalidArch nativeArch = 0
169+
const invalidArch linuxAuditArch = 0
170170

171-
func archToNative(arch libseccomp.ScmpArch) (nativeArch, error) {
171+
func scmpArchToAuditArch(arch libseccomp.ScmpArch) (linuxAuditArch, error) {
172172
switch arch {
173173
case libseccomp.ArchNative:
174174
// Convert to actual native architecture.
175175
arch, err := libseccomp.GetNativeArch()
176176
if err != nil {
177177
return invalidArch, fmt.Errorf("unable to get native arch: %w", err)
178178
}
179-
return archToNative(arch)
179+
return scmpArchToAuditArch(arch)
180180
case libseccomp.ArchX86:
181-
return nativeArch(C.C_AUDIT_ARCH_I386), nil
181+
return linuxAuditArch(C.C_AUDIT_ARCH_I386), nil
182182
case libseccomp.ArchAMD64, libseccomp.ArchX32:
183183
// NOTE: x32 is treated like x86_64 except all x32 syscalls have the
184184
// 30th bit of the syscall number set to indicate that it's not a
185185
// normal x86_64 syscall.
186-
return nativeArch(C.C_AUDIT_ARCH_X86_64), nil
186+
return linuxAuditArch(C.C_AUDIT_ARCH_X86_64), nil
187187
case libseccomp.ArchARM:
188-
return nativeArch(C.C_AUDIT_ARCH_ARM), nil
188+
return linuxAuditArch(C.C_AUDIT_ARCH_ARM), nil
189189
case libseccomp.ArchARM64:
190-
return nativeArch(C.C_AUDIT_ARCH_AARCH64), nil
190+
return linuxAuditArch(C.C_AUDIT_ARCH_AARCH64), nil
191191
case libseccomp.ArchMIPS:
192-
return nativeArch(C.C_AUDIT_ARCH_MIPS), nil
192+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPS), nil
193193
case libseccomp.ArchMIPS64:
194-
return nativeArch(C.C_AUDIT_ARCH_MIPS64), nil
194+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPS64), nil
195195
case libseccomp.ArchMIPS64N32:
196-
return nativeArch(C.C_AUDIT_ARCH_MIPS64N32), nil
196+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPS64N32), nil
197197
case libseccomp.ArchMIPSEL:
198-
return nativeArch(C.C_AUDIT_ARCH_MIPSEL), nil
198+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPSEL), nil
199199
case libseccomp.ArchMIPSEL64:
200-
return nativeArch(C.C_AUDIT_ARCH_MIPSEL64), nil
200+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPSEL64), nil
201201
case libseccomp.ArchMIPSEL64N32:
202-
return nativeArch(C.C_AUDIT_ARCH_MIPSEL64N32), nil
202+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPSEL64N32), nil
203203
case libseccomp.ArchPPC:
204-
return nativeArch(C.C_AUDIT_ARCH_PPC), nil
204+
return linuxAuditArch(C.C_AUDIT_ARCH_PPC), nil
205205
case libseccomp.ArchPPC64:
206-
return nativeArch(C.C_AUDIT_ARCH_PPC64), nil
206+
return linuxAuditArch(C.C_AUDIT_ARCH_PPC64), nil
207207
case libseccomp.ArchPPC64LE:
208-
return nativeArch(C.C_AUDIT_ARCH_PPC64LE), nil
208+
return linuxAuditArch(C.C_AUDIT_ARCH_PPC64LE), nil
209209
case libseccomp.ArchS390:
210-
return nativeArch(C.C_AUDIT_ARCH_S390), nil
210+
return linuxAuditArch(C.C_AUDIT_ARCH_S390), nil
211211
case libseccomp.ArchS390X:
212-
return nativeArch(C.C_AUDIT_ARCH_S390X), nil
212+
return linuxAuditArch(C.C_AUDIT_ARCH_S390X), nil
213213
case libseccomp.ArchRISCV64:
214-
return nativeArch(C.C_AUDIT_ARCH_RISCV64), nil
214+
return linuxAuditArch(C.C_AUDIT_ARCH_RISCV64), nil
215215
default:
216216
return invalidArch, fmt.Errorf("unknown architecture: %v", arch)
217217
}
218218
}
219219

220-
type lastSyscallMap map[nativeArch]map[libseccomp.ScmpArch]libseccomp.ScmpSyscall
220+
type lastSyscallMap map[linuxAuditArch]map[libseccomp.ScmpArch]libseccomp.ScmpSyscall
221221

222222
// Figure out largest syscall number referenced in the filter for each
223223
// architecture. We will be generating code based on the native architecture
@@ -234,17 +234,17 @@ func findLastSyscalls(config *configs.Seccomp) (lastSyscallMap, error) {
234234
}
235235

236236
// Figure out native architecture representation of the architecture.
237-
nativeArch, err := archToNative(arch)
237+
auditArch, err := scmpArchToAuditArch(arch)
238238
if err != nil {
239239
return nil, fmt.Errorf("cannot map architecture %v to AUDIT_ARCH_ constant: %w", arch, err)
240240
}
241241

242-
if _, ok := lastSyscalls[nativeArch]; !ok {
243-
lastSyscalls[nativeArch] = map[libseccomp.ScmpArch]libseccomp.ScmpSyscall{}
242+
if _, ok := lastSyscalls[auditArch]; !ok {
243+
lastSyscalls[auditArch] = map[libseccomp.ScmpArch]libseccomp.ScmpSyscall{}
244244
}
245-
if _, ok := lastSyscalls[nativeArch][arch]; ok {
245+
if _, ok := lastSyscalls[auditArch][arch]; ok {
246246
// Because of ArchNative we may hit the same entry multiple times.
247-
// Just skip it if we've seen this (nativeArch, ScmpArch)
247+
// Just skip it if we've seen this (linuxAuditArch, ScmpArch)
248248
// combination before.
249249
continue
250250
}
@@ -262,10 +262,11 @@ func findLastSyscalls(config *configs.Seccomp) (lastSyscallMap, error) {
262262
}
263263
}
264264
if largestSyscall != 0 {
265-
lastSyscalls[nativeArch][arch] = largestSyscall
265+
logrus.Debugf("seccomp: largest syscall number for arch %v is %v", arch, largestSyscall)
266+
lastSyscalls[auditArch][arch] = largestSyscall
266267
} else {
267-
logrus.Warnf("could not find any syscalls for arch %s", ociArch)
268-
delete(lastSyscalls[nativeArch], arch)
268+
logrus.Warnf("could not find any syscalls for arch %v", arch)
269+
delete(lastSyscalls[auditArch], arch)
269270
}
270271
}
271272
return lastSyscalls, nil
@@ -283,10 +284,10 @@ func findLastSyscalls(config *configs.Seccomp) (lastSyscallMap, error) {
283284
// close_range(2) which were added out-of-order in the syscall table between
284285
// kernel releases.
285286
func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error) {
286-
// A jump-table for each nativeArch used to generate the initial
287+
// A jump-table for each linuxAuditArch used to generate the initial
287288
// conditional jumps -- measured from the *END* of the program so they
288289
// remain valid after prepending to the tail.
289-
archJumpTable := map[nativeArch]uint32{}
290+
archJumpTable := map[linuxAuditArch]uint32{}
290291

291292
// Generate our own -ENOSYS rules for each architecture. They have to be
292293
// generated in reverse (prepended to the tail of the program) because the
@@ -299,7 +300,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
299300
}
300301

301302
// Generate the syscall -ENOSYS rules.
302-
for nativeArch, maxSyscalls := range lastSyscalls {
303+
for auditArch, maxSyscalls := range lastSyscalls {
303304
// The number of instructions from the tail of this section which need
304305
// to be jumped in order to reach the -ENOSYS return. If the section
305306
// does not jump, it will fall through to the actual filter.
@@ -380,7 +381,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
380381

381382
// If we're on x86 we need to add a check for x32 and if we're in
382383
// the wrong mode we jump over the section.
383-
if uint32(nativeArch) == uint32(C.C_AUDIT_ARCH_X86_64) {
384+
if uint32(auditArch) == uint32(C.C_AUDIT_ARCH_X86_64) {
384385
// Generate a prefix to check the mode.
385386
switch scmpArch {
386387
case libseccomp.ArchAMD64:
@@ -409,8 +410,8 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
409410
section = append(section, sectionTail...)
410411
case 2:
411412
// x32 and x86_64 are a unique case, we can't handle any others.
412-
if uint32(nativeArch) != uint32(C.C_AUDIT_ARCH_X86_64) {
413-
return nil, fmt.Errorf("unknown architecture overlap on native arch %#x", nativeArch)
413+
if uint32(auditArch) != uint32(C.C_AUDIT_ARCH_X86_64) {
414+
return nil, fmt.Errorf("unknown architecture overlap on native arch %#x", auditArch)
414415
}
415416

416417
x32sysno, ok := maxSyscalls[libseccomp.ArchX32]
@@ -487,7 +488,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
487488
programTail = append(section, programTail...)
488489

489490
// Update jump table.
490-
archJumpTable[nativeArch] = uint32(len(programTail))
491+
archJumpTable[auditArch] = uint32(len(programTail))
491492
}
492493

493494
// Add a dummy "jump to filter" for any architecture we might miss below.
@@ -507,9 +508,9 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
507508
// architectures based on how large the jumps are going to be, or
508509
// re-sort the candidate architectures each time to make sure that we
509510
// pick the largest jump which is going to be smaller than 255.
510-
for nativeArch := range lastSyscalls {
511+
for auditArch := range lastSyscalls {
511512
// We jump forwards but the jump table is calculated from the *END*.
512-
jump := uint32(len(programTail)) - archJumpTable[nativeArch]
513+
jump := uint32(len(programTail)) - archJumpTable[auditArch]
513514

514515
// Same routine as above -- this is a basic jeq check, complicated
515516
// slightly if it turns out that we need to do a long jump.
@@ -518,7 +519,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
518519
// jeq [arch],[jump]
519520
bpf.JumpIf{
520521
Cond: bpf.JumpEqual,
521-
Val: uint32(nativeArch),
522+
Val: uint32(auditArch),
522523
SkipTrue: uint8(jump),
523524
},
524525
}, programTail...)
@@ -527,7 +528,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
527528
// jne [arch],1
528529
bpf.JumpIf{
529530
Cond: bpf.JumpNotEqual,
530-
Val: uint32(nativeArch),
531+
Val: uint32(auditArch),
531532
SkipTrue: 1,
532533
},
533534
// ja [jump]

libcontainer/seccomp/patchbpf/enosys_linux_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type seccompData struct {
2323
}
2424

2525
// mockSyscallPayload creates a fake seccomp_data struct with the given data.
26-
func mockSyscallPayload(t *testing.T, sysno libseccomp.ScmpSyscall, arch nativeArch, args ...uint64) []byte {
26+
func mockSyscallPayload(t *testing.T, sysno libseccomp.ScmpSyscall, arch linuxAuditArch, args ...uint64) []byte {
2727
var buf bytes.Buffer
2828

2929
data := seccompData{
@@ -150,8 +150,8 @@ func testEnosysStub(t *testing.T, defaultAction configs.Action, arches []string)
150150

151151
for _, arch := range testArches {
152152
type syscallTest struct {
153-
syscall string
154153
sysno libseccomp.ScmpSyscall
154+
syscall string
155155
expected uint32
156156
}
157157

@@ -160,7 +160,7 @@ func testEnosysStub(t *testing.T, defaultAction configs.Action, arches []string)
160160
t.Fatalf("unknown libseccomp architecture %q: %v", arch, err)
161161
}
162162

163-
nativeArch, err := archToNative(scmpArch)
163+
auditArch, err := scmpArchToAuditArch(scmpArch)
164164
if err != nil {
165165
t.Fatalf("unknown audit architecture %q: %v", arch, err)
166166
}
@@ -179,9 +179,9 @@ func testEnosysStub(t *testing.T, defaultAction configs.Action, arches []string)
179179
t.Fatalf("unknown syscall %q on arch %q: %v", syscall, arch, err)
180180
}
181181
syscallTests = append(syscallTests, syscallTest{
182-
syscall,
183-
sysno,
184-
expected,
182+
sysno: sysno,
183+
syscall: syscall,
184+
expected: expected,
185185
})
186186
}
187187

@@ -233,7 +233,7 @@ func testEnosysStub(t *testing.T, defaultAction configs.Action, arches []string)
233233
test.expected = retFallthrough
234234
}
235235

236-
payload := mockSyscallPayload(t, test.sysno, nativeArch, 0x1337, 0xF00BA5)
236+
payload := mockSyscallPayload(t, test.sysno, auditArch, 0x1337, 0xF00BA5)
237237
// NOTE: golang.org/x/net/bpf returns int here rather
238238
// than uint32.
239239
rawRet, err := filter.Run(payload)
@@ -247,7 +247,7 @@ func testEnosysStub(t *testing.T, defaultAction configs.Action, arches []string)
247247
t.Logf(" [%4.1d] %s", idx, insn)
248248
}
249249
t.Logf("payload: %#v", payload)
250-
t.Errorf("filter %s(%d) %q(%d): got %#x, want %#x", arch, nativeArch, test.syscall, test.sysno, ret, test.expected)
250+
t.Errorf("filter %s(%d) %q(%d): got %#x, want %#x", arch, auditArch, test.syscall, test.sysno, ret, test.expected)
251251
}
252252
}
253253
}

0 commit comments

Comments
 (0)