Skip to content

Commit b288abe

Browse files
committed
seccomp: patchbpf: rename nativeArch -> linuxAuditArch
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 8e69225 commit b288abe

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

libcontainer/seccomp/patchbpf/enosys_linux.go

+41-40
Original file line numberDiff line numberDiff line change
@@ -171,60 +171,60 @@ func disassembleFilter(filter *libseccomp.ScmpFilter) ([]bpf.Instruction, error)
171171
return program, nil
172172
}
173173

174-
type nativeArch uint32
174+
type linuxAuditArch uint32
175175

176-
const invalidArch nativeArch = 0
176+
const invalidArch linuxAuditArch = 0
177177

178-
func archToNative(arch libseccomp.ScmpArch) (nativeArch, error) {
178+
func scmpArchToAuditArch(arch libseccomp.ScmpArch) (linuxAuditArch, error) {
179179
switch arch {
180180
case libseccomp.ArchNative:
181181
// Convert to actual native architecture.
182182
arch, err := libseccomp.GetNativeArch()
183183
if err != nil {
184184
return invalidArch, fmt.Errorf("unable to get native arch: %w", err)
185185
}
186-
return archToNative(arch)
186+
return scmpArchToAuditArch(arch)
187187
case libseccomp.ArchX86:
188-
return nativeArch(C.C_AUDIT_ARCH_I386), nil
188+
return linuxAuditArch(C.C_AUDIT_ARCH_I386), nil
189189
case libseccomp.ArchAMD64, libseccomp.ArchX32:
190190
// NOTE: x32 is treated like x86_64 except all x32 syscalls have the
191191
// 30th bit of the syscall number set to indicate that it's not a
192192
// normal x86_64 syscall.
193-
return nativeArch(C.C_AUDIT_ARCH_X86_64), nil
193+
return linuxAuditArch(C.C_AUDIT_ARCH_X86_64), nil
194194
case libseccomp.ArchARM:
195-
return nativeArch(C.C_AUDIT_ARCH_ARM), nil
195+
return linuxAuditArch(C.C_AUDIT_ARCH_ARM), nil
196196
case libseccomp.ArchARM64:
197-
return nativeArch(C.C_AUDIT_ARCH_AARCH64), nil
197+
return linuxAuditArch(C.C_AUDIT_ARCH_AARCH64), nil
198198
case libseccomp.ArchMIPS:
199-
return nativeArch(C.C_AUDIT_ARCH_MIPS), nil
199+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPS), nil
200200
case libseccomp.ArchMIPS64:
201-
return nativeArch(C.C_AUDIT_ARCH_MIPS64), nil
201+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPS64), nil
202202
case libseccomp.ArchMIPS64N32:
203-
return nativeArch(C.C_AUDIT_ARCH_MIPS64N32), nil
203+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPS64N32), nil
204204
case libseccomp.ArchMIPSEL:
205-
return nativeArch(C.C_AUDIT_ARCH_MIPSEL), nil
205+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPSEL), nil
206206
case libseccomp.ArchMIPSEL64:
207-
return nativeArch(C.C_AUDIT_ARCH_MIPSEL64), nil
207+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPSEL64), nil
208208
case libseccomp.ArchMIPSEL64N32:
209-
return nativeArch(C.C_AUDIT_ARCH_MIPSEL64N32), nil
209+
return linuxAuditArch(C.C_AUDIT_ARCH_MIPSEL64N32), nil
210210
case libseccomp.ArchPPC:
211-
return nativeArch(C.C_AUDIT_ARCH_PPC), nil
211+
return linuxAuditArch(C.C_AUDIT_ARCH_PPC), nil
212212
case libseccomp.ArchPPC64:
213-
return nativeArch(C.C_AUDIT_ARCH_PPC64), nil
213+
return linuxAuditArch(C.C_AUDIT_ARCH_PPC64), nil
214214
case libseccomp.ArchPPC64LE:
215-
return nativeArch(C.C_AUDIT_ARCH_PPC64LE), nil
215+
return linuxAuditArch(C.C_AUDIT_ARCH_PPC64LE), nil
216216
case libseccomp.ArchS390:
217-
return nativeArch(C.C_AUDIT_ARCH_S390), nil
217+
return linuxAuditArch(C.C_AUDIT_ARCH_S390), nil
218218
case libseccomp.ArchS390X:
219-
return nativeArch(C.C_AUDIT_ARCH_S390X), nil
219+
return linuxAuditArch(C.C_AUDIT_ARCH_S390X), nil
220220
case libseccomp.ArchRISCV64:
221-
return nativeArch(C.C_AUDIT_ARCH_RISCV64), nil
221+
return linuxAuditArch(C.C_AUDIT_ARCH_RISCV64), nil
222222
default:
223223
return invalidArch, fmt.Errorf("unknown architecture: %v", arch)
224224
}
225225
}
226226

227-
type lastSyscallMap map[nativeArch]map[libseccomp.ScmpArch]libseccomp.ScmpSyscall
227+
type lastSyscallMap map[linuxAuditArch]map[libseccomp.ScmpArch]libseccomp.ScmpSyscall
228228

229229
// Figure out largest syscall number referenced in the filter for each
230230
// architecture. We will be generating code based on the native architecture
@@ -241,17 +241,17 @@ func findLastSyscalls(config *configs.Seccomp) (lastSyscallMap, error) {
241241
}
242242

243243
// Figure out native architecture representation of the architecture.
244-
nativeArch, err := archToNative(arch)
244+
auditArch, err := scmpArchToAuditArch(arch)
245245
if err != nil {
246246
return nil, fmt.Errorf("cannot map architecture %v to AUDIT_ARCH_ constant: %w", arch, err)
247247
}
248248

249-
if _, ok := lastSyscalls[nativeArch]; !ok {
250-
lastSyscalls[nativeArch] = map[libseccomp.ScmpArch]libseccomp.ScmpSyscall{}
249+
if _, ok := lastSyscalls[auditArch]; !ok {
250+
lastSyscalls[auditArch] = map[libseccomp.ScmpArch]libseccomp.ScmpSyscall{}
251251
}
252-
if _, ok := lastSyscalls[nativeArch][arch]; ok {
252+
if _, ok := lastSyscalls[auditArch][arch]; ok {
253253
// Because of ArchNative we may hit the same entry multiple times.
254-
// Just skip it if we've seen this (nativeArch, ScmpArch)
254+
// Just skip it if we've seen this (linuxAuditArch, ScmpArch)
255255
// combination before.
256256
continue
257257
}
@@ -269,10 +269,11 @@ func findLastSyscalls(config *configs.Seccomp) (lastSyscallMap, error) {
269269
}
270270
}
271271
if largestSyscall != 0 {
272-
lastSyscalls[nativeArch][arch] = largestSyscall
272+
logrus.Debugf("seccomp: largest syscall number for arch %v is %v", arch, largestSyscall)
273+
lastSyscalls[auditArch][arch] = largestSyscall
273274
} else {
274-
logrus.Warnf("could not find any syscalls for arch %s", ociArch)
275-
delete(lastSyscalls[nativeArch], arch)
275+
logrus.Warnf("could not find any syscalls for arch %v", arch)
276+
delete(lastSyscalls[auditArch], arch)
276277
}
277278
}
278279
return lastSyscalls, nil
@@ -290,10 +291,10 @@ func findLastSyscalls(config *configs.Seccomp) (lastSyscallMap, error) {
290291
// close_range(2) which were added out-of-order in the syscall table between
291292
// kernel releases.
292293
func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error) {
293-
// A jump-table for each nativeArch used to generate the initial
294+
// A jump-table for each linuxAuditArch used to generate the initial
294295
// conditional jumps -- measured from the *END* of the program so they
295296
// remain valid after prepending to the tail.
296-
archJumpTable := map[nativeArch]uint32{}
297+
archJumpTable := map[linuxAuditArch]uint32{}
297298

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

308309
// Generate the syscall -ENOSYS rules.
309-
for nativeArch, maxSyscalls := range lastSyscalls {
310+
for auditArch, maxSyscalls := range lastSyscalls {
310311
// The number of instructions from the tail of this section which need
311312
// to be jumped in order to reach the -ENOSYS return. If the section
312313
// does not jump, it will fall through to the actual filter.
@@ -387,7 +388,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
387388

388389
// If we're on x86 we need to add a check for x32 and if we're in
389390
// the wrong mode we jump over the section.
390-
if uint32(nativeArch) == uint32(C.C_AUDIT_ARCH_X86_64) {
391+
if uint32(auditArch) == uint32(C.C_AUDIT_ARCH_X86_64) {
391392
// Generate a prefix to check the mode.
392393
switch scmpArch {
393394
case libseccomp.ArchAMD64:
@@ -416,8 +417,8 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
416417
section = append(section, sectionTail...)
417418
case 2:
418419
// x32 and x86_64 are a unique case, we can't handle any others.
419-
if uint32(nativeArch) != uint32(C.C_AUDIT_ARCH_X86_64) {
420-
return nil, fmt.Errorf("unknown architecture overlap on native arch %#x", nativeArch)
420+
if uint32(auditArch) != uint32(C.C_AUDIT_ARCH_X86_64) {
421+
return nil, fmt.Errorf("unknown architecture overlap on native arch %#x", auditArch)
421422
}
422423

423424
x32sysno, ok := maxSyscalls[libseccomp.ArchX32]
@@ -494,7 +495,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
494495
programTail = append(section, programTail...)
495496

496497
// Update jump table.
497-
archJumpTable[nativeArch] = uint32(len(programTail))
498+
archJumpTable[auditArch] = uint32(len(programTail))
498499
}
499500

500501
// Add a dummy "jump to filter" for any architecture we might miss below.
@@ -514,9 +515,9 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
514515
// architectures based on how large the jumps are going to be, or
515516
// re-sort the candidate architectures each time to make sure that we
516517
// pick the largest jump which is going to be smaller than 255.
517-
for nativeArch := range lastSyscalls {
518+
for auditArch := range lastSyscalls {
518519
// We jump forwards but the jump table is calculated from the *END*.
519-
jump := uint32(len(programTail)) - archJumpTable[nativeArch]
520+
jump := uint32(len(programTail)) - archJumpTable[auditArch]
520521

521522
// Same routine as above -- this is a basic jeq check, complicated
522523
// slightly if it turns out that we need to do a long jump.
@@ -525,7 +526,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
525526
// jeq [arch],[jump]
526527
bpf.JumpIf{
527528
Cond: bpf.JumpEqual,
528-
Val: uint32(nativeArch),
529+
Val: uint32(auditArch),
529530
SkipTrue: uint8(jump),
530531
},
531532
}, programTail...)
@@ -534,7 +535,7 @@ func generateEnosysStub(lastSyscalls lastSyscallMap) ([]bpf.Instruction, error)
534535
// jne [arch],1
535536
bpf.JumpIf{
536537
Cond: bpf.JumpNotEqual,
537-
Val: uint32(nativeArch),
538+
Val: uint32(auditArch),
538539
SkipTrue: 1,
539540
},
540541
// ja [jump]

libcontainer/seccomp/patchbpf/enosys_linux_test.go

+8-8
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)