-
Notifications
You must be signed in to change notification settings - Fork 2
/
seccomp.go
271 lines (241 loc) · 6.87 KB
/
seccomp.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
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
// Copyright (C) Felix Geyer <debfx@fobos.de>
package main
import (
"fmt"
seccomp "github.com/seccomp/libseccomp-golang"
"golang.org/x/sys/unix"
)
type seccompRule struct {
Action seccomp.ScmpAction
Syscall seccomp.ScmpSyscall
Arg uint
Op seccomp.ScmpCompareOp
OpValue1 uint64
OpValue2 uint64
}
func loadFilter(defaultAction seccomp.ScmpAction, debug bool, rules []seccompRule) (*seccomp.ScmpFilter, error) {
filter, err := seccomp.NewFilter(defaultAction)
if err != nil {
return nil, fmt.Errorf("creating new filter failed: %w", err)
}
api_level, err := seccomp.GetAPI()
if err != nil {
return nil, fmt.Errorf("getting api version failed: %w", err)
}
if debug && api_level >= 3 {
err = filter.SetLogBit(true)
if err != nil {
return nil, fmt.Errorf("enabling logging failed: %w", err)
}
}
// enable binary tree optimization on larger filters
if len(rules) > 32 && api_level >= 4 {
err = filter.SetOptimize(2)
// ignore error since it's not fatal
if err != nil && debug {
fmt.Printf("seccomp binary tree optimization not available: %v\n", err)
}
}
for _, rule := range rules {
if rule.Op != seccomp.CompareInvalid {
condition, err := seccomp.MakeCondition(rule.Arg, rule.Op, rule.OpValue1, rule.OpValue2)
if err != nil {
return nil, fmt.Errorf("creating condition failed: %w", err)
}
err = filter.AddRuleConditional(rule.Syscall, rule.Action, []seccomp.ScmpCondition{condition})
if err != nil {
return nil, fmt.Errorf("adding conditional rule failed: %w", err)
}
} else {
err := filter.AddRule(rule.Syscall, rule.Action)
if err != nil {
return nil, fmt.Errorf("adding rule failed: %w", err)
}
}
}
return filter, nil
}
func loadSeccomp(filterName string, debug bool) ([]*seccomp.ScmpFilter, error) {
/*
minimal:
- allow by default
- deny mount syscalls, chroot/pivot_root, new user namespace, ioctl(TIOCSTI)
default:
- ENOSYS by default
- allow list of syscalls
- EPERM on known syscalls
devel:
- like default, but addtionally allow ptrace
*/
filters := []*seccomp.ScmpFilter{}
actionEperm := seccomp.ActErrno.SetReturnCode(int16(unix.EPERM))
actionEnosys := seccomp.ActErrno.SetReturnCode(int16(unix.ENOSYS))
actionEafnosupport := seccomp.ActErrno.SetReturnCode(int16(unix.EAFNOSUPPORT))
var defaultActionMain seccomp.ScmpAction
rulesMain := []seccompRule{}
if filterName == "minimal" {
defaultActionMain = seccomp.ActAllow
for _, syscallName := range SeccompMinimalEperm {
syscallNo, err := seccomp.GetSyscallFromName(syscallName)
if err != nil {
// skip syscalls that aren't available
continue
}
rulesMain = append(rulesMain, seccompRule{
Action: actionEperm,
Syscall: syscallNo,
})
}
for _, syscallName := range SeccompMinimalEnosys {
syscallNo, err := seccomp.GetSyscallFromName(syscallName)
if err != nil {
// skip syscalls that aren't available
continue
}
rulesMain = append(rulesMain, seccompRule{
Action: actionEnosys,
Syscall: syscallNo,
})
}
} else {
defaultActionMain = actionEnosys
for _, syscallName := range SeccompAllow {
syscallNo, err := seccomp.GetSyscallFromName(syscallName)
if err != nil {
// skip syscalls that aren't available
continue
}
rulesMain = append(rulesMain, seccompRule{
Action: seccomp.ActAllow,
Syscall: syscallNo,
})
}
for _, syscallName := range SeccompEperm {
syscallNo, err := seccomp.GetSyscallFromName(syscallName)
if err != nil {
// skip syscalls that aren't available
continue
}
rulesMain = append(rulesMain, seccompRule{
Action: actionEperm,
Syscall: syscallNo,
})
}
var develAction seccomp.ScmpAction
if filterName == "devel" {
develAction = seccomp.ActAllow
} else {
develAction = actionEperm
}
for _, syscallName := range SeccompAllowDevel {
syscallNo, err := seccomp.GetSyscallFromName(syscallName)
if err != nil {
// skip syscalls that aren't available
continue
}
rulesMain = append(rulesMain, seccompRule{
Action: develAction,
Syscall: syscallNo,
})
}
// allow only AF_UNIX (1), AF_INET (2), AF_INET6 (10) and AF_NETLINK (16)
for _, i := range []int{1, 2, 10, 16} {
rulesMain = append(rulesMain, seccompRule{
Action: seccomp.ActAllow,
Syscall: unix.SYS_SOCKET,
Arg: 0,
Op: seccomp.CompareEqual,
OpValue1: uint64(i),
})
}
rulesMain = append(rulesMain, seccompRule{
Action: actionEafnosupport,
Syscall: unix.SYS_SOCKET,
Arg: 0,
Op: seccomp.CompareLess,
OpValue1: 1,
})
rulesMain = append(rulesMain, seccompRule{
Action: actionEafnosupport,
Syscall: unix.SYS_SOCKET,
Arg: 0,
Op: seccomp.CompareGreater,
OpValue1: 16,
})
for i := 3; i < 10; i++ {
rulesMain = append(rulesMain, seccompRule{
Action: actionEafnosupport,
Syscall: unix.SYS_SOCKET,
Arg: 0,
Op: seccomp.CompareEqual,
OpValue1: uint64(i),
})
}
for i := 11; i < 16; i++ {
rulesMain = append(rulesMain, seccompRule{
Action: actionEafnosupport,
Syscall: unix.SYS_SOCKET,
Arg: 0,
Op: seccomp.CompareEqual,
OpValue1: uint64(i),
})
}
// only allow personality(PER_LINUX)
rulesMain = append(rulesMain, seccompRule{
Action: seccomp.ActAllow,
Syscall: unix.SYS_PERSONALITY,
Arg: 0,
Op: seccomp.CompareEqual,
OpValue1: 0,
})
rulesMain = append(rulesMain, seccompRule{
Action: actionEperm,
Syscall: unix.SYS_PERSONALITY,
Arg: 0,
Op: seccomp.CompareNotEqual,
OpValue1: 0,
})
}
filterMain, err := loadFilter(defaultActionMain, debug, rulesMain)
if err != nil {
return nil, fmt.Errorf("loading main seccomp filter failed: %w", err)
}
filters = append(filters, filterMain)
rulesMaskedEqual := []seccompRule{
// don't allow faking input to the controlling tty (CVE-2017-5226)
{
Action: actionEperm,
Syscall: unix.SYS_IOCTL,
Arg: 1,
Op: seccomp.CompareMaskedEqual,
OpValue1: 0xFFFFFFFF,
OpValue2: unix.TIOCSTI,
},
// block copy/paste operations on virtual consoles (CVE-2023-28100)
{
Action: actionEperm,
Syscall: unix.SYS_IOCTL,
Arg: 1,
Op: seccomp.CompareMaskedEqual,
OpValue1: 0xFFFFFFFF,
OpValue2: unix.TIOCLINUX,
},
// block creating a new user namespace
{
Action: actionEperm,
Syscall: unix.SYS_CLONE,
Arg: 0,
Op: seccomp.CompareMaskedEqual,
OpValue1: unix.CLONE_NEWUSER,
OpValue2: unix.CLONE_NEWUSER,
},
}
filterMaskedEqual, err := loadFilter(seccomp.ActAllow, debug, rulesMaskedEqual)
if err != nil {
return nil, fmt.Errorf("loading masked-equal seccomp filter failed: %w", err)
}
// add after filterMain so it is evaluated first by the kernel
filters = append(filters, filterMaskedEqual)
return filters, nil
}