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>
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .TH SECCOMP 2 2019-11-19 "Linux" "Linux Programmer's Manual"
30 seccomp \- operate on Secure Computing state of the process
33 .B #include <linux/seccomp.h>
34 .B #include <linux/filter.h>
35 .B #include <linux/audit.h>
36 .B #include <linux/signal.h>
37 .B #include <sys/ptrace.h>
38 .\" Kees Cook noted: Anything that uses SECCOMP_RET_TRACE returns will
39 .\" need <sys/ptrace.h>
41 .BI "int seccomp(unsigned int " operation ", unsigned int " flags \
47 system call operates on the Secure Computing (seccomp) state of the
50 Currently, Linux supports the following
54 .BR SECCOMP_SET_MODE_STRICT
55 The only system calls that the calling thread is permitted to make are
63 Other system calls result in the delivery of a
66 Strict secure computing mode is useful for number-crunching
67 applications that may need to execute untrusted byte code, perhaps
68 obtained by reading from a pipe or socket.
70 Note that although the calling thread can no longer call
74 to block all signals apart from
80 (for example) is not sufficient for restricting the process's execution time.
81 Instead, to reliably terminate the process,
84 This can be done by using
94 to set the hard limit for
97 This operation is available only if the kernel is configured with
107 This operation is functionally identical to the call:
111 prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
115 .BR SECCOMP_SET_MODE_FILTER
116 The system calls allowed are defined by a pointer to a Berkeley Packet
117 Filter (BPF) passed via
119 This argument is a pointer to a
120 .IR "struct\ sock_fprog" ;
121 it can be designed to filter arbitrary system calls and system call
123 If the filter is invalid,
134 is allowed by the filter, any child processes will be constrained to
135 the same system call filters as the parent.
139 the existing filters will be preserved across a call to
143 .BR SECCOMP_SET_MODE_FILTER
144 operation, either the calling thread must have the
146 capability in its user namespace, or the thread must already have the
149 If that bit was not already set by an ancestor of this thread,
150 the thread must make the following call:
154 prctl(PR_SET_NO_NEW_PRIVS, 1);
159 .BR SECCOMP_SET_MODE_FILTER
160 operation fails and returns
164 This requirement ensures that an unprivileged process cannot apply
165 a malicious filter and then invoke a set-user-ID or
166 other privileged program using
168 thus potentially compromising that program.
169 (Such a malicious filter might, for example, cause an attempt to use
171 to set the caller's user IDs to nonzero values to instead
172 return 0 without actually making the system call.
173 Thus, the program might be tricked into retaining superuser privileges
174 in circumstances where it is possible to influence it to do
175 dangerous things because it did not actually drop privileges.)
181 is allowed by the attached filter, further filters may be added.
182 This will increase evaluation time, but allows for further reduction of
183 the attack surface during execution of a thread.
186 .BR SECCOMP_SET_MODE_FILTER
187 operation is available only if the kernel is configured with
188 .BR CONFIG_SECCOMP_FILTER
193 is 0, this operation is functionally identical to the call:
197 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, args);
206 .BR SECCOMP_FILTER_FLAG_TSYNC
207 When adding a new filter, synchronize all other threads of the calling
208 process to the same seccomp filter tree.
209 A "filter tree" is the ordered list of filters attached to a thread.
210 (Attaching identical filters in separate
212 calls results in different filters from this perspective.)
214 If any thread cannot synchronize to the same filter tree,
215 the call will not attach the new seccomp filter,
216 and will fail, returning the first thread ID found that cannot synchronize.
217 Synchronization will fail if another thread in the same process is in
218 .BR SECCOMP_MODE_STRICT
219 or if it has attached new seccomp filters to itself,
220 diverging from the calling thread's filter tree.
222 .BR SECCOMP_FILTER_FLAG_LOG " (since Linux 4.14)"
223 .\" commit e66a39977985b1e69e17c4042cb290768eca9b02
224 All filter return actions except
225 .BR SECCOMP_RET_ALLOW
227 An administrator may override this filter flag by preventing specific
228 actions from being logged via the
229 .IR /proc/sys/kernel/seccomp/actions_logged
232 .BR SECCOMP_FILTER_FLAG_SPEC_ALLOW " (since Linux 4.17)"
233 .\" commit 00a02d0c502a06d15e07b857f8ff921e3e402675
234 Disable Speculative Store Bypass mitigation.
237 .BR SECCOMP_GET_ACTION_AVAIL " (since Linux 4.14)"
238 .\" commit d612b1fd8010d0d67b5287fe146b8b55bcbb8655
239 Test to see if an action is supported by the kernel.
240 This operation is helpful to confirm that the kernel knows
241 of a more recently added filter return action
242 since the kernel treats all unknown actions as
243 .BR SECCOMP_RET_KILL_PROCESS .
249 must be a pointer to an unsigned 32-bit filter return action.
251 When adding filters via
252 .BR SECCOMP_SET_MODE_FILTER ,
254 points to a filter program:
259 unsigned short len; /* Number of BPF instructions */
260 struct sock_filter *filter; /* Pointer to array of
266 Each program must contain one or more BPF instructions:
270 struct sock_filter { /* Filter block */
271 __u16 code; /* Actual filter code */
272 __u8 jt; /* Jump true */
273 __u8 jf; /* Jump false */
274 __u32 k; /* Generic multiuse field */
279 When executing the instructions, the BPF program operates on the
280 system call information made available (i.e., use the
282 addressing mode) as a (read-only)
283 .\" Quoting Kees Cook:
284 .\" If BPF even allows changing the data, it's not copied back to
285 .\" the syscall when it runs. Anything wanting to do things like
286 .\" that would need to use ptrace to catch the call and directly
287 .\" modify the registers before continuing with the call.
288 buffer of the following form:
292 struct seccomp_data {
293 int nr; /* System call number */
294 __u32 arch; /* AUDIT_ARCH_* value
295 (see <linux/audit.h>) */
296 __u64 instruction_pointer; /* CPU instruction pointer */
297 __u64 args[6]; /* Up to 6 system call arguments */
302 Because numbering of system calls varies between architectures and
303 some architectures (e.g., x86-64) allow user-space code to use
304 the calling conventions of multiple architectures
305 (and the convention being used may vary over the life of a process that uses
307 to execute binaries that employ the different conventions),
308 it is usually necessary to verify the value of the
312 It is strongly recommended to use an allow-list approach whenever
313 possible because such an approach is more robust and simple.
314 A deny-list will have to be updated whenever a potentially
315 dangerous system call is added (or a dangerous flag or option if those
316 are deny-listed), and it is often possible to alter the
317 representation of a value without altering its meaning, leading to
325 field is not unique for all calling conventions.
326 The x86-64 ABI and the x32 ABI both use
327 .BR AUDIT_ARCH_X86_64
330 and they run on the same processors.
332 .BR __X32_SYSCALL_BIT
333 is used on the system call number to tell the two ABIs apart.
334 .\" As noted by Dave Drysdale in a note at the end of
335 .\" https://lwn.net/Articles/604515/
336 .\" One additional detail to point out for the x32 ABI case:
337 .\" the syscall number gets a high bit set (__X32_SYSCALL_BIT),
338 .\" to mark it as an x32 call.
340 .\" If x32 support is included in the kernel, then __SYSCALL_MASK
341 .\" will have a value that is not all-ones, and this will trigger
342 .\" an extra instruction in system_call to mask off the extra bit,
343 .\" so that the syscall table indexing still works.
345 This means that in order to create a seccomp-based
346 deny-list for system calls performed through the x86-64 ABI,
347 it is necessary to not only check that
350 .BR AUDIT_ARCH_X86_64 ,
351 but also to explicitly reject all system calls that contain
352 .BR __X32_SYSCALL_BIT
357 .I instruction_pointer
358 field provides the address of the machine-language instruction that
359 performed the system call.
360 This might be useful in conjunction with the use of
362 to perform checks based on which region (mapping) of the program
363 made the system call.
364 (Probably, it is wise to lock down the
368 system calls to prevent the program from subverting such checks.)
370 When checking values from
372 against a deny-list, keep in mind that arguments are often
373 silently truncated before being processed, but after the seccomp check.
374 For example, this happens if the i386 ABI is used on an
375 x86-64 kernel: although the kernel will normally not look beyond
376 the 32 lowest bits of the arguments, the values of the full
377 64-bit registers will be present in the seccomp data.
378 A less surprising example is that if the x86-64 ABI is used to perform
379 a system call that takes an argument of type
381 the more-significant half of the argument register is ignored by
382 the system call, but visible in the seccomp data.
384 A seccomp filter returns a 32-bit value consisting of two parts:
385 the most significant 16 bits
386 (corresponding to the mask defined by the constant
387 .BR SECCOMP_RET_ACTION_FULL )
388 contain one of the "action" values listed below;
389 the least significant 16-bits (defined by the constant
390 .BR SECCOMP_RET_DATA )
391 are "data" to be associated with this return value.
393 If multiple filters exist, they are \fIall\fP executed,
394 in reverse order of their addition to the filter tree\(emthat is,
395 the most recently installed filter is executed first.
396 (Note that all filters will be called
397 even if one of the earlier filters returns
398 .BR SECCOMP_RET_KILL .
399 This is done to simplify the kernel code and to provide a
400 tiny speed-up in the execution of sets of filters by
401 avoiding a check for this uncommon case.)
402 .\" From an Aug 2015 conversation with Kees Cook where I asked why *all*
403 .\" filters are applied even if one of the early filters returns
404 .\" SECCOMP_RET_KILL:
406 .\" It's just because it would be an optimization that would only speed up
407 .\" the RET_KILL case, but it's the uncommon one and the one that doesn't
408 .\" benefit meaningfully from such a change (you need to kill the process
409 .\" really quickly?). We would speed up killing a program at the (albeit
410 .\" tiny) expense to all other filtered programs. Best to keep the filter
411 .\" execution logic clear, simple, and as fast as possible for all
413 The return value for the evaluation of a given system call is the first-seen
414 action value of highest precedence (along with its accompanying data)
415 returned by execution of all of the filters.
417 In decreasing order of precedence,
418 the action values that may be returned by a seccomp filter are:
420 .BR SECCOMP_RET_KILL_PROCESS " (since Linux 4.14)"
421 .\" commit 4d3b0b05aae9ee9ce0970dc4cc0fb3fad5e85945
422 .\" commit 0466bdb99e8744bc9befa8d62a317f0fd7fd7421
423 This value results in immediate termination of the process,
425 The system call is not executed.
427 .BR SECCOMP_RET_KILL_THREAD
428 below, all threads in the thread group are terminated.
429 (For a discussion of thread groups, see the description of the
434 The process terminates
439 Even if a signal handler has been registered for
441 the handler will be ignored in this case and the process always terminates.
442 To a parent process that is waiting on this process (using
444 or similar), the returned
446 will indicate that its child was terminated as though by a
450 .BR SECCOMP_RET_KILL_THREAD " (or " SECCOMP_RET_KILL )
451 This value results in immediate termination of the thread
452 that made the system call.
453 The system call is not executed.
454 Other threads in the same thread group will continue to execute.
456 The thread terminates
462 .BR SECCOMP_RET_KILL_PROCESS
465 .\" See these commits:
466 .\" seccomp: dump core when using SECCOMP_RET_KILL
467 .\" (b25e67161c295c98acda92123b2dd1e7d8642901)
468 .\" seccomp: Only dump core when single-threaded
469 .\" (d7276e321ff8a53106a59c85ca46d03e34288893)
471 any process terminated in this way would not trigger a coredump
476 as having a default action of termination with a core dump).
478 a single-threaded process will dump core if terminated in this way.
481 .BR SECCOMP_RET_KILL_PROCESS
483 .BR SECCOMP_RET_KILL_THREAD
484 was added as a synonym for
485 .BR SECCOMP_RET_KILL ,
486 in order to more clearly distinguish the two actions.
489 This value results in the kernel sending a thread-directed
491 signal to the triggering thread.
492 (The system call is not executed.)
493 Various fields will be set in the
497 associated with signal:
505 will show the address of the system call instruction.
510 will indicate which system call was attempted.
519 portion of the filter return value.
522 The program counter will be as though the system call happened
523 (i.e., the program counter will not point to the system call instruction).
524 The return value register will contain an architecture\-dependent value;
525 if resuming execution, set it to something appropriate for the system call.
526 (The architecture dependency is because replacing it with
528 could overwrite some useful information.)
530 .BR SECCOMP_RET_ERRNO
531 This value results in the
533 portion of the filter's return value being passed to user space as the
535 value without executing the system call.
537 .BR SECCOMP_RET_TRACE
538 When returned, this value will cause the kernel to attempt to notify a
540 tracer prior to executing the system call.
541 If there is no tracer present,
542 the system call is not executed and returns a failure status with
547 A tracer will be notified if it requests
548 .BR PTRACE_O_TRACESECCOMP
550 .IR ptrace(PTRACE_SETOPTIONS) .
551 The tracer will be notified of a
552 .BR PTRACE_EVENT_SECCOMP
555 portion of the filter's return value will be available to the tracer via
556 .BR PTRACE_GETEVENTMSG .
558 The tracer can skip the system call by changing the system call number
560 Alternatively, the tracer can change the system call
561 requested by changing the system call to a valid system call number.
562 If the tracer asks to skip the system call, then the system call will
563 appear to return the value that the tracer puts in the return value register.
565 .\" This was changed in ce6526e8afa4.
566 .\" A related hole, using PTRACE_SYSCALL instead of SECCOMP_RET_TRACE, was
567 .\" changed in arch-specific commits, e.g. 93e35efb8de4 for X86 and
568 .\" 0f3912fd934c for ARM.
569 Before kernel 4.8, the seccomp check will not be run again after the tracer is
571 (This means that, on older kernels, seccomp-based sandboxes
574 .BR ptrace (2)\(emeven
576 sandboxed processes\(emwithout extreme care;
577 ptracers can use this mechanism to escape from the seccomp sandbox.)
579 .BR SECCOMP_RET_LOG " (since Linux 4.14)"
580 .\" commit 59f5cf44a38284eb9e76270c786fb6cc62ef8ac4
581 This value results in the system call being executed after
582 the filter return action is logged.
583 An administrator may override the logging of this action via
585 .IR /proc/sys/kernel/seccomp/actions_logged
588 .BR SECCOMP_RET_ALLOW
589 This value results in the system call being executed.
591 If an action value other than one of the above is specified,
592 then the filter action is treated as either
593 .BR SECCOMP_RET_KILL_PROCESS
595 .\" commit 4d3b0b05aae9ee9ce0970dc4cc0fb3fad5e85945
597 .BR SECCOMP_RET_KILL_THREAD
598 (in Linux 4.13 and earlier).
601 The files in the directory
602 .IR /proc/sys/kernel/seccomp
603 provide additional seccomp information and configuration:
605 .IR actions_avail " (since Linux 4.14)"
606 .\" commit 8e5f1ad116df6b0de65eac458d5e7c318d1c05af
607 A read-only ordered list of seccomp filter return actions in string form.
608 The ordering, from left-to-right, is in decreasing order of precedence.
609 The list represents the set of seccomp filter return actions
610 supported by the kernel.
612 .IR actions_logged " (since Linux 4.14)"
613 .\" commit 0ddec0fc8900201c0897b87b762b7c420436662f
614 A read-write ordered list of seccomp filter return actions that
615 are allowed to be logged.
616 Writes to the file do not need to be in ordered form but reads from
617 the file will be ordered in the same way as the
621 It is important to note that the value of
623 does not prevent certain filter return actions from being logged when
624 the audit subsystem is configured to audit a task.
625 If the action is not found in the
627 file, the final decision on whether to audit the action for that task is
628 ultimately left up to the audit subsystem to decide for all filter return
630 .BR SECCOMP_RET_ALLOW .
632 The "allow" string is not accepted in the
634 file as it is not possible to log
635 .BR SECCOMP_RET_ALLOW
637 Attempting to write "allow" to the file will fail with the error
640 .SS Audit logging of seccomp actions
641 .\" commit 59f5cf44a38284eb9e76270c786fb6cc62ef8ac4
642 Since Linux 4.14, the kernel provides the facility to log the
643 actions returned by seccomp filters in the audit log.
644 The kernel makes the decision to log an action based on
645 the action type, whether or not the action is present in the
647 file, and whether kernel auditing is enabled
648 (e.g., via the kernel boot option
650 .\" or auditing could be enabled via the netlink API (AUDIT_SET)
651 The rules are as follows:
654 .BR SECCOMP_RET_ALLOW ,
655 the action is not logged.
657 Otherwise, if the action is either
658 .BR SECCOMP_RET_KILL_PROCESS
660 .BR SECCOMP_RET_KILL_THREAD ,
661 and that action appears in the
663 file, the action is logged.
665 Otherwise, if the filter has requested logging (the
666 .BR SECCOMP_FILTER_FLAG_LOG
668 and the action appears in the
670 file, the action is logged.
672 Otherwise, if kernel auditing is enabled and the process is being audited
674 the action is logged.
676 Otherwise, the action is not logged.
682 .BR SECCOMP_FILTER_FLAG_TSYNC
684 the return value is the ID of the thread
685 that caused the synchronization failure.
686 (This ID is a kernel thread ID of the type returned by
690 On other errors, \-1 is returned, and
692 is set to indicate the cause of the error.
695 can fail for the following reasons:
698 The caller did not have the
700 capability in its user namespace, or had not set
703 .BR SECCOMP_SET_MODE_FILTER .
707 was not a valid address.
711 is unknown or is not supported by this kernel version or configuration.
716 are invalid for the given
723 but the specified offset was not aligned to a 32-bit boundary or exceeded
724 .IR "sizeof(struct\ seccomp_data)" .
727 .\" See kernel/seccomp.c::seccomp_may_assign_mode() in 3.18 sources
728 A secure computing mode has already been set, and
730 differs from the existing setting.
735 .BR SECCOMP_SET_MODE_FILTER ,
736 but the filter program pointed to by
738 was not valid or the length of the filter program was zero or exceeded
746 .\" ENOMEM in kernel/seccomp.c::seccomp_attach_filter() in 3.18 sources
747 The total length of all filter programs attached
748 to the calling thread would exceed
749 .B MAX_INSNS_PER_PATH
750 (32768) instructions.
751 Note that for the purposes of calculating this limit,
752 each already existing filter program incurs an
753 overhead penalty of 4 instructions.
758 .BR SECCOMP_GET_ACTION_AVAIL ,
759 but the kernel does not support the filter return action specified by
763 Another thread caused a failure during thread sync, but its ID could not
768 system call first appeared in Linux 3.17.
769 .\" FIXME . Add glibc version
773 system call is a nonstandard Linux extension.
775 Rather than hand-coding seccomp filters as shown in the example below,
776 you may prefer to employ the
778 library, which provides a front-end for generating seccomp filters.
783 .IR /proc/[pid]/status
784 file provides a method of viewing the seccomp mode of a process; see
788 provides a superset of the functionality provided by the
791 operation (which does not support
796 .B PTRACE_SECCOMP_GET_FILTER
797 operation can be used to dump a process's seccomp filters.
799 .SS Architecture support for seccomp BPF
800 Architecture support for seccomp BPF filtering
801 .\" Check by grepping for HAVE_ARCH_SECCOMP_FILTER in Kconfig files in
802 .\" kernel source. Last checked in Linux 4.16-rc source.
803 is available on the following architectures:
805 x86-64, i386, x32 (since Linux 3.5)
808 ARM (since Linux 3.8)
810 s390 (since Linux 3.8)
812 MIPS (since Linux 3.16)
814 ARM-64 (since Linux 3.19)
816 PowerPC (since Linux 4.3)
818 Tile (since Linux 4.3)
820 PA-RISC (since Linux 4.6)
821 .\" User mode Linux since Linux 4.6
825 There are various subtleties to consider when applying seccomp filters
826 to a program, including the following:
828 Some traditional system calls have user-space implementations in the
830 on many architectures.
831 Notable examples include
832 .BR clock_gettime (2),
833 .BR gettimeofday (2),
836 On such architectures,
837 seccomp filtering for these system calls will have no effect.
838 (However, there are cases where the
840 implementations may fall back to invoking the true system call,
841 in which case seccomp filters would see the system call.)
843 Seccomp filtering is based on system call numbers.
844 However, applications typically do not directly invoke system calls,
845 but instead call wrapper functions in the C library which
846 in turn invoke the system calls.
847 Consequently, one must be aware of the following:
850 The glibc wrappers for some traditional system calls may actually
851 employ system calls with different names in the kernel.
854 wrapper function actually employs the
858 wrapper function actually calls
861 The behavior of wrapper functions may vary across architectures,
862 according to the range of system calls provided on those architectures.
863 In other words, the same wrapper function may invoke
864 different system calls on different architectures.
866 Finally, the behavior of wrapper functions can change across glibc versions.
867 For example, in older versions, the glibc wrapper function for
869 invoked the system call of the same name,
870 but starting in glibc 2.26, the implementation switched to calling
872 on all architectures.
875 The consequence of the above points is that it may be necessary
876 to filter for a system call other than might be expected.
877 Various manual pages in Section 2 provide helpful details
878 about the differences between wrapper functions and
879 the underlying system calls in subsections entitled
880 .IR "C library/kernel differences" .
882 Furthermore, note that the application of seccomp filters
883 even risks causing bugs in an application,
884 when the filters cause unexpected failures for legitimate operations
885 that the application might need to perform.
886 Such bugs may not easily be discovered when testing the seccomp
887 filters if the bugs occur in rarely used application code paths.
889 .SS Seccomp-specific BPF details
890 Note the following BPF details specific to seccomp filters:
896 size modifiers are not supported: all operations must load and store
900 To access the contents of the
904 addressing mode modifier.
908 addressing mode modifier yields an immediate mode operand
909 whose value is the size of the
913 The program below accepts four or more arguments.
914 The first three arguments are a system call number,
915 a numeric architecture identifier, and an error number.
916 The program uses these values to construct a BPF filter
917 that is used at run time to perform the following checks:
919 If the program is not running on the specified architecture,
920 the BPF filter causes system calls to fail with the error
923 If the program attempts to execute the system call with the specified number,
924 the BPF filter causes the system call to fail, with
926 being set to the specified error number.
928 The remaining command-line arguments specify
929 the pathname and additional arguments of a program
930 that the example program should attempt to execute using
932 (a library function that employs the
935 Some example runs of the program are shown below.
937 First, we display the architecture that we are running on (x86-64)
938 and then construct a shell function that looks up system call
939 numbers on this architecture:
946 cat /usr/src/linux/arch/x86/syscalls/syscall_64.tbl | \e
947 awk '$2 != "x32" && $3 == "'$1'" { print $1 }'
952 When the BPF filter rejects a system call (case [2] above),
953 it causes the system call to fail with the error number
954 specified on the command line.
955 In the experiments shown here, we'll use error number 99:
960 EADDRNOTAVAIL 99 Cannot assign requested address
964 In the following example, we attempt to run the command
966 but the BPF filter rejects the
968 system call, so that the command is not even executed:
972 $ \fBsyscall_nr execve\fP
975 Usage: ./a.out <syscall_nr> <arch> <errno> <prog> [<args>]
976 Hint for <arch>: AUDIT_ARCH_I386: 0x40000003
977 AUDIT_ARCH_X86_64: 0xC000003E
978 $ \fB./a.out 59 0xC000003E 99 /bin/whoami\fP
979 execv: Cannot assign requested address
983 In the next example, the BPF filter rejects the
985 system call, so that, although it is successfully started, the
987 command is not able to write output:
991 $ \fBsyscall_nr write\fP
993 $ \fB./a.out 1 0xC000003E 99 /bin/whoami\fP
997 In the final example,
998 the BPF filter rejects a system call that is not used by the
1000 command, so it is able to successfully execute and produce output:
1004 $ \fBsyscall_nr preadv\fP
1006 $ \fB./a.out 295 0xC000003E 99 /bin/whoami\fP
1017 #include <linux/audit.h>
1018 #include <linux/filter.h>
1019 #include <linux/seccomp.h>
1020 #include <sys/prctl.h>
1022 #define X32_SYSCALL_BIT 0x40000000
1025 install_filter(int syscall_nr, int t_arch, int f_errno)
1027 unsigned int upper_nr_limit = 0xffffffff;
1029 /* Assume that AUDIT_ARCH_X86_64 means the normal x86-64 ABI
1030 (in the x32 ABI, all system calls have bit 30 set in the
1031 'nr' field, meaning the numbers are >= X32_SYSCALL_BIT) */
1032 if (t_arch == AUDIT_ARCH_X86_64)
1033 upper_nr_limit = X32_SYSCALL_BIT - 1;
1035 struct sock_filter filter[] = {
1036 /* [0] Load architecture from 'seccomp_data' buffer into
1038 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1039 (offsetof(struct seccomp_data, arch))),
1041 /* [1] Jump forward 5 instructions if architecture does not
1043 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, t_arch, 0, 5),
1045 /* [2] Load system call number from 'seccomp_data' buffer into
1047 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1048 (offsetof(struct seccomp_data, nr))),
1050 /* [3] Check ABI - only needed for x86-64 in deny-list use
1051 cases. Use BPF_JGT instead of checking against the bit
1052 mask to avoid having to reload the syscall number. */
1053 BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, upper_nr_limit, 3, 0),
1055 /* [4] Jump forward 1 instruction if system call number
1056 does not match 'syscall_nr' */
1057 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, syscall_nr, 0, 1),
1059 /* [5] Matching architecture and system call: don't execute
1060 the system call, and return 'f_errno' in 'errno' */
1061 BPF_STMT(BPF_RET | BPF_K,
1062 SECCOMP_RET_ERRNO | (f_errno & SECCOMP_RET_DATA)),
1064 /* [6] Destination of system call number mismatch: allow other
1066 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
1068 /* [7] Destination of architecture mismatch: kill task */
1069 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL),
1072 struct sock_fprog prog = {
1073 .len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
1077 if (seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog)) {
1086 main(int argc, char **argv)
1089 fprintf(stderr, "Usage: "
1090 "%s <syscall_nr> <arch> <errno> <prog> [<args>]\en"
1091 "Hint for <arch>: AUDIT_ARCH_I386: 0x%X\en"
1092 " AUDIT_ARCH_X86_64: 0x%X\en"
1093 "\en", argv[0], AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
1097 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1102 if (install_filter(strtol(argv[1], NULL, 0),
1103 strtol(argv[2], NULL, 0),
1104 strtol(argv[3], NULL, 0)))
1107 execv(argv[4], &argv[4]);
1123 Various pages from the
1126 .BR scmp_sys_resolver (1),
1127 .BR seccomp_export_bpf (3),
1128 .BR seccomp_init (3),
1129 .BR seccomp_load (3),
1131 .BR seccomp_rule_add (3).
1133 The kernel source files
1134 .IR Documentation/networking/filter.txt
1136 .IR Documentation/userspace\-api/seccomp_filter.rst
1137 .\" commit c061f33f35be0ccc80f4b8e0aea5dfd2ed7e01a3
1139 .IR Documentation/prctl/seccomp_filter.txt
1142 McCanne, S.\& and Jacobson, V.\& (1992)
1143 .IR "The BSD Packet Filter: A New Architecture for User-level Packet Capture" ,
1144 Proceedings of the USENIX Winter 1993 Conference
1145 .UR http://www.tcpdump.org/papers/bpf\-usenix93.pdf