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