-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_element.go
48 lines (43 loc) · 1.12 KB
/
filter_element.go
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
// SPDX-Licence-Identifier: MIT
package goseccomp
import "golang.org/x/net/bpf"
// FilterElement is a part of a seccomp filter grouping calls that leads to the same decision.
type FilterElement struct {
Match []SyscallCallFilter
Decision Decision
}
func (f *FilterElement) compile(arch string) []bpf.Instruction {
if len(f.Match) == 0 {
return nil
}
var distanceToDecision uint = 0
instructions := []bpf.Instruction{
f.Decision.compile(),
}
for _, syscall := range f.Match {
var distanceToNext uint = 0
if distanceToDecision == 0 {
distanceToNext = 1
}
newInstructions := syscall.compile(distanceToDecision, distanceToNext, arch)
distanceToDecision += uint(len(newInstructions))
instructions = append(newInstructions, instructions...)
}
return instructions
}
func (f *FilterElement) keepLeastPreciseMatch() {
var newMatch []SyscallCallFilter
OUTER:
for _, filter := range f.Match {
for i, mFilter := range newMatch {
if filter.Match(mFilter) {
if mFilter.IsMorePrecise(filter) {
newMatch[i] = filter
}
continue OUTER
}
}
newMatch = append(newMatch, filter)
}
f.Match = newMatch
}