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