Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/kernel/seccomp.c | |
4 | * | |
5 | * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com> | |
6 | * | |
e2cfabdf WD |
7 | * Copyright (C) 2012 Google, Inc. |
8 | * Will Drewry <wad@chromium.org> | |
9 | * | |
10 | * This defines a simple but solid secure-computing facility. | |
11 | * | |
12 | * Mode 1 uses a fixed list of allowed system calls. | |
13 | * Mode 2 allows user-defined system call filters in the form | |
14 | * of Berkeley Packet Filters/Linux Socket Filters. | |
1da177e4 LT |
15 | */ |
16 | ||
0b5fa229 | 17 | #include <linux/refcount.h> |
85e7bac3 | 18 | #include <linux/audit.h> |
5b101740 | 19 | #include <linux/compat.h> |
b25e6716 | 20 | #include <linux/coredump.h> |
8e5f1ad1 | 21 | #include <linux/kmemleak.h> |
5c307089 KC |
22 | #include <linux/nospec.h> |
23 | #include <linux/prctl.h> | |
e2cfabdf | 24 | #include <linux/sched.h> |
68db0cf1 | 25 | #include <linux/sched/task_stack.h> |
e2cfabdf | 26 | #include <linux/seccomp.h> |
c8bee430 | 27 | #include <linux/slab.h> |
48dc92b9 | 28 | #include <linux/syscalls.h> |
8e5f1ad1 | 29 | #include <linux/sysctl.h> |
1da177e4 | 30 | |
a4412fc9 | 31 | #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER |
e2cfabdf | 32 | #include <asm/syscall.h> |
a4412fc9 | 33 | #endif |
e2cfabdf WD |
34 | |
35 | #ifdef CONFIG_SECCOMP_FILTER | |
6a21cc50 | 36 | #include <linux/file.h> |
e2cfabdf | 37 | #include <linux/filter.h> |
c2e1f2e3 | 38 | #include <linux/pid.h> |
fb0fadf9 | 39 | #include <linux/ptrace.h> |
e2cfabdf | 40 | #include <linux/security.h> |
e2cfabdf WD |
41 | #include <linux/tracehook.h> |
42 | #include <linux/uaccess.h> | |
6a21cc50 | 43 | #include <linux/anon_inodes.h> |
9f87dcf1 | 44 | #include <linux/lockdep.h> |
6a21cc50 TA |
45 | |
46 | enum notify_state { | |
47 | SECCOMP_NOTIFY_INIT, | |
48 | SECCOMP_NOTIFY_SENT, | |
49 | SECCOMP_NOTIFY_REPLIED, | |
50 | }; | |
51 | ||
52 | struct seccomp_knotif { | |
53 | /* The struct pid of the task whose filter triggered the notification */ | |
54 | struct task_struct *task; | |
55 | ||
56 | /* The "cookie" for this request; this is unique for this filter. */ | |
57 | u64 id; | |
58 | ||
59 | /* | |
60 | * The seccomp data. This pointer is valid the entire time this | |
61 | * notification is active, since it comes from __seccomp_filter which | |
62 | * eclipses the entire lifecycle here. | |
63 | */ | |
64 | const struct seccomp_data *data; | |
65 | ||
66 | /* | |
67 | * Notification states. When SECCOMP_RET_USER_NOTIF is returned, a | |
68 | * struct seccomp_knotif is created and starts out in INIT. Once the | |
69 | * handler reads the notification off of an FD, it transitions to SENT. | |
70 | * If a signal is received the state transitions back to INIT and | |
71 | * another message is sent. When the userspace handler replies, state | |
72 | * transitions to REPLIED. | |
73 | */ | |
74 | enum notify_state state; | |
75 | ||
76 | /* The return values, only valid when in SECCOMP_NOTIFY_REPLIED */ | |
77 | int error; | |
78 | long val; | |
fb3c5386 | 79 | u32 flags; |
6a21cc50 TA |
80 | |
81 | /* Signals when this has entered SECCOMP_NOTIFY_REPLIED */ | |
82 | struct completion ready; | |
83 | ||
84 | struct list_head list; | |
85 | }; | |
86 | ||
87 | /** | |
88 | * struct notification - container for seccomp userspace notifications. Since | |
89 | * most seccomp filters will not have notification listeners attached and this | |
90 | * structure is fairly large, we store the notification-specific stuff in a | |
91 | * separate structure. | |
92 | * | |
93 | * @request: A semaphore that users of this notification can wait on for | |
94 | * changes. Actual reads and writes are still controlled with | |
95 | * filter->notify_lock. | |
96 | * @next_id: The id of the next request. | |
97 | * @notifications: A list of struct seccomp_knotif elements. | |
6a21cc50 TA |
98 | */ |
99 | struct notification { | |
100 | struct semaphore request; | |
101 | u64 next_id; | |
102 | struct list_head notifications; | |
6a21cc50 | 103 | }; |
e2cfabdf WD |
104 | |
105 | /** | |
106 | * struct seccomp_filter - container for seccomp BPF programs | |
107 | * | |
b707ddee CB |
108 | * @refs: Reference count to manage the object lifetime. |
109 | * A filter's reference count is incremented for each directly | |
110 | * attached task, once for the dependent filter, and if | |
111 | * requested for the user notifier. When @refs reaches zero, | |
112 | * the filter can be freed. | |
99cdb8b9 CB |
113 | * @users: A filter's @users count is incremented for each directly |
114 | * attached task (filter installation, fork(), thread_sync), | |
115 | * and once for the dependent filter (tracked in filter->prev). | |
116 | * When it reaches zero it indicates that no direct or indirect | |
117 | * users of that filter exist. No new tasks can get associated with | |
118 | * this filter after reaching 0. The @users count is always smaller | |
119 | * or equal to @refs. Hence, reaching 0 for @users does not mean | |
120 | * the filter can be freed. | |
e66a3997 | 121 | * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged |
e2cfabdf | 122 | * @prev: points to a previously installed, or inherited, filter |
285fdfc5 | 123 | * @prog: the BPF program to evaluate |
6a21cc50 TA |
124 | * @notif: the struct that holds all notification related information |
125 | * @notify_lock: A lock for all notification-related accesses. | |
76194c4e | 126 | * @wqh: A wait queue for poll if a notifier is in use. |
e2cfabdf WD |
127 | * |
128 | * seccomp_filter objects are organized in a tree linked via the @prev | |
129 | * pointer. For any task, it appears to be a singly-linked list starting | |
130 | * with current->seccomp.filter, the most recently attached or inherited filter. | |
131 | * However, multiple filters may share a @prev node, by way of fork(), which | |
132 | * results in a unidirectional tree existing in memory. This is similar to | |
133 | * how namespaces work. | |
134 | * | |
135 | * seccomp_filter objects should never be modified after being attached | |
b707ddee | 136 | * to a task_struct (other than @refs). |
e2cfabdf WD |
137 | */ |
138 | struct seccomp_filter { | |
b707ddee | 139 | refcount_t refs; |
99cdb8b9 | 140 | refcount_t users; |
e66a3997 | 141 | bool log; |
e2cfabdf | 142 | struct seccomp_filter *prev; |
7ae457c1 | 143 | struct bpf_prog *prog; |
6a21cc50 TA |
144 | struct notification *notif; |
145 | struct mutex notify_lock; | |
76194c4e | 146 | wait_queue_head_t wqh; |
e2cfabdf WD |
147 | }; |
148 | ||
149 | /* Limit any path through the tree to 256KB worth of instructions. */ | |
150 | #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) | |
151 | ||
bd4cf0ed | 152 | /* |
e2cfabdf WD |
153 | * Endianness is explicitly ignored and left for BPF program authors to manage |
154 | * as per the specific architecture. | |
155 | */ | |
bd4cf0ed | 156 | static void populate_seccomp_data(struct seccomp_data *sd) |
e2cfabdf | 157 | { |
bd4cf0ed AS |
158 | struct task_struct *task = current; |
159 | struct pt_regs *regs = task_pt_regs(task); | |
2eac7648 | 160 | unsigned long args[6]; |
e2cfabdf | 161 | |
bd4cf0ed | 162 | sd->nr = syscall_get_nr(task, regs); |
16add411 | 163 | sd->arch = syscall_get_arch(task); |
b35f549d | 164 | syscall_get_arguments(task, regs, args); |
2eac7648 DB |
165 | sd->args[0] = args[0]; |
166 | sd->args[1] = args[1]; | |
167 | sd->args[2] = args[2]; | |
168 | sd->args[3] = args[3]; | |
169 | sd->args[4] = args[4]; | |
170 | sd->args[5] = args[5]; | |
bd4cf0ed | 171 | sd->instruction_pointer = KSTK_EIP(task); |
e2cfabdf WD |
172 | } |
173 | ||
174 | /** | |
175 | * seccomp_check_filter - verify seccomp filter code | |
176 | * @filter: filter to verify | |
177 | * @flen: length of filter | |
178 | * | |
4df95ff4 | 179 | * Takes a previously checked filter (by bpf_check_classic) and |
e2cfabdf WD |
180 | * redirects all filter code that loads struct sk_buff data |
181 | * and related data through seccomp_bpf_load. It also | |
182 | * enforces length and alignment checking of those loads. | |
183 | * | |
184 | * Returns 0 if the rule set is legal or -EINVAL if not. | |
185 | */ | |
186 | static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) | |
187 | { | |
188 | int pc; | |
189 | for (pc = 0; pc < flen; pc++) { | |
190 | struct sock_filter *ftest = &filter[pc]; | |
191 | u16 code = ftest->code; | |
192 | u32 k = ftest->k; | |
193 | ||
194 | switch (code) { | |
34805931 | 195 | case BPF_LD | BPF_W | BPF_ABS: |
bd4cf0ed | 196 | ftest->code = BPF_LDX | BPF_W | BPF_ABS; |
e2cfabdf WD |
197 | /* 32-bit aligned and not out of bounds. */ |
198 | if (k >= sizeof(struct seccomp_data) || k & 3) | |
199 | return -EINVAL; | |
200 | continue; | |
34805931 | 201 | case BPF_LD | BPF_W | BPF_LEN: |
bd4cf0ed | 202 | ftest->code = BPF_LD | BPF_IMM; |
e2cfabdf WD |
203 | ftest->k = sizeof(struct seccomp_data); |
204 | continue; | |
34805931 | 205 | case BPF_LDX | BPF_W | BPF_LEN: |
bd4cf0ed | 206 | ftest->code = BPF_LDX | BPF_IMM; |
e2cfabdf WD |
207 | ftest->k = sizeof(struct seccomp_data); |
208 | continue; | |
209 | /* Explicitly include allowed calls. */ | |
34805931 DB |
210 | case BPF_RET | BPF_K: |
211 | case BPF_RET | BPF_A: | |
212 | case BPF_ALU | BPF_ADD | BPF_K: | |
213 | case BPF_ALU | BPF_ADD | BPF_X: | |
214 | case BPF_ALU | BPF_SUB | BPF_K: | |
215 | case BPF_ALU | BPF_SUB | BPF_X: | |
216 | case BPF_ALU | BPF_MUL | BPF_K: | |
217 | case BPF_ALU | BPF_MUL | BPF_X: | |
218 | case BPF_ALU | BPF_DIV | BPF_K: | |
219 | case BPF_ALU | BPF_DIV | BPF_X: | |
220 | case BPF_ALU | BPF_AND | BPF_K: | |
221 | case BPF_ALU | BPF_AND | BPF_X: | |
222 | case BPF_ALU | BPF_OR | BPF_K: | |
223 | case BPF_ALU | BPF_OR | BPF_X: | |
224 | case BPF_ALU | BPF_XOR | BPF_K: | |
225 | case BPF_ALU | BPF_XOR | BPF_X: | |
226 | case BPF_ALU | BPF_LSH | BPF_K: | |
227 | case BPF_ALU | BPF_LSH | BPF_X: | |
228 | case BPF_ALU | BPF_RSH | BPF_K: | |
229 | case BPF_ALU | BPF_RSH | BPF_X: | |
230 | case BPF_ALU | BPF_NEG: | |
231 | case BPF_LD | BPF_IMM: | |
232 | case BPF_LDX | BPF_IMM: | |
233 | case BPF_MISC | BPF_TAX: | |
234 | case BPF_MISC | BPF_TXA: | |
235 | case BPF_LD | BPF_MEM: | |
236 | case BPF_LDX | BPF_MEM: | |
237 | case BPF_ST: | |
238 | case BPF_STX: | |
239 | case BPF_JMP | BPF_JA: | |
240 | case BPF_JMP | BPF_JEQ | BPF_K: | |
241 | case BPF_JMP | BPF_JEQ | BPF_X: | |
242 | case BPF_JMP | BPF_JGE | BPF_K: | |
243 | case BPF_JMP | BPF_JGE | BPF_X: | |
244 | case BPF_JMP | BPF_JGT | BPF_K: | |
245 | case BPF_JMP | BPF_JGT | BPF_X: | |
246 | case BPF_JMP | BPF_JSET | BPF_K: | |
247 | case BPF_JMP | BPF_JSET | BPF_X: | |
e2cfabdf WD |
248 | continue; |
249 | default: | |
250 | return -EINVAL; | |
251 | } | |
252 | } | |
253 | return 0; | |
254 | } | |
255 | ||
256 | /** | |
285fdfc5 MS |
257 | * seccomp_run_filters - evaluates all seccomp filters against @sd |
258 | * @sd: optional seccomp data to be passed to filters | |
deb4de8b KC |
259 | * @match: stores struct seccomp_filter that resulted in the return value, |
260 | * unless filter returned SECCOMP_RET_ALLOW, in which case it will | |
261 | * be unchanged. | |
e2cfabdf WD |
262 | * |
263 | * Returns valid seccomp BPF response codes. | |
264 | */ | |
0466bdb9 | 265 | #define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL))) |
deb4de8b KC |
266 | static u32 seccomp_run_filters(const struct seccomp_data *sd, |
267 | struct seccomp_filter **match) | |
e2cfabdf | 268 | { |
acf3b2c7 | 269 | u32 ret = SECCOMP_RET_ALLOW; |
8225d385 PK |
270 | /* Make sure cross-thread synced filter points somewhere sane. */ |
271 | struct seccomp_filter *f = | |
506458ef | 272 | READ_ONCE(current->seccomp.filter); |
acf3b2c7 WD |
273 | |
274 | /* Ensure unexpected behavior doesn't result in failing open. */ | |
0d42d73a | 275 | if (WARN_ON(f == NULL)) |
4d3b0b05 | 276 | return SECCOMP_RET_KILL_PROCESS; |
acf3b2c7 | 277 | |
e2cfabdf WD |
278 | /* |
279 | * All filters in the list are evaluated and the lowest BPF return | |
acf3b2c7 | 280 | * value always takes priority (ignoring the DATA). |
e2cfabdf | 281 | */ |
3ba2530c | 282 | for (; f; f = f->prev) { |
3d9f773c | 283 | u32 cur_ret = bpf_prog_run_pin_on_cpu(f->prog, sd); |
8f577cad | 284 | |
0466bdb9 | 285 | if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) { |
acf3b2c7 | 286 | ret = cur_ret; |
deb4de8b KC |
287 | *match = f; |
288 | } | |
e2cfabdf WD |
289 | } |
290 | return ret; | |
291 | } | |
1f41b450 | 292 | #endif /* CONFIG_SECCOMP_FILTER */ |
e2cfabdf | 293 | |
1f41b450 KC |
294 | static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode) |
295 | { | |
69f6a34b | 296 | assert_spin_locked(¤t->sighand->siglock); |
dbd95212 | 297 | |
1f41b450 KC |
298 | if (current->seccomp.mode && current->seccomp.mode != seccomp_mode) |
299 | return false; | |
300 | ||
301 | return true; | |
302 | } | |
303 | ||
8bf37d8c | 304 | void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { } |
5c307089 | 305 | |
3ba2530c | 306 | static inline void seccomp_assign_mode(struct task_struct *task, |
00a02d0c KC |
307 | unsigned long seccomp_mode, |
308 | unsigned long flags) | |
1f41b450 | 309 | { |
69f6a34b | 310 | assert_spin_locked(&task->sighand->siglock); |
dbd95212 | 311 | |
3ba2530c KC |
312 | task->seccomp.mode = seccomp_mode; |
313 | /* | |
314 | * Make sure TIF_SECCOMP cannot be set before the mode (and | |
315 | * filter) is set. | |
316 | */ | |
317 | smp_mb__before_atomic(); | |
00a02d0c KC |
318 | /* Assume default seccomp processes want spec flaw mitigation. */ |
319 | if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0) | |
8bf37d8c | 320 | arch_seccomp_spec_mitigate(task); |
3ba2530c | 321 | set_tsk_thread_flag(task, TIF_SECCOMP); |
1f41b450 KC |
322 | } |
323 | ||
324 | #ifdef CONFIG_SECCOMP_FILTER | |
c2e1f2e3 KC |
325 | /* Returns 1 if the parent is an ancestor of the child. */ |
326 | static int is_ancestor(struct seccomp_filter *parent, | |
327 | struct seccomp_filter *child) | |
328 | { | |
329 | /* NULL is the root ancestor. */ | |
330 | if (parent == NULL) | |
331 | return 1; | |
332 | for (; child; child = child->prev) | |
333 | if (child == parent) | |
334 | return 1; | |
335 | return 0; | |
336 | } | |
337 | ||
338 | /** | |
339 | * seccomp_can_sync_threads: checks if all threads can be synchronized | |
340 | * | |
341 | * Expects sighand and cred_guard_mutex locks to be held. | |
342 | * | |
343 | * Returns 0 on success, -ve on error, or the pid of a thread which was | |
6beff00b | 344 | * either not in the correct seccomp mode or did not have an ancestral |
c2e1f2e3 KC |
345 | * seccomp filter. |
346 | */ | |
347 | static inline pid_t seccomp_can_sync_threads(void) | |
348 | { | |
349 | struct task_struct *thread, *caller; | |
350 | ||
351 | BUG_ON(!mutex_is_locked(¤t->signal->cred_guard_mutex)); | |
69f6a34b | 352 | assert_spin_locked(¤t->sighand->siglock); |
c2e1f2e3 KC |
353 | |
354 | /* Validate all threads being eligible for synchronization. */ | |
355 | caller = current; | |
356 | for_each_thread(caller, thread) { | |
357 | pid_t failed; | |
358 | ||
359 | /* Skip current, since it is initiating the sync. */ | |
360 | if (thread == caller) | |
361 | continue; | |
362 | ||
363 | if (thread->seccomp.mode == SECCOMP_MODE_DISABLED || | |
364 | (thread->seccomp.mode == SECCOMP_MODE_FILTER && | |
365 | is_ancestor(thread->seccomp.filter, | |
366 | caller->seccomp.filter))) | |
367 | continue; | |
368 | ||
369 | /* Return the first thread that cannot be synchronized. */ | |
370 | failed = task_pid_vnr(thread); | |
371 | /* If the pid cannot be resolved, then return -ESRCH */ | |
0d42d73a | 372 | if (WARN_ON(failed == 0)) |
c2e1f2e3 KC |
373 | failed = -ESRCH; |
374 | return failed; | |
375 | } | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
3a15fb6e CB |
380 | static inline void seccomp_filter_free(struct seccomp_filter *filter) |
381 | { | |
382 | if (filter) { | |
383 | bpf_prog_destroy(filter->prog); | |
384 | kfree(filter); | |
385 | } | |
386 | } | |
387 | ||
99cdb8b9 CB |
388 | static void __seccomp_filter_orphan(struct seccomp_filter *orig) |
389 | { | |
390 | while (orig && refcount_dec_and_test(&orig->users)) { | |
391 | if (waitqueue_active(&orig->wqh)) | |
392 | wake_up_poll(&orig->wqh, EPOLLHUP); | |
393 | orig = orig->prev; | |
394 | } | |
395 | } | |
396 | ||
3a15fb6e CB |
397 | static void __put_seccomp_filter(struct seccomp_filter *orig) |
398 | { | |
399 | /* Clean up single-reference branches iteratively. */ | |
400 | while (orig && refcount_dec_and_test(&orig->refs)) { | |
401 | struct seccomp_filter *freeme = orig; | |
402 | orig = orig->prev; | |
403 | seccomp_filter_free(freeme); | |
404 | } | |
405 | } | |
406 | ||
99cdb8b9 CB |
407 | static void __seccomp_filter_release(struct seccomp_filter *orig) |
408 | { | |
409 | /* Notify about any unused filters in the task's former filter tree. */ | |
410 | __seccomp_filter_orphan(orig); | |
411 | /* Finally drop all references to the task's former tree. */ | |
412 | __put_seccomp_filter(orig); | |
413 | } | |
414 | ||
3a15fb6e | 415 | /** |
99cdb8b9 CB |
416 | * seccomp_filter_release - Detach the task from its filter tree, |
417 | * drop its reference count, and notify | |
418 | * about unused filters | |
3a15fb6e CB |
419 | * |
420 | * This function should only be called when the task is exiting as | |
421 | * it detaches it from its filter tree. As such, READ_ONCE() and | |
422 | * barriers are not needed here, as would normally be needed. | |
423 | */ | |
424 | void seccomp_filter_release(struct task_struct *tsk) | |
425 | { | |
426 | struct seccomp_filter *orig = tsk->seccomp.filter; | |
427 | ||
428 | /* Detach task from its filter tree. */ | |
429 | tsk->seccomp.filter = NULL; | |
99cdb8b9 | 430 | __seccomp_filter_release(orig); |
3a15fb6e CB |
431 | } |
432 | ||
c2e1f2e3 KC |
433 | /** |
434 | * seccomp_sync_threads: sets all threads to use current's filter | |
435 | * | |
436 | * Expects sighand and cred_guard_mutex locks to be held, and for | |
437 | * seccomp_can_sync_threads() to have returned success already | |
438 | * without dropping the locks. | |
439 | * | |
440 | */ | |
00a02d0c | 441 | static inline void seccomp_sync_threads(unsigned long flags) |
c2e1f2e3 KC |
442 | { |
443 | struct task_struct *thread, *caller; | |
444 | ||
445 | BUG_ON(!mutex_is_locked(¤t->signal->cred_guard_mutex)); | |
69f6a34b | 446 | assert_spin_locked(¤t->sighand->siglock); |
c2e1f2e3 KC |
447 | |
448 | /* Synchronize all threads. */ | |
449 | caller = current; | |
450 | for_each_thread(caller, thread) { | |
451 | /* Skip current, since it needs no changes. */ | |
452 | if (thread == caller) | |
453 | continue; | |
454 | ||
455 | /* Get a task reference for the new leaf node. */ | |
456 | get_seccomp_filter(caller); | |
99cdb8b9 | 457 | |
c2e1f2e3 KC |
458 | /* |
459 | * Drop the task reference to the shared ancestor since | |
460 | * current's path will hold a reference. (This also | |
461 | * allows a put before the assignment.) | |
462 | */ | |
99cdb8b9 CB |
463 | __seccomp_filter_release(thread->seccomp.filter); |
464 | ||
465 | /* Make our new filter tree visible. */ | |
c2e1f2e3 KC |
466 | smp_store_release(&thread->seccomp.filter, |
467 | caller->seccomp.filter); | |
c818c03b KC |
468 | atomic_set(&thread->seccomp.filter_count, |
469 | atomic_read(&thread->seccomp.filter_count)); | |
103502a3 JH |
470 | |
471 | /* | |
472 | * Don't let an unprivileged task work around | |
473 | * the no_new_privs restriction by creating | |
474 | * a thread that sets it up, enters seccomp, | |
475 | * then dies. | |
476 | */ | |
477 | if (task_no_new_privs(caller)) | |
478 | task_set_no_new_privs(thread); | |
479 | ||
c2e1f2e3 KC |
480 | /* |
481 | * Opt the other thread into seccomp if needed. | |
482 | * As threads are considered to be trust-realm | |
483 | * equivalent (see ptrace_may_access), it is safe to | |
484 | * allow one thread to transition the other. | |
485 | */ | |
103502a3 | 486 | if (thread->seccomp.mode == SECCOMP_MODE_DISABLED) |
00a02d0c KC |
487 | seccomp_assign_mode(thread, SECCOMP_MODE_FILTER, |
488 | flags); | |
c2e1f2e3 KC |
489 | } |
490 | } | |
491 | ||
e2cfabdf | 492 | /** |
c8bee430 | 493 | * seccomp_prepare_filter: Prepares a seccomp filter for use. |
e2cfabdf WD |
494 | * @fprog: BPF program to install |
495 | * | |
c8bee430 | 496 | * Returns filter on success or an ERR_PTR on failure. |
e2cfabdf | 497 | */ |
c8bee430 | 498 | static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog) |
e2cfabdf | 499 | { |
ac67eb2c DB |
500 | struct seccomp_filter *sfilter; |
501 | int ret; | |
97f2645f | 502 | const bool save_orig = IS_ENABLED(CONFIG_CHECKPOINT_RESTORE); |
e2cfabdf WD |
503 | |
504 | if (fprog->len == 0 || fprog->len > BPF_MAXINSNS) | |
c8bee430 | 505 | return ERR_PTR(-EINVAL); |
d9e12f42 | 506 | |
c8bee430 | 507 | BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter)); |
e2cfabdf WD |
508 | |
509 | /* | |
119ce5c8 | 510 | * Installing a seccomp filter requires that the task has |
e2cfabdf WD |
511 | * CAP_SYS_ADMIN in its namespace or be running with no_new_privs. |
512 | * This avoids scenarios where unprivileged tasks can affect the | |
513 | * behavior of privileged children. | |
514 | */ | |
1d4457f9 | 515 | if (!task_no_new_privs(current) && |
c1a85a00 MM |
516 | security_capable(current_cred(), current_user_ns(), |
517 | CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) != 0) | |
c8bee430 | 518 | return ERR_PTR(-EACCES); |
e2cfabdf | 519 | |
bd4cf0ed | 520 | /* Allocate a new seccomp_filter */ |
ac67eb2c DB |
521 | sfilter = kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN); |
522 | if (!sfilter) | |
d9e12f42 | 523 | return ERR_PTR(-ENOMEM); |
ac67eb2c | 524 | |
6a21cc50 | 525 | mutex_init(&sfilter->notify_lock); |
ac67eb2c | 526 | ret = bpf_prog_create_from_user(&sfilter->prog, fprog, |
f8e529ed | 527 | seccomp_check_filter, save_orig); |
ac67eb2c DB |
528 | if (ret < 0) { |
529 | kfree(sfilter); | |
530 | return ERR_PTR(ret); | |
d9e12f42 | 531 | } |
bd4cf0ed | 532 | |
b707ddee | 533 | refcount_set(&sfilter->refs, 1); |
99cdb8b9 | 534 | refcount_set(&sfilter->users, 1); |
76194c4e | 535 | init_waitqueue_head(&sfilter->wqh); |
e2cfabdf | 536 | |
ac67eb2c | 537 | return sfilter; |
e2cfabdf WD |
538 | } |
539 | ||
540 | /** | |
c8bee430 | 541 | * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog |
e2cfabdf WD |
542 | * @user_filter: pointer to the user data containing a sock_fprog. |
543 | * | |
544 | * Returns 0 on success and non-zero otherwise. | |
545 | */ | |
c8bee430 KC |
546 | static struct seccomp_filter * |
547 | seccomp_prepare_user_filter(const char __user *user_filter) | |
e2cfabdf WD |
548 | { |
549 | struct sock_fprog fprog; | |
c8bee430 | 550 | struct seccomp_filter *filter = ERR_PTR(-EFAULT); |
e2cfabdf WD |
551 | |
552 | #ifdef CONFIG_COMPAT | |
5c38065e | 553 | if (in_compat_syscall()) { |
e2cfabdf WD |
554 | struct compat_sock_fprog fprog32; |
555 | if (copy_from_user(&fprog32, user_filter, sizeof(fprog32))) | |
556 | goto out; | |
557 | fprog.len = fprog32.len; | |
558 | fprog.filter = compat_ptr(fprog32.filter); | |
559 | } else /* falls through to the if below. */ | |
560 | #endif | |
561 | if (copy_from_user(&fprog, user_filter, sizeof(fprog))) | |
562 | goto out; | |
c8bee430 | 563 | filter = seccomp_prepare_filter(&fprog); |
e2cfabdf | 564 | out: |
c8bee430 KC |
565 | return filter; |
566 | } | |
567 | ||
568 | /** | |
569 | * seccomp_attach_filter: validate and attach filter | |
570 | * @flags: flags to change filter behavior | |
571 | * @filter: seccomp filter to add to the current process | |
572 | * | |
dbd95212 KC |
573 | * Caller must be holding current->sighand->siglock lock. |
574 | * | |
7a0df7fb TA |
575 | * Returns 0 on success, -ve on error, or |
576 | * - in TSYNC mode: the pid of a thread which was either not in the correct | |
577 | * seccomp mode or did not have an ancestral seccomp filter | |
578 | * - in NEW_LISTENER mode: the fd of the new listener | |
c8bee430 KC |
579 | */ |
580 | static long seccomp_attach_filter(unsigned int flags, | |
581 | struct seccomp_filter *filter) | |
582 | { | |
583 | unsigned long total_insns; | |
584 | struct seccomp_filter *walker; | |
585 | ||
69f6a34b | 586 | assert_spin_locked(¤t->sighand->siglock); |
dbd95212 | 587 | |
c8bee430 KC |
588 | /* Validate resulting filter length. */ |
589 | total_insns = filter->prog->len; | |
590 | for (walker = current->seccomp.filter; walker; walker = walker->prev) | |
591 | total_insns += walker->prog->len + 4; /* 4 instr penalty */ | |
592 | if (total_insns > MAX_INSNS_PER_PATH) | |
593 | return -ENOMEM; | |
594 | ||
c2e1f2e3 KC |
595 | /* If thread sync has been requested, check that it is possible. */ |
596 | if (flags & SECCOMP_FILTER_FLAG_TSYNC) { | |
597 | int ret; | |
598 | ||
599 | ret = seccomp_can_sync_threads(); | |
51891498 TA |
600 | if (ret) { |
601 | if (flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) | |
602 | return -ESRCH; | |
603 | else | |
604 | return ret; | |
605 | } | |
c2e1f2e3 KC |
606 | } |
607 | ||
e66a3997 TH |
608 | /* Set log flag, if present. */ |
609 | if (flags & SECCOMP_FILTER_FLAG_LOG) | |
610 | filter->log = true; | |
611 | ||
c8bee430 KC |
612 | /* |
613 | * If there is an existing filter, make it the prev and don't drop its | |
614 | * task reference. | |
615 | */ | |
616 | filter->prev = current->seccomp.filter; | |
617 | current->seccomp.filter = filter; | |
c818c03b | 618 | atomic_inc(¤t->seccomp.filter_count); |
c8bee430 | 619 | |
c2e1f2e3 KC |
620 | /* Now that the new filter is in place, synchronize to all threads. */ |
621 | if (flags & SECCOMP_FILTER_FLAG_TSYNC) | |
00a02d0c | 622 | seccomp_sync_threads(flags); |
c2e1f2e3 | 623 | |
c8bee430 | 624 | return 0; |
e2cfabdf WD |
625 | } |
626 | ||
084f5601 | 627 | static void __get_seccomp_filter(struct seccomp_filter *filter) |
66a733ea | 628 | { |
b707ddee | 629 | refcount_inc(&filter->refs); |
66a733ea ON |
630 | } |
631 | ||
e2cfabdf WD |
632 | /* get_seccomp_filter - increments the reference count of the filter on @tsk */ |
633 | void get_seccomp_filter(struct task_struct *tsk) | |
634 | { | |
635 | struct seccomp_filter *orig = tsk->seccomp.filter; | |
636 | if (!orig) | |
637 | return; | |
66a733ea | 638 | __get_seccomp_filter(orig); |
99cdb8b9 | 639 | refcount_inc(&orig->users); |
e2cfabdf WD |
640 | } |
641 | ||
ae7795bc | 642 | static void seccomp_init_siginfo(kernel_siginfo_t *info, int syscall, int reason) |
b25e6716 | 643 | { |
3b10db2b | 644 | clear_siginfo(info); |
b25e6716 MF |
645 | info->si_signo = SIGSYS; |
646 | info->si_code = SYS_SECCOMP; | |
647 | info->si_call_addr = (void __user *)KSTK_EIP(current); | |
648 | info->si_errno = reason; | |
16add411 | 649 | info->si_arch = syscall_get_arch(current); |
b25e6716 MF |
650 | info->si_syscall = syscall; |
651 | } | |
652 | ||
bb6ea430 WD |
653 | /** |
654 | * seccomp_send_sigsys - signals the task to allow in-process syscall emulation | |
655 | * @syscall: syscall number to send to userland | |
656 | * @reason: filter-supplied reason code to send to userland (via si_errno) | |
657 | * | |
658 | * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info. | |
659 | */ | |
660 | static void seccomp_send_sigsys(int syscall, int reason) | |
661 | { | |
ae7795bc | 662 | struct kernel_siginfo info; |
b25e6716 | 663 | seccomp_init_siginfo(&info, syscall, reason); |
a89e9b8a | 664 | force_sig_info(&info); |
bb6ea430 | 665 | } |
e2cfabdf | 666 | #endif /* CONFIG_SECCOMP_FILTER */ |
1da177e4 | 667 | |
0ddec0fc | 668 | /* For use with seccomp_actions_logged */ |
4d3b0b05 KC |
669 | #define SECCOMP_LOG_KILL_PROCESS (1 << 0) |
670 | #define SECCOMP_LOG_KILL_THREAD (1 << 1) | |
0ddec0fc TH |
671 | #define SECCOMP_LOG_TRAP (1 << 2) |
672 | #define SECCOMP_LOG_ERRNO (1 << 3) | |
673 | #define SECCOMP_LOG_TRACE (1 << 4) | |
59f5cf44 TH |
674 | #define SECCOMP_LOG_LOG (1 << 5) |
675 | #define SECCOMP_LOG_ALLOW (1 << 6) | |
6a21cc50 | 676 | #define SECCOMP_LOG_USER_NOTIF (1 << 7) |
0ddec0fc | 677 | |
4d3b0b05 KC |
678 | static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS | |
679 | SECCOMP_LOG_KILL_THREAD | | |
fd76875c KC |
680 | SECCOMP_LOG_TRAP | |
681 | SECCOMP_LOG_ERRNO | | |
6a21cc50 | 682 | SECCOMP_LOG_USER_NOTIF | |
fd76875c | 683 | SECCOMP_LOG_TRACE | |
59f5cf44 | 684 | SECCOMP_LOG_LOG; |
0ddec0fc | 685 | |
e66a3997 TH |
686 | static inline void seccomp_log(unsigned long syscall, long signr, u32 action, |
687 | bool requested) | |
0ddec0fc TH |
688 | { |
689 | bool log = false; | |
690 | ||
691 | switch (action) { | |
692 | case SECCOMP_RET_ALLOW: | |
e66a3997 | 693 | break; |
0ddec0fc | 694 | case SECCOMP_RET_TRAP: |
e66a3997 TH |
695 | log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP; |
696 | break; | |
0ddec0fc | 697 | case SECCOMP_RET_ERRNO: |
e66a3997 TH |
698 | log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO; |
699 | break; | |
0ddec0fc | 700 | case SECCOMP_RET_TRACE: |
e66a3997 | 701 | log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE; |
0ddec0fc | 702 | break; |
6a21cc50 TA |
703 | case SECCOMP_RET_USER_NOTIF: |
704 | log = requested && seccomp_actions_logged & SECCOMP_LOG_USER_NOTIF; | |
705 | break; | |
59f5cf44 TH |
706 | case SECCOMP_RET_LOG: |
707 | log = seccomp_actions_logged & SECCOMP_LOG_LOG; | |
708 | break; | |
fd76875c | 709 | case SECCOMP_RET_KILL_THREAD: |
fd76875c | 710 | log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD; |
4d3b0b05 KC |
711 | break; |
712 | case SECCOMP_RET_KILL_PROCESS: | |
713 | default: | |
714 | log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS; | |
0ddec0fc TH |
715 | } |
716 | ||
717 | /* | |
326bee02 TH |
718 | * Emit an audit message when the action is RET_KILL_*, RET_LOG, or the |
719 | * FILTER_FLAG_LOG bit was set. The admin has the ability to silence | |
720 | * any action from being logged by removing the action name from the | |
721 | * seccomp_actions_logged sysctl. | |
0ddec0fc | 722 | */ |
326bee02 TH |
723 | if (!log) |
724 | return; | |
0ddec0fc | 725 | |
326bee02 | 726 | audit_seccomp(syscall, signr, action); |
0ddec0fc TH |
727 | } |
728 | ||
1da177e4 LT |
729 | /* |
730 | * Secure computing mode 1 allows only read/write/exit/sigreturn. | |
731 | * To be fully secure this must be combined with rlimit | |
732 | * to limit the stack allocations too. | |
733 | */ | |
cb4253aa | 734 | static const int mode1_syscalls[] = { |
1da177e4 LT |
735 | __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn, |
736 | 0, /* null terminated */ | |
737 | }; | |
738 | ||
a4412fc9 | 739 | static void __secure_computing_strict(int this_syscall) |
1da177e4 | 740 | { |
cb4253aa | 741 | const int *syscall_whitelist = mode1_syscalls; |
a4412fc9 | 742 | #ifdef CONFIG_COMPAT |
5c38065e | 743 | if (in_compat_syscall()) |
c983f0e8 | 744 | syscall_whitelist = get_compat_mode1_syscalls(); |
a4412fc9 AL |
745 | #endif |
746 | do { | |
747 | if (*syscall_whitelist == this_syscall) | |
748 | return; | |
749 | } while (*++syscall_whitelist); | |
750 | ||
751 | #ifdef SECCOMP_DEBUG | |
752 | dump_stack(); | |
753 | #endif | |
fd76875c | 754 | seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true); |
a4412fc9 AL |
755 | do_exit(SIGKILL); |
756 | } | |
757 | ||
758 | #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER | |
759 | void secure_computing_strict(int this_syscall) | |
760 | { | |
761 | int mode = current->seccomp.mode; | |
762 | ||
97f2645f | 763 | if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) && |
13c4a901 TA |
764 | unlikely(current->ptrace & PT_SUSPEND_SECCOMP)) |
765 | return; | |
766 | ||
221272f9 | 767 | if (mode == SECCOMP_MODE_DISABLED) |
a4412fc9 AL |
768 | return; |
769 | else if (mode == SECCOMP_MODE_STRICT) | |
770 | __secure_computing_strict(this_syscall); | |
771 | else | |
772 | BUG(); | |
773 | } | |
774 | #else | |
13aa72f0 AL |
775 | |
776 | #ifdef CONFIG_SECCOMP_FILTER | |
6a21cc50 TA |
777 | static u64 seccomp_next_notify_id(struct seccomp_filter *filter) |
778 | { | |
779 | /* | |
780 | * Note: overflow is ok here, the id just needs to be unique per | |
781 | * filter. | |
782 | */ | |
783 | lockdep_assert_held(&filter->notify_lock); | |
784 | return filter->notif->next_id++; | |
785 | } | |
786 | ||
fb3c5386 CB |
787 | static int seccomp_do_user_notification(int this_syscall, |
788 | struct seccomp_filter *match, | |
789 | const struct seccomp_data *sd) | |
6a21cc50 TA |
790 | { |
791 | int err; | |
fb3c5386 | 792 | u32 flags = 0; |
6a21cc50 TA |
793 | long ret = 0; |
794 | struct seccomp_knotif n = {}; | |
795 | ||
796 | mutex_lock(&match->notify_lock); | |
797 | err = -ENOSYS; | |
798 | if (!match->notif) | |
799 | goto out; | |
800 | ||
801 | n.task = current; | |
802 | n.state = SECCOMP_NOTIFY_INIT; | |
803 | n.data = sd; | |
804 | n.id = seccomp_next_notify_id(match); | |
805 | init_completion(&n.ready); | |
806 | list_add(&n.list, &match->notif->notifications); | |
807 | ||
808 | up(&match->notif->request); | |
76194c4e | 809 | wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM); |
6a21cc50 TA |
810 | mutex_unlock(&match->notify_lock); |
811 | ||
812 | /* | |
813 | * This is where we wait for a reply from userspace. | |
814 | */ | |
815 | err = wait_for_completion_interruptible(&n.ready); | |
816 | mutex_lock(&match->notify_lock); | |
817 | if (err == 0) { | |
818 | ret = n.val; | |
819 | err = n.error; | |
fb3c5386 | 820 | flags = n.flags; |
6a21cc50 TA |
821 | } |
822 | ||
823 | /* | |
824 | * Note that it's possible the listener died in between the time when | |
825 | * we were notified of a respons (or a signal) and when we were able to | |
826 | * re-acquire the lock, so only delete from the list if the | |
827 | * notification actually exists. | |
828 | * | |
829 | * Also note that this test is only valid because there's no way to | |
830 | * *reattach* to a notifier right now. If one is added, we'll need to | |
831 | * keep track of the notif itself and make sure they match here. | |
832 | */ | |
833 | if (match->notif) | |
834 | list_del(&n.list); | |
835 | out: | |
836 | mutex_unlock(&match->notify_lock); | |
fb3c5386 CB |
837 | |
838 | /* Userspace requests to continue the syscall. */ | |
839 | if (flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE) | |
840 | return 0; | |
841 | ||
6a21cc50 TA |
842 | syscall_set_return_value(current, task_pt_regs(current), |
843 | err, ret); | |
fb3c5386 | 844 | return -1; |
6a21cc50 TA |
845 | } |
846 | ||
ce6526e8 KC |
847 | static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, |
848 | const bool recheck_after_trace) | |
13aa72f0 AL |
849 | { |
850 | u32 filter_ret, action; | |
deb4de8b | 851 | struct seccomp_filter *match = NULL; |
13aa72f0 | 852 | int data; |
db511391 | 853 | struct seccomp_data sd_local; |
1da177e4 | 854 | |
3ba2530c KC |
855 | /* |
856 | * Make sure that any changes to mode from another thread have | |
857 | * been seen after TIF_SECCOMP was seen. | |
858 | */ | |
859 | rmb(); | |
860 | ||
db511391 TA |
861 | if (!sd) { |
862 | populate_seccomp_data(&sd_local); | |
863 | sd = &sd_local; | |
864 | } | |
865 | ||
deb4de8b | 866 | filter_ret = seccomp_run_filters(sd, &match); |
13aa72f0 | 867 | data = filter_ret & SECCOMP_RET_DATA; |
0466bdb9 | 868 | action = filter_ret & SECCOMP_RET_ACTION_FULL; |
13aa72f0 AL |
869 | |
870 | switch (action) { | |
871 | case SECCOMP_RET_ERRNO: | |
580c57f1 KC |
872 | /* Set low-order bits as an errno, capped at MAX_ERRNO. */ |
873 | if (data > MAX_ERRNO) | |
874 | data = MAX_ERRNO; | |
d39bd00d | 875 | syscall_set_return_value(current, task_pt_regs(current), |
13aa72f0 AL |
876 | -data, 0); |
877 | goto skip; | |
878 | ||
879 | case SECCOMP_RET_TRAP: | |
880 | /* Show the handler the original registers. */ | |
d39bd00d | 881 | syscall_rollback(current, task_pt_regs(current)); |
13aa72f0 AL |
882 | /* Let the filter pass back 16 bits of data. */ |
883 | seccomp_send_sigsys(this_syscall, data); | |
884 | goto skip; | |
885 | ||
886 | case SECCOMP_RET_TRACE: | |
ce6526e8 KC |
887 | /* We've been put in this state by the ptracer already. */ |
888 | if (recheck_after_trace) | |
889 | return 0; | |
890 | ||
8112c4f1 KC |
891 | /* ENOSYS these calls if there is no tracer attached. */ |
892 | if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) { | |
893 | syscall_set_return_value(current, | |
894 | task_pt_regs(current), | |
895 | -ENOSYS, 0); | |
896 | goto skip; | |
897 | } | |
898 | ||
899 | /* Allow the BPF to provide the event message */ | |
900 | ptrace_event(PTRACE_EVENT_SECCOMP, data); | |
901 | /* | |
902 | * The delivery of a fatal signal during event | |
485a252a KC |
903 | * notification may silently skip tracer notification, |
904 | * which could leave us with a potentially unmodified | |
905 | * syscall that the tracer would have liked to have | |
906 | * changed. Since the process is about to die, we just | |
907 | * force the syscall to be skipped and let the signal | |
908 | * kill the process and correctly handle any tracer exit | |
909 | * notifications. | |
8112c4f1 KC |
910 | */ |
911 | if (fatal_signal_pending(current)) | |
485a252a | 912 | goto skip; |
8112c4f1 KC |
913 | /* Check if the tracer forced the syscall to be skipped. */ |
914 | this_syscall = syscall_get_nr(current, task_pt_regs(current)); | |
915 | if (this_syscall < 0) | |
916 | goto skip; | |
917 | ||
ce6526e8 KC |
918 | /* |
919 | * Recheck the syscall, since it may have changed. This | |
920 | * intentionally uses a NULL struct seccomp_data to force | |
921 | * a reload of all registers. This does not goto skip since | |
922 | * a skip would have already been reported. | |
923 | */ | |
924 | if (__seccomp_filter(this_syscall, NULL, true)) | |
925 | return -1; | |
926 | ||
8112c4f1 | 927 | return 0; |
13aa72f0 | 928 | |
6a21cc50 | 929 | case SECCOMP_RET_USER_NOTIF: |
fb3c5386 CB |
930 | if (seccomp_do_user_notification(this_syscall, match, sd)) |
931 | goto skip; | |
932 | ||
933 | return 0; | |
6a21cc50 | 934 | |
59f5cf44 TH |
935 | case SECCOMP_RET_LOG: |
936 | seccomp_log(this_syscall, 0, action, true); | |
937 | return 0; | |
938 | ||
13aa72f0 | 939 | case SECCOMP_RET_ALLOW: |
deb4de8b KC |
940 | /* |
941 | * Note that the "match" filter will always be NULL for | |
942 | * this action since SECCOMP_RET_ALLOW is the starting | |
943 | * state in seccomp_run_filters(). | |
944 | */ | |
8112c4f1 | 945 | return 0; |
13aa72f0 | 946 | |
fd76875c | 947 | case SECCOMP_RET_KILL_THREAD: |
4d3b0b05 | 948 | case SECCOMP_RET_KILL_PROCESS: |
131b6351 | 949 | default: |
e66a3997 | 950 | seccomp_log(this_syscall, SIGSYS, action, true); |
d7276e32 | 951 | /* Dump core only if this is the last remaining thread. */ |
4d3b0b05 KC |
952 | if (action == SECCOMP_RET_KILL_PROCESS || |
953 | get_nr_threads(current) == 1) { | |
ae7795bc | 954 | kernel_siginfo_t info; |
131b6351 | 955 | |
d7276e32 KC |
956 | /* Show the original registers in the dump. */ |
957 | syscall_rollback(current, task_pt_regs(current)); | |
958 | /* Trigger a manual coredump since do_exit skips it. */ | |
959 | seccomp_init_siginfo(&info, this_syscall, data); | |
960 | do_coredump(&info); | |
961 | } | |
4d3b0b05 KC |
962 | if (action == SECCOMP_RET_KILL_PROCESS) |
963 | do_group_exit(SIGSYS); | |
964 | else | |
965 | do_exit(SIGSYS); | |
13aa72f0 AL |
966 | } |
967 | ||
968 | unreachable(); | |
969 | ||
970 | skip: | |
e66a3997 | 971 | seccomp_log(this_syscall, 0, action, match ? match->log : false); |
8112c4f1 KC |
972 | return -1; |
973 | } | |
974 | #else | |
ce6526e8 KC |
975 | static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, |
976 | const bool recheck_after_trace) | |
8112c4f1 KC |
977 | { |
978 | BUG(); | |
13aa72f0 | 979 | } |
1da177e4 | 980 | #endif |
13aa72f0 | 981 | |
8112c4f1 | 982 | int __secure_computing(const struct seccomp_data *sd) |
13aa72f0 AL |
983 | { |
984 | int mode = current->seccomp.mode; | |
8112c4f1 | 985 | int this_syscall; |
13aa72f0 | 986 | |
97f2645f | 987 | if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) && |
13c4a901 | 988 | unlikely(current->ptrace & PT_SUSPEND_SECCOMP)) |
8112c4f1 KC |
989 | return 0; |
990 | ||
991 | this_syscall = sd ? sd->nr : | |
992 | syscall_get_nr(current, task_pt_regs(current)); | |
13c4a901 | 993 | |
13aa72f0 | 994 | switch (mode) { |
e2cfabdf | 995 | case SECCOMP_MODE_STRICT: |
13aa72f0 | 996 | __secure_computing_strict(this_syscall); /* may call do_exit */ |
8112c4f1 | 997 | return 0; |
13aa72f0 | 998 | case SECCOMP_MODE_FILTER: |
ce6526e8 | 999 | return __seccomp_filter(this_syscall, sd, false); |
1da177e4 LT |
1000 | default: |
1001 | BUG(); | |
1002 | } | |
13aa72f0 | 1003 | } |
a4412fc9 | 1004 | #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */ |
1d9d02fe AA |
1005 | |
1006 | long prctl_get_seccomp(void) | |
1007 | { | |
1008 | return current->seccomp.mode; | |
1009 | } | |
1010 | ||
e2cfabdf | 1011 | /** |
3b23dd12 | 1012 | * seccomp_set_mode_strict: internal function for setting strict seccomp |
e2cfabdf WD |
1013 | * |
1014 | * Once current->seccomp.mode is non-zero, it may not be changed. | |
1015 | * | |
1016 | * Returns 0 on success or -EINVAL on failure. | |
1017 | */ | |
3b23dd12 | 1018 | static long seccomp_set_mode_strict(void) |
1d9d02fe | 1019 | { |
3b23dd12 | 1020 | const unsigned long seccomp_mode = SECCOMP_MODE_STRICT; |
e2cfabdf | 1021 | long ret = -EINVAL; |
1d9d02fe | 1022 | |
dbd95212 KC |
1023 | spin_lock_irq(¤t->sighand->siglock); |
1024 | ||
1f41b450 | 1025 | if (!seccomp_may_assign_mode(seccomp_mode)) |
1d9d02fe AA |
1026 | goto out; |
1027 | ||
cf99abac | 1028 | #ifdef TIF_NOTSC |
3b23dd12 | 1029 | disable_TSC(); |
cf99abac | 1030 | #endif |
00a02d0c | 1031 | seccomp_assign_mode(current, seccomp_mode, 0); |
3b23dd12 KC |
1032 | ret = 0; |
1033 | ||
1034 | out: | |
dbd95212 | 1035 | spin_unlock_irq(¤t->sighand->siglock); |
3b23dd12 KC |
1036 | |
1037 | return ret; | |
1038 | } | |
1039 | ||
e2cfabdf | 1040 | #ifdef CONFIG_SECCOMP_FILTER |
6a21cc50 TA |
1041 | static int seccomp_notify_release(struct inode *inode, struct file *file) |
1042 | { | |
1043 | struct seccomp_filter *filter = file->private_data; | |
1044 | struct seccomp_knotif *knotif; | |
1045 | ||
a811dc61 TA |
1046 | if (!filter) |
1047 | return 0; | |
1048 | ||
6a21cc50 TA |
1049 | mutex_lock(&filter->notify_lock); |
1050 | ||
1051 | /* | |
1052 | * If this file is being closed because e.g. the task who owned it | |
1053 | * died, let's wake everyone up who was waiting on us. | |
1054 | */ | |
1055 | list_for_each_entry(knotif, &filter->notif->notifications, list) { | |
1056 | if (knotif->state == SECCOMP_NOTIFY_REPLIED) | |
1057 | continue; | |
1058 | ||
1059 | knotif->state = SECCOMP_NOTIFY_REPLIED; | |
1060 | knotif->error = -ENOSYS; | |
1061 | knotif->val = 0; | |
1062 | ||
1063 | complete(&knotif->ready); | |
1064 | } | |
1065 | ||
1066 | kfree(filter->notif); | |
1067 | filter->notif = NULL; | |
1068 | mutex_unlock(&filter->notify_lock); | |
1069 | __put_seccomp_filter(filter); | |
1070 | return 0; | |
1071 | } | |
1072 | ||
9f87dcf1 SD |
1073 | /* must be called with notif_lock held */ |
1074 | static inline struct seccomp_knotif * | |
1075 | find_notification(struct seccomp_filter *filter, u64 id) | |
1076 | { | |
1077 | struct seccomp_knotif *cur; | |
1078 | ||
1079 | lockdep_assert_held(&filter->notify_lock); | |
1080 | ||
1081 | list_for_each_entry(cur, &filter->notif->notifications, list) { | |
1082 | if (cur->id == id) | |
1083 | return cur; | |
1084 | } | |
1085 | ||
1086 | return NULL; | |
1087 | } | |
1088 | ||
1089 | ||
6a21cc50 TA |
1090 | static long seccomp_notify_recv(struct seccomp_filter *filter, |
1091 | void __user *buf) | |
1092 | { | |
1093 | struct seccomp_knotif *knotif = NULL, *cur; | |
1094 | struct seccomp_notif unotif; | |
1095 | ssize_t ret; | |
1096 | ||
2882d53c SD |
1097 | /* Verify that we're not given garbage to keep struct extensible. */ |
1098 | ret = check_zeroed_user(buf, sizeof(unotif)); | |
1099 | if (ret < 0) | |
1100 | return ret; | |
1101 | if (!ret) | |
1102 | return -EINVAL; | |
1103 | ||
6a21cc50 TA |
1104 | memset(&unotif, 0, sizeof(unotif)); |
1105 | ||
1106 | ret = down_interruptible(&filter->notif->request); | |
1107 | if (ret < 0) | |
1108 | return ret; | |
1109 | ||
1110 | mutex_lock(&filter->notify_lock); | |
1111 | list_for_each_entry(cur, &filter->notif->notifications, list) { | |
1112 | if (cur->state == SECCOMP_NOTIFY_INIT) { | |
1113 | knotif = cur; | |
1114 | break; | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | /* | |
1119 | * If we didn't find a notification, it could be that the task was | |
1120 | * interrupted by a fatal signal between the time we were woken and | |
1121 | * when we were able to acquire the rw lock. | |
1122 | */ | |
1123 | if (!knotif) { | |
1124 | ret = -ENOENT; | |
1125 | goto out; | |
1126 | } | |
1127 | ||
1128 | unotif.id = knotif->id; | |
1129 | unotif.pid = task_pid_vnr(knotif->task); | |
1130 | unotif.data = *(knotif->data); | |
1131 | ||
1132 | knotif->state = SECCOMP_NOTIFY_SENT; | |
76194c4e | 1133 | wake_up_poll(&filter->wqh, EPOLLOUT | EPOLLWRNORM); |
6a21cc50 TA |
1134 | ret = 0; |
1135 | out: | |
1136 | mutex_unlock(&filter->notify_lock); | |
1137 | ||
1138 | if (ret == 0 && copy_to_user(buf, &unotif, sizeof(unotif))) { | |
1139 | ret = -EFAULT; | |
1140 | ||
1141 | /* | |
1142 | * Userspace screwed up. To make sure that we keep this | |
1143 | * notification alive, let's reset it back to INIT. It | |
1144 | * may have died when we released the lock, so we need to make | |
1145 | * sure it's still around. | |
1146 | */ | |
6a21cc50 | 1147 | mutex_lock(&filter->notify_lock); |
9f87dcf1 | 1148 | knotif = find_notification(filter, unotif.id); |
6a21cc50 TA |
1149 | if (knotif) { |
1150 | knotif->state = SECCOMP_NOTIFY_INIT; | |
1151 | up(&filter->notif->request); | |
1152 | } | |
1153 | mutex_unlock(&filter->notify_lock); | |
1154 | } | |
1155 | ||
1156 | return ret; | |
1157 | } | |
1158 | ||
1159 | static long seccomp_notify_send(struct seccomp_filter *filter, | |
1160 | void __user *buf) | |
1161 | { | |
1162 | struct seccomp_notif_resp resp = {}; | |
9f87dcf1 | 1163 | struct seccomp_knotif *knotif; |
6a21cc50 TA |
1164 | long ret; |
1165 | ||
1166 | if (copy_from_user(&resp, buf, sizeof(resp))) | |
1167 | return -EFAULT; | |
1168 | ||
fb3c5386 CB |
1169 | if (resp.flags & ~SECCOMP_USER_NOTIF_FLAG_CONTINUE) |
1170 | return -EINVAL; | |
1171 | ||
1172 | if ((resp.flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE) && | |
1173 | (resp.error || resp.val)) | |
6a21cc50 TA |
1174 | return -EINVAL; |
1175 | ||
1176 | ret = mutex_lock_interruptible(&filter->notify_lock); | |
1177 | if (ret < 0) | |
1178 | return ret; | |
1179 | ||
9f87dcf1 | 1180 | knotif = find_notification(filter, resp.id); |
6a21cc50 TA |
1181 | if (!knotif) { |
1182 | ret = -ENOENT; | |
1183 | goto out; | |
1184 | } | |
1185 | ||
1186 | /* Allow exactly one reply. */ | |
1187 | if (knotif->state != SECCOMP_NOTIFY_SENT) { | |
1188 | ret = -EINPROGRESS; | |
1189 | goto out; | |
1190 | } | |
1191 | ||
1192 | ret = 0; | |
1193 | knotif->state = SECCOMP_NOTIFY_REPLIED; | |
1194 | knotif->error = resp.error; | |
1195 | knotif->val = resp.val; | |
fb3c5386 | 1196 | knotif->flags = resp.flags; |
6a21cc50 TA |
1197 | complete(&knotif->ready); |
1198 | out: | |
1199 | mutex_unlock(&filter->notify_lock); | |
1200 | return ret; | |
1201 | } | |
1202 | ||
1203 | static long seccomp_notify_id_valid(struct seccomp_filter *filter, | |
1204 | void __user *buf) | |
1205 | { | |
9f87dcf1 | 1206 | struct seccomp_knotif *knotif; |
6a21cc50 TA |
1207 | u64 id; |
1208 | long ret; | |
1209 | ||
1210 | if (copy_from_user(&id, buf, sizeof(id))) | |
1211 | return -EFAULT; | |
1212 | ||
1213 | ret = mutex_lock_interruptible(&filter->notify_lock); | |
1214 | if (ret < 0) | |
1215 | return ret; | |
1216 | ||
9f87dcf1 SD |
1217 | knotif = find_notification(filter, id); |
1218 | if (knotif && knotif->state == SECCOMP_NOTIFY_SENT) | |
1219 | ret = 0; | |
1220 | else | |
1221 | ret = -ENOENT; | |
6a21cc50 | 1222 | |
6a21cc50 TA |
1223 | mutex_unlock(&filter->notify_lock); |
1224 | return ret; | |
1225 | } | |
1226 | ||
1227 | static long seccomp_notify_ioctl(struct file *file, unsigned int cmd, | |
1228 | unsigned long arg) | |
1229 | { | |
1230 | struct seccomp_filter *filter = file->private_data; | |
1231 | void __user *buf = (void __user *)arg; | |
1232 | ||
1233 | switch (cmd) { | |
1234 | case SECCOMP_IOCTL_NOTIF_RECV: | |
1235 | return seccomp_notify_recv(filter, buf); | |
1236 | case SECCOMP_IOCTL_NOTIF_SEND: | |
1237 | return seccomp_notify_send(filter, buf); | |
1238 | case SECCOMP_IOCTL_NOTIF_ID_VALID: | |
1239 | return seccomp_notify_id_valid(filter, buf); | |
1240 | default: | |
1241 | return -EINVAL; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | static __poll_t seccomp_notify_poll(struct file *file, | |
1246 | struct poll_table_struct *poll_tab) | |
1247 | { | |
1248 | struct seccomp_filter *filter = file->private_data; | |
1249 | __poll_t ret = 0; | |
1250 | struct seccomp_knotif *cur; | |
1251 | ||
76194c4e | 1252 | poll_wait(file, &filter->wqh, poll_tab); |
6a21cc50 | 1253 | |
319deec7 | 1254 | if (mutex_lock_interruptible(&filter->notify_lock) < 0) |
6a21cc50 TA |
1255 | return EPOLLERR; |
1256 | ||
1257 | list_for_each_entry(cur, &filter->notif->notifications, list) { | |
1258 | if (cur->state == SECCOMP_NOTIFY_INIT) | |
1259 | ret |= EPOLLIN | EPOLLRDNORM; | |
1260 | if (cur->state == SECCOMP_NOTIFY_SENT) | |
1261 | ret |= EPOLLOUT | EPOLLWRNORM; | |
1262 | if ((ret & EPOLLIN) && (ret & EPOLLOUT)) | |
1263 | break; | |
1264 | } | |
1265 | ||
1266 | mutex_unlock(&filter->notify_lock); | |
1267 | ||
99cdb8b9 CB |
1268 | if (refcount_read(&filter->users) == 0) |
1269 | ret |= EPOLLHUP; | |
1270 | ||
6a21cc50 TA |
1271 | return ret; |
1272 | } | |
1273 | ||
1274 | static const struct file_operations seccomp_notify_ops = { | |
1275 | .poll = seccomp_notify_poll, | |
1276 | .release = seccomp_notify_release, | |
1277 | .unlocked_ioctl = seccomp_notify_ioctl, | |
3db81afd | 1278 | .compat_ioctl = seccomp_notify_ioctl, |
6a21cc50 TA |
1279 | }; |
1280 | ||
1281 | static struct file *init_listener(struct seccomp_filter *filter) | |
1282 | { | |
1283 | struct file *ret = ERR_PTR(-EBUSY); | |
1284 | struct seccomp_filter *cur; | |
1285 | ||
1286 | for (cur = current->seccomp.filter; cur; cur = cur->prev) { | |
1287 | if (cur->notif) | |
1288 | goto out; | |
1289 | } | |
1290 | ||
1291 | ret = ERR_PTR(-ENOMEM); | |
1292 | filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL); | |
1293 | if (!filter->notif) | |
1294 | goto out; | |
1295 | ||
1296 | sema_init(&filter->notif->request, 0); | |
1297 | filter->notif->next_id = get_random_u64(); | |
1298 | INIT_LIST_HEAD(&filter->notif->notifications); | |
6a21cc50 TA |
1299 | |
1300 | ret = anon_inode_getfile("seccomp notify", &seccomp_notify_ops, | |
1301 | filter, O_RDWR); | |
1302 | if (IS_ERR(ret)) | |
1303 | goto out_notif; | |
1304 | ||
1305 | /* The file has a reference to it now */ | |
1306 | __get_seccomp_filter(filter); | |
1307 | ||
1308 | out_notif: | |
1309 | if (IS_ERR(ret)) | |
1310 | kfree(filter->notif); | |
1311 | out: | |
1312 | return ret; | |
1313 | } | |
1314 | ||
3b23dd12 KC |
1315 | /** |
1316 | * seccomp_set_mode_filter: internal function for setting seccomp filter | |
48dc92b9 | 1317 | * @flags: flags to change filter behavior |
3b23dd12 KC |
1318 | * @filter: struct sock_fprog containing filter |
1319 | * | |
1320 | * This function may be called repeatedly to install additional filters. | |
1321 | * Every filter successfully installed will be evaluated (in reverse order) | |
1322 | * for each system call the task makes. | |
1323 | * | |
1324 | * Once current->seccomp.mode is non-zero, it may not be changed. | |
1325 | * | |
1326 | * Returns 0 on success or -EINVAL on failure. | |
1327 | */ | |
48dc92b9 KC |
1328 | static long seccomp_set_mode_filter(unsigned int flags, |
1329 | const char __user *filter) | |
3b23dd12 KC |
1330 | { |
1331 | const unsigned long seccomp_mode = SECCOMP_MODE_FILTER; | |
c8bee430 | 1332 | struct seccomp_filter *prepared = NULL; |
3b23dd12 | 1333 | long ret = -EINVAL; |
6a21cc50 TA |
1334 | int listener = -1; |
1335 | struct file *listener_f = NULL; | |
3b23dd12 | 1336 | |
48dc92b9 | 1337 | /* Validate flags. */ |
c2e1f2e3 | 1338 | if (flags & ~SECCOMP_FILTER_FLAG_MASK) |
dbd95212 | 1339 | return -EINVAL; |
48dc92b9 | 1340 | |
7a0df7fb TA |
1341 | /* |
1342 | * In the successful case, NEW_LISTENER returns the new listener fd. | |
1343 | * But in the failure case, TSYNC returns the thread that died. If you | |
1344 | * combine these two flags, there's no way to tell whether something | |
51891498 TA |
1345 | * succeeded or failed. So, let's disallow this combination if the user |
1346 | * has not explicitly requested no errors from TSYNC. | |
7a0df7fb TA |
1347 | */ |
1348 | if ((flags & SECCOMP_FILTER_FLAG_TSYNC) && | |
51891498 TA |
1349 | (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) && |
1350 | ((flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) == 0)) | |
7a0df7fb TA |
1351 | return -EINVAL; |
1352 | ||
c8bee430 KC |
1353 | /* Prepare the new filter before holding any locks. */ |
1354 | prepared = seccomp_prepare_user_filter(filter); | |
1355 | if (IS_ERR(prepared)) | |
1356 | return PTR_ERR(prepared); | |
1357 | ||
6a21cc50 TA |
1358 | if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) { |
1359 | listener = get_unused_fd_flags(O_CLOEXEC); | |
1360 | if (listener < 0) { | |
1361 | ret = listener; | |
1362 | goto out_free; | |
1363 | } | |
1364 | ||
1365 | listener_f = init_listener(prepared); | |
1366 | if (IS_ERR(listener_f)) { | |
1367 | put_unused_fd(listener); | |
1368 | ret = PTR_ERR(listener_f); | |
1369 | goto out_free; | |
1370 | } | |
1371 | } | |
1372 | ||
c2e1f2e3 KC |
1373 | /* |
1374 | * Make sure we cannot change seccomp or nnp state via TSYNC | |
1375 | * while another thread is in the middle of calling exec. | |
1376 | */ | |
1377 | if (flags & SECCOMP_FILTER_FLAG_TSYNC && | |
1378 | mutex_lock_killable(¤t->signal->cred_guard_mutex)) | |
6a21cc50 | 1379 | goto out_put_fd; |
c2e1f2e3 | 1380 | |
dbd95212 KC |
1381 | spin_lock_irq(¤t->sighand->siglock); |
1382 | ||
3b23dd12 KC |
1383 | if (!seccomp_may_assign_mode(seccomp_mode)) |
1384 | goto out; | |
1385 | ||
c8bee430 | 1386 | ret = seccomp_attach_filter(flags, prepared); |
3b23dd12 | 1387 | if (ret) |
e2cfabdf | 1388 | goto out; |
c8bee430 KC |
1389 | /* Do not free the successfully attached filter. */ |
1390 | prepared = NULL; | |
1d9d02fe | 1391 | |
00a02d0c | 1392 | seccomp_assign_mode(current, seccomp_mode, flags); |
e2cfabdf | 1393 | out: |
dbd95212 | 1394 | spin_unlock_irq(¤t->sighand->siglock); |
c2e1f2e3 KC |
1395 | if (flags & SECCOMP_FILTER_FLAG_TSYNC) |
1396 | mutex_unlock(¤t->signal->cred_guard_mutex); | |
6a21cc50 TA |
1397 | out_put_fd: |
1398 | if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) { | |
7a0df7fb | 1399 | if (ret) { |
a811dc61 | 1400 | listener_f->private_data = NULL; |
6a21cc50 TA |
1401 | fput(listener_f); |
1402 | put_unused_fd(listener); | |
1403 | } else { | |
1404 | fd_install(listener, listener_f); | |
1405 | ret = listener; | |
1406 | } | |
1407 | } | |
c2e1f2e3 | 1408 | out_free: |
c8bee430 | 1409 | seccomp_filter_free(prepared); |
1d9d02fe AA |
1410 | return ret; |
1411 | } | |
3b23dd12 | 1412 | #else |
48dc92b9 KC |
1413 | static inline long seccomp_set_mode_filter(unsigned int flags, |
1414 | const char __user *filter) | |
3b23dd12 KC |
1415 | { |
1416 | return -EINVAL; | |
1417 | } | |
1418 | #endif | |
d78ab02c | 1419 | |
d612b1fd TH |
1420 | static long seccomp_get_action_avail(const char __user *uaction) |
1421 | { | |
1422 | u32 action; | |
1423 | ||
1424 | if (copy_from_user(&action, uaction, sizeof(action))) | |
1425 | return -EFAULT; | |
1426 | ||
1427 | switch (action) { | |
0466bdb9 | 1428 | case SECCOMP_RET_KILL_PROCESS: |
fd76875c | 1429 | case SECCOMP_RET_KILL_THREAD: |
d612b1fd TH |
1430 | case SECCOMP_RET_TRAP: |
1431 | case SECCOMP_RET_ERRNO: | |
6a21cc50 | 1432 | case SECCOMP_RET_USER_NOTIF: |
d612b1fd | 1433 | case SECCOMP_RET_TRACE: |
59f5cf44 | 1434 | case SECCOMP_RET_LOG: |
d612b1fd TH |
1435 | case SECCOMP_RET_ALLOW: |
1436 | break; | |
1437 | default: | |
1438 | return -EOPNOTSUPP; | |
1439 | } | |
1440 | ||
1441 | return 0; | |
1442 | } | |
1443 | ||
6a21cc50 TA |
1444 | static long seccomp_get_notif_sizes(void __user *usizes) |
1445 | { | |
1446 | struct seccomp_notif_sizes sizes = { | |
1447 | .seccomp_notif = sizeof(struct seccomp_notif), | |
1448 | .seccomp_notif_resp = sizeof(struct seccomp_notif_resp), | |
1449 | .seccomp_data = sizeof(struct seccomp_data), | |
1450 | }; | |
1451 | ||
1452 | if (copy_to_user(usizes, &sizes, sizeof(sizes))) | |
1453 | return -EFAULT; | |
1454 | ||
1455 | return 0; | |
1456 | } | |
1457 | ||
48dc92b9 KC |
1458 | /* Common entry point for both prctl and syscall. */ |
1459 | static long do_seccomp(unsigned int op, unsigned int flags, | |
a5662e4d | 1460 | void __user *uargs) |
48dc92b9 KC |
1461 | { |
1462 | switch (op) { | |
1463 | case SECCOMP_SET_MODE_STRICT: | |
1464 | if (flags != 0 || uargs != NULL) | |
1465 | return -EINVAL; | |
1466 | return seccomp_set_mode_strict(); | |
1467 | case SECCOMP_SET_MODE_FILTER: | |
1468 | return seccomp_set_mode_filter(flags, uargs); | |
d612b1fd TH |
1469 | case SECCOMP_GET_ACTION_AVAIL: |
1470 | if (flags != 0) | |
1471 | return -EINVAL; | |
1472 | ||
1473 | return seccomp_get_action_avail(uargs); | |
6a21cc50 TA |
1474 | case SECCOMP_GET_NOTIF_SIZES: |
1475 | if (flags != 0) | |
1476 | return -EINVAL; | |
1477 | ||
1478 | return seccomp_get_notif_sizes(uargs); | |
48dc92b9 KC |
1479 | default: |
1480 | return -EINVAL; | |
1481 | } | |
1482 | } | |
1483 | ||
1484 | SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags, | |
a5662e4d | 1485 | void __user *, uargs) |
48dc92b9 KC |
1486 | { |
1487 | return do_seccomp(op, flags, uargs); | |
1488 | } | |
1489 | ||
d78ab02c KC |
1490 | /** |
1491 | * prctl_set_seccomp: configures current->seccomp.mode | |
1492 | * @seccomp_mode: requested mode to use | |
1493 | * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER | |
1494 | * | |
1495 | * Returns 0 on success or -EINVAL on failure. | |
1496 | */ | |
a5662e4d | 1497 | long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter) |
d78ab02c | 1498 | { |
48dc92b9 | 1499 | unsigned int op; |
a5662e4d | 1500 | void __user *uargs; |
48dc92b9 | 1501 | |
3b23dd12 KC |
1502 | switch (seccomp_mode) { |
1503 | case SECCOMP_MODE_STRICT: | |
48dc92b9 KC |
1504 | op = SECCOMP_SET_MODE_STRICT; |
1505 | /* | |
1506 | * Setting strict mode through prctl always ignored filter, | |
1507 | * so make sure it is always NULL here to pass the internal | |
1508 | * check in do_seccomp(). | |
1509 | */ | |
1510 | uargs = NULL; | |
1511 | break; | |
3b23dd12 | 1512 | case SECCOMP_MODE_FILTER: |
48dc92b9 KC |
1513 | op = SECCOMP_SET_MODE_FILTER; |
1514 | uargs = filter; | |
1515 | break; | |
3b23dd12 KC |
1516 | default: |
1517 | return -EINVAL; | |
1518 | } | |
48dc92b9 KC |
1519 | |
1520 | /* prctl interface doesn't have flags, so they are always zero. */ | |
1521 | return do_seccomp(op, 0, uargs); | |
d78ab02c | 1522 | } |
f8e529ed TA |
1523 | |
1524 | #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE) | |
f06eae83 TA |
1525 | static struct seccomp_filter *get_nth_filter(struct task_struct *task, |
1526 | unsigned long filter_off) | |
f8e529ed | 1527 | { |
f06eae83 TA |
1528 | struct seccomp_filter *orig, *filter; |
1529 | unsigned long count; | |
f8e529ed | 1530 | |
f06eae83 TA |
1531 | /* |
1532 | * Note: this is only correct because the caller should be the (ptrace) | |
1533 | * tracer of the task, otherwise lock_task_sighand is needed. | |
1534 | */ | |
f8e529ed | 1535 | spin_lock_irq(&task->sighand->siglock); |
f06eae83 | 1536 | |
f8e529ed | 1537 | if (task->seccomp.mode != SECCOMP_MODE_FILTER) { |
f06eae83 TA |
1538 | spin_unlock_irq(&task->sighand->siglock); |
1539 | return ERR_PTR(-EINVAL); | |
f8e529ed TA |
1540 | } |
1541 | ||
f06eae83 TA |
1542 | orig = task->seccomp.filter; |
1543 | __get_seccomp_filter(orig); | |
1544 | spin_unlock_irq(&task->sighand->siglock); | |
1545 | ||
1546 | count = 0; | |
1547 | for (filter = orig; filter; filter = filter->prev) | |
f8e529ed | 1548 | count++; |
f8e529ed TA |
1549 | |
1550 | if (filter_off >= count) { | |
f06eae83 | 1551 | filter = ERR_PTR(-ENOENT); |
f8e529ed TA |
1552 | goto out; |
1553 | } | |
f8e529ed | 1554 | |
f06eae83 TA |
1555 | count -= filter_off; |
1556 | for (filter = orig; filter && count > 1; filter = filter->prev) | |
f8e529ed | 1557 | count--; |
f8e529ed TA |
1558 | |
1559 | if (WARN_ON(count != 1 || !filter)) { | |
f06eae83 | 1560 | filter = ERR_PTR(-ENOENT); |
f8e529ed TA |
1561 | goto out; |
1562 | } | |
1563 | ||
f06eae83 TA |
1564 | __get_seccomp_filter(filter); |
1565 | ||
1566 | out: | |
1567 | __put_seccomp_filter(orig); | |
1568 | return filter; | |
1569 | } | |
1570 | ||
1571 | long seccomp_get_filter(struct task_struct *task, unsigned long filter_off, | |
1572 | void __user *data) | |
1573 | { | |
1574 | struct seccomp_filter *filter; | |
1575 | struct sock_fprog_kern *fprog; | |
1576 | long ret; | |
1577 | ||
1578 | if (!capable(CAP_SYS_ADMIN) || | |
1579 | current->seccomp.mode != SECCOMP_MODE_DISABLED) { | |
1580 | return -EACCES; | |
1581 | } | |
1582 | ||
1583 | filter = get_nth_filter(task, filter_off); | |
1584 | if (IS_ERR(filter)) | |
1585 | return PTR_ERR(filter); | |
1586 | ||
f8e529ed TA |
1587 | fprog = filter->prog->orig_prog; |
1588 | if (!fprog) { | |
470bf1f2 | 1589 | /* This must be a new non-cBPF filter, since we save |
f8e529ed TA |
1590 | * every cBPF filter's orig_prog above when |
1591 | * CONFIG_CHECKPOINT_RESTORE is enabled. | |
1592 | */ | |
1593 | ret = -EMEDIUMTYPE; | |
1594 | goto out; | |
1595 | } | |
1596 | ||
1597 | ret = fprog->len; | |
1598 | if (!data) | |
1599 | goto out; | |
1600 | ||
f8e529ed TA |
1601 | if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog))) |
1602 | ret = -EFAULT; | |
1603 | ||
f8e529ed | 1604 | out: |
66a733ea | 1605 | __put_seccomp_filter(filter); |
f8e529ed | 1606 | return ret; |
f8e529ed | 1607 | } |
f8e529ed | 1608 | |
26500475 TA |
1609 | long seccomp_get_metadata(struct task_struct *task, |
1610 | unsigned long size, void __user *data) | |
1611 | { | |
1612 | long ret; | |
1613 | struct seccomp_filter *filter; | |
1614 | struct seccomp_metadata kmd = {}; | |
1615 | ||
1616 | if (!capable(CAP_SYS_ADMIN) || | |
1617 | current->seccomp.mode != SECCOMP_MODE_DISABLED) { | |
1618 | return -EACCES; | |
1619 | } | |
1620 | ||
1621 | size = min_t(unsigned long, size, sizeof(kmd)); | |
1622 | ||
63bb0045 TA |
1623 | if (size < sizeof(kmd.filter_off)) |
1624 | return -EINVAL; | |
1625 | ||
1626 | if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off))) | |
26500475 TA |
1627 | return -EFAULT; |
1628 | ||
1629 | filter = get_nth_filter(task, kmd.filter_off); | |
1630 | if (IS_ERR(filter)) | |
1631 | return PTR_ERR(filter); | |
1632 | ||
26500475 TA |
1633 | if (filter->log) |
1634 | kmd.flags |= SECCOMP_FILTER_FLAG_LOG; | |
1635 | ||
1636 | ret = size; | |
1637 | if (copy_to_user(data, &kmd, size)) | |
1638 | ret = -EFAULT; | |
1639 | ||
1640 | __put_seccomp_filter(filter); | |
f8e529ed TA |
1641 | return ret; |
1642 | } | |
1643 | #endif | |
8e5f1ad1 TH |
1644 | |
1645 | #ifdef CONFIG_SYSCTL | |
1646 | ||
1647 | /* Human readable action names for friendly sysctl interaction */ | |
0466bdb9 | 1648 | #define SECCOMP_RET_KILL_PROCESS_NAME "kill_process" |
fd76875c | 1649 | #define SECCOMP_RET_KILL_THREAD_NAME "kill_thread" |
8e5f1ad1 TH |
1650 | #define SECCOMP_RET_TRAP_NAME "trap" |
1651 | #define SECCOMP_RET_ERRNO_NAME "errno" | |
6a21cc50 | 1652 | #define SECCOMP_RET_USER_NOTIF_NAME "user_notif" |
8e5f1ad1 | 1653 | #define SECCOMP_RET_TRACE_NAME "trace" |
59f5cf44 | 1654 | #define SECCOMP_RET_LOG_NAME "log" |
8e5f1ad1 TH |
1655 | #define SECCOMP_RET_ALLOW_NAME "allow" |
1656 | ||
fd76875c | 1657 | static const char seccomp_actions_avail[] = |
0466bdb9 | 1658 | SECCOMP_RET_KILL_PROCESS_NAME " " |
fd76875c KC |
1659 | SECCOMP_RET_KILL_THREAD_NAME " " |
1660 | SECCOMP_RET_TRAP_NAME " " | |
1661 | SECCOMP_RET_ERRNO_NAME " " | |
6a21cc50 | 1662 | SECCOMP_RET_USER_NOTIF_NAME " " |
fd76875c KC |
1663 | SECCOMP_RET_TRACE_NAME " " |
1664 | SECCOMP_RET_LOG_NAME " " | |
1665 | SECCOMP_RET_ALLOW_NAME; | |
8e5f1ad1 | 1666 | |
0ddec0fc TH |
1667 | struct seccomp_log_name { |
1668 | u32 log; | |
1669 | const char *name; | |
1670 | }; | |
1671 | ||
1672 | static const struct seccomp_log_name seccomp_log_names[] = { | |
0466bdb9 | 1673 | { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME }, |
fd76875c | 1674 | { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME }, |
0ddec0fc TH |
1675 | { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME }, |
1676 | { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME }, | |
6a21cc50 | 1677 | { SECCOMP_LOG_USER_NOTIF, SECCOMP_RET_USER_NOTIF_NAME }, |
0ddec0fc | 1678 | { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME }, |
59f5cf44 | 1679 | { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME }, |
0ddec0fc TH |
1680 | { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME }, |
1681 | { } | |
1682 | }; | |
1683 | ||
1684 | static bool seccomp_names_from_actions_logged(char *names, size_t size, | |
beb44aca TH |
1685 | u32 actions_logged, |
1686 | const char *sep) | |
0ddec0fc TH |
1687 | { |
1688 | const struct seccomp_log_name *cur; | |
beb44aca | 1689 | bool append_sep = false; |
0ddec0fc TH |
1690 | |
1691 | for (cur = seccomp_log_names; cur->name && size; cur++) { | |
1692 | ssize_t ret; | |
1693 | ||
1694 | if (!(actions_logged & cur->log)) | |
1695 | continue; | |
1696 | ||
beb44aca TH |
1697 | if (append_sep) { |
1698 | ret = strscpy(names, sep, size); | |
0ddec0fc TH |
1699 | if (ret < 0) |
1700 | return false; | |
1701 | ||
1702 | names += ret; | |
1703 | size -= ret; | |
1704 | } else | |
beb44aca | 1705 | append_sep = true; |
0ddec0fc TH |
1706 | |
1707 | ret = strscpy(names, cur->name, size); | |
1708 | if (ret < 0) | |
1709 | return false; | |
1710 | ||
1711 | names += ret; | |
1712 | size -= ret; | |
1713 | } | |
1714 | ||
1715 | return true; | |
1716 | } | |
1717 | ||
1718 | static bool seccomp_action_logged_from_name(u32 *action_logged, | |
1719 | const char *name) | |
1720 | { | |
1721 | const struct seccomp_log_name *cur; | |
1722 | ||
1723 | for (cur = seccomp_log_names; cur->name; cur++) { | |
1724 | if (!strcmp(cur->name, name)) { | |
1725 | *action_logged = cur->log; | |
1726 | return true; | |
1727 | } | |
1728 | } | |
1729 | ||
1730 | return false; | |
1731 | } | |
1732 | ||
1733 | static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names) | |
1734 | { | |
1735 | char *name; | |
1736 | ||
1737 | *actions_logged = 0; | |
1738 | while ((name = strsep(&names, " ")) && *name) { | |
1739 | u32 action_logged = 0; | |
1740 | ||
1741 | if (!seccomp_action_logged_from_name(&action_logged, name)) | |
1742 | return false; | |
1743 | ||
1744 | *actions_logged |= action_logged; | |
1745 | } | |
1746 | ||
1747 | return true; | |
1748 | } | |
1749 | ||
d013db02 TH |
1750 | static int read_actions_logged(struct ctl_table *ro_table, void __user *buffer, |
1751 | size_t *lenp, loff_t *ppos) | |
0ddec0fc TH |
1752 | { |
1753 | char names[sizeof(seccomp_actions_avail)]; | |
1754 | struct ctl_table table; | |
d013db02 TH |
1755 | |
1756 | memset(names, 0, sizeof(names)); | |
1757 | ||
1758 | if (!seccomp_names_from_actions_logged(names, sizeof(names), | |
beb44aca | 1759 | seccomp_actions_logged, " ")) |
d013db02 TH |
1760 | return -EINVAL; |
1761 | ||
1762 | table = *ro_table; | |
1763 | table.data = names; | |
1764 | table.maxlen = sizeof(names); | |
1765 | return proc_dostring(&table, 0, buffer, lenp, ppos); | |
1766 | } | |
1767 | ||
1768 | static int write_actions_logged(struct ctl_table *ro_table, void __user *buffer, | |
ea6eca77 | 1769 | size_t *lenp, loff_t *ppos, u32 *actions_logged) |
0ddec0fc TH |
1770 | { |
1771 | char names[sizeof(seccomp_actions_avail)]; | |
1772 | struct ctl_table table; | |
1773 | int ret; | |
1774 | ||
d013db02 | 1775 | if (!capable(CAP_SYS_ADMIN)) |
0ddec0fc TH |
1776 | return -EPERM; |
1777 | ||
1778 | memset(names, 0, sizeof(names)); | |
1779 | ||
0ddec0fc TH |
1780 | table = *ro_table; |
1781 | table.data = names; | |
1782 | table.maxlen = sizeof(names); | |
d013db02 | 1783 | ret = proc_dostring(&table, 1, buffer, lenp, ppos); |
0ddec0fc TH |
1784 | if (ret) |
1785 | return ret; | |
1786 | ||
ea6eca77 | 1787 | if (!seccomp_actions_logged_from_names(actions_logged, table.data)) |
d013db02 | 1788 | return -EINVAL; |
0ddec0fc | 1789 | |
ea6eca77 | 1790 | if (*actions_logged & SECCOMP_LOG_ALLOW) |
d013db02 | 1791 | return -EINVAL; |
0ddec0fc | 1792 | |
ea6eca77 | 1793 | seccomp_actions_logged = *actions_logged; |
0ddec0fc TH |
1794 | return 0; |
1795 | } | |
0ddec0fc | 1796 | |
ea6eca77 TH |
1797 | static void audit_actions_logged(u32 actions_logged, u32 old_actions_logged, |
1798 | int ret) | |
1799 | { | |
1800 | char names[sizeof(seccomp_actions_avail)]; | |
1801 | char old_names[sizeof(seccomp_actions_avail)]; | |
1802 | const char *new = names; | |
1803 | const char *old = old_names; | |
0ddec0fc | 1804 | |
ea6eca77 TH |
1805 | if (!audit_enabled) |
1806 | return; | |
1807 | ||
1808 | memset(names, 0, sizeof(names)); | |
1809 | memset(old_names, 0, sizeof(old_names)); | |
1810 | ||
1811 | if (ret) | |
1812 | new = "?"; | |
1813 | else if (!actions_logged) | |
1814 | new = "(none)"; | |
1815 | else if (!seccomp_names_from_actions_logged(names, sizeof(names), | |
1816 | actions_logged, ",")) | |
1817 | new = "?"; | |
1818 | ||
1819 | if (!old_actions_logged) | |
1820 | old = "(none)"; | |
1821 | else if (!seccomp_names_from_actions_logged(old_names, | |
1822 | sizeof(old_names), | |
1823 | old_actions_logged, ",")) | |
1824 | old = "?"; | |
1825 | ||
1826 | return audit_seccomp_actions_logged(new, old, !ret); | |
1827 | } | |
1828 | ||
d013db02 | 1829 | static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write, |
32927393 | 1830 | void *buffer, size_t *lenp, |
d013db02 TH |
1831 | loff_t *ppos) |
1832 | { | |
ea6eca77 TH |
1833 | int ret; |
1834 | ||
1835 | if (write) { | |
1836 | u32 actions_logged = 0; | |
1837 | u32 old_actions_logged = seccomp_actions_logged; | |
1838 | ||
1839 | ret = write_actions_logged(ro_table, buffer, lenp, ppos, | |
1840 | &actions_logged); | |
1841 | audit_actions_logged(actions_logged, old_actions_logged, ret); | |
1842 | } else | |
1843 | ret = read_actions_logged(ro_table, buffer, lenp, ppos); | |
1844 | ||
1845 | return ret; | |
0ddec0fc TH |
1846 | } |
1847 | ||
8e5f1ad1 TH |
1848 | static struct ctl_path seccomp_sysctl_path[] = { |
1849 | { .procname = "kernel", }, | |
1850 | { .procname = "seccomp", }, | |
1851 | { } | |
1852 | }; | |
1853 | ||
1854 | static struct ctl_table seccomp_sysctl_table[] = { | |
1855 | { | |
1856 | .procname = "actions_avail", | |
1857 | .data = (void *) &seccomp_actions_avail, | |
1858 | .maxlen = sizeof(seccomp_actions_avail), | |
1859 | .mode = 0444, | |
1860 | .proc_handler = proc_dostring, | |
1861 | }, | |
0ddec0fc TH |
1862 | { |
1863 | .procname = "actions_logged", | |
1864 | .mode = 0644, | |
1865 | .proc_handler = seccomp_actions_logged_handler, | |
1866 | }, | |
8e5f1ad1 TH |
1867 | { } |
1868 | }; | |
1869 | ||
1870 | static int __init seccomp_sysctl_init(void) | |
1871 | { | |
1872 | struct ctl_table_header *hdr; | |
1873 | ||
1874 | hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table); | |
1875 | if (!hdr) | |
1876 | pr_warn("seccomp: sysctl registration failed\n"); | |
1877 | else | |
1878 | kmemleak_not_leak(hdr); | |
1879 | ||
1880 | return 0; | |
1881 | } | |
1882 | ||
1883 | device_initcall(seccomp_sysctl_init) | |
1884 | ||
1885 | #endif /* CONFIG_SYSCTL */ |