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