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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
9 .TH seccomp 2 (date) "Linux man-pages (unreleased)"
11 seccomp \- operate on Secure Computing state of the process
14 .RI ( libc ", " \-lc )
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>
27 .BI "int syscall(SYS_seccomp, unsigned int " operation ", unsigned int " flags ,
32 glibc provides no wrapper for
34 necessitating the use of
39 system call operates on the Secure Computing (seccomp) state of the
42 Currently, Linux supports the following
46 .B SECCOMP_SET_MODE_STRICT
47 The only system calls that the calling thread is permitted to make are
55 Other system calls result in the termination of the calling thread,
56 or termination of the entire process with the
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.
63 Note that although the calling thread can no longer call
67 to block all signals apart from
73 (for example) is not sufficient for restricting the process's execution time.
74 Instead, to reliably terminate the process,
77 This can be done by using
87 to set the hard limit for
90 This operation is available only if the kernel is configured with
100 This operation is functionally identical to the call:
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
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
116 If the filter is invalid,
127 is allowed by the filter, any child processes will be constrained to
128 the same system call filters as the parent.
132 the existing filters will be preserved across a call to
136 .B SECCOMP_SET_MODE_FILTER
137 operation, either the calling thread must have the
139 capability in its user namespace, or the thread must already have the
142 If that bit was not already set by an ancestor of this thread,
143 the thread must make the following call:
147 prctl(PR_SET_NO_NEW_PRIVS, 1);
152 .B SECCOMP_SET_MODE_FILTER
153 operation fails and returns
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
161 thus potentially compromising that program.
162 (Such a malicious filter might, for example, cause an attempt to use
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.)
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
186 is 0, this operation is functionally identical to the call:
190 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, args);
199 .BR SECCOMP_FILTER_FLAG_LOG " (since Linux 4.14)"
200 .\" commit e66a39977985b1e69e17c4042cb290768eca9b02
201 All filter return actions except
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
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)
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
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 .
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.
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 __u16 seccomp_notif; /* Size of notification structure */
279 __u16 seccomp_notif_resp; /* Size of response structure */
280 __u16 seccomp_data; /* Size of \[aq]struct seccomp_data\[aq] */
285 .BR seccomp_unotify (2)
289 When adding filters via
290 .BR SECCOMP_SET_MODE_FILTER ,
292 points to a filter program:
297 unsigned short len; /* Number of BPF instructions */
298 struct sock_filter *filter; /* Pointer to array of
304 Each program must contain one or more BPF instructions:
308 struct sock_filter { /* Filter block */
309 __u16 code; /* Actual filter code */
310 __u8 jt; /* Jump true */
311 __u8 jf; /* Jump false */
312 __u32 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
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:
330 struct seccomp_data {
331 int nr; /* System call number */
332 __u32 arch; /* AUDIT_ARCH_* value
333 (see <linux/audit.h>) */
334 __u64 instruction_pointer; /* CPU instruction pointer */
335 __u64 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
345 to execute binaries that employ the different conventions),
346 it is usually necessary to verify the value of the
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
363 field is not unique for all calling conventions.
364 The x86-64 ABI and the x32 ABI both use
368 and they run on the same processors.
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
385 or it must recognize syscalls with and without
388 A list of system calls to be denied based on
390 that does not also contain
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
399 in the ranges 512-547 as well as the corresponding non-x32 syscalls ORed
401 .BR __X32_SYSCALL_BIT .
407 .BR __X32_SYSCALL_BIT )
408 would result in invocations of
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
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
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
431 system calls to prevent the program from subverting such checks.)
433 When checking values from
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
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\[em]that 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
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,
488 The system call is not executed.
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
497 The process terminates
502 Even if a signal handler has been registered for
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
507 or similar), the returned
509 will indicate that its child was terminated as though by a
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
525 .B SECCOMP_RET_KILL_PROCESS
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)
534 any process terminated in this way would not trigger a coredump
539 as having a default action of termination with a core dump).
541 a single-threaded process will dump core if terminated in this way.
544 .B SECCOMP_RET_KILL_PROCESS
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.
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.
558 This value results in the kernel sending a thread-directed
560 signal to the triggering thread.
561 (The system call is not executed.)
562 Various fields will be set in the
566 associated with signal:
574 will show the address of the system call instruction.
579 will indicate which system call was attempted.
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
597 could overwrite some useful information.)
600 This value results in the
602 portion of the filter's return value being passed to user space as the
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
615 (similar to what happens when a filter returns
617 and there is no tracer).
619 .BR seccomp_unotify (2)
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 .
627 When returned, this value will cause the kernel to attempt to notify a
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
636 A tracer will be notified if it requests
637 .B PTRACE_O_TRACESECCOMP
639 .IR ptrace(PTRACE_SETOPTIONS) .
640 The tracer will be notified of a
641 .B PTRACE_EVENT_SECCOMP
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
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 Linux 4.8, the seccomp check will not be run again after the tracer is
660 (This means that, on older kernels, seccomp-based sandboxes
663 .BR ptrace (2)\[em]even
665 sandboxed processes\[em]without 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
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
688 .\" commit 4d3b0b05aae9ee9ce0970dc4cc0fb3fad5e85945
690 .B SECCOMP_RET_KILL_THREAD
691 (in Linux 4.13 and earlier).
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
714 It is important to note that the value of
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
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
723 .BR SECCOMP_RET_ALLOW .
725 The "allow" string is not accepted in the
727 file as it is not possible to log
730 Attempting to write "allow" to the file will fail with the error
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
740 file, and whether kernel auditing is enabled
741 (e.g., via the kernel boot option
743 .\" or auditing could be enabled via the netlink API (AUDIT_SET)
744 The rules are as follows:
747 .BR SECCOMP_RET_ALLOW ,
748 the action is not logged.
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
756 file, the action is logged.
758 Otherwise, if the filter has requested logging (the
759 .B SECCOMP_FILTER_FLAG_LOG
761 and the action appears in the
763 file, the action is logged.
765 Otherwise, if kernel auditing is enabled and the process is being audited
767 the action is logged.
769 Otherwise, the action is not logged.
775 .B SECCOMP_FILTER_FLAG_TSYNC
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
783 On other errors, \-1 is returned, and
785 is set to indicate the error.
788 can fail for the following reasons:
791 The caller did not have the
793 capability in its user namespace, or had not set
796 .BR SECCOMP_SET_MODE_FILTER .
799 While installing a new filter, the
800 .B SECCOMP_FILTER_FLAG_NEW_LISTENER
802 but a previous filter had already been installed with that flag.
806 was not a valid address.
810 is unknown or is not supported by this kernel version or configuration.
815 are invalid for the given
822 but the specified offset was not aligned to a 32-bit boundary or exceeded
823 .IR "sizeof(struct\~seccomp_data)" .
826 .\" See kernel/seccomp.c::seccomp_may_assign_mode() in Linux 3.18 sources
827 A secure computing mode has already been set, and
829 differs from the existing setting.
834 .BR SECCOMP_SET_MODE_FILTER ,
835 but the filter program pointed to by
837 was not valid or the length of the filter program was zero or exceeded
845 .\" ENOMEM in kernel/seccomp.c::seccomp_attach_filter() in Linux 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.
857 .BR SECCOMP_GET_ACTION_AVAIL ,
858 but the kernel does not support the filter return action specified by
862 Another thread caused a failure during thread sync, but its ID could not
868 .\" FIXME . Add glibc version
870 Rather than hand-coding seccomp filters as shown in the example below,
871 you may prefer to employ the
873 library, which provides a front-end for generating seccomp filters.
878 .IR /proc/ pid /status
879 file provides a method of viewing the seccomp mode of a process; see
883 provides a superset of the functionality provided by the
886 operation (which does not support
891 .B PTRACE_SECCOMP_GET_FILTER
892 operation can be used to dump a process's seccomp filters.
894 .SS Architecture support for seccomp BPF
895 Architecture support for seccomp BPF filtering
896 .\" Check by grepping for HAVE_ARCH_SECCOMP_FILTER in Kconfig files in
897 .\" kernel source. Last checked in Linux 4.16-rc source.
898 is available on the following architectures:
900 x86-64, i386, x32 (since Linux 3.5)
903 ARM (since Linux 3.8)
905 s390 (since Linux 3.8)
907 MIPS (since Linux 3.16)
909 ARM-64 (since Linux 3.19)
911 PowerPC (since Linux 4.3)
913 Tile (since Linux 4.3)
915 PA-RISC (since Linux 4.6)
916 .\" User mode Linux since Linux 4.6
920 There are various subtleties to consider when applying seccomp filters
921 to a program, including the following:
923 Some traditional system calls have user-space implementations in the
925 on many architectures.
926 Notable examples include
927 .BR clock_gettime (2),
928 .BR gettimeofday (2),
931 On such architectures,
932 seccomp filtering for these system calls will have no effect.
933 (However, there are cases where the
935 implementations may fall back to invoking the true system call,
936 in which case seccomp filters would see the system call.)
938 Seccomp filtering is based on system call numbers.
939 However, applications typically do not directly invoke system calls,
940 but instead call wrapper functions in the C library which
941 in turn invoke the system calls.
942 Consequently, one must be aware of the following:
945 The glibc wrappers for some traditional system calls may actually
946 employ system calls with different names in the kernel.
949 wrapper function actually employs the
953 wrapper function actually calls
956 The behavior of wrapper functions may vary across architectures,
957 according to the range of system calls provided on those architectures.
958 In other words, the same wrapper function may invoke
959 different system calls on different architectures.
961 Finally, the behavior of wrapper functions can change across glibc versions.
962 For example, in older versions, the glibc wrapper function for
964 invoked the system call of the same name,
965 but starting in glibc 2.26, the implementation switched to calling
967 on all architectures.
970 The consequence of the above points is that it may be necessary
971 to filter for a system call other than might be expected.
972 Various manual pages in Section 2 provide helpful details
973 about the differences between wrapper functions and
974 the underlying system calls in subsections entitled
975 .IR "C library/kernel differences" .
977 Furthermore, note that the application of seccomp filters
978 even risks causing bugs in an application,
979 when the filters cause unexpected failures for legitimate operations
980 that the application might need to perform.
981 Such bugs may not easily be discovered when testing the seccomp
982 filters if the bugs occur in rarely used application code paths.
984 .SS Seccomp-specific BPF details
985 Note the following BPF details specific to seccomp filters:
991 size modifiers are not supported: all operations must load and store
995 To access the contents of the
999 addressing mode modifier.
1003 addressing mode modifier yields an immediate mode operand
1004 whose value is the size of the
1008 The program below accepts four or more arguments.
1009 The first three arguments are a system call number,
1010 a numeric architecture identifier, and an error number.
1011 The program uses these values to construct a BPF filter
1012 that is used at run time to perform the following checks:
1014 If the program is not running on the specified architecture,
1015 the BPF filter causes system calls to fail with the error
1018 If the program attempts to execute the system call with the specified number,
1019 the BPF filter causes the system call to fail, with
1021 being set to the specified error number.
1023 The remaining command-line arguments specify
1024 the pathname and additional arguments of a program
1025 that the example program should attempt to execute using
1027 (a library function that employs the
1030 Some example runs of the program are shown below.
1032 First, we display the architecture that we are running on (x86-64)
1033 and then construct a shell function that looks up system call
1034 numbers on this architecture:
1041 cat /usr/src/linux/arch/x86/syscalls/syscall_64.tbl | \e
1042 awk \[aq]$2 != "x32" && $3 == "\[aq]$1\[aq]" { print $1 }\[aq]
1047 When the BPF filter rejects a system call (case [2] above),
1048 it causes the system call to fail with the error number
1049 specified on the command line.
1050 In the experiments shown here, we'll use error number 99:
1055 EADDRNOTAVAIL 99 Cannot assign requested address
1059 In the following example, we attempt to run the command
1061 but the BPF filter rejects the
1063 system call, so that the command is not even executed:
1067 $ \fBsyscall_nr execve\fP
1070 Usage: ./a.out <syscall_nr> <arch> <errno> <prog> [<args>]
1071 Hint for <arch>: AUDIT_ARCH_I386: 0x40000003
1072 AUDIT_ARCH_X86_64: 0xC000003E
1073 $ \fB./a.out 59 0xC000003E 99 /bin/whoami\fP
1074 execv: Cannot assign requested address
1078 In the next example, the BPF filter rejects the
1080 system call, so that, although it is successfully started, the
1082 command is not able to write output:
1086 $ \fBsyscall_nr write\fP
1088 $ \fB./a.out 1 0xC000003E 99 /bin/whoami\fP
1092 In the final example,
1093 the BPF filter rejects a system call that is not used by the
1095 command, so it is able to successfully execute and produce output:
1099 $ \fBsyscall_nr preadv\fP
1101 $ \fB./a.out 295 0xC000003E 99 /bin/whoami\fP
1106 .\" SRC BEGIN (seccomp.c)
1108 #include <linux/audit.h>
1109 #include <linux/filter.h>
1110 #include <linux/seccomp.h>
1114 #include <sys/prctl.h>
1115 #include <sys/syscall.h>
1118 #define X32_SYSCALL_BIT 0x40000000
1119 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
1122 install_filter(int syscall_nr, unsigned int t_arch, int f_errno)
1124 unsigned int upper_nr_limit = 0xffffffff;
1126 /* Assume that AUDIT_ARCH_X86_64 means the normal x86\-64 ABI
1127 (in the x32 ABI, all system calls have bit 30 set in the
1128 \[aq]nr\[aq] field, meaning the numbers are >= X32_SYSCALL_BIT). */
1129 if (t_arch == AUDIT_ARCH_X86_64)
1130 upper_nr_limit = X32_SYSCALL_BIT \- 1;
1132 struct sock_filter filter[] = {
1133 /* [0] Load architecture from \[aq]seccomp_data\[aq] buffer into
1135 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1136 (offsetof(struct seccomp_data, arch))),
1138 /* [1] Jump forward 5 instructions if architecture does not
1139 match \[aq]t_arch\[aq]. */
1140 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, t_arch, 0, 5),
1142 /* [2] Load system call number from \[aq]seccomp_data\[aq] buffer into
1144 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1145 (offsetof(struct seccomp_data, nr))),
1147 /* [3] Check ABI \- only needed for x86\-64 in deny\-list use
1148 cases. Use BPF_JGT instead of checking against the bit
1149 mask to avoid having to reload the syscall number. */
1150 BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, upper_nr_limit, 3, 0),
1152 /* [4] Jump forward 1 instruction if system call number
1153 does not match \[aq]syscall_nr\[aq]. */
1154 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, syscall_nr, 0, 1),
1156 /* [5] Matching architecture and system call: don\[aq]t execute
1157 the system call, and return \[aq]f_errno\[aq] in \[aq]errno\[aq]. */
1158 BPF_STMT(BPF_RET | BPF_K,
1159 SECCOMP_RET_ERRNO | (f_errno & SECCOMP_RET_DATA)),
1161 /* [6] Destination of system call number mismatch: allow other
1163 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
1165 /* [7] Destination of architecture mismatch: kill process. */
1166 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
1169 struct sock_fprog prog = {
1170 .len = ARRAY_SIZE(filter),
1174 if (syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog)) {
1183 main(int argc, char *argv[])
1186 fprintf(stderr, "Usage: "
1187 "%s <syscall_nr> <arch> <errno> <prog> [<args>]\en"
1188 "Hint for <arch>: AUDIT_ARCH_I386: 0x%X\en"
1189 " AUDIT_ARCH_X86_64: 0x%X\en"
1190 "\en", argv[0], AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
1194 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1199 if (install_filter(strtol(argv[1], NULL, 0),
1200 strtoul(argv[2], NULL, 0),
1201 strtol(argv[3], NULL, 0)))
1204 execv(argv[4], &argv[4]);
1216 .BR seccomp_unotify (2),
1222 Various pages from the
1225 .BR scmp_sys_resolver (1),
1226 .BR seccomp_export_bpf (3),
1227 .BR seccomp_init (3),
1228 .BR seccomp_load (3),
1230 .BR seccomp_rule_add (3).
1232 The kernel source files
1233 .I Documentation/networking/filter.txt
1235 .I Documentation/userspace\-api/seccomp_filter.rst
1236 .\" commit c061f33f35be0ccc80f4b8e0aea5dfd2ed7e01a3
1238 .I Documentation/prctl/seccomp_filter.txt
1241 McCanne, S.\& and Jacobson, V.\& (1992)
1242 .IR "The BSD Packet Filter: A New Architecture for User-level Packet Capture" ,
1243 Proceedings of the USENIX Winter 1993 Conference
1244 .UR http://www.tcpdump.org/papers/bpf\-usenix93.pdf