]> git.ipfire.org Git - thirdparty/man-pages.git/blob
d6b856c32
[thirdparty/man-pages.git] /
1 .\" Copyright (C) 2014 Kees Cook <keescook@chromium.org>
2 .\" and Copyright (C) 2012 Will Drewry <wad@chromium.org>
3 .\" and Copyright (C) 2008, 2014,2017 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" and Copyright (C) 2017 Tyler Hicks <tyhicks@canonical.com>
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
28 .TH SECCOMP 2 2020-06-09 "Linux" "Linux Programmer's Manual"
29 .SH NAME
30 seccomp \- operate on Secure Computing state of the process
31 .SH SYNOPSIS
32 .nf
33 .B #include <linux/seccomp.h>
34 .B #include <linux/filter.h>
35 .B #include <linux/audit.h>
36 .B #include <linux/signal.h>
37 .B #include <sys/ptrace.h>
38 .\" Kees Cook noted: Anything that uses SECCOMP_RET_TRACE returns will
39 .\" need <sys/ptrace.h>
40 .PP
41 .BI "int seccomp(unsigned int " operation ", unsigned int " flags \
42 ", void *" args );
43 .fi
44 .SH DESCRIPTION
45 The
46 .BR seccomp ()
47 system call operates on the Secure Computing (seccomp) state of the
48 calling process.
49 .PP
50 Currently, Linux supports the following
51 .IR operation
52 values:
53 .TP
54 .BR SECCOMP_SET_MODE_STRICT
55 The only system calls that the calling thread is permitted to make are
56 .BR read (2),
57 .BR write (2),
58 .BR _exit (2)
59 (but not
60 .BR exit_group (2)),
61 and
62 .BR sigreturn (2).
63 Other system calls result in the delivery of a
64 .BR SIGKILL
65 signal.
66 Strict secure computing mode is useful for number-crunching
67 applications that may need to execute untrusted byte code, perhaps
68 obtained by reading from a pipe or socket.
69 .IP
70 Note that although the calling thread can no longer call
71 .BR sigprocmask (2),
72 it can use
73 .BR sigreturn (2)
74 to block all signals apart from
75 .BR SIGKILL
76 and
77 .BR SIGSTOP .
78 This means that
79 .BR alarm (2)
80 (for example) is not sufficient for restricting the process's execution time.
81 Instead, to reliably terminate the process,
82 .BR SIGKILL
83 must be used.
84 This can be done by using
85 .BR timer_create (2)
86 with
87 .BR SIGEV_SIGNAL
88 and
89 .IR sigev_signo
90 set to
91 .BR SIGKILL ,
92 or by using
93 .BR setrlimit (2)
94 to set the hard limit for
95 .BR RLIMIT_CPU .
96 .IP
97 This operation is available only if the kernel is configured with
98 .BR CONFIG_SECCOMP
99 enabled.
100 .IP
101 The value of
102 .IR flags
103 must be 0, and
104 .IR args
105 must be NULL.
106 .IP
107 This operation is functionally identical to the call:
108 .IP
109 .in +4n
110 .EX
111 prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
112 .EE
113 .in
114 .TP
115 .BR SECCOMP_SET_MODE_FILTER
116 The system calls allowed are defined by a pointer to a Berkeley Packet
117 Filter (BPF) passed via
118 .IR args .
119 This argument is a pointer to a
120 .IR "struct\ sock_fprog" ;
121 it can be designed to filter arbitrary system calls and system call
122 arguments.
123 If the filter is invalid,
124 .BR seccomp ()
125 fails, returning
126 .BR EINVAL
127 in
128 .IR errno .
129 .IP
130 If
131 .BR fork (2)
132 or
133 .BR clone (2)
134 is allowed by the filter, any child processes will be constrained to
135 the same system call filters as the parent.
136 If
137 .BR execve (2)
138 is allowed,
139 the existing filters will be preserved across a call to
140 .BR execve (2).
141 .IP
142 In order to use the
143 .BR SECCOMP_SET_MODE_FILTER
144 operation, either the calling thread must have the
145 .BR CAP_SYS_ADMIN
146 capability in its user namespace, or the thread must already have the
147 .I no_new_privs
148 bit set.
149 If that bit was not already set by an ancestor of this thread,
150 the thread must make the following call:
151 .IP
152 .in +4n
153 .EX
154 prctl(PR_SET_NO_NEW_PRIVS, 1);
155 .EE
156 .in
157 .IP
158 Otherwise, the
159 .BR SECCOMP_SET_MODE_FILTER
160 operation fails and returns
161 .BR EACCES
162 in
163 .IR errno .
164 This requirement ensures that an unprivileged process cannot apply
165 a malicious filter and then invoke a set-user-ID or
166 other privileged program using
167 .BR execve (2),
168 thus potentially compromising that program.
169 (Such a malicious filter might, for example, cause an attempt to use
170 .BR setuid (2)
171 to set the caller's user IDs to nonzero values to instead
172 return 0 without actually making the system call.
173 Thus, the program might be tricked into retaining superuser privileges
174 in circumstances where it is possible to influence it to do
175 dangerous things because it did not actually drop privileges.)
176 .IP
177 If
178 .BR prctl (2)
179 or
180 .BR seccomp ()
181 is allowed by the attached filter, further filters may be added.
182 This will increase evaluation time, but allows for further reduction of
183 the attack surface during execution of a thread.
184 .IP
185 The
186 .BR SECCOMP_SET_MODE_FILTER
187 operation is available only if the kernel is configured with
188 .BR CONFIG_SECCOMP_FILTER
189 enabled.
190 .IP
191 When
192 .IR flags
193 is 0, this operation is functionally identical to the call:
194 .IP
195 .in +4n
196 .EX
197 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, args);
198 .EE
199 .in
200 .IP
201 The recognized
202 .IR flags
203 are:
204 .RS
205 .TP
206 .BR SECCOMP_FILTER_FLAG_TSYNC
207 When adding a new filter, synchronize all other threads of the calling
208 process to the same seccomp filter tree.
209 A "filter tree" is the ordered list of filters attached to a thread.
210 (Attaching identical filters in separate
211 .BR seccomp ()
212 calls results in different filters from this perspective.)
213 .IP
214 If any thread cannot synchronize to the same filter tree,
215 the call will not attach the new seccomp filter,
216 and will fail, returning the first thread ID found that cannot synchronize.
217 Synchronization will fail if another thread in the same process is in
218 .BR SECCOMP_MODE_STRICT
219 or if it has attached new seccomp filters to itself,
220 diverging from the calling thread's filter tree.
221 .TP
222 .BR SECCOMP_FILTER_FLAG_LOG " (since Linux 4.14)"
223 .\" commit e66a39977985b1e69e17c4042cb290768eca9b02
224 All filter return actions except
225 .BR SECCOMP_RET_ALLOW
226 should be logged.
227 An administrator may override this filter flag by preventing specific
228 actions from being logged via the
229 .IR /proc/sys/kernel/seccomp/actions_logged
230 file.
231 .TP
232 .BR SECCOMP_FILTER_FLAG_SPEC_ALLOW " (since Linux 4.17)"
233 .\" commit 00a02d0c502a06d15e07b857f8ff921e3e402675
234 Disable Speculative Store Bypass mitigation.
235 .RE
236 .TP
237 .BR SECCOMP_GET_ACTION_AVAIL " (since Linux 4.14)"
238 .\" commit d612b1fd8010d0d67b5287fe146b8b55bcbb8655
239 Test to see if an action is supported by the kernel.
240 This operation is helpful to confirm that the kernel knows
241 of a more recently added filter return action
242 since the kernel treats all unknown actions as
243 .BR SECCOMP_RET_KILL_PROCESS .
244 .IP
245 The value of
246 .IR flags
247 must be 0, and
248 .IR args
249 must be a pointer to an unsigned 32-bit filter return action.
250 .SS Filters
251 When adding filters via
252 .BR SECCOMP_SET_MODE_FILTER ,
253 .IR args
254 points to a filter program:
255 .PP
256 .in +4n
257 .EX
258 struct sock_fprog {
259 unsigned short len; /* Number of BPF instructions */
260 struct sock_filter *filter; /* Pointer to array of
261 BPF instructions */
262 };
263 .EE
264 .in
265 .PP
266 Each program must contain one or more BPF instructions:
267 .PP
268 .in +4n
269 .EX
270 struct sock_filter { /* Filter block */
271 __u16 code; /* Actual filter code */
272 __u8 jt; /* Jump true */
273 __u8 jf; /* Jump false */
274 __u32 k; /* Generic multiuse field */
275 };
276 .EE
277 .in
278 .PP
279 When executing the instructions, the BPF program operates on the
280 system call information made available (i.e., use the
281 .BR BPF_ABS
282 addressing mode) as a (read-only)
283 .\" Quoting Kees Cook:
284 .\" If BPF even allows changing the data, it's not copied back to
285 .\" the syscall when it runs. Anything wanting to do things like
286 .\" that would need to use ptrace to catch the call and directly
287 .\" modify the registers before continuing with the call.
288 buffer of the following form:
289 .PP
290 .in +4n
291 .EX
292 struct seccomp_data {
293 int nr; /* System call number */
294 __u32 arch; /* AUDIT_ARCH_* value
295 (see <linux/audit.h>) */
296 __u64 instruction_pointer; /* CPU instruction pointer */
297 __u64 args[6]; /* Up to 6 system call arguments */
298 };
299 .EE
300 .in
301 .PP
302 Because numbering of system calls varies between architectures and
303 some architectures (e.g., x86-64) allow user-space code to use
304 the calling conventions of multiple architectures
305 (and the convention being used may vary over the life of a process that uses
306 .BR execve (2)
307 to execute binaries that employ the different conventions),
308 it is usually necessary to verify the value of the
309 .IR arch
310 field.
311 .PP
312 It is strongly recommended to use an allow-list approach whenever
313 possible because such an approach is more robust and simple.
314 A deny-list will have to be updated whenever a potentially
315 dangerous system call is added (or a dangerous flag or option if those
316 are deny-listed), and it is often possible to alter the
317 representation of a value without altering its meaning, leading to
318 a deny-list bypass.
319 See also
320 .IR Caveats
321 below.
322 .PP
323 The
324 .IR arch
325 field is not unique for all calling conventions.
326 The x86-64 ABI and the x32 ABI both use
327 .BR AUDIT_ARCH_X86_64
328 as
329 .IR arch ,
330 and they run on the same processors.
331 Instead, the mask
332 .BR __X32_SYSCALL_BIT
333 is used on the system call number to tell the two ABIs apart.
334 .\" As noted by Dave Drysdale in a note at the end of
335 .\" https://lwn.net/Articles/604515/
336 .\" One additional detail to point out for the x32 ABI case:
337 .\" the syscall number gets a high bit set (__X32_SYSCALL_BIT),
338 .\" to mark it as an x32 call.
339 .\"
340 .\" If x32 support is included in the kernel, then __SYSCALL_MASK
341 .\" will have a value that is not all-ones, and this will trigger
342 .\" an extra instruction in system_call to mask off the extra bit,
343 .\" so that the syscall table indexing still works.
344 .PP
345 This means that a policy must either deny all syscalls with
346 .BR __X32_SYSCALL_BIT
347 or it must recognize syscalls with and without
348 .BR __X32_SYSCALL_BIT
349 set.
350 A list of system calls to be denied based on
351 .IR nr
352 that does not also contain
353 .IR nr
354 values with
355 .BR __X32_SYSCALL_BIT
356 set can be bypassed by a malicious program that sets
357 .BR __X32_SYSCALL_BIT .
358 .PP
359 Additionally, kernels prior to Linux 5.4 incorrectly permitted
360 .IR nr
361 in the ranges 512-547 as well as the corresponding non-x32 syscalls ORed
362 with
363 .BR __X32_SYSCALL_BIT .
364 For example,
365 .IR nr
366 == 521 and
367 .IR nr
368 == (101 |
369 .BR __X32_SYSCALL_BIT )
370 would result in invocations of
371 .BR ptrace (2)
372 with potentially confused x32-vs-x86_64 semantics in the kernel.
373 Policies intended to work on kernels before Linux 5.4 must ensure that they
374 deny or otherwise correctly handle these system calls.
375 On Linux 5.4 and newer,
376 .\" commit 6365b842aae4490ebfafadfc6bb27a6d3cc54757
377 such system calls will fail with the error
378 .BR ENOSYS ,
379 without doing anything.
380 .PP
381 The
382 .I instruction_pointer
383 field provides the address of the machine-language instruction that
384 performed the system call.
385 This might be useful in conjunction with the use of
386 .I /proc/[pid]/maps
387 to perform checks based on which region (mapping) of the program
388 made the system call.
389 (Probably, it is wise to lock down the
390 .BR mmap (2)
391 and
392 .BR mprotect (2)
393 system calls to prevent the program from subverting such checks.)
394 .PP
395 When checking values from
396 .IR args,
397 keep in mind that arguments are often
398 silently truncated before being processed, but after the seccomp check.
399 For example, this happens if the i386 ABI is used on an
400 x86-64 kernel: although the kernel will normally not look beyond
401 the 32 lowest bits of the arguments, the values of the full
402 64-bit registers will be present in the seccomp data.
403 A less surprising example is that if the x86-64 ABI is used to perform
404 a system call that takes an argument of type
405 .IR int ,
406 the more-significant half of the argument register is ignored by
407 the system call, but visible in the seccomp data.
408 .PP
409 A seccomp filter returns a 32-bit value consisting of two parts:
410 the most significant 16 bits
411 (corresponding to the mask defined by the constant
412 .BR SECCOMP_RET_ACTION_FULL )
413 contain one of the "action" values listed below;
414 the least significant 16-bits (defined by the constant
415 .BR SECCOMP_RET_DATA )
416 are "data" to be associated with this return value.
417 .PP
418 If multiple filters exist, they are \fIall\fP executed,
419 in reverse order of their addition to the filter tree\(emthat is,
420 the most recently installed filter is executed first.
421 (Note that all filters will be called
422 even if one of the earlier filters returns
423 .BR SECCOMP_RET_KILL .
424 This is done to simplify the kernel code and to provide a
425 tiny speed-up in the execution of sets of filters by
426 avoiding a check for this uncommon case.)
427 .\" From an Aug 2015 conversation with Kees Cook where I asked why *all*
428 .\" filters are applied even if one of the early filters returns
429 .\" SECCOMP_RET_KILL:
430 .\"
431 .\" It's just because it would be an optimization that would only speed up
432 .\" the RET_KILL case, but it's the uncommon one and the one that doesn't
433 .\" benefit meaningfully from such a change (you need to kill the process
434 .\" really quickly?). We would speed up killing a program at the (albeit
435 .\" tiny) expense to all other filtered programs. Best to keep the filter
436 .\" execution logic clear, simple, and as fast as possible for all
437 .\" filters.
438 The return value for the evaluation of a given system call is the first-seen
439 action value of highest precedence (along with its accompanying data)
440 returned by execution of all of the filters.
441 .PP
442 In decreasing order of precedence,
443 the action values that may be returned by a seccomp filter are:
444 .TP
445 .BR SECCOMP_RET_KILL_PROCESS " (since Linux 4.14)"
446 .\" commit 4d3b0b05aae9ee9ce0970dc4cc0fb3fad5e85945
447 .\" commit 0466bdb99e8744bc9befa8d62a317f0fd7fd7421
448 This value results in immediate termination of the process,
449 with a core dump.
450 The system call is not executed.
451 By contrast with
452 .BR SECCOMP_RET_KILL_THREAD
453 below, all threads in the thread group are terminated.
454 (For a discussion of thread groups, see the description of the
455 .BR CLONE_THREAD
456 flag in
457 .BR clone (2).)
458 .IP
459 The process terminates
460 .I "as though"
461 killed by a
462 .B SIGSYS
463 signal.
464 Even if a signal handler has been registered for
465 .BR SIGSYS ,
466 the handler will be ignored in this case and the process always terminates.
467 To a parent process that is waiting on this process (using
468 .BR waitpid (2)
469 or similar), the returned
470 .I wstatus
471 will indicate that its child was terminated as though by a
472 .BR SIGSYS
473 signal.
474 .TP
475 .BR SECCOMP_RET_KILL_THREAD " (or " SECCOMP_RET_KILL )
476 This value results in immediate termination of the thread
477 that made the system call.
478 The system call is not executed.
479 Other threads in the same thread group will continue to execute.
480 .IP
481 The thread terminates
482 .I "as though"
483 killed by a
484 .B SIGSYS
485 signal.
486 See
487 .BR SECCOMP_RET_KILL_PROCESS
488 above.
489 .IP
490 .\" See these commits:
491 .\" seccomp: dump core when using SECCOMP_RET_KILL
492 .\" (b25e67161c295c98acda92123b2dd1e7d8642901)
493 .\" seccomp: Only dump core when single-threaded
494 .\" (d7276e321ff8a53106a59c85ca46d03e34288893)
495 Before Linux 4.11,
496 any process terminated in this way would not trigger a coredump
497 (even though
498 .B SIGSYS
499 is documented in
500 .BR signal (7)
501 as having a default action of termination with a core dump).
502 Since Linux 4.11,
503 a single-threaded process will dump core if terminated in this way.
504 .IP
505 With the addition of
506 .BR SECCOMP_RET_KILL_PROCESS
507 in Linux 4.14,
508 .BR SECCOMP_RET_KILL_THREAD
509 was added as a synonym for
510 .BR SECCOMP_RET_KILL ,
511 in order to more clearly distinguish the two actions.
512 .IP
513 .BR Note :
514 the use of
515 .BR SECCOMP_RET_KILL_THREAD
516 to kill a single thread in a multithreaded process is likely to leave the
517 process in a permanently inconsistent and possibly corrupt state.
518 .TP
519 .BR SECCOMP_RET_TRAP
520 This value results in the kernel sending a thread-directed
521 .BR SIGSYS
522 signal to the triggering thread.
523 (The system call is not executed.)
524 Various fields will be set in the
525 .I siginfo_t
526 structure (see
527 .BR sigaction (2))
528 associated with signal:
529 .RS
530 .IP * 3
531 .I si_signo
532 will contain
533 .BR SIGSYS .
534 .IP *
535 .IR si_call_addr
536 will show the address of the system call instruction.
537 .IP *
538 .IR si_syscall
539 and
540 .IR si_arch
541 will indicate which system call was attempted.
542 .IP *
543 .I si_code
544 will contain
545 .BR SYS_SECCOMP .
546 .IP *
547 .I si_errno
548 will contain the
549 .BR SECCOMP_RET_DATA
550 portion of the filter return value.
551 .RE
552 .IP
553 The program counter will be as though the system call happened
554 (i.e., the program counter will not point to the system call instruction).
555 The return value register will contain an architecture\-dependent value;
556 if resuming execution, set it to something appropriate for the system call.
557 (The architecture dependency is because replacing it with
558 .BR ENOSYS
559 could overwrite some useful information.)
560 .TP
561 .BR SECCOMP_RET_ERRNO
562 This value results in the
563 .B SECCOMP_RET_DATA
564 portion of the filter's return value being passed to user space as the
565 .IR errno
566 value without executing the system call.
567 .TP
568 .BR SECCOMP_RET_TRACE
569 When returned, this value will cause the kernel to attempt to notify a
570 .BR ptrace (2)-based
571 tracer prior to executing the system call.
572 If there is no tracer present,
573 the system call is not executed and returns a failure status with
574 .I errno
575 set to
576 .BR ENOSYS .
577 .IP
578 A tracer will be notified if it requests
579 .BR PTRACE_O_TRACESECCOMP
580 using
581 .IR ptrace(PTRACE_SETOPTIONS) .
582 The tracer will be notified of a
583 .BR PTRACE_EVENT_SECCOMP
584 and the
585 .BR SECCOMP_RET_DATA
586 portion of the filter's return value will be available to the tracer via
587 .BR PTRACE_GETEVENTMSG .
588 .IP
589 The tracer can skip the system call by changing the system call number
590 to \-1.
591 Alternatively, the tracer can change the system call
592 requested by changing the system call to a valid system call number.
593 If the tracer asks to skip the system call, then the system call will
594 appear to return the value that the tracer puts in the return value register.
595 .IP
596 .\" This was changed in ce6526e8afa4.
597 .\" A related hole, using PTRACE_SYSCALL instead of SECCOMP_RET_TRACE, was
598 .\" changed in arch-specific commits, e.g. 93e35efb8de4 for X86 and
599 .\" 0f3912fd934c for ARM.
600 Before kernel 4.8, the seccomp check will not be run again after the tracer is
601 notified.
602 (This means that, on older kernels, seccomp-based sandboxes
603 .B "must not"
604 allow use of
605 .BR ptrace (2)\(emeven
606 of other
607 sandboxed processes\(emwithout extreme care;
608 ptracers can use this mechanism to escape from the seccomp sandbox.)
609 .TP
610 .BR SECCOMP_RET_LOG " (since Linux 4.14)"
611 .\" commit 59f5cf44a38284eb9e76270c786fb6cc62ef8ac4
612 This value results in the system call being executed after
613 the filter return action is logged.
614 An administrator may override the logging of this action via
615 the
616 .IR /proc/sys/kernel/seccomp/actions_logged
617 file.
618 .TP
619 .BR SECCOMP_RET_ALLOW
620 This value results in the system call being executed.
621 .PP
622 If an action value other than one of the above is specified,
623 then the filter action is treated as either
624 .BR SECCOMP_RET_KILL_PROCESS
625 (since Linux 4.14)
626 .\" commit 4d3b0b05aae9ee9ce0970dc4cc0fb3fad5e85945
627 or
628 .BR SECCOMP_RET_KILL_THREAD
629 (in Linux 4.13 and earlier).
630 .\"
631 .SS /proc interfaces
632 The files in the directory
633 .IR /proc/sys/kernel/seccomp
634 provide additional seccomp information and configuration:
635 .TP
636 .IR actions_avail " (since Linux 4.14)"
637 .\" commit 8e5f1ad116df6b0de65eac458d5e7c318d1c05af
638 A read-only ordered list of seccomp filter return actions in string form.
639 The ordering, from left-to-right, is in decreasing order of precedence.
640 The list represents the set of seccomp filter return actions
641 supported by the kernel.
642 .TP
643 .IR actions_logged " (since Linux 4.14)"
644 .\" commit 0ddec0fc8900201c0897b87b762b7c420436662f
645 A read-write ordered list of seccomp filter return actions that
646 are allowed to be logged.
647 Writes to the file do not need to be in ordered form but reads from
648 the file will be ordered in the same way as the
649 .IR actions_avail
650 file.
651 .IP
652 It is important to note that the value of
653 .IR actions_logged
654 does not prevent certain filter return actions from being logged when
655 the audit subsystem is configured to audit a task.
656 If the action is not found in the
657 .IR actions_logged
658 file, the final decision on whether to audit the action for that task is
659 ultimately left up to the audit subsystem to decide for all filter return
660 actions other than
661 .BR SECCOMP_RET_ALLOW .
662 .IP
663 The "allow" string is not accepted in the
664 .IR actions_logged
665 file as it is not possible to log
666 .BR SECCOMP_RET_ALLOW
667 actions.
668 Attempting to write "allow" to the file will fail with the error
669 .BR EINVAL .
670 .\"
671 .SS Audit logging of seccomp actions
672 .\" commit 59f5cf44a38284eb9e76270c786fb6cc62ef8ac4
673 Since Linux 4.14, the kernel provides the facility to log the
674 actions returned by seccomp filters in the audit log.
675 The kernel makes the decision to log an action based on
676 the action type, whether or not the action is present in the
677 .I actions_logged
678 file, and whether kernel auditing is enabled
679 (e.g., via the kernel boot option
680 .IR audit=1 ).
681 .\" or auditing could be enabled via the netlink API (AUDIT_SET)
682 The rules are as follows:
683 .IP * 3
684 If the action is
685 .BR SECCOMP_RET_ALLOW ,
686 the action is not logged.
687 .IP *
688 Otherwise, if the action is either
689 .BR SECCOMP_RET_KILL_PROCESS
690 or
691 .BR SECCOMP_RET_KILL_THREAD ,
692 and that action appears in the
693 .IR actions_logged
694 file, the action is logged.
695 .IP *
696 Otherwise, if the filter has requested logging (the
697 .BR SECCOMP_FILTER_FLAG_LOG
698 flag)
699 and the action appears in the
700 .IR actions_logged
701 file, the action is logged.
702 .IP *
703 Otherwise, if kernel auditing is enabled and the process is being audited
704 .RB ( autrace (8)),
705 the action is logged.
706 .IP *
707 Otherwise, the action is not logged.
708 .SH RETURN VALUE
709 On success,
710 .BR seccomp ()
711 returns 0.
712 On error, if
713 .BR SECCOMP_FILTER_FLAG_TSYNC
714 was used,
715 the return value is the ID of the thread
716 that caused the synchronization failure.
717 (This ID is a kernel thread ID of the type returned by
718 .BR clone (2)
719 and
720 .BR gettid (2).)
721 On other errors, \-1 is returned, and
722 .IR errno
723 is set to indicate the cause of the error.
724 .SH ERRORS
725 .BR seccomp ()
726 can fail for the following reasons:
727 .TP
728 .BR EACCES
729 The caller did not have the
730 .BR CAP_SYS_ADMIN
731 capability in its user namespace, or had not set
732 .IR no_new_privs
733 before using
734 .BR SECCOMP_SET_MODE_FILTER .
735 .TP
736 .BR EFAULT
737 .IR args
738 was not a valid address.
739 .TP
740 .BR EINVAL
741 .IR operation
742 is unknown or is not supported by this kernel version or configuration.
743 .TP
744 .B EINVAL
745 The specified
746 .IR flags
747 are invalid for the given
748 .IR operation .
749 .TP
750 .BR EINVAL
751 .I operation
752 included
753 .BR BPF_ABS ,
754 but the specified offset was not aligned to a 32-bit boundary or exceeded
755 .IR "sizeof(struct\ seccomp_data)" .
756 .TP
757 .BR EINVAL
758 .\" See kernel/seccomp.c::seccomp_may_assign_mode() in 3.18 sources
759 A secure computing mode has already been set, and
760 .I operation
761 differs from the existing setting.
762 .TP
763 .BR EINVAL
764 .I operation
765 specified
766 .BR SECCOMP_SET_MODE_FILTER ,
767 but the filter program pointed to by
768 .I args
769 was not valid or the length of the filter program was zero or exceeded
770 .B BPF_MAXINSNS
771 (4096) instructions.
772 .TP
773 .BR ENOMEM
774 Out of memory.
775 .TP
776 .BR ENOMEM
777 .\" ENOMEM in kernel/seccomp.c::seccomp_attach_filter() in 3.18 sources
778 The total length of all filter programs attached
779 to the calling thread would exceed
780 .B MAX_INSNS_PER_PATH
781 (32768) instructions.
782 Note that for the purposes of calculating this limit,
783 each already existing filter program incurs an
784 overhead penalty of 4 instructions.
785 .TP
786 .BR EOPNOTSUPP
787 .I operation
788 specified
789 .BR SECCOMP_GET_ACTION_AVAIL ,
790 but the kernel does not support the filter return action specified by
791 .IR args .
792 .TP
793 .BR ESRCH
794 Another thread caused a failure during thread sync, but its ID could not
795 be determined.
796 .SH VERSIONS
797 The
798 .BR seccomp ()
799 system call first appeared in Linux 3.17.
800 .\" FIXME . Add glibc version
801 .SH CONFORMING TO
802 The
803 .BR seccomp ()
804 system call is a nonstandard Linux extension.
805 .SH NOTES
806 Rather than hand-coding seccomp filters as shown in the example below,
807 you may prefer to employ the
808 .I libseccomp
809 library, which provides a front-end for generating seccomp filters.
810 .PP
811 The
812 .IR Seccomp
813 field of the
814 .IR /proc/[pid]/status
815 file provides a method of viewing the seccomp mode of a process; see
816 .BR proc (5).
817 .PP
818 .BR seccomp ()
819 provides a superset of the functionality provided by the
820 .BR prctl (2)
821 .BR PR_SET_SECCOMP
822 operation (which does not support
823 .IR flags ).
824 .PP
825 Since Linux 4.4, the
826 .BR ptrace (2)
827 .B PTRACE_SECCOMP_GET_FILTER
828 operation can be used to dump a process's seccomp filters.
829 .\"
830 .SS Architecture support for seccomp BPF
831 Architecture support for seccomp BPF filtering
832 .\" Check by grepping for HAVE_ARCH_SECCOMP_FILTER in Kconfig files in
833 .\" kernel source. Last checked in Linux 4.16-rc source.
834 is available on the following architectures:
835 .IP * 3
836 x86-64, i386, x32 (since Linux 3.5)
837 .PD 0
838 .IP *
839 ARM (since Linux 3.8)
840 .IP *
841 s390 (since Linux 3.8)
842 .IP *
843 MIPS (since Linux 3.16)
844 .IP *
845 ARM-64 (since Linux 3.19)
846 .IP *
847 PowerPC (since Linux 4.3)
848 .IP *
849 Tile (since Linux 4.3)
850 .IP *
851 PA-RISC (since Linux 4.6)
852 .\" User mode Linux since Linux 4.6
853 .PD
854 .\"
855 .SS Caveats
856 There are various subtleties to consider when applying seccomp filters
857 to a program, including the following:
858 .IP * 3
859 Some traditional system calls have user-space implementations in the
860 .BR vdso (7)
861 on many architectures.
862 Notable examples include
863 .BR clock_gettime (2),
864 .BR gettimeofday (2),
865 and
866 .BR time (2).
867 On such architectures,
868 seccomp filtering for these system calls will have no effect.
869 (However, there are cases where the
870 .BR vdso (7)
871 implementations may fall back to invoking the true system call,
872 in which case seccomp filters would see the system call.)
873 .IP *
874 Seccomp filtering is based on system call numbers.
875 However, applications typically do not directly invoke system calls,
876 but instead call wrapper functions in the C library which
877 in turn invoke the system calls.
878 Consequently, one must be aware of the following:
879 .RS
880 .IP \(bu 3
881 The glibc wrappers for some traditional system calls may actually
882 employ system calls with different names in the kernel.
883 For example, the
884 .BR exit (2)
885 wrapper function actually employs the
886 .BR exit_group (2)
887 system call, and the
888 .BR fork (2)
889 wrapper function actually calls
890 .BR clone (2).
891 .IP \(bu
892 The behavior of wrapper functions may vary across architectures,
893 according to the range of system calls provided on those architectures.
894 In other words, the same wrapper function may invoke
895 different system calls on different architectures.
896 .IP \(bu
897 Finally, the behavior of wrapper functions can change across glibc versions.
898 For example, in older versions, the glibc wrapper function for
899 .BR open (2)
900 invoked the system call of the same name,
901 but starting in glibc 2.26, the implementation switched to calling
902 .BR openat (2)
903 on all architectures.
904 .RE
905 .PP
906 The consequence of the above points is that it may be necessary
907 to filter for a system call other than might be expected.
908 Various manual pages in Section 2 provide helpful details
909 about the differences between wrapper functions and
910 the underlying system calls in subsections entitled
911 .IR "C library/kernel differences" .
912 .PP
913 Furthermore, note that the application of seccomp filters
914 even risks causing bugs in an application,
915 when the filters cause unexpected failures for legitimate operations
916 that the application might need to perform.
917 Such bugs may not easily be discovered when testing the seccomp
918 filters if the bugs occur in rarely used application code paths.
919 .\"
920 .SS Seccomp-specific BPF details
921 Note the following BPF details specific to seccomp filters:
922 .IP * 3
923 The
924 .B BPF_H
925 and
926 .B BPF_B
927 size modifiers are not supported: all operations must load and store
928 (4-byte) words
929 .RB ( BPF_W ).
930 .IP *
931 To access the contents of the
932 .I seccomp_data
933 buffer, use the
934 .B BPF_ABS
935 addressing mode modifier.
936 .IP *
937 The
938 .B BPF_LEN
939 addressing mode modifier yields an immediate mode operand
940 whose value is the size of the
941 .IR seccomp_data
942 buffer.
943 .SH EXAMPLES
944 The program below accepts four or more arguments.
945 The first three arguments are a system call number,
946 a numeric architecture identifier, and an error number.
947 The program uses these values to construct a BPF filter
948 that is used at run time to perform the following checks:
949 .IP [1] 4
950 If the program is not running on the specified architecture,
951 the BPF filter causes system calls to fail with the error
952 .BR ENOSYS .
953 .IP [2]
954 If the program attempts to execute the system call with the specified number,
955 the BPF filter causes the system call to fail, with
956 .I errno
957 being set to the specified error number.
958 .PP
959 The remaining command-line arguments specify
960 the pathname and additional arguments of a program
961 that the example program should attempt to execute using
962 .BR execv (3)
963 (a library function that employs the
964 .BR execve (2)
965 system call).
966 Some example runs of the program are shown below.
967 .PP
968 First, we display the architecture that we are running on (x86-64)
969 and then construct a shell function that looks up system call
970 numbers on this architecture:
971 .PP
972 .in +4n
973 .EX
974 $ \fBuname \-m\fP
975 x86_64
976 $ \fBsyscall_nr() {
977 cat /usr/src/linux/arch/x86/syscalls/syscall_64.tbl | \e
978 awk \(aq$2 != "x32" && $3 == "\(aq$1\(aq" { print $1 }\(aq
979 }\fP
980 .EE
981 .in
982 .PP
983 When the BPF filter rejects a system call (case [2] above),
984 it causes the system call to fail with the error number
985 specified on the command line.
986 In the experiments shown here, we'll use error number 99:
987 .PP
988 .in +4n
989 .EX
990 $ \fBerrno 99\fP
991 EADDRNOTAVAIL 99 Cannot assign requested address
992 .EE
993 .in
994 .PP
995 In the following example, we attempt to run the command
996 .BR whoami (1),
997 but the BPF filter rejects the
998 .BR execve (2)
999 system call, so that the command is not even executed:
1000 .PP
1001 .in +4n
1002 .EX
1003 $ \fBsyscall_nr execve\fP
1004 59
1005 $ \fB./a.out\fP
1006 Usage: ./a.out <syscall_nr> <arch> <errno> <prog> [<args>]
1007 Hint for <arch>: AUDIT_ARCH_I386: 0x40000003
1008 AUDIT_ARCH_X86_64: 0xC000003E
1009 $ \fB./a.out 59 0xC000003E 99 /bin/whoami\fP
1010 execv: Cannot assign requested address
1011 .EE
1012 .in
1013 .PP
1014 In the next example, the BPF filter rejects the
1015 .BR write (2)
1016 system call, so that, although it is successfully started, the
1017 .BR whoami (1)
1018 command is not able to write output:
1019 .PP
1020 .in +4n
1021 .EX
1022 $ \fBsyscall_nr write\fP
1023 1
1024 $ \fB./a.out 1 0xC000003E 99 /bin/whoami\fP
1025 .EE
1026 .in
1027 .PP
1028 In the final example,
1029 the BPF filter rejects a system call that is not used by the
1030 .BR whoami (1)
1031 command, so it is able to successfully execute and produce output:
1032 .PP
1033 .in +4n
1034 .EX
1035 $ \fBsyscall_nr preadv\fP
1036 295
1037 $ \fB./a.out 295 0xC000003E 99 /bin/whoami\fP
1038 cecilia
1039 .EE
1040 .in
1041 .SS Program source
1042 .EX
1043 #include <errno.h>
1044 #include <stddef.h>
1045 #include <stdio.h>
1046 #include <stdlib.h>
1047 #include <unistd.h>
1048 #include <linux/audit.h>
1049 #include <linux/filter.h>
1050 #include <linux/seccomp.h>
1051 #include <sys/prctl.h>
1052
1053 #define X32_SYSCALL_BIT 0x40000000
1054
1055 static int
1056 install_filter(int syscall_nr, int t_arch, int f_errno)
1057 {
1058 unsigned int upper_nr_limit = 0xffffffff;
1059
1060 /* Assume that AUDIT_ARCH_X86_64 means the normal x86-64 ABI
1061 (in the x32 ABI, all system calls have bit 30 set in the
1062 \(aqnr\(aq field, meaning the numbers are >= X32_SYSCALL_BIT) */
1063 if (t_arch == AUDIT_ARCH_X86_64)
1064 upper_nr_limit = X32_SYSCALL_BIT - 1;
1065
1066 struct sock_filter filter[] = {
1067 /* [0] Load architecture from \(aqseccomp_data\(aq buffer into
1068 accumulator */
1069 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1070 (offsetof(struct seccomp_data, arch))),
1071
1072 /* [1] Jump forward 5 instructions if architecture does not
1073 match \(aqt_arch\(aq */
1074 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, t_arch, 0, 5),
1075
1076 /* [2] Load system call number from \(aqseccomp_data\(aq buffer into
1077 accumulator */
1078 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1079 (offsetof(struct seccomp_data, nr))),
1080
1081 /* [3] Check ABI - only needed for x86-64 in deny-list use
1082 cases. Use BPF_JGT instead of checking against the bit
1083 mask to avoid having to reload the syscall number. */
1084 BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, upper_nr_limit, 3, 0),
1085
1086 /* [4] Jump forward 1 instruction if system call number
1087 does not match \(aqsyscall_nr\(aq */
1088 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, syscall_nr, 0, 1),
1089
1090 /* [5] Matching architecture and system call: don\(aqt execute
1091 the system call, and return \(aqf_errno\(aq in \(aqerrno\(aq */
1092 BPF_STMT(BPF_RET | BPF_K,
1093 SECCOMP_RET_ERRNO | (f_errno & SECCOMP_RET_DATA)),
1094
1095 /* [6] Destination of system call number mismatch: allow other
1096 system calls */
1097 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
1098
1099 /* [7] Destination of architecture mismatch: kill process */
1100 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
1101 };
1102
1103 struct sock_fprog prog = {
1104 .len = sizeof(filter) / sizeof(filter[0]),
1105 .filter = filter,
1106 };
1107
1108 if (seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog)) {
1109 perror("seccomp");
1110 return 1;
1111 }
1112
1113 return 0;
1114 }
1115
1116 int
1117 main(int argc, char **argv)
1118 {
1119 if (argc < 5) {
1120 fprintf(stderr, "Usage: "
1121 "%s <syscall_nr> <arch> <errno> <prog> [<args>]\en"
1122 "Hint for <arch>: AUDIT_ARCH_I386: 0x%X\en"
1123 " AUDIT_ARCH_X86_64: 0x%X\en"
1124 "\en", argv[0], AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
1125 exit(EXIT_FAILURE);
1126 }
1127
1128 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1129 perror("prctl");
1130 exit(EXIT_FAILURE);
1131 }
1132
1133 if (install_filter(strtol(argv[1], NULL, 0),
1134 strtol(argv[2], NULL, 0),
1135 strtol(argv[3], NULL, 0)))
1136 exit(EXIT_FAILURE);
1137
1138 execv(argv[4], &argv[4]);
1139 perror("execv");
1140 exit(EXIT_FAILURE);
1141 }
1142 .EE
1143 .SH SEE ALSO
1144 .BR bpfc (1),
1145 .BR strace (1),
1146 .BR bpf (2),
1147 .BR prctl (2),
1148 .BR ptrace (2),
1149 .BR sigaction (2),
1150 .BR proc (5),
1151 .BR signal (7),
1152 .BR socket (7)
1153 .PP
1154 Various pages from the
1155 .I libseccomp
1156 library, including:
1157 .BR scmp_sys_resolver (1),
1158 .BR seccomp_export_bpf (3),
1159 .BR seccomp_init (3),
1160 .BR seccomp_load (3),
1161 and
1162 .BR seccomp_rule_add (3).
1163 .PP
1164 The kernel source files
1165 .IR Documentation/networking/filter.txt
1166 and
1167 .IR Documentation/userspace\-api/seccomp_filter.rst
1168 .\" commit c061f33f35be0ccc80f4b8e0aea5dfd2ed7e01a3
1169 (or
1170 .IR Documentation/prctl/seccomp_filter.txt
1171 before Linux 4.13).
1172 .PP
1173 McCanne, S.\& and Jacobson, V.\& (1992)
1174 .IR "The BSD Packet Filter: A New Architecture for User-level Packet Capture" ,
1175 Proceedings of the USENIX Winter 1993 Conference
1176 .UR http://www.tcpdump.org/papers/bpf\-usenix93.pdf
1177 .UE