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>
7 .\" %%%LICENSE_START(VERBATIM)
8 .\" Permission is granted to make and distribute verbatim copies of this
9 .\" manual provided the copyright notice and this permission notice are
10 .\" preserved on all copies.
12 .\" Permission is granted to copy and distribute modified versions of this
13 .\" manual under the conditions for verbatim copying, provided that the
14 .\" entire resulting derived work is distributed under the terms of a
15 .\" permission notice identical to this one.
17 .\" Since the Linux kernel and libraries are constantly changing, this
18 .\" manual page may be incorrect or out-of-date. The author(s) assume no
19 .\" responsibility for errors or omissions, or for damages resulting from
20 .\" the use of the information contained herein. The author(s) may not
21 .\" have taken the same level of care in the production of this manual,
22 .\" which is licensed free of charge, as they might when working
25 .\" Formatted or processed versions of this manual, if unaccompanied by
26 .\" the source, must acknowledge the copyright and authors of this work.
29 .TH SECCOMP 2 2021-03-22 "Linux" "Linux Programmer's Manual"
31 seccomp \- operate on Secure Computing state of the process
34 .BR "#include <linux/seccomp.h>" " /* Definition of " SECCOMP_* " constants */"
35 .BR "#include <linux/filter.h>" " /* Definition of " "struct sock_fprog" " */"
36 .BR "#include <linux/audit.h>" " /* Definition of " AUDIT_* " constants */"
37 .BR "#include <linux/signal.h>" " /* Definition of " SIG* " constants */"
38 .BR "#include <sys/ptrace.h>" " /* Definition of " PTRACE_* " constants */"
39 .\" Kees Cook noted: Anything that uses SECCOMP_RET_TRACE returns will
40 .\" need <sys/ptrace.h>
41 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
42 .B #include <unistd.h>
44 .BI "int syscall(SYS_seccomp, unsigned int " operation ", unsigned int " flags ,
49 glibc provides no wrapper for
51 necessitating the use of
56 system call operates on the Secure Computing (seccomp) state of the
59 Currently, Linux supports the following
63 .BR SECCOMP_SET_MODE_STRICT
64 The only system calls that the calling thread is permitted to make are
72 Other system calls result in the delivery of a
75 Strict secure computing mode is useful for number-crunching
76 applications that may need to execute untrusted byte code, perhaps
77 obtained by reading from a pipe or socket.
79 Note that although the calling thread can no longer call
83 to block all signals apart from
89 (for example) is not sufficient for restricting the process's execution time.
90 Instead, to reliably terminate the process,
93 This can be done by using
103 to set the hard limit for
106 This operation is available only if the kernel is configured with
116 This operation is functionally identical to the call:
120 prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
124 .BR SECCOMP_SET_MODE_FILTER
125 The system calls allowed are defined by a pointer to a Berkeley Packet
126 Filter (BPF) passed via
128 This argument is a pointer to a
129 .IR "struct\ sock_fprog" ;
130 it can be designed to filter arbitrary system calls and system call
132 If the filter is invalid,
143 is allowed by the filter, any child processes will be constrained to
144 the same system call filters as the parent.
148 the existing filters will be preserved across a call to
152 .BR SECCOMP_SET_MODE_FILTER
153 operation, either the calling thread must have the
155 capability in its user namespace, or the thread must already have the
158 If that bit was not already set by an ancestor of this thread,
159 the thread must make the following call:
163 prctl(PR_SET_NO_NEW_PRIVS, 1);
168 .BR SECCOMP_SET_MODE_FILTER
169 operation fails and returns
173 This requirement ensures that an unprivileged process cannot apply
174 a malicious filter and then invoke a set-user-ID or
175 other privileged program using
177 thus potentially compromising that program.
178 (Such a malicious filter might, for example, cause an attempt to use
180 to set the caller's user IDs to nonzero values to instead
181 return 0 without actually making the system call.
182 Thus, the program might be tricked into retaining superuser privileges
183 in circumstances where it is possible to influence it to do
184 dangerous things because it did not actually drop privileges.)
190 is allowed by the attached filter, further filters may be added.
191 This will increase evaluation time, but allows for further reduction of
192 the attack surface during execution of a thread.
195 .BR SECCOMP_SET_MODE_FILTER
196 operation is available only if the kernel is configured with
197 .BR CONFIG_SECCOMP_FILTER
202 is 0, this operation is functionally identical to the call:
206 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, args);
215 .BR SECCOMP_FILTER_FLAG_LOG " (since Linux 4.14)"
216 .\" commit e66a39977985b1e69e17c4042cb290768eca9b02
217 All filter return actions except
218 .BR SECCOMP_RET_ALLOW
220 An administrator may override this filter flag by preventing specific
221 actions from being logged via the
222 .IR /proc/sys/kernel/seccomp/actions_logged
225 .BR SECCOMP_FILTER_FLAG_NEW_LISTENER " (since Linux 5.0)"
226 .\" commit 6a21cc50f0c7f87dae5259f6cfefe024412313f6
227 After successfully installing the filter program,
228 return a new user-space notification file descriptor.
229 (The close-on-exec flag is set for the file descriptor.)
230 When the filter returns
231 .BR SECCOMP_RET_USER_NOTIF
232 a notification will be sent to this file descriptor.
234 At most one seccomp filter using the
235 .BR SECCOMP_FILTER_FLAG_NEW_LISTENER
236 flag can be installed for a thread.
239 .BR seccomp_unotify (2)
242 .BR SECCOMP_FILTER_FLAG_SPEC_ALLOW " (since Linux 4.17)"
243 .\" commit 00a02d0c502a06d15e07b857f8ff921e3e402675
244 Disable Speculative Store Bypass mitigation.
246 .BR SECCOMP_FILTER_FLAG_TSYNC
247 When adding a new filter, synchronize all other threads of the calling
248 process to the same seccomp filter tree.
249 A "filter tree" is the ordered list of filters attached to a thread.
250 (Attaching identical filters in separate
252 calls results in different filters from this perspective.)
254 If any thread cannot synchronize to the same filter tree,
255 the call will not attach the new seccomp filter,
256 and will fail, returning the first thread ID found that cannot synchronize.
257 Synchronization will fail if another thread in the same process is in
258 .BR SECCOMP_MODE_STRICT
259 or if it has attached new seccomp filters to itself,
260 diverging from the calling thread's filter tree.
263 .BR SECCOMP_GET_ACTION_AVAIL " (since Linux 4.14)"
264 .\" commit d612b1fd8010d0d67b5287fe146b8b55bcbb8655
265 Test to see if an action is supported by the kernel.
266 This operation is helpful to confirm that the kernel knows
267 of a more recently added filter return action
268 since the kernel treats all unknown actions as
269 .BR SECCOMP_RET_KILL_PROCESS .
275 must be a pointer to an unsigned 32-bit filter return action.
277 .BR SECCOMP_GET_NOTIF_SIZES " (since Linux 5.0)"
278 .\" commit 6a21cc50f0c7f87dae5259f6cfefe024412313f6
279 Get the sizes of the seccomp user-space notification structures.
280 Since these structures may evolve and grow over time,
281 this command can be used to determine how
282 much memory to allocate for sending and receiving notifications.
288 must be a pointer to a
289 .IR "struct seccomp_notif_sizes" ,
290 which has the following form:
293 struct seccomp_notif_sizes
294 __u16 seccomp_notif; /* Size of notification structure */
295 __u16 seccomp_notif_resp; /* Size of response structure */
296 __u16 seccomp_data; /* Size of \(aqstruct seccomp_data\(aq */
301 .BR seccomp_unotify (2)
305 When adding filters via
306 .BR SECCOMP_SET_MODE_FILTER ,
308 points to a filter program:
313 unsigned short len; /* Number of BPF instructions */
314 struct sock_filter *filter; /* Pointer to array of
320 Each program must contain one or more BPF instructions:
324 struct sock_filter { /* Filter block */
325 __u16 code; /* Actual filter code */
326 __u8 jt; /* Jump true */
327 __u8 jf; /* Jump false */
328 __u32 k; /* Generic multiuse field */
333 When executing the instructions, the BPF program operates on the
334 system call information made available (i.e., use the
336 addressing mode) as a (read-only)
337 .\" Quoting Kees Cook:
338 .\" If BPF even allows changing the data, it's not copied back to
339 .\" the syscall when it runs. Anything wanting to do things like
340 .\" that would need to use ptrace to catch the call and directly
341 .\" modify the registers before continuing with the call.
342 buffer of the following form:
346 struct seccomp_data {
347 int nr; /* System call number */
348 __u32 arch; /* AUDIT_ARCH_* value
349 (see <linux/audit.h>) */
350 __u64 instruction_pointer; /* CPU instruction pointer */
351 __u64 args[6]; /* Up to 6 system call arguments */
356 Because numbering of system calls varies between architectures and
357 some architectures (e.g., x86-64) allow user-space code to use
358 the calling conventions of multiple architectures
359 (and the convention being used may vary over the life of a process that uses
361 to execute binaries that employ the different conventions),
362 it is usually necessary to verify the value of the
366 It is strongly recommended to use an allow-list approach whenever
367 possible because such an approach is more robust and simple.
368 A deny-list will have to be updated whenever a potentially
369 dangerous system call is added (or a dangerous flag or option if those
370 are deny-listed), and it is often possible to alter the
371 representation of a value without altering its meaning, leading to
379 field is not unique for all calling conventions.
380 The x86-64 ABI and the x32 ABI both use
381 .BR AUDIT_ARCH_X86_64
384 and they run on the same processors.
386 .BR __X32_SYSCALL_BIT
387 is used on the system call number to tell the two ABIs apart.
388 .\" As noted by Dave Drysdale in a note at the end of
389 .\" https://lwn.net/Articles/604515/
390 .\" One additional detail to point out for the x32 ABI case:
391 .\" the syscall number gets a high bit set (__X32_SYSCALL_BIT),
392 .\" to mark it as an x32 call.
394 .\" If x32 support is included in the kernel, then __SYSCALL_MASK
395 .\" will have a value that is not all-ones, and this will trigger
396 .\" an extra instruction in system_call to mask off the extra bit,
397 .\" so that the syscall table indexing still works.
399 This means that a policy must either deny all syscalls with
400 .BR __X32_SYSCALL_BIT
401 or it must recognize syscalls with and without
402 .BR __X32_SYSCALL_BIT
404 A list of system calls to be denied based on
406 that does not also contain
409 .BR __X32_SYSCALL_BIT
410 set can be bypassed by a malicious program that sets
411 .BR __X32_SYSCALL_BIT .
413 Additionally, kernels prior to Linux 5.4 incorrectly permitted
415 in the ranges 512-547 as well as the corresponding non-x32 syscalls ORed
417 .BR __X32_SYSCALL_BIT .
423 .BR __X32_SYSCALL_BIT )
424 would result in invocations of
426 with potentially confused x32-vs-x86_64 semantics in the kernel.
427 Policies intended to work on kernels before Linux 5.4 must ensure that they
428 deny or otherwise correctly handle these system calls.
429 On Linux 5.4 and newer,
430 .\" commit 6365b842aae4490ebfafadfc6bb27a6d3cc54757
431 such system calls will fail with the error
433 without doing anything.
436 .I instruction_pointer
437 field provides the address of the machine-language instruction that
438 performed the system call.
439 This might be useful in conjunction with the use of
441 to perform checks based on which region (mapping) of the program
442 made the system call.
443 (Probably, it is wise to lock down the
447 system calls to prevent the program from subverting such checks.)
449 When checking values from
451 keep in mind that arguments are often
452 silently truncated before being processed, but after the seccomp check.
453 For example, this happens if the i386 ABI is used on an
454 x86-64 kernel: although the kernel will normally not look beyond
455 the 32 lowest bits of the arguments, the values of the full
456 64-bit registers will be present in the seccomp data.
457 A less surprising example is that if the x86-64 ABI is used to perform
458 a system call that takes an argument of type
460 the more-significant half of the argument register is ignored by
461 the system call, but visible in the seccomp data.
463 A seccomp filter returns a 32-bit value consisting of two parts:
464 the most significant 16 bits
465 (corresponding to the mask defined by the constant
466 .BR SECCOMP_RET_ACTION_FULL )
467 contain one of the "action" values listed below;
468 the least significant 16-bits (defined by the constant
469 .BR SECCOMP_RET_DATA )
470 are "data" to be associated with this return value.
472 If multiple filters exist, they are \fIall\fP executed,
473 in reverse order of their addition to the filter tree\(emthat is,
474 the most recently installed filter is executed first.
475 (Note that all filters will be called
476 even if one of the earlier filters returns
477 .BR SECCOMP_RET_KILL .
478 This is done to simplify the kernel code and to provide a
479 tiny speed-up in the execution of sets of filters by
480 avoiding a check for this uncommon case.)
481 .\" From an Aug 2015 conversation with Kees Cook where I asked why *all*
482 .\" filters are applied even if one of the early filters returns
483 .\" SECCOMP_RET_KILL:
485 .\" It's just because it would be an optimization that would only speed up
486 .\" the RET_KILL case, but it's the uncommon one and the one that doesn't
487 .\" benefit meaningfully from such a change (you need to kill the process
488 .\" really quickly?). We would speed up killing a program at the (albeit
489 .\" tiny) expense to all other filtered programs. Best to keep the filter
490 .\" execution logic clear, simple, and as fast as possible for all
492 The return value for the evaluation of a given system call is the first-seen
493 action value of highest precedence (along with its accompanying data)
494 returned by execution of all of the filters.
496 In decreasing order of precedence,
497 the action values that may be returned by a seccomp filter are:
499 .BR SECCOMP_RET_KILL_PROCESS " (since Linux 4.14)"
500 .\" commit 4d3b0b05aae9ee9ce0970dc4cc0fb3fad5e85945
501 .\" commit 0466bdb99e8744bc9befa8d62a317f0fd7fd7421
502 This value results in immediate termination of the process,
504 The system call is not executed.
506 .BR SECCOMP_RET_KILL_THREAD
507 below, all threads in the thread group are terminated.
508 (For a discussion of thread groups, see the description of the
513 The process terminates
518 Even if a signal handler has been registered for
520 the handler will be ignored in this case and the process always terminates.
521 To a parent process that is waiting on this process (using
523 or similar), the returned
525 will indicate that its child was terminated as though by a
529 .BR SECCOMP_RET_KILL_THREAD " (or " SECCOMP_RET_KILL )
530 This value results in immediate termination of the thread
531 that made the system call.
532 The system call is not executed.
533 Other threads in the same thread group will continue to execute.
535 The thread terminates
541 .BR SECCOMP_RET_KILL_PROCESS
544 .\" See these commits:
545 .\" seccomp: dump core when using SECCOMP_RET_KILL
546 .\" (b25e67161c295c98acda92123b2dd1e7d8642901)
547 .\" seccomp: Only dump core when single-threaded
548 .\" (d7276e321ff8a53106a59c85ca46d03e34288893)
550 any process terminated in this way would not trigger a coredump
555 as having a default action of termination with a core dump).
557 a single-threaded process will dump core if terminated in this way.
560 .BR SECCOMP_RET_KILL_PROCESS
562 .BR SECCOMP_RET_KILL_THREAD
563 was added as a synonym for
564 .BR SECCOMP_RET_KILL ,
565 in order to more clearly distinguish the two actions.
569 .BR SECCOMP_RET_KILL_THREAD
570 to kill a single thread in a multithreaded process is likely to leave the
571 process in a permanently inconsistent and possibly corrupt state.
574 This value results in the kernel sending a thread-directed
576 signal to the triggering thread.
577 (The system call is not executed.)
578 Various fields will be set in the
582 associated with signal:
590 will show the address of the system call instruction.
595 will indicate which system call was attempted.
604 portion of the filter return value.
607 The program counter will be as though the system call happened
608 (i.e., the program counter will not point to the system call instruction).
609 The return value register will contain an architecture\-dependent value;
610 if resuming execution, set it to something appropriate for the system call.
611 (The architecture dependency is because replacing it with
613 could overwrite some useful information.)
615 .BR SECCOMP_RET_ERRNO
616 This value results in the
618 portion of the filter's return value being passed to user space as the
620 value without executing the system call.
622 .BR SECCOMP_RET_USER_NOTIF " (since Linux 5.0)"
623 .\" commit 6a21cc50f0c7f87dae5259f6cfefe024412313f6
624 Forward the system call to an attached user-space supervisor
625 process to allow that process to decide what to do with the system call.
626 If there is no attached supervisor (either
627 because the filter was not installed with the
628 .BR SECCOMP_FILTER_FLAG_NEW_LISTENER
629 flag or because the file descriptor was closed), the filter returns
631 (similar to what happens when a filter returns
632 .BR SECCOMP_RET_TRACE
633 and there is no tracer).
635 .BR seccomp_unotify (2)
638 Note that the supervisor process will not be notified
639 if another filter returns an action value with a precedence greater than
640 .BR SECCOMP_RET_USER_NOTIF .
642 .BR SECCOMP_RET_TRACE
643 When returned, this value will cause the kernel to attempt to notify a
645 tracer prior to executing the system call.
646 If there is no tracer present,
647 the system call is not executed and returns a failure status with
652 A tracer will be notified if it requests
653 .BR PTRACE_O_TRACESECCOMP
655 .IR ptrace(PTRACE_SETOPTIONS) .
656 The tracer will be notified of a
657 .BR PTRACE_EVENT_SECCOMP
660 portion of the filter's return value will be available to the tracer via
661 .BR PTRACE_GETEVENTMSG .
663 The tracer can skip the system call by changing the system call number
665 Alternatively, the tracer can change the system call
666 requested by changing the system call to a valid system call number.
667 If the tracer asks to skip the system call, then the system call will
668 appear to return the value that the tracer puts in the return value register.
670 .\" This was changed in ce6526e8afa4.
671 .\" A related hole, using PTRACE_SYSCALL instead of SECCOMP_RET_TRACE, was
672 .\" changed in arch-specific commits, e.g. 93e35efb8de4 for X86 and
673 .\" 0f3912fd934c for ARM.
674 Before kernel 4.8, the seccomp check will not be run again after the tracer is
676 (This means that, on older kernels, seccomp-based sandboxes
679 .BR ptrace (2)\(emeven
681 sandboxed processes\(emwithout extreme care;
682 ptracers can use this mechanism to escape from the seccomp sandbox.)
684 Note that a tracer process will not be notified
685 if another filter returns an action value with a precedence greater than
686 .BR SECCOMP_RET_TRACE .
688 .BR SECCOMP_RET_LOG " (since Linux 4.14)"
689 .\" commit 59f5cf44a38284eb9e76270c786fb6cc62ef8ac4
690 This value results in the system call being executed after
691 the filter return action is logged.
692 An administrator may override the logging of this action via
694 .IR /proc/sys/kernel/seccomp/actions_logged
697 .BR SECCOMP_RET_ALLOW
698 This value results in the system call being executed.
700 If an action value other than one of the above is specified,
701 then the filter action is treated as either
702 .BR SECCOMP_RET_KILL_PROCESS
704 .\" commit 4d3b0b05aae9ee9ce0970dc4cc0fb3fad5e85945
706 .BR SECCOMP_RET_KILL_THREAD
707 (in Linux 4.13 and earlier).
710 The files in the directory
711 .IR /proc/sys/kernel/seccomp
712 provide additional seccomp information and configuration:
714 .IR actions_avail " (since Linux 4.14)"
715 .\" commit 8e5f1ad116df6b0de65eac458d5e7c318d1c05af
716 A read-only ordered list of seccomp filter return actions in string form.
717 The ordering, from left-to-right, is in decreasing order of precedence.
718 The list represents the set of seccomp filter return actions
719 supported by the kernel.
721 .IR actions_logged " (since Linux 4.14)"
722 .\" commit 0ddec0fc8900201c0897b87b762b7c420436662f
723 A read-write ordered list of seccomp filter return actions that
724 are allowed to be logged.
725 Writes to the file do not need to be in ordered form but reads from
726 the file will be ordered in the same way as the
730 It is important to note that the value of
732 does not prevent certain filter return actions from being logged when
733 the audit subsystem is configured to audit a task.
734 If the action is not found in the
736 file, the final decision on whether to audit the action for that task is
737 ultimately left up to the audit subsystem to decide for all filter return
739 .BR SECCOMP_RET_ALLOW .
741 The "allow" string is not accepted in the
743 file as it is not possible to log
744 .BR SECCOMP_RET_ALLOW
746 Attempting to write "allow" to the file will fail with the error
749 .SS Audit logging of seccomp actions
750 .\" commit 59f5cf44a38284eb9e76270c786fb6cc62ef8ac4
751 Since Linux 4.14, the kernel provides the facility to log the
752 actions returned by seccomp filters in the audit log.
753 The kernel makes the decision to log an action based on
754 the action type, whether or not the action is present in the
756 file, and whether kernel auditing is enabled
757 (e.g., via the kernel boot option
759 .\" or auditing could be enabled via the netlink API (AUDIT_SET)
760 The rules are as follows:
763 .BR SECCOMP_RET_ALLOW ,
764 the action is not logged.
766 Otherwise, if the action is either
767 .BR SECCOMP_RET_KILL_PROCESS
769 .BR SECCOMP_RET_KILL_THREAD ,
770 and that action appears in the
772 file, the action is logged.
774 Otherwise, if the filter has requested logging (the
775 .BR SECCOMP_FILTER_FLAG_LOG
777 and the action appears in the
779 file, the action is logged.
781 Otherwise, if kernel auditing is enabled and the process is being audited
783 the action is logged.
785 Otherwise, the action is not logged.
791 .BR SECCOMP_FILTER_FLAG_TSYNC
793 the return value is the ID of the thread
794 that caused the synchronization failure.
795 (This ID is a kernel thread ID of the type returned by
799 On other errors, \-1 is returned, and
801 is set to indicate the error.
804 can fail for the following reasons:
807 The caller did not have the
809 capability in its user namespace, or had not set
812 .BR SECCOMP_SET_MODE_FILTER .
815 While installing a new filter, the
816 .BR SECCOMP_FILTER_FLAG_NEW_LISTENER
818 but a previous filter had already been installed with that flag.
822 was not a valid address.
826 is unknown or is not supported by this kernel version or configuration.
831 are invalid for the given
838 but the specified offset was not aligned to a 32-bit boundary or exceeded
839 .IR "sizeof(struct\ seccomp_data)" .
842 .\" See kernel/seccomp.c::seccomp_may_assign_mode() in 3.18 sources
843 A secure computing mode has already been set, and
845 differs from the existing setting.
850 .BR SECCOMP_SET_MODE_FILTER ,
851 but the filter program pointed to by
853 was not valid or the length of the filter program was zero or exceeded
861 .\" ENOMEM in kernel/seccomp.c::seccomp_attach_filter() in 3.18 sources
862 The total length of all filter programs attached
863 to the calling thread would exceed
864 .B MAX_INSNS_PER_PATH
865 (32768) instructions.
866 Note that for the purposes of calculating this limit,
867 each already existing filter program incurs an
868 overhead penalty of 4 instructions.
873 .BR SECCOMP_GET_ACTION_AVAIL ,
874 but the kernel does not support the filter return action specified by
878 Another thread caused a failure during thread sync, but its ID could not
883 system call first appeared in Linux 3.17.
884 .\" FIXME . Add glibc version
888 system call is a nonstandard Linux extension.
890 Rather than hand-coding seccomp filters as shown in the example below,
891 you may prefer to employ the
893 library, which provides a front-end for generating seccomp filters.
898 .IR /proc/[pid]/status
899 file provides a method of viewing the seccomp mode of a process; see
903 provides a superset of the functionality provided by the
906 operation (which does not support
911 .B PTRACE_SECCOMP_GET_FILTER
912 operation can be used to dump a process's seccomp filters.
914 .SS Architecture support for seccomp BPF
915 Architecture support for seccomp BPF filtering
916 .\" Check by grepping for HAVE_ARCH_SECCOMP_FILTER in Kconfig files in
917 .\" kernel source. Last checked in Linux 4.16-rc source.
918 is available on the following architectures:
920 x86-64, i386, x32 (since Linux 3.5)
923 ARM (since Linux 3.8)
925 s390 (since Linux 3.8)
927 MIPS (since Linux 3.16)
929 ARM-64 (since Linux 3.19)
931 PowerPC (since Linux 4.3)
933 Tile (since Linux 4.3)
935 PA-RISC (since Linux 4.6)
936 .\" User mode Linux since Linux 4.6
940 There are various subtleties to consider when applying seccomp filters
941 to a program, including the following:
943 Some traditional system calls have user-space implementations in the
945 on many architectures.
946 Notable examples include
947 .BR clock_gettime (2),
948 .BR gettimeofday (2),
951 On such architectures,
952 seccomp filtering for these system calls will have no effect.
953 (However, there are cases where the
955 implementations may fall back to invoking the true system call,
956 in which case seccomp filters would see the system call.)
958 Seccomp filtering is based on system call numbers.
959 However, applications typically do not directly invoke system calls,
960 but instead call wrapper functions in the C library which
961 in turn invoke the system calls.
962 Consequently, one must be aware of the following:
965 The glibc wrappers for some traditional system calls may actually
966 employ system calls with different names in the kernel.
969 wrapper function actually employs the
973 wrapper function actually calls
976 The behavior of wrapper functions may vary across architectures,
977 according to the range of system calls provided on those architectures.
978 In other words, the same wrapper function may invoke
979 different system calls on different architectures.
981 Finally, the behavior of wrapper functions can change across glibc versions.
982 For example, in older versions, the glibc wrapper function for
984 invoked the system call of the same name,
985 but starting in glibc 2.26, the implementation switched to calling
987 on all architectures.
990 The consequence of the above points is that it may be necessary
991 to filter for a system call other than might be expected.
992 Various manual pages in Section 2 provide helpful details
993 about the differences between wrapper functions and
994 the underlying system calls in subsections entitled
995 .IR "C library/kernel differences" .
997 Furthermore, note that the application of seccomp filters
998 even risks causing bugs in an application,
999 when the filters cause unexpected failures for legitimate operations
1000 that the application might need to perform.
1001 Such bugs may not easily be discovered when testing the seccomp
1002 filters if the bugs occur in rarely used application code paths.
1004 .SS Seccomp-specific BPF details
1005 Note the following BPF details specific to seccomp filters:
1011 size modifiers are not supported: all operations must load and store
1015 To access the contents of the
1019 addressing mode modifier.
1023 addressing mode modifier yields an immediate mode operand
1024 whose value is the size of the
1028 The program below accepts four or more arguments.
1029 The first three arguments are a system call number,
1030 a numeric architecture identifier, and an error number.
1031 The program uses these values to construct a BPF filter
1032 that is used at run time to perform the following checks:
1034 If the program is not running on the specified architecture,
1035 the BPF filter causes system calls to fail with the error
1038 If the program attempts to execute the system call with the specified number,
1039 the BPF filter causes the system call to fail, with
1041 being set to the specified error number.
1043 The remaining command-line arguments specify
1044 the pathname and additional arguments of a program
1045 that the example program should attempt to execute using
1047 (a library function that employs the
1050 Some example runs of the program are shown below.
1052 First, we display the architecture that we are running on (x86-64)
1053 and then construct a shell function that looks up system call
1054 numbers on this architecture:
1061 cat /usr/src/linux/arch/x86/syscalls/syscall_64.tbl | \e
1062 awk \(aq$2 != "x32" && $3 == "\(aq$1\(aq" { print $1 }\(aq
1067 When the BPF filter rejects a system call (case [2] above),
1068 it causes the system call to fail with the error number
1069 specified on the command line.
1070 In the experiments shown here, we'll use error number 99:
1075 EADDRNOTAVAIL 99 Cannot assign requested address
1079 In the following example, we attempt to run the command
1081 but the BPF filter rejects the
1083 system call, so that the command is not even executed:
1087 $ \fBsyscall_nr execve\fP
1090 Usage: ./a.out <syscall_nr> <arch> <errno> <prog> [<args>]
1091 Hint for <arch>: AUDIT_ARCH_I386: 0x40000003
1092 AUDIT_ARCH_X86_64: 0xC000003E
1093 $ \fB./a.out 59 0xC000003E 99 /bin/whoami\fP
1094 execv: Cannot assign requested address
1098 In the next example, the BPF filter rejects the
1100 system call, so that, although it is successfully started, the
1102 command is not able to write output:
1106 $ \fBsyscall_nr write\fP
1108 $ \fB./a.out 1 0xC000003E 99 /bin/whoami\fP
1112 In the final example,
1113 the BPF filter rejects a system call that is not used by the
1115 command, so it is able to successfully execute and produce output:
1119 $ \fBsyscall_nr preadv\fP
1121 $ \fB./a.out 295 0xC000003E 99 /bin/whoami\fP
1132 #include <linux/audit.h>
1133 #include <linux/filter.h>
1134 #include <linux/seccomp.h>
1135 #include <sys/prctl.h>
1137 #define X32_SYSCALL_BIT 0x40000000
1138 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
1141 install_filter(int syscall_nr, int t_arch, int f_errno)
1143 unsigned int upper_nr_limit = 0xffffffff;
1145 /* Assume that AUDIT_ARCH_X86_64 means the normal x86\-64 ABI
1146 (in the x32 ABI, all system calls have bit 30 set in the
1147 \(aqnr\(aq field, meaning the numbers are >= X32_SYSCALL_BIT). */
1148 if (t_arch == AUDIT_ARCH_X86_64)
1149 upper_nr_limit = X32_SYSCALL_BIT \- 1;
1151 struct sock_filter filter[] = {
1152 /* [0] Load architecture from \(aqseccomp_data\(aq buffer into
1154 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1155 (offsetof(struct seccomp_data, arch))),
1157 /* [1] Jump forward 5 instructions if architecture does not
1158 match \(aqt_arch\(aq. */
1159 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, t_arch, 0, 5),
1161 /* [2] Load system call number from \(aqseccomp_data\(aq buffer into
1163 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1164 (offsetof(struct seccomp_data, nr))),
1166 /* [3] Check ABI \- only needed for x86\-64 in deny\-list use
1167 cases. Use BPF_JGT instead of checking against the bit
1168 mask to avoid having to reload the syscall number. */
1169 BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, upper_nr_limit, 3, 0),
1171 /* [4] Jump forward 1 instruction if system call number
1172 does not match \(aqsyscall_nr\(aq. */
1173 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, syscall_nr, 0, 1),
1175 /* [5] Matching architecture and system call: don\(aqt execute
1176 the system call, and return \(aqf_errno\(aq in \(aqerrno\(aq. */
1177 BPF_STMT(BPF_RET | BPF_K,
1178 SECCOMP_RET_ERRNO | (f_errno & SECCOMP_RET_DATA)),
1180 /* [6] Destination of system call number mismatch: allow other
1182 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
1184 /* [7] Destination of architecture mismatch: kill process. */
1185 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
1188 struct sock_fprog prog = {
1189 .len = ARRAY_SIZE(filter),
1193 if (seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog)) {
1202 main(int argc, char *argv[])
1205 fprintf(stderr, "Usage: "
1206 "%s <syscall_nr> <arch> <errno> <prog> [<args>]\en"
1207 "Hint for <arch>: AUDIT_ARCH_I386: 0x%X\en"
1208 " AUDIT_ARCH_X86_64: 0x%X\en"
1209 "\en", argv[0], AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
1213 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1218 if (install_filter(strtol(argv[1], NULL, 0),
1219 strtol(argv[2], NULL, 0),
1220 strtol(argv[3], NULL, 0)))
1223 execv(argv[4], &argv[4]);
1234 .BR seccomp_unotify (2),
1240 Various pages from the
1243 .BR scmp_sys_resolver (1),
1244 .BR seccomp_export_bpf (3),
1245 .BR seccomp_init (3),
1246 .BR seccomp_load (3),
1248 .BR seccomp_rule_add (3).
1250 The kernel source files
1251 .IR Documentation/networking/filter.txt
1253 .IR Documentation/userspace\-api/seccomp_filter.rst
1254 .\" commit c061f33f35be0ccc80f4b8e0aea5dfd2ed7e01a3
1256 .IR Documentation/prctl/seccomp_filter.txt
1259 McCanne, S.\& and Jacobson, V.\& (1992)
1260 .IR "The BSD Packet Filter: A New Architecture for User-level Packet Capture" ,
1261 Proceedings of the USENIX Winter 1993 Conference
1262 .UR http://www.tcpdump.org/papers/bpf\-usenix93.pdf