1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package bpf

import "sort"

// referenceAction computes the expected seccomp action for a given arch/nr/args
// directly from the rule list, independent of the BPF program.  It is the oracle
// the interpreter is checked against in tests, and mirrors the semantics of
// compileSyscallFilter (linux.cc):
//
//   - unknown arch                       -> RET_KILL_PROCESS
//   - nr not in the arch's allowlist     -> RET_ERRNO(ENOSYS)
//   - setxattr family                    -> RET_ERRNO(ENOTSUP)
//   - chmod family with setuid/setgid    -> RET_ERRNO(EPERM)
//   - chmod family otherwise / allowed   -> RET_ALLOW
//
// nativeSystem selects the same arch profiles as Compile.
func referenceAction(nativeSystem string, arch, nr uint32, args [6]uint64) uint32 {
	profiles, ok := targetArches[nativeSystem]
	if !ok {
		return RetKillProcess
	}
	var prof *archProfile
	for i := range profiles {
		if profiles[i].auditArch == arch {
			prof = &profiles[i]
			break
		}
	}
	if prof == nil {
		return RetKillProcess
	}
	table := syscallTables[prof.name]
	if table == nil {
		return RetKillProcess
	}

	// Build nr -> rule for this arch (first rule wins on duplicate numbers, to
	// match the compiler's dedupByNr which keeps the first in sorted order).
	type nrRule struct {
		nr uint32
		r  rule
	}
	var rules []nrRule
	for _, r := range lixRules {
		if n, ok := table[r.name]; ok {
			rules = append(rules, nrRule{nr: n, r: r})
		}
	}
	sort.SliceStable(rules, func(a, b int) bool { return rules[a].nr < rules[b].nr })

	var matched *rule
	for i := range rules {
		if rules[i].nr == nr {
			matched = &rules[i].r
			break
		}
	}
	if matched == nil {
		return Errno(errENOSYS)
	}
	switch matched.kind {
	case kindAllow:
		return RetAllow
	case kindErrno:
		return Errno(matched.errno)
	case kindMaskedEq:
		if uint32(args[matched.argIdx])&matched.mask == 0 {
			return RetAllow
		}
		return Errno(matched.errno)
	default:
		return Errno(errENOSYS)
	}
}