-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
187 lines (172 loc) · 4.59 KB
/
filter.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SPDX-Licence-Identifier: MIT
package goseccomp
import (
"fmt"
"runtime"
"github.com/diconico07/goseccomp/lowlevel"
"golang.org/x/net/bpf"
)
var CurrentArch string = runtime.GOARCH
// Filter represents a full fledged seccomp filter
type Filter struct {
// Elements is a slice of FilterElements that build the filter
Elements []FilterElement
// DefaultDecision is the decision that get applied if nothing match
DefaultDecision Decision
// Architecture is the architecture for which the filter is designed.
// If Architecture doesn't match process will be killed.
Architecture string
}
func (f *Filter) mergeAllDuplicatesDecisions() {
var newElements []FilterElement
OUTER:
for _, filter := range f.Elements {
for i, mFilter := range newElements {
if filter.Decision == mFilter.Decision {
newElements[i].Match = append(
newElements[i].Match,
filter.Match...,
)
continue OUTER
}
}
newElements = append(newElements, filter)
}
f.Elements = newElements
}
func (f *Filter) mergeConsecutiveDuplicateDecisions() {
var newElements []FilterElement
for _, filter := range f.Elements {
if len(newElements) != 0 {
if filter.Decision == newElements[len(newElements)-1].Decision {
newElements[len(newElements)-1].Match = append(
newElements[len(newElements)-1].Match,
filter.Match...,
)
continue
}
}
newElements = append(newElements, filter)
}
f.Elements = newElements
}
func (f *Filter) splitOrderElements() {
var lastOrderedFilter int = 0
OUTER:
for {
for x, filter := range f.Elements[lastOrderedFilter:] {
for i, element := range filter.Match {
bestPosition := 0
for j, mFilter := range f.Elements[lastOrderedFilter:] {
for _, mElement := range mFilter.Match {
if element.Match(mElement) {
if !element.IsMorePrecise(mElement) {
bestPosition = j + 1
}
}
}
}
if bestPosition != 0 {
newFilter := FilterElement{
Match: []SyscallCallFilter{
element,
},
Decision: filter.Decision,
}
f.Elements[x].Match = append(
f.Elements[x].Match[:i],
f.Elements[x].Match[i+1:]...,
)
if bestPosition == len(f.Elements) {
f.Elements = append(f.Elements, newFilter)
} else {
f.Elements = append(
f.Elements[:bestPosition+1],
f.Elements[bestPosition:]...,
)
f.Elements[bestPosition] = newFilter
}
continue OUTER
}
}
lastOrderedFilter++
}
break
}
}
// Optimize re-order filter elements to have an ordered Filter that
// take all given decisions accordingly, spurious filter elements are removed.
func (f *Filter) Optimize() {
f.mergeAllDuplicatesDecisions()
f.splitOrderElements()
f.mergeConsecutiveDuplicateDecisions()
for x := range f.Elements {
f.Elements[x].keepLeastPreciseMatch()
}
}
// Compile produce a slice of BPF raw instructions ready to be injected
// into the seccomp syscall.
func (f *Filter) Compile() ([]bpf.RawInstruction, error) {
if !lowlevel.SeccompGetActionAvail(uint(f.DefaultDecision.Type)) {
return nil, fmt.Errorf(
"action '%v' unavailable",
f.DefaultDecision.Type,
)
}
for _, filter := range f.Elements {
if !lowlevel.SeccompGetActionAvail(uint(filter.Decision.Type)) {
return nil, fmt.Errorf(
"action '%v' unavailable",
filter.Decision.Type,
)
}
}
// Need to drop the redundant filters and order conflicting ones
bpfProg := []bpf.Instruction{
lowlevel.LoadSeccompDataField("Arch", false, f.Architecture),
bpf.JumpIf{
Cond: bpf.JumpEqual,
SkipTrue: 1,
Val: lowlevel.GetAuditArch(f.Architecture),
},
bpf.RetConstant{Val: lowlevel.SECCOMP_RET_KILL_PROCESS},
lowlevel.LoadSeccompDataField("Number", false, f.Architecture),
}
for _, filter := range f.Elements {
bpfProg = append(bpfProg, filter.compile(f.Architecture)...)
}
bpfProg = append(
bpfProg,
f.DefaultDecision.compile(),
)
rawBpf, err := bpf.Assemble(bpfProg)
if err != nil {
return nil, err
}
return rawBpf, nil
}
// Insert compiles and insert the given Filter in the current thread.
// Will set the NoNewPrivs bit. To be effective this must be done before
// any thread gets created.
func (f *Filter) Insert() error {
err := lowlevel.NoNewPrivs()
if err != nil {
return err
}
var flags uint
for _, filter := range f.Elements {
if filter.Decision.Type == UserNotify {
flags |= lowlevel.SECCOMP_FILTER_FLAG_NEW_LISTENER
}
}
compiled, err := f.Compile()
if err != nil {
return err
}
// TODO: Handle file descriptor when using new listener
_, err = lowlevel.SeccompSetModeFilter(compiled, flags)
if err != nil {
return err
}
return nil
}