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