-
Notifications
You must be signed in to change notification settings - Fork 19
/
filter_test.go
482 lines (427 loc) · 10.9 KB
/
filter_test.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package seccomp
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"os"
"sort"
"testing"
"golang.org/x/net/bpf"
"github.com/elastic/go-seccomp-bpf/arch"
)
var dump = flag.Bool("dump", false, "dump seccomp filter instructions to stdout")
// The simulator expects big-endian, but seccomp_data uses native endian.
// As a workaround send in big endian data.
// https://github.com/golang/go/issues/20556
// https://github.com/torvalds/linux/blob/v4.16/kernel/seccomp.c#L73-L74
var simulatorEndian = binary.BigEndian
func init() {
nativeEndian = simulatorEndian
}
type SeccompData struct {
NR int32
Arch uint32
InstructionPointer uint64
Args [6]uint64
}
type SeccompTest struct {
Data SeccompData
Rtn Action
}
func simulateSyscalls(t testing.TB, policy *Policy, tests []SeccompTest) {
t.Helper()
filter, err := policy.Assemble()
if err != nil {
t.Fatal(err)
}
vm, err := bpf.NewVM(filter)
if err != nil {
t.Fatal(err)
}
for n, tc := range tests {
buf := new(bytes.Buffer)
if err := binary.Write(buf, simulatorEndian, tc.Data); err != nil {
t.Fatal(err)
}
rtn, err := vm.Run(buf.Bytes())
if err != nil {
t.Fatal(err)
}
if Action(rtn) != tc.Rtn {
t.Errorf("Expected %v, but got %v for test case %v with seccomp_data=%#v",
tc.Rtn, Action(rtn), n+1, tc.Data)
}
}
}
// conditionTests maps the operations to the corresponding Go operations to test the 64-bit operations.
var conditionTests = []struct {
cond Operation
eval func(policy, input uint64) bool
}{
{cond: Equal, eval: func(input, policy uint64) bool { return input == policy }},
{cond: NotEqual, eval: func(input, policy uint64) bool { return input != policy }},
{cond: GreaterThan, eval: func(input, policy uint64) bool { return input > policy }},
{cond: GreaterOrEqual, eval: func(input, policy uint64) bool { return input >= policy }},
{cond: LessThan, eval: func(input, policy uint64) bool { return input < policy }},
{cond: LessOrEqual, eval: func(input, policy uint64) bool { return input <= policy }},
{cond: BitsSet, eval: func(input, policy uint64) bool { return input&policy != 0 }},
{cond: BitsNotSet, eval: func(input, policy uint64) bool { return input&policy == 0 }},
}
const (
testArgument1 = 0x0102_0304_0506_0708
testArgument2 = 0x1020_3040_5060_7080
)
// hand crafted conditionInput to test the 64-bit operations.
var conditionInput = []uint64{
0x0000_0000_0000_0000,
0x0000_0000_0506_0708,
0x0002_0304_0506_0707,
0x0102_0304_0000_0000,
0x0102_0304_0506_0707,
testArgument1,
testArgument2,
0x0102_0304_0506_0709,
0x0202_0304_0506_0708,
0xFFFF_FFFF_FFFF_FFFF,
}
func TestPolicyAssembleBlacklist(t *testing.T) {
policy := &Policy{
arch: arch.X86_64,
DefaultAction: ActionAllow,
Syscalls: []SyscallGroup{
{
Names: []string{"execve", "fork"},
Action: ActionKillThread,
},
},
}
if *dump {
policy.Dump(os.Stdout)
}
simulateSyscalls(t, policy, []SeccompTest{
{
SeccompData{NR: 59, Arch: uint32(arch.X86_64.ID)},
ActionKillThread,
},
{
SeccompData{NR: 57, Arch: uint32(arch.X86_64.ID)},
ActionKillThread,
},
{
SeccompData{NR: 4, Arch: uint32(arch.X86_64.ID)},
ActionAllow,
},
{
SeccompData{NR: 4, Arch: uint32(arch.ARM.ID)},
ActionAllow,
},
{
// Attempts to bypass the filter by using X32 syscalls on X86_64
// are met with ENOSYS.
SeccompData{NR: int32(arch.X32.SyscallNames["execve"] + arch.X32.SeccompMask), Arch: uint32(arch.X86_64.ID)},
ActionErrno | Action(errnoENOSYS),
},
})
}
func TestPolicyAssembleWhitelist(t *testing.T) {
policy := &Policy{
arch: arch.X86_64,
DefaultAction: ActionKillProcess,
Syscalls: []SyscallGroup{
{
Names: []string{"execve", "fork"},
Action: ActionAllow,
},
{
Names: []string{"clone", "listen"},
Action: ActionAllow,
},
},
}
if *dump {
policy.Dump(os.Stdout)
}
simulateSyscalls(t, policy, []SeccompTest{
{
SeccompData{NR: 59, Arch: uint32(arch.X86_64.ID)},
ActionAllow,
},
{
SeccompData{NR: 4, Arch: uint32(arch.X86_64.ID)},
ActionKillProcess,
},
{
SeccompData{NR: 4, Arch: uint32(arch.ARM.ID)},
ActionKillProcess,
},
})
}
func TestPolicyAssembleLongList(t *testing.T) {
// Sort syscall numbers to make manual review of filters with -dump easier.
syscallNumbers := make([]int, 0, len(arch.X86_64.SyscallNumbers))
for nr := range arch.X86_64.SyscallNumbers {
syscallNumbers = append(syscallNumbers, nr)
}
sort.Ints(syscallNumbers)
for i := 1; i <= len(syscallNumbers); i++ {
filterSize := i
t.Run(fmt.Sprintf("size=%d", filterSize), func(t *testing.T) {
var syscallNames []string
var tests []SeccompTest
for _, nr := range syscallNumbers[:filterSize] {
name := arch.X86_64.SyscallNumbers[nr]
var action Action
if name != "exit" {
syscallNames = append(syscallNames, name)
action = ActionAllow
} else {
action = ActionKillProcess
}
tests = append(tests, SeccompTest{
SeccompData{NR: int32(nr), Arch: uint32(arch.X86_64.ID)},
action,
})
// Incorrect arch should always kill process.
tests = append(tests, SeccompTest{
SeccompData{NR: int32(nr), Arch: uint32(arch.ARM.ID)},
ActionKillProcess,
})
}
policy := &Policy{
arch: arch.X86_64,
DefaultAction: ActionKillProcess,
Syscalls: []SyscallGroup{
{
Names: syscallNames,
Action: ActionAllow,
},
},
}
if *dump {
policy.Dump(os.Stdout)
}
simulateSyscalls(t, policy, tests)
})
}
}
func TestPolicyAssembleDefault(t *testing.T) {
policy := Policy{
DefaultAction: ActionAllow,
Syscalls: []SyscallGroup{
{
Action: ActionErrno,
Names: []string{
"execve",
"fork",
"vfork",
"execveat",
},
},
},
}
for _, arch := range []*arch.Info{arch.ARM, arch.I386, arch.X86_64} {
policy.arch = arch
_, err := policy.Assemble()
if err != nil {
t.Errorf("failed to assemble default policy on %v: %v", arch.Name, err)
}
}
}
func TestSimpleLongList(t *testing.T) {
syscallNumbers := make([]int, 0, len(arch.X86_64.SyscallNumbers))
for nr := range arch.X86_64.SyscallNumbers {
syscallNumbers = append(syscallNumbers, nr)
}
sort.Ints(syscallNumbers)
names := make([]string, 0, 6)
for i := 1; i < 6; i++ {
names = append(names, arch.X86_64.SyscallNumbers[i])
}
names = append(names, "read")
policy := &Policy{
arch: arch.X86_64,
DefaultAction: ActionAllow,
Syscalls: []SyscallGroup{
{
Names: names,
Action: ActionKillThread,
},
},
}
if *dump {
policy.Dump(os.Stdout)
}
simulateSyscalls(t, policy, []SeccompTest{
{
SeccompData{NR: 0, Arch: uint32(arch.X86_64.ID)},
ActionKillThread,
},
})
}
func TestSimpleConditions(t *testing.T) {
for _, tc := range conditionTests {
t.Run(string(tc.cond), func(t *testing.T) {
policy := &Policy{
arch: arch.X86_64,
DefaultAction: ActionAllow,
Syscalls: []SyscallGroup{
{
Names: []string{},
Action: ActionKillThread,
NamesWithCondtions: []NameWithConditions{
{
Name: "read",
Conditions: []Condition{
{
Argument: 0,
Operation: tc.cond,
Value: testArgument1,
},
},
},
},
},
},
}
if *dump {
policy.Dump(os.Stdout)
}
syscalls := make([]SeccompTest, 0, len(conditionInput))
for _, i := range conditionInput {
expected := ActionAllow
if tc.eval(i, testArgument1) {
expected = ActionKillThread
}
t := SeccompTest{
SeccompData{NR: 0, Arch: uint32(arch.X86_64.ID), Args: [6]uint64{i}},
expected,
}
syscalls = append(syscalls, t)
}
simulateSyscalls(t, policy, syscalls)
})
}
}
func TestTwoArgumentConditions(t *testing.T) {
for _, tc := range conditionTests {
t.Run(string(tc.cond), func(t *testing.T) {
policy := &Policy{
arch: arch.X86_64,
DefaultAction: ActionAllow,
Syscalls: []SyscallGroup{
{
Names: []string{},
Action: ActionKillThread,
NamesWithCondtions: []NameWithConditions{
{
Name: "read",
Conditions: []Condition{
{
Argument: 0,
Operation: tc.cond,
Value: testArgument1,
},
{
Argument: 1,
Operation: tc.cond,
Value: testArgument2,
},
},
},
},
},
},
}
if *dump {
policy.Dump(os.Stdout)
}
syscalls := make([]SeccompTest, 0, len(conditionInput))
for _, arg1 := range conditionInput {
for _, arg2 := range conditionInput {
expected := ActionAllow
if tc.eval(arg1, testArgument1) && tc.eval(arg2, testArgument2) {
expected = ActionKillThread
}
t := SeccompTest{
SeccompData{NR: 0, Arch: uint32(arch.X86_64.ID), Args: [6]uint64{arg1, arg2}},
expected,
}
syscalls = append(syscalls, t)
}
}
simulateSyscalls(t, policy, syscalls)
})
}
}
func TestLongConditions(t *testing.T) {
filter := make([]NameWithConditions, 0, 20)
for i := uint64(0); i < 20; i++ {
arguments := make([]Condition, 0, 6)
for arg := uint32(0); arg < 6; arg++ {
argument := Condition{
Argument: arg,
Operation: Equal,
Value: i*6 + uint64(arg),
}
arguments = append(arguments, argument)
}
f := NameWithConditions{
Name: "write",
Conditions: arguments,
}
filter = append(filter, f)
}
policy := &Policy{
arch: arch.X86_64,
DefaultAction: ActionAllow,
Syscalls: []SyscallGroup{
{
Names: []string{},
Action: ActionKillThread,
NamesWithCondtions: filter,
},
},
}
if *dump {
policy.Dump(os.Stdout)
}
lastCheck := uint64(len(filter)-1) * 6
simulateSyscalls(t, policy, []SeccompTest{
{
SeccompData{NR: 1, Arch: uint32(arch.X86_64.ID), Args: [6]uint64{0, 1, 2, 3, 4, 5}},
ActionKillThread,
},
{
SeccompData{NR: 1, Arch: uint32(arch.X86_64.ID), Args: [6]uint64{
lastCheck,
lastCheck + 1,
lastCheck + 2,
lastCheck + 3,
lastCheck + 4,
lastCheck + 5,
}},
ActionKillThread,
},
{
SeccompData{NR: 1, Arch: uint32(arch.X86_64.ID), Args: [6]uint64{0, 1, 2, 3, 4, 0}},
ActionAllow,
},
})
}