/
create_profiles.go
335 lines (278 loc) · 9.01 KB
/
create_profiles.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
package generate
import (
"errors"
"fmt"
"strings"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
// createProfiles takes the declaration files and combines them into apparmor and seccomp profiles
func createProfiles(declarations []Declaration) (specs.LinuxSeccomp, AppArmorProfileConfig, error) {
var (
apparmorProfileName string
secAllows []string
secTraps []string
secTraces []string
secKills []string
secErrnos []string
capAllows []string
capDenies []string
fsReadOnlies []string
fsLogPaths []string
fsWritablePaths []string
fsAllowExecs []string
fsDenyExecs []string
netRaw bool
netPacket bool
netProtocols []string
sysArches = []string{"x86", "x86_64", "x32"}
sysDefaultAction = "errno"
)
// Combine declaration fields
for _, dec := range declarations {
// Profile name
apparmorProfileName = fmt.Sprintf("%s_%s", apparmorProfileName, dec.Name)
// System calls
secAllows = append(secAllows, dec.SystemCalls.Allow...)
secTraps = append(secTraps, dec.SystemCalls.Trap...)
secTraces = append(secTraces, dec.SystemCalls.Trace...)
secKills = append(secKills, dec.SystemCalls.Kill...)
secErrnos = append(secErrnos, dec.SystemCalls.Errno...)
// Capabilities
capAllows = append(capAllows, dec.Capabilities.Allow...)
capDenies = append(capDenies, dec.Capabilities.Deny...)
// File system rules
fsReadOnlies = append(fsReadOnlies, dec.Filesystem.ReadOnlyPaths...)
fsLogPaths = append(fsLogPaths, dec.Filesystem.LogOnWritePaths...)
fsWritablePaths = append(fsWritablePaths, dec.Filesystem.WritablePaths...)
fsAllowExecs = append(fsAllowExecs, dec.Filesystem.AllowExec...)
fsDenyExecs = append(fsDenyExecs, dec.Filesystem.DenyExec...)
// Network Configurations
netRaw = netRaw && dec.Network.Raw
netPacket = netPacket && dec.Network.Packet
netProtocols = append(netProtocols, dec.Network.Protocols...)
// System Configurations
sysArches = append(sysArches, dec.System.Architectures...)
if shouldOverwrite(dec.System.DefaultSyscallAction, sysDefaultAction) {
sysDefaultAction = dec.System.DefaultSyscallAction
}
}
///////////////////////
// //
// SECCOMP PARSING //
// //
///////////////////////
// Determine the action for each syscall rule based on precedence
// explained here: https://www.kernel.org/doc/Documentation/prctl/seccomp_filter.txt
// ignore if default is already set to that action
syscallRules := make(map[string]specs.LinuxSeccompAction)
if sysDefaultAction != "allow" {
for _, syscall := range secAllows {
syscallRules[syscall] = specs.ActAllow
}
}
if sysDefaultAction != "trace" {
for _, syscall := range secTraces {
syscallRules[syscall] = specs.ActTrace
}
}
if sysDefaultAction != "trap" {
for _, syscall := range secTraps {
syscallRules[syscall] = specs.ActTrap
}
}
if sysDefaultAction != "errno" {
for _, syscall := range secErrnos {
syscallRules[syscall] = specs.ActErrno
}
}
if sysDefaultAction != "kill" {
for _, syscall := range secKills {
syscallRules[syscall] = specs.ActKill
}
}
seccompProfile := specs.LinuxSeccomp{}
allowRule := specs.LinuxSyscall{Action: specs.ActAllow}
trapRule := specs.LinuxSyscall{Action: specs.ActTrap}
traceRule := specs.LinuxSyscall{Action: specs.ActTrace}
errnoRule := specs.LinuxSyscall{Action: specs.ActErrno}
killRule := specs.LinuxSyscall{Action: specs.ActKill}
// Create seccomp rules
for syscall, seccompAction := range syscallRules {
// If syscall arguments were specified, build and append unique filter
if strings.Contains(syscall, ":") {
name, args, err := collectArguments(syscall)
if err != nil {
return seccompProfile, AppArmorProfileConfig{}, err
}
new := specs.LinuxSyscall{
Names: []string{name},
Action: seccompAction,
Args: []specs.LinuxSeccompArg{args},
}
seccompProfile.Syscalls = append(seccompProfile.Syscalls, new)
continue
}
// Add to argument-less rules
switch seccompAction {
case specs.ActAllow:
allowRule.Names = append(allowRule.Names, syscall)
// Add corresponding capability needed to run syscall
cap := capabilityNeededForSyscall(syscall)
if cap != "" {
capAllows = append(capAllows)
}
case specs.ActTrap:
trapRule.Names = append(trapRule.Names, syscall)
case specs.ActTrace:
traceRule.Names = append(traceRule.Names, syscall)
case specs.ActErrno:
errnoRule.Names = append(errnoRule.Names, syscall)
case specs.ActKill:
killRule.Names = append(killRule.Names, syscall)
default:
return seccompProfile, AppArmorProfileConfig{}, errors.New("unrecognized seccomp action")
}
}
// Add argument-less rules
if len(allowRule.Names) > 0 {
seccompProfile.Syscalls = append(seccompProfile.Syscalls, allowRule)
}
if len(trapRule.Names) > 0 {
seccompProfile.Syscalls = append(seccompProfile.Syscalls, trapRule)
}
if len(traceRule.Names) > 0 {
seccompProfile.Syscalls = append(seccompProfile.Syscalls, traceRule)
}
if len(errnoRule.Names) > 0 {
seccompProfile.Syscalls = append(seccompProfile.Syscalls, errnoRule)
}
if len(killRule.Names) > 0 {
seccompProfile.Syscalls = append(seccompProfile.Syscalls, killRule)
}
// Determine seccomp architectures
for _, i := range sysArches {
ociArch, err := ociArchitecture(i)
if err != nil {
return seccompProfile, AppArmorProfileConfig{}, fmt.Errorf("%+v: unrecognized architecture: %s", err, i)
}
if !archContains(seccompProfile.Architectures, ociArch) {
seccompProfile.Architectures = append(seccompProfile.Architectures, ociArch)
}
}
// Set seccomp default action
def, err := ociSeccompAction(sysDefaultAction)
if err != nil {
return seccompProfile, AppArmorProfileConfig{}, err
}
seccompProfile.DefaultAction = def
//////////////////////
// //
// APPARMOR PARSING //
// //
//////////////////////
// Have shared capabilities map
capabilities := make(map[string]bool)
// set capabilities to allow
for _, cap := range capAllows {
capabilities[cap] = true
}
// set capabilities to deny, override any duplicates that allow
for _, cap := range capDenies {
capabilities[cap] = false
}
capabilitiesConfig := Capabilities{}
// Populate generatable capabilities configuration
for cap, allowed := range capabilities {
if allowed {
capabilitiesConfig.Allow = append(capabilitiesConfig.Allow, cap)
} else {
capabilitiesConfig.Deny = append(capabilitiesConfig.Deny, cap)
}
}
filesystemConfig := FileSystem{
ReadOnlyPaths: fsReadOnlies,
LogOnWritePaths: fsLogPaths,
WritablePaths: fsWritablePaths,
AllowExec: fsAllowExecs, //TODO: duplicates in DenyExec should override AllowExec
DenyExec: fsDenyExecs,
}
netConfig := Network{
Raw: netRaw,
Packet: netPacket,
Protocols: netProtocols,
}
apc := AppArmorProfileConfig{
Name: apparmorProfileName,
Filesystem: filesystemConfig,
Network: netConfig,
Capabilities: capabilitiesConfig,
}
return seccompProfile, apc, nil
}
// returns the capabilty that needs to be added to the apparmor for a specified syscall in seccomp
func capabilityNeededForSyscall(syscall string) string {
relations := map[string]string{
"open_by_handle_at": "dac_read_search",
"bpf": "sys_admin",
"clone": "sys_admin",
"fanotify_init": "sys_admin",
"lookup_dcookie": "sys_admin",
"mount": "sys_admin",
"name_to_handle_at": "sys_admin",
"perf_event_open": "sys_admin",
"quotactl": "sys_admin",
"setdomainname": "sys_admin",
"sethostname": "sys_admin",
"setns": "sys_admin",
"umount": "sys_admin",
"umount2": "sys_admin",
"unshare": "sys_admin",
"reboot": "sys_boot",
"chroot": "sys_chroot",
"delete_module": "sys_module",
"init_module": "sys_module",
"finit_module": "sys_module",
"query_module": "sys_module",
"acct": "sys_pact",
"kcmp": "sys_ptrace",
"process_vm_readv": "sys_ptrace",
"process_vm_writev": "sys_ptrace",
"ptrace": "sys_ptrace",
"iopl": "sys_rawio",
"ioperm": "sys_rawio",
"settimeofday": "sys_time",
"stime": "sys_time",
"clock_settime": "sys_time",
"vhangup": "sys_tty_config",
"fchownat": "chown",
"lchown": "chown",
"lchown32": "chown",
"fchown32": "chown",
"chown": "chown",
"fchown": "chown",
"chown32": "chown",
"mlock": "ipc_lock",
"mlock2": "ipc_lock",
"mlockall": "ipc_lock",
}
return relations[syscall]
}
// for use with default action
func shouldOverwrite(new, old string) bool {
precedence := map[string]int{
"allow": 1,
"trap": 2,
"trace": 3,
"errno": 4,
"kill": 5,
}
return (precedence[new] > precedence[old])
}
func archContains(slice []specs.Arch, item specs.Arch) bool {
for _, element := range slice {
if element == item {
return true
}
}
return false
}