aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/seccomp/presets.go
blob: 4b76af61829ec02ffa27ef88c94ef4e4bc2a0b4c (plain)
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
package seccomp

/* flatpak commit 4c3bf179e2e4a2a298cd1db1d045adaf3f564532 */

import (
	. "syscall"

	. "hakurei.app/container/std"
	. "hakurei.app/ext"
)

func Preset(presets FilterPreset, flags ExportFlag) (rules []NativeRule) {
	allowedPersonality := PersonaLinux
	if presets&PresetLinux32 != 0 {
		allowedPersonality = PersonaLinux32
	}
	presetDevelFinal := presetDevel(ScmpDatum(allowedPersonality))

	l := len(presetCommon)
	if presets&PresetDenyNS != 0 {
		l += len(presetNamespace)
	}
	if presets&PresetDenyTTY != 0 {
		l += len(presetTTY)
	}
	if presets&PresetDenyDevel != 0 {
		l += len(presetDevelFinal)
	}
	if flags&AllowMultiarch == 0 {
		l += len(presetEmu)
	}
	if presets&PresetExt != 0 {
		l += len(presetCommonExt)
		if presets&PresetDenyNS != 0 {
			l += len(presetNamespaceExt)
		}
		if flags&AllowMultiarch == 0 {
			l += len(presetEmuExt)
		}
	}

	rules = make([]NativeRule, 0, l)
	rules = append(rules, presetCommon...)
	if presets&PresetDenyNS != 0 {
		rules = append(rules, presetNamespace...)
	}
	if presets&PresetDenyTTY != 0 {
		rules = append(rules, presetTTY...)
	}
	if presets&PresetDenyDevel != 0 {
		rules = append(rules, presetDevelFinal...)
	}
	if flags&AllowMultiarch == 0 {
		rules = append(rules, presetEmu...)
	}
	if presets&PresetExt != 0 {
		rules = append(rules, presetCommonExt...)
		if presets&PresetDenyNS != 0 {
			rules = append(rules, presetNamespaceExt...)
		}
		if flags&AllowMultiarch == 0 {
			rules = append(rules, presetEmuExt...)
		}
	}

	return
}

var (
	presetCommon = []NativeRule{
		/* Block dmesg */
		{Syscall: SNR_SYSLOG, Errno: ScmpErrno(EPERM), Arg: nil},
		/* Useless old syscall */
		{Syscall: SNR_USELIB, Errno: ScmpErrno(EPERM), Arg: nil},
		/* Don't allow disabling accounting */
		{Syscall: SNR_ACCT, Errno: ScmpErrno(EPERM), Arg: nil},
		/* Don't allow reading current quota use */
		{Syscall: SNR_QUOTACTL, Errno: ScmpErrno(EPERM), Arg: nil},

		/* Don't allow access to the kernel keyring */
		{Syscall: SNR_ADD_KEY, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_KEYCTL, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_REQUEST_KEY, Errno: ScmpErrno(EPERM), Arg: nil},

		/* Scary VM/NUMA ops */
		{Syscall: SNR_MOVE_PAGES, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_MBIND, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_GET_MEMPOLICY, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SET_MEMPOLICY, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_MIGRATE_PAGES, Errno: ScmpErrno(EPERM), Arg: nil},
	}

	/* hakurei: project-specific extensions */
	presetCommonExt = []NativeRule{
		/* system calls for changing the system clock */
		{Syscall: SNR_ADJTIMEX, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_CLOCK_ADJTIME, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_CLOCK_ADJTIME64, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_CLOCK_SETTIME, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_CLOCK_SETTIME64, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETTIMEOFDAY, Errno: ScmpErrno(EPERM), Arg: nil},

		/* loading and unloading of kernel modules */
		{Syscall: SNR_DELETE_MODULE, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_FINIT_MODULE, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_INIT_MODULE, Errno: ScmpErrno(EPERM), Arg: nil},

		/* system calls for rebooting and reboot preparation */
		{Syscall: SNR_KEXEC_FILE_LOAD, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_KEXEC_LOAD, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_REBOOT, Errno: ScmpErrno(EPERM), Arg: nil},

		/* system calls for enabling/disabling swap devices */
		{Syscall: SNR_SWAPOFF, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SWAPON, Errno: ScmpErrno(EPERM), Arg: nil},
	}

	presetNamespace = []NativeRule{
		/* Don't allow subnamespace setups: */
		{Syscall: SNR_UNSHARE, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETNS, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_MOUNT, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_UMOUNT, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_UMOUNT2, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_PIVOT_ROOT, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_CHROOT, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_CLONE, Errno: ScmpErrno(EPERM),
			Arg: &ScmpArgCmp{Arg: cloneArg, Op: SCMP_CMP_MASKED_EQ, DatumA: CLONE_NEWUSER, DatumB: CLONE_NEWUSER}},

		/* seccomp can't look into clone3()'s struct clone_args to check whether
		 * the flags are OK, so we have no choice but to block clone3().
		 * Return ENOSYS so user-space will fall back to clone().
		 * (CVE-2021-41133; see also https://github.com/moby/moby/commit/9f6b562d)
		 */
		{Syscall: SNR_CLONE3, Errno: ScmpErrno(ENOSYS), Arg: nil},

		/* New mount manipulation APIs can also change our VFS. There's no
		 * legitimate reason to do these in the sandbox, so block all of them
		 * rather than thinking about which ones might be dangerous.
		 * (CVE-2021-41133) */
		{Syscall: SNR_OPEN_TREE, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_MOVE_MOUNT, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_FSOPEN, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_FSCONFIG, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_FSMOUNT, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_FSPICK, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_MOUNT_SETATTR, Errno: ScmpErrno(ENOSYS), Arg: nil},
	}

	/* hakurei: project-specific extensions */
	presetNamespaceExt = []NativeRule{
		/* changing file ownership */
		{Syscall: SNR_CHOWN, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_CHOWN32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_FCHOWN, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_FCHOWN32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_FCHOWNAT, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_LCHOWN, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_LCHOWN32, Errno: ScmpErrno(EPERM), Arg: nil},

		/* system calls for changing user ID and group ID credentials */
		{Syscall: SNR_SETGID, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETGID32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETGROUPS, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETGROUPS32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETREGID, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETREGID32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETRESGID, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETRESGID32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETRESUID, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETRESUID32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETREUID, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETREUID32, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETUID, Errno: ScmpErrno(EPERM), Arg: nil},
		{Syscall: SNR_SETUID32, Errno: ScmpErrno(EPERM), Arg: nil},
	}

	presetTTY = []NativeRule{
		/* Don't allow faking input to the controlling tty (CVE-2017-5226) */
		{Syscall: SNR_IOCTL, Errno: ScmpErrno(EPERM),
			Arg: &ScmpArgCmp{Arg: 1, Op: SCMP_CMP_MASKED_EQ, DatumA: 0xFFFFFFFF, DatumB: TIOCSTI}},
		/* In the unlikely event that the controlling tty is a Linux virtual
		 * console (/dev/tty2 or similar), copy/paste operations have an effect
		 * similar to TIOCSTI (CVE-2023-28100) */
		{Syscall: SNR_IOCTL, Errno: ScmpErrno(EPERM),
			Arg: &ScmpArgCmp{Arg: 1, Op: SCMP_CMP_MASKED_EQ, DatumA: 0xFFFFFFFF, DatumB: TIOCLINUX}},
	}

	presetEmu = []NativeRule{
		/* modify_ldt is a historic source of interesting information leaks,
		 * so it's disabled as a hardening measure.
		 * However, it is required to run old 16-bit applications
		 * as well as some Wine patches, so it's allowed in multiarch. */
		{Syscall: SNR_MODIFY_LDT, Errno: ScmpErrno(EPERM), Arg: nil},
	}

	/* hakurei: project-specific extensions */
	presetEmuExt = []NativeRule{
		{Syscall: SNR_SUBPAGE_PROT, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_SWITCH_ENDIAN, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_VM86, Errno: ScmpErrno(ENOSYS), Arg: nil},
		{Syscall: SNR_VM86OLD, Errno: ScmpErrno(ENOSYS), Arg: nil},
	}
)

func presetDevel(allowedPersonality ScmpDatum) []NativeRule {
	return []NativeRule{
		/* Profiling operations; we expect these to be done by tools from outside
		 * the sandbox.  In particular perf has been the source of many CVEs. */
		{Syscall: SNR_PERF_EVENT_OPEN, Errno: ScmpErrno(EPERM), Arg: nil},
		/* Don't allow you to switch to bsd emulation or whatnot */
		{Syscall: SNR_PERSONALITY, Errno: ScmpErrno(EPERM),
			Arg: &ScmpArgCmp{Arg: 0, Op: SCMP_CMP_NE, DatumA: allowedPersonality}},

		{Syscall: SNR_PTRACE, Errno: ScmpErrno(EPERM), Arg: nil},
	}
}