]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/capabilities.7
pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2...
[thirdparty/man-pages.git] / man7 / capabilities.7
1 .\" Copyright (c) 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" 6 Aug 2002 - Initial Creation
26 .\" Modified 2003-05-23, Michael Kerrisk, <mtk.manpages@gmail.com>
27 .\" Modified 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com>
28 .\" 2004-12-08, mtk Added O_NOATIME for CAP_FOWNER
29 .\" 2005-08-16, mtk, Added CAP_AUDIT_CONTROL and CAP_AUDIT_WRITE
30 .\" 2008-07-15, Serge Hallyn <serue@us.bbm.com>
31 .\" Document file capabilities, per-process capability
32 .\" bounding set, changed semantics for CAP_SETPCAP,
33 .\" and other changes in 2.6.2[45].
34 .\" Add CAP_MAC_ADMIN, CAP_MAC_OVERRIDE, CAP_SETFCAP.
35 .\" 2008-07-15, mtk
36 .\" Add text describing circumstances in which CAP_SETPCAP
37 .\" (theoretically) permits a thread to change the
38 .\" capability sets of another thread.
39 .\" Add section describing rules for programmatically
40 .\" adjusting thread capability sets.
41 .\" Describe rationale for capability bounding set.
42 .\" Document "securebits" flags.
43 .\" Add text noting that if we set the effective flag for one file
44 .\" capability, then we must also set the effective flag for all
45 .\" other capabilities where the permitted or inheritable bit is set.
46 .\" 2011-09-07, mtk/Serge hallyn: Add CAP_SYSLOG
47 .\"
48 .TH CAPABILITIES 7 2019-08-02 "Linux" "Linux Programmer's Manual"
49 .SH NAME
50 capabilities \- overview of Linux capabilities
51 .SH DESCRIPTION
52 For the purpose of performing permission checks,
53 traditional UNIX implementations distinguish two categories of processes:
54 .I privileged
55 processes (whose effective user ID is 0, referred to as superuser or root),
56 and
57 .I unprivileged
58 processes (whose effective UID is nonzero).
59 Privileged processes bypass all kernel permission checks,
60 while unprivileged processes are subject to full permission
61 checking based on the process's credentials
62 (usually: effective UID, effective GID, and supplementary group list).
63 .PP
64 Starting with kernel 2.2, Linux divides the privileges traditionally
65 associated with superuser into distinct units, known as
66 .IR capabilities ,
67 which can be independently enabled and disabled.
68 Capabilities are a per-thread attribute.
69 .\"
70 .SS Capabilities list
71 The following list shows the capabilities implemented on Linux,
72 and the operations or behaviors that each capability permits:
73 .TP
74 .BR CAP_AUDIT_CONTROL " (since Linux 2.6.11)"
75 Enable and disable kernel auditing; change auditing filter rules;
76 retrieve auditing status and filtering rules.
77 .TP
78 .BR CAP_AUDIT_READ " (since Linux 3.16)"
79 .\" commit a29b694aa1739f9d76538e34ae25524f9c549d59
80 .\" commit 3a101b8de0d39403b2c7e5c23fd0b005668acf48
81 Allow reading the audit log via a multicast netlink socket.
82 .TP
83 .BR CAP_AUDIT_WRITE " (since Linux 2.6.11)"
84 Write records to kernel auditing log.
85 .\" FIXME Add FAN_ENABLE_AUDIT
86 .TP
87 .BR CAP_BLOCK_SUSPEND " (since Linux 3.5)"
88 Employ features that can block system suspend
89 .RB ( epoll (7)
90 .BR EPOLLWAKEUP ,
91 .IR /proc/sys/wake_lock ).
92 .TP
93 .B CAP_CHOWN
94 Make arbitrary changes to file UIDs and GIDs (see
95 .BR chown (2)).
96 .TP
97 .B CAP_DAC_OVERRIDE
98 Bypass file read, write, and execute permission checks.
99 (DAC is an abbreviation of "discretionary access control".)
100 .TP
101 .B CAP_DAC_READ_SEARCH
102 .PD 0
103 .RS
104 .IP * 2
105 Bypass file read permission checks and
106 directory read and execute permission checks;
107 .IP *
108 invoke
109 .BR open_by_handle_at (2);
110 .IP *
111 use the
112 .BR linkat (2)
113 .B AT_EMPTY_PATH
114 flag to create a link to a file referred to by a file descriptor.
115 .RE
116 .PD
117 .TP
118 .B CAP_FOWNER
119 .PD 0
120 .RS
121 .IP * 2
122 Bypass permission checks on operations that normally
123 require the filesystem UID of the process to match the UID of
124 the file (e.g.,
125 .BR chmod (2),
126 .BR utime (2)),
127 excluding those operations covered by
128 .B CAP_DAC_OVERRIDE
129 and
130 .BR CAP_DAC_READ_SEARCH ;
131 .IP *
132 set inode flags (see
133 .BR ioctl_iflags (2))
134 on arbitrary files;
135 .IP *
136 set Access Control Lists (ACLs) on arbitrary files;
137 .IP *
138 ignore directory sticky bit on file deletion;
139 .IP *
140 modify
141 .I user
142 extended attributes on sticky directory owned by any user;
143 .IP *
144 specify
145 .B O_NOATIME
146 for arbitrary files in
147 .BR open (2)
148 and
149 .BR fcntl (2).
150 .RE
151 .PD
152 .TP
153 .B CAP_FSETID
154 .PD 0
155 .RS
156 .IP * 2
157 Don't clear set-user-ID and set-group-ID mode
158 bits when a file is modified;
159 .IP *
160 set the set-group-ID bit for a file whose GID does not match
161 the filesystem or any of the supplementary GIDs of the calling process.
162 .RE
163 .PD
164 .TP
165 .B CAP_IPC_LOCK
166 .\" FIXME . As at Linux 3.2, there are some strange uses of this capability
167 .\" in other places; they probably should be replaced with something else.
168 Lock memory
169 .RB ( mlock (2),
170 .BR mlockall (2),
171 .BR mmap (2),
172 .BR shmctl (2)).
173 .TP
174 .B CAP_IPC_OWNER
175 Bypass permission checks for operations on System V IPC objects.
176 .TP
177 .B CAP_KILL
178 Bypass permission checks for sending signals (see
179 .BR kill (2)).
180 This includes use of the
181 .BR ioctl (2)
182 .B KDSIGACCEPT
183 operation.
184 .\" FIXME . CAP_KILL also has an effect for threads + setting child
185 .\" termination signal to other than SIGCHLD: without this
186 .\" capability, the termination signal reverts to SIGCHLD
187 .\" if the child does an exec(). What is the rationale
188 .\" for this?
189 .TP
190 .BR CAP_LEASE " (since Linux 2.4)"
191 Establish leases on arbitrary files (see
192 .BR fcntl (2)).
193 .TP
194 .B CAP_LINUX_IMMUTABLE
195 Set the
196 .B FS_APPEND_FL
197 and
198 .B FS_IMMUTABLE_FL
199 inode flags (see
200 .BR ioctl_iflags (2)).
201 .TP
202 .BR CAP_MAC_ADMIN " (since Linux 2.6.25)"
203 Allow MAC configuration or state changes.
204 Implemented for the Smack Linux Security Module (LSM).
205 .TP
206 .BR CAP_MAC_OVERRIDE " (since Linux 2.6.25)"
207 Override Mandatory Access Control (MAC).
208 Implemented for the Smack LSM.
209 .TP
210 .BR CAP_MKNOD " (since Linux 2.4)"
211 Create special files using
212 .BR mknod (2).
213 .TP
214 .B CAP_NET_ADMIN
215 Perform various network-related operations:
216 .PD 0
217 .RS
218 .IP * 2
219 interface configuration;
220 .IP *
221 administration of IP firewall, masquerading, and accounting;
222 .IP *
223 modify routing tables;
224 .IP *
225 bind to any address for transparent proxying;
226 .IP *
227 set type-of-service (TOS)
228 .IP *
229 clear driver statistics;
230 .IP *
231 set promiscuous mode;
232 .IP *
233 enabling multicasting;
234 .IP *
235 use
236 .BR setsockopt (2)
237 to set the following socket options:
238 .BR SO_DEBUG ,
239 .BR SO_MARK ,
240 .BR SO_PRIORITY
241 (for a priority outside the range 0 to 6),
242 .BR SO_RCVBUFFORCE ,
243 and
244 .BR SO_SNDBUFFORCE .
245 .RE
246 .PD
247 .TP
248 .B CAP_NET_BIND_SERVICE
249 Bind a socket to Internet domain privileged ports
250 (port numbers less than 1024).
251 .TP
252 .B CAP_NET_BROADCAST
253 (Unused) Make socket broadcasts, and listen to multicasts.
254 .\" FIXME Since Linux 4.2, there are use cases for netlink sockets
255 .\" commit 59324cf35aba5336b611074028777838a963d03b
256 .TP
257 .B CAP_NET_RAW
258 .PD 0
259 .RS
260 .IP * 2
261 Use RAW and PACKET sockets;
262 .IP *
263 bind to any address for transparent proxying.
264 .RE
265 .PD
266 .\" Also various IP options and setsockopt(SO_BINDTODEVICE)
267 .TP
268 .B CAP_SETGID
269 .RS
270 .PD 0
271 .IP * 2
272 Make arbitrary manipulations of process GIDs and supplementary GID list;
273 .IP *
274 forge GID when passing socket credentials via UNIX domain sockets;
275 .IP *
276 write a group ID mapping in a user namespace (see
277 .BR user_namespaces (7)).
278 .PD
279 .RE
280 .TP
281 .BR CAP_SETFCAP " (since Linux 2.6.24)"
282 Set arbitrary capabilities on a file.
283 .TP
284 .B CAP_SETPCAP
285 If file capabilities are supported (i.e., since Linux 2.6.24):
286 add any capability from the calling thread's bounding set
287 to its inheritable set;
288 drop capabilities from the bounding set (via
289 .BR prctl (2)
290 .BR PR_CAPBSET_DROP );
291 make changes to the
292 .I securebits
293 flags.
294 .IP
295 If file capabilities are not supported (i.e., kernels before Linux 2.6.24):
296 grant or remove any capability in the
297 caller's permitted capability set to or from any other process.
298 (This property of
299 .B CAP_SETPCAP
300 is not available when the kernel is configured to support
301 file capabilities, since
302 .B CAP_SETPCAP
303 has entirely different semantics for such kernels.)
304 .TP
305 .B CAP_SETUID
306 .RS
307 .PD 0
308 .IP * 2
309 Make arbitrary manipulations of process UIDs
310 .RB ( setuid (2),
311 .BR setreuid (2),
312 .BR setresuid (2),
313 .BR setfsuid (2));
314 .IP *
315 forge UID when passing socket credentials via UNIX domain sockets;
316 .IP *
317 write a user ID mapping in a user namespace (see
318 .BR user_namespaces (7)).
319 .PD
320 .RE
321 .\" FIXME CAP_SETUID also an effect in exec(); document this.
322 .TP
323 .B CAP_SYS_ADMIN
324 .IR Note :
325 this capability is overloaded; see
326 .IR "Notes to kernel developers" ,
327 below.
328 .IP
329 .PD 0
330 .RS
331 .IP * 2
332 Perform a range of system administration operations including:
333 .BR quotactl (2),
334 .BR mount (2),
335 .BR umount (2),
336 .BR pivot_root (2),
337 .BR swapon (2),
338 .BR swapoff (2),
339 .BR sethostname (2),
340 and
341 .BR setdomainname (2);
342 .IP *
343 perform privileged
344 .BR syslog (2)
345 operations (since Linux 2.6.37,
346 .BR CAP_SYSLOG
347 should be used to permit such operations);
348 .IP *
349 perform
350 .B VM86_REQUEST_IRQ
351 .BR vm86 (2)
352 command;
353 .IP *
354 perform
355 .B IPC_SET
356 and
357 .B IPC_RMID
358 operations on arbitrary System V IPC objects;
359 .IP *
360 override
361 .B RLIMIT_NPROC
362 resource limit;
363 .IP *
364 perform operations on
365 .I trusted
366 and
367 .I security
368 Extended Attributes (see
369 .BR xattr (7));
370 .IP *
371 use
372 .BR lookup_dcookie (2);
373 .IP *
374 use
375 .BR ioprio_set (2)
376 to assign
377 .B IOPRIO_CLASS_RT
378 and (before Linux 2.6.25)
379 .B IOPRIO_CLASS_IDLE
380 I/O scheduling classes;
381 .IP *
382 forge PID when passing socket credentials via UNIX domain sockets;
383 .IP *
384 exceed
385 .IR /proc/sys/fs/file-max ,
386 the system-wide limit on the number of open files,
387 in system calls that open files (e.g.,
388 .BR accept (2),
389 .BR execve (2),
390 .BR open (2),
391 .BR pipe (2));
392 .IP *
393 employ
394 .B CLONE_*
395 flags that create new namespaces with
396 .BR clone (2)
397 and
398 .BR unshare (2)
399 (but, since Linux 3.8,
400 creating user namespaces does not require any capability);
401 .IP *
402 call
403 .BR perf_event_open (2);
404 .IP *
405 access privileged
406 .I perf
407 event information;
408 .IP *
409 call
410 .BR setns (2)
411 (requires
412 .B CAP_SYS_ADMIN
413 in the
414 .I target
415 namespace);
416 .IP *
417 call
418 .BR fanotify_init (2);
419 .IP *
420 call
421 .BR bpf (2);
422 .IP *
423 perform privileged
424 .B KEYCTL_CHOWN
425 and
426 .B KEYCTL_SETPERM
427 .BR keyctl (2)
428 operations;
429 .IP *
430 perform
431 .BR madvise (2)
432 .B MADV_HWPOISON
433 operation;
434 .IP *
435 employ the
436 .B TIOCSTI
437 .BR ioctl (2)
438 to insert characters into the input queue of a terminal other than
439 the caller's controlling terminal;
440 .IP *
441 employ the obsolete
442 .BR nfsservctl (2)
443 system call;
444 .IP *
445 employ the obsolete
446 .BR bdflush (2)
447 system call;
448 .IP *
449 perform various privileged block-device
450 .BR ioctl (2)
451 operations;
452 .IP *
453 perform various privileged filesystem
454 .BR ioctl (2)
455 operations;
456 .IP *
457 perform privileged
458 .BR ioctl (2)
459 operations on the
460 .IR /dev/random
461 device (see
462 .BR random (4));
463 .IP *
464 install a
465 .BR seccomp (2)
466 filter without first having to set the
467 .I no_new_privs
468 thread attribute;
469 .IP *
470 modify allow/deny rules for device control groups;
471 .IP *
472 employ the
473 .BR ptrace (2)
474 .B PTRACE_SECCOMP_GET_FILTER
475 operation to dump tracee's seccomp filters;
476 .IP *
477 employ the
478 .BR ptrace (2)
479 .B PTRACE_SETOPTIONS
480 operation to suspend the tracee's seccomp protections (i.e., the
481 .B PTRACE_O_SUSPEND_SECCOMP
482 flag);
483 .IP *
484 perform administrative operations on many device drivers.
485 .RE
486 .PD
487 .TP
488 .B CAP_SYS_BOOT
489 Use
490 .BR reboot (2)
491 and
492 .BR kexec_load (2).
493 .TP
494 .B CAP_SYS_CHROOT
495 .RS
496 .PD 0
497 .IP * 2
498 Use
499 .BR chroot (2);
500 .IP *
501 change mount namespaces using
502 .BR setns (2).
503 .PD
504 .RE
505 .TP
506 .B CAP_SYS_MODULE
507 .RS
508 .PD 0
509 .IP * 2
510 Load and unload kernel modules
511 (see
512 .BR init_module (2)
513 and
514 .BR delete_module (2));
515 .IP *
516 in kernels before 2.6.25:
517 drop capabilities from the system-wide capability bounding set.
518 .PD
519 .RE
520 .TP
521 .B CAP_SYS_NICE
522 .PD 0
523 .RS
524 .IP * 2
525 Raise process nice value
526 .RB ( nice (2),
527 .BR setpriority (2))
528 and change the nice value for arbitrary processes;
529 .IP *
530 set real-time scheduling policies for calling process,
531 and set scheduling policies and priorities for arbitrary processes
532 .RB ( sched_setscheduler (2),
533 .BR sched_setparam (2),
534 .BR sched_setattr (2));
535 .IP *
536 set CPU affinity for arbitrary processes
537 .RB ( sched_setaffinity (2));
538 .IP *
539 set I/O scheduling class and priority for arbitrary processes
540 .RB ( ioprio_set (2));
541 .IP *
542 apply
543 .BR migrate_pages (2)
544 to arbitrary processes and allow processes
545 to be migrated to arbitrary nodes;
546 .\" FIXME CAP_SYS_NICE also has the following effect for
547 .\" migrate_pages(2):
548 .\" do_migrate_pages(mm, &old, &new,
549 .\" capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
550 .\"
551 .\" Document this.
552 .IP *
553 apply
554 .BR move_pages (2)
555 to arbitrary processes;
556 .IP *
557 use the
558 .B MPOL_MF_MOVE_ALL
559 flag with
560 .BR mbind (2)
561 and
562 .BR move_pages (2).
563 .RE
564 .PD
565 .TP
566 .B CAP_SYS_PACCT
567 Use
568 .BR acct (2).
569 .TP
570 .B CAP_SYS_PTRACE
571 .PD 0
572 .RS
573 .IP * 2
574 Trace arbitrary processes using
575 .BR ptrace (2);
576 .IP *
577 apply
578 .BR get_robust_list (2)
579 to arbitrary processes;
580 .IP *
581 transfer data to or from the memory of arbitrary processes using
582 .BR process_vm_readv (2)
583 and
584 .BR process_vm_writev (2);
585 .IP *
586 inspect processes using
587 .BR kcmp (2).
588 .RE
589 .PD
590 .TP
591 .B CAP_SYS_RAWIO
592 .PD 0
593 .RS
594 .IP * 2
595 Perform I/O port operations
596 .RB ( iopl (2)
597 and
598 .BR ioperm (2));
599 .IP *
600 access
601 .IR /proc/kcore ;
602 .IP *
603 employ the
604 .B FIBMAP
605 .BR ioctl (2)
606 operation;
607 .IP *
608 open devices for accessing x86 model-specific registers (MSRs, see
609 .BR msr (4));
610 .IP *
611 update
612 .IR /proc/sys/vm/mmap_min_addr ;
613 .IP *
614 create memory mappings at addresses below the value specified by
615 .IR /proc/sys/vm/mmap_min_addr ;
616 .IP *
617 map files in
618 .IR /proc/bus/pci ;
619 .IP *
620 open
621 .IR /dev/mem
622 and
623 .IR /dev/kmem ;
624 .IP *
625 perform various SCSI device commands;
626 .IP *
627 perform certain operations on
628 .BR hpsa (4)
629 and
630 .BR cciss (4)
631 devices;
632 .IP *
633 perform a range of device-specific operations on other devices.
634 .RE
635 .PD
636 .TP
637 .B CAP_SYS_RESOURCE
638 .PD 0
639 .RS
640 .IP * 2
641 Use reserved space on ext2 filesystems;
642 .IP *
643 make
644 .BR ioctl (2)
645 calls controlling ext3 journaling;
646 .IP *
647 override disk quota limits;
648 .IP *
649 increase resource limits (see
650 .BR setrlimit (2));
651 .IP *
652 override
653 .B RLIMIT_NPROC
654 resource limit;
655 .IP *
656 override maximum number of consoles on console allocation;
657 .IP *
658 override maximum number of keymaps;
659 .IP *
660 allow more than 64hz interrupts from the real-time clock;
661 .IP *
662 raise
663 .I msg_qbytes
664 limit for a System V message queue above the limit in
665 .I /proc/sys/kernel/msgmnb
666 (see
667 .BR msgop (2)
668 and
669 .BR msgctl (2));
670 .IP *
671 allow the
672 .B RLIMIT_NOFILE
673 resource limit on the number of "in-flight" file descriptors
674 to be bypassed when passing file descriptors to another process
675 via a UNIX domain socket (see
676 .BR unix (7));
677 .IP *
678 override the
679 .I /proc/sys/fs/pipe-size-max
680 limit when setting the capacity of a pipe using the
681 .B F_SETPIPE_SZ
682 .BR fcntl (2)
683 command.
684 .IP *
685 use
686 .BR F_SETPIPE_SZ
687 to increase the capacity of a pipe above the limit specified by
688 .IR /proc/sys/fs/pipe-max-size ;
689 .IP *
690 override
691 .I /proc/sys/fs/mqueue/queues_max
692 limit when creating POSIX message queues (see
693 .BR mq_overview (7));
694 .IP *
695 employ the
696 .BR prctl (2)
697 .B PR_SET_MM
698 operation;
699 .IP *
700 set
701 .IR /proc/[pid]/oom_score_adj
702 to a value lower than the value last set by a process with
703 .BR CAP_SYS_RESOURCE .
704 .RE
705 .PD
706 .TP
707 .B CAP_SYS_TIME
708 Set system clock
709 .RB ( settimeofday (2),
710 .BR stime (2),
711 .BR adjtimex (2));
712 set real-time (hardware) clock.
713 .TP
714 .B CAP_SYS_TTY_CONFIG
715 Use
716 .BR vhangup (2);
717 employ various privileged
718 .BR ioctl (2)
719 operations on virtual terminals.
720 .TP
721 .BR CAP_SYSLOG " (since Linux 2.6.37)"
722 .RS
723 .PD 0
724 .IP * 2
725 Perform privileged
726 .BR syslog (2)
727 operations.
728 See
729 .BR syslog (2)
730 for information on which operations require privilege.
731 .IP *
732 View kernel addresses exposed via
733 .I /proc
734 and other interfaces when
735 .IR /proc/sys/kernel/kptr_restrict
736 has the value 1.
737 (See the discussion of the
738 .I kptr_restrict
739 in
740 .BR proc (5).)
741 .PD
742 .RE
743 .TP
744 .BR CAP_WAKE_ALARM " (since Linux 3.0)"
745 Trigger something that will wake up the system (set
746 .B CLOCK_REALTIME_ALARM
747 and
748 .B CLOCK_BOOTTIME_ALARM
749 timers).
750 .\"
751 .SS Past and current implementation
752 A full implementation of capabilities requires that:
753 .IP 1. 3
754 For all privileged operations,
755 the kernel must check whether the thread has the required
756 capability in its effective set.
757 .IP 2.
758 The kernel must provide system calls allowing a thread's capability sets to
759 be changed and retrieved.
760 .IP 3.
761 The filesystem must support attaching capabilities to an executable file,
762 so that a process gains those capabilities when the file is executed.
763 .PP
764 Before kernel 2.6.24, only the first two of these requirements are met;
765 since kernel 2.6.24, all three requirements are met.
766 .\"
767 .SS Notes to kernel developers
768 When adding a new kernel feature that should be governed by a capability,
769 consider the following points.
770 .IP * 3
771 The goal of capabilities is divide the power of superuser into pieces,
772 such that if a program that has one or more capabilities is compromised,
773 its power to do damage to the system would be less than the same program
774 running with root privilege.
775 .IP *
776 You have the choice of either creating a new capability for your new feature,
777 or associating the feature with one of the existing capabilities.
778 In order to keep the set of capabilities to a manageable size,
779 the latter option is preferable,
780 unless there are compelling reasons to take the former option.
781 (There is also a technical limit:
782 the size of capability sets is currently limited to 64 bits.)
783 .IP *
784 To determine which existing capability might best be associated
785 with your new feature, review the list of capabilities above in order
786 to find a "silo" into which your new feature best fits.
787 One approach to take is to determine if there are other features
788 requiring capabilities that will always be used along with the new feature.
789 If the new feature is useless without these other features,
790 you should use the same capability as the other features.
791 .IP *
792 .IR Don't
793 choose
794 .B CAP_SYS_ADMIN
795 if you can possibly avoid it!
796 A vast proportion of existing capability checks are associated
797 with this capability (see the partial list above).
798 It can plausibly be called "the new root",
799 since on the one hand, it confers a wide range of powers,
800 and on the other hand,
801 its broad scope means that this is the capability
802 that is required by many privileged programs.
803 Don't make the problem worse.
804 The only new features that should be associated with
805 .B CAP_SYS_ADMIN
806 are ones that
807 .I closely
808 match existing uses in that silo.
809 .IP *
810 If you have determined that it really is necessary to create
811 a new capability for your feature,
812 don't make or name it as a "single-use" capability.
813 Thus, for example, the addition of the highly specific
814 .BR CAP_SYS_PACCT
815 was probably a mistake.
816 Instead, try to identify and name your new capability as a broader
817 silo into which other related future use cases might fit.
818 .\"
819 .SS Thread capability sets
820 Each thread has the following capability sets containing zero or more
821 of the above capabilities:
822 .TP
823 .IR Permitted
824 This is a limiting superset for the effective
825 capabilities that the thread may assume.
826 It is also a limiting superset for the capabilities that
827 may be added to the inheritable set by a thread that does not have the
828 .B CAP_SETPCAP
829 capability in its effective set.
830 .IP
831 If a thread drops a capability from its permitted set,
832 it can never reacquire that capability (unless it
833 .BR execve (2)s
834 either a set-user-ID-root program, or
835 a program whose associated file capabilities grant that capability).
836 .TP
837 .IR Inheritable
838 This is a set of capabilities preserved across an
839 .BR execve (2).
840 Inheritable capabilities remain inheritable when executing any program,
841 and inheritable capabilities are added to the permitted set when executing
842 a program that has the corresponding bits set in the file inheritable set.
843 .IP
844 Because inheritable capabilities are not generally preserved across
845 .BR execve (2)
846 when running as a non-root user, applications that wish to run helper
847 programs with elevated capabilities should consider using
848 ambient capabilities, described below.
849 .TP
850 .IR Effective
851 This is the set of capabilities used by the kernel to
852 perform permission checks for the thread.
853 .TP
854 .IR Bounding " (per-thread since Linux 2.6.25)"
855 The capability bounding set is a mechanism that can be used
856 to limit the capabilities that are gained during
857 .BR execve (2).
858 .IP
859 Since Linux 2.6.25, this is a per-thread capability set.
860 In older kernels, the capability bounding set was a system wide attribute
861 shared by all threads on the system.
862 .IP
863 .IP
864 For more details on the capability bounding set, see below.
865 .TP
866 .IR Ambient " (since Linux 4.3)"
867 .\" commit 58319057b7847667f0c9585b9de0e8932b0fdb08
868 This is a set of capabilities that are preserved across an
869 .BR execve (2)
870 of a program that is not privileged.
871 The ambient capability set obeys the invariant that no capability
872 can ever be ambient if it is not both permitted and inheritable.
873 .IP
874 The ambient capability set can be directly modified using
875 .BR prctl (2).
876 Ambient capabilities are automatically lowered if either of
877 the corresponding permitted or inheritable capabilities is lowered.
878 .IP
879 Executing a program that changes UID or GID due to the
880 set-user-ID or set-group-ID bits or executing a program that has
881 any file capabilities set will clear the ambient set.
882 Ambient capabilities are added to the permitted set and
883 assigned to the effective set when
884 .BR execve (2)
885 is called.
886 If ambient capabilities cause a process's permitted and effective
887 capabilities to increase during an
888 .BR execve (2),
889 this does not trigger the secure-execution mode described in
890 .BR ld.so (8).
891 .PP
892 A child created via
893 .BR fork (2)
894 inherits copies of its parent's capability sets.
895 See below for a discussion of the treatment of capabilities during
896 .BR execve (2).
897 .PP
898 Using
899 .BR capset (2),
900 a thread may manipulate its own capability sets (see below).
901 .PP
902 Since Linux 3.2, the file
903 .I /proc/sys/kernel/cap_last_cap
904 .\" commit 73efc0394e148d0e15583e13712637831f926720
905 exposes the numerical value of the highest capability
906 supported by the running kernel;
907 this can be used to determine the highest bit
908 that may be set in a capability set.
909 .\"
910 .SS File capabilities
911 Since kernel 2.6.24, the kernel supports
912 associating capability sets with an executable file using
913 .BR setcap (8).
914 The file capability sets are stored in an extended attribute (see
915 .BR setxattr (2)
916 and
917 .BR xattr (7))
918 named
919 .IR "security.capability" .
920 Writing to this extended attribute requires the
921 .BR CAP_SETFCAP
922 capability.
923 The file capability sets,
924 in conjunction with the capability sets of the thread,
925 determine the capabilities of a thread after an
926 .BR execve (2).
927 .PP
928 The three file capability sets are:
929 .TP
930 .IR Permitted " (formerly known as " forced ):
931 These capabilities are automatically permitted to the thread,
932 regardless of the thread's inheritable capabilities.
933 .TP
934 .IR Inheritable " (formerly known as " allowed ):
935 This set is ANDed with the thread's inheritable set to determine which
936 inheritable capabilities are enabled in the permitted set of
937 the thread after the
938 .BR execve (2).
939 .TP
940 .IR Effective :
941 This is not a set, but rather just a single bit.
942 If this bit is set, then during an
943 .BR execve (2)
944 all of the new permitted capabilities for the thread are
945 also raised in the effective set.
946 If this bit is not set, then after an
947 .BR execve (2),
948 none of the new permitted capabilities is in the new effective set.
949 .IP
950 Enabling the file effective capability bit implies
951 that any file permitted or inheritable capability that causes a
952 thread to acquire the corresponding permitted capability during an
953 .BR execve (2)
954 (see the transformation rules described below) will also acquire that
955 capability in its effective set.
956 Therefore, when assigning capabilities to a file
957 .RB ( setcap (8),
958 .BR cap_set_file (3),
959 .BR cap_set_fd (3)),
960 if we specify the effective flag as being enabled for any capability,
961 then the effective flag must also be specified as enabled
962 for all other capabilities for which the corresponding permitted or
963 inheritable flags is enabled.
964 .\"
965 .SS File capability extended attribute versioning
966 To allow extensibility,
967 the kernel supports a scheme to encode a version number inside the
968 .I security.capability
969 extended attribute that is used to implement file capabilities.
970 These version numbers are internal to the implementation,
971 and not directly visible to user-space applications.
972 To date, the following versions are supported:
973 .TP
974 .BR VFS_CAP_REVISION_1
975 This was the original file capability implementation,
976 which supported 32-bit masks for file capabilities.
977 .TP
978 .BR VFS_CAP_REVISION_2 " (since Linux 2.6.25)"
979 .\" commit e338d263a76af78fe8f38a72131188b58fceb591
980 This version allows for file capability masks that are 64 bits in size,
981 and was necessary as the number of supported capabilities grew beyond 32.
982 The kernel transparently continues to support the execution of files
983 that have 32-bit version 1 capability masks,
984 but when adding capabilities to files that did not previously
985 have capabilities, or modifying the capabilities of existing files,
986 it automatically uses the version 2 scheme
987 (or possibly the version 3 scheme, as described below).
988 .TP
989 .BR VFS_CAP_REVISION_3 " (since Linux 4.14)"
990 .\" commit 8db6c34f1dbc8e06aa016a9b829b06902c3e1340
991 Version 3 file capabilities are provided
992 to support namespaced file capabilities (described below).
993 .IP
994 As with version 2 file capabilities,
995 version 3 capability masks are 64 bits in size.
996 But in addition, the root user ID of namespace is encoded in the
997 .I security.capability
998 extended attribute.
999 (A namespace's root user ID is the value that user ID 0
1000 inside that namespace maps to in the initial user namespace.)
1001 .IP
1002 Version 3 file capabilities are designed to coexist
1003 with version 2 capabilities;
1004 that is, on a modern Linux system,
1005 there may be some files with version 2 capabilities
1006 while others have version 3 capabilities.
1007 .PP
1008 Before Linux 4.14,
1009 the only kind of file capability extended attribute
1010 that could be attached to a file was a
1011 .B VFS_CAP_REVISION_2
1012 attribute.
1013 Since Linux 4.14,
1014 the version of the
1015 .I security.capability
1016 extended attribute that is attached to a file
1017 depends on the circumstances in which the attribute was created.
1018 .PP
1019 Starting with Linux 4.14, a
1020 .I security.capability
1021 extended attribute is automatically created as (or converted to)
1022 a version 3
1023 .RB ( VFS_CAP_REVISION_3 )
1024 attribute if both of the following are true:
1025 .IP (1) 4
1026 The thread writing the attribute resides in a noninitial user namespace.
1027 (More precisely: the thread resides in a user namespace other
1028 than the one from which the underlying filesystem was mounted.)
1029 .IP (2)
1030 The thread has the
1031 .BR CAP_SETFCAP
1032 capability over the file inode,
1033 meaning that (a) the thread has the
1034 .B CAP_SETFCAP
1035 capability in its own user namespace;
1036 and (b) the UID and GID of the file inode have mappings in
1037 the writer's user namespace.
1038 .PP
1039 When a
1040 .BR VFS_CAP_REVISION_3
1041 .I security.capability
1042 extended attribute is created, the root user ID of the creating thread's
1043 user namespace is saved in the extended attribute.
1044 .PP
1045 By contrast, creating or modifying a
1046 .I security.capability
1047 extended attribute from a privileged
1048 .RB ( CAP_SETFCAP )
1049 thread that resides in the
1050 namespace where the underlying filesystem was mounted
1051 (this normally means the initial user namespace)
1052 automatically results in the creation of a version 2
1053 .RB ( VFS_CAP_REVISION_2 )
1054 attribute.
1055 .PP
1056 Note that the creation of a version 3
1057 .I security.capability
1058 extended attribute is automatic.
1059 That is to say, when a user-space application writes
1060 .RB ( setxattr (2))
1061 a
1062 .I security.capability
1063 attribute in the version 2 format,
1064 the kernel will automatically create a version 3 attribute
1065 if the attribute is created in the circumstances described above.
1066 Correspondingly, when a version 3
1067 .I security.capability
1068 attribute is retrieved
1069 .RB ( getxattr (2))
1070 by a process that resides inside a user namespace that was created by the
1071 root user ID (or a descendant of that user namespace),
1072 the returned attribute is (automatically)
1073 simplified to appear as a version 2 attribute
1074 (i.e., the returned value is the size of a version 2 attribute and does
1075 not include the root user ID).
1076 These automatic translations mean that no changes are required to
1077 user-space tools (e.g.,
1078 .BR setcap (1)
1079 and
1080 .BR getcap (1))
1081 in order for those tools to be used to create and retrieve version 3
1082 .I security.capability
1083 attributes.
1084 .PP
1085 Note that a file can have either a version 2 or a version 3
1086 .I security.capability
1087 extended attribute associated with it, but not both:
1088 creation or modification of the
1089 .I security.capability
1090 extended attribute will automatically modify the version
1091 according to the circumstances in which the extended attribute is
1092 created or modified.
1093 .\"
1094 .SS Transformation of capabilities during execve()
1095 .PP
1096 During an
1097 .BR execve (2),
1098 the kernel calculates the new capabilities of
1099 the process using the following algorithm:
1100 .PP
1101 .in +4n
1102 .EX
1103 P'(ambient) = (file is privileged) ? 0 : P(ambient)
1104
1105 P'(permitted) = (P(inheritable) & F(inheritable)) |
1106 (F(permitted) & P(bounding)) | P'(ambient)
1107
1108 P'(effective) = F(effective) ? P'(permitted) : P'(ambient)
1109
1110 P'(inheritable) = P(inheritable) [i.e., unchanged]
1111
1112 P'(bounding) = P(bounding) [i.e., unchanged]
1113 .EE
1114 .in
1115 .PP
1116 where:
1117 .RS 4
1118 .IP P() 6
1119 denotes the value of a thread capability set before the
1120 .BR execve (2)
1121 .IP P'()
1122 denotes the value of a thread capability set after the
1123 .BR execve (2)
1124 .IP F()
1125 denotes a file capability set
1126 .RE
1127 .PP
1128 Note the following details relating to the above capability
1129 transformation rules:
1130 .IP * 3
1131 The ambient capability set is present only since Linux 4.3.
1132 When determining the transformation of the ambient set during
1133 .BR execve (2),
1134 a privileged file is one that has capabilities or
1135 has the set-user-ID or set-group-ID bit set.
1136 .IP *
1137 Prior to Linux 2.6.25,
1138 the bounding set was a system-wide attribute shared by all threads.
1139 That system-wide value was employed to calculate the new permitted set during
1140 .BR execve (2)
1141 in the same manner as shown above for
1142 .IR P(bounding) .
1143 .PP
1144 .IR Note :
1145 during the capability transitions described above,
1146 file capabilities may be ignored (treated as empty) for the same reasons
1147 that the set-user-ID and set-group-ID bits are ignored; see
1148 .BR execve (2).
1149 File capabilities are similarly ignored if the kernel was booted with the
1150 .I no_file_caps
1151 option.
1152 .PP
1153 .IR Note :
1154 according to the rules above,
1155 if a process with nonzero user IDs performs an
1156 .BR execve (2)
1157 then any capabilities that are present in
1158 its permitted and effective sets will be cleared.
1159 For the treatment of capabilities when a process with a
1160 user ID of zero performs an
1161 .BR execve (2),
1162 see below under
1163 .IR "Capabilities and execution of programs by root" .
1164 .\"
1165 .SS Safety checking for capability-dumb binaries
1166 A capability-dumb binary is an application that has been
1167 marked to have file capabilities, but has not been converted to use the
1168 .BR libcap (3)
1169 API to manipulate its capabilities.
1170 (In other words, this is a traditional set-user-ID-root program
1171 that has been switched to use file capabilities,
1172 but whose code has not been modified to understand capabilities.)
1173 For such applications,
1174 the effective capability bit is set on the file,
1175 so that the file permitted capabilities are automatically
1176 enabled in the process effective set when executing the file.
1177 The kernel recognizes a file which has the effective capability bit set
1178 as capability-dumb for the purpose of the check described here.
1179 .PP
1180 When executing a capability-dumb binary,
1181 the kernel checks if the process obtained all permitted capabilities
1182 that were specified in the file permitted set,
1183 after the capability transformations described above have been performed.
1184 (The typical reason why this might
1185 .I not
1186 occur is that the capability bounding set masked out some
1187 of the capabilities in the file permitted set.)
1188 If the process did not obtain the full set of
1189 file permitted capabilities, then
1190 .BR execve (2)
1191 fails with the error
1192 .BR EPERM .
1193 This prevents possible security risks that could arise when
1194 a capability-dumb application is executed with less privilege that it needs.
1195 Note that, by definition,
1196 the application could not itself recognize this problem,
1197 since it does not employ the
1198 .BR libcap (3)
1199 API.
1200 .\"
1201 .SS Capabilities and execution of programs by root
1202 .\" See cap_bprm_set_creds(), bprm_caps_from_vfs_cap() and
1203 .\" handle_privileged_root() in security/commoncap.c (Linux 5.0 source)
1204 In order to mirror traditional UNIX semantics,
1205 the kernel performs special treatment of file capabilities when
1206 a process with UID 0 (root) executes a program and
1207 when a set-user-ID-root program is executed.
1208 .PP
1209 After having performed any changes to the process effective ID that
1210 were triggered by the set-user-ID mode bit of the binary\(eme.g.,
1211 switching the effective user ID to 0 (root) because
1212 a set-user-ID-root program was executed\(emthe
1213 kernel calculates the file capability sets as follows:
1214 .IP 1. 3
1215 If the real or effective user ID of the process is 0 (root),
1216 then the file inheritable and permitted sets are ignored;
1217 instead they are notionally considered to be all ones
1218 (i.e., all capabilities enabled).
1219 (There is one exception to this behavior, described below in
1220 .IR "Set-user-ID-root programs that have file capabilities" .)
1221 .IP 2.
1222 If the effective user ID of the process is 0 (root) or
1223 the file effective bit is in fact enabled,
1224 then the file effective bit is notionally defined to be one (enabled).
1225 .PP
1226 These notional values for the file's capability sets are then used
1227 as described above to calculate the transformation of the process's
1228 capabilities during
1229 .BR execve (2).
1230 .PP
1231 Thus, when a process with nonzero UIDs
1232 .BR execve (2)s
1233 a set-user-ID-root program that does not have capabilities attached,
1234 or when a process whose real and effective UIDs are zero
1235 .BR execve (2)s
1236 a program, the calculation of the process's new
1237 permitted capabilities simplifies to:
1238 .PP
1239 .in +4n
1240 .EX
1241 P'(permitted) = P(inheritable) | P(bounding)
1242
1243 P'(effective) = P'(permitted)
1244 .EE
1245 .in
1246 .PP
1247 Consequently, the process gains all capabilities in its permitted and
1248 effective capability sets,
1249 except those masked out by the capability bounding set.
1250 (In the calculation of P'(permitted),
1251 the P'(ambient) term can be simplified away because it is by
1252 definition a proper subset of P(inheritable).)
1253 .PP
1254 The special treatments of user ID 0 (root) described in this subsection
1255 can be disabled using the securebits mechanism described below.
1256 .\"
1257 .\"
1258 .SS Set-user-ID-root programs that have file capabilities
1259 There is one exception to the behavior described under
1260 .IR "Capabilities and execution of programs by root" .
1261 If (a) the binary that is being executed has capabilities attached and
1262 (b) the real user ID of the process is
1263 .I not
1264 0 (root) and
1265 (c) the effective user ID of the process
1266 .I is
1267 0 (root), then the file capability bits are honored
1268 (i.e., they are not notionally considered to be all ones).
1269 The usual way in which this situation can arise is when executing
1270 a set-UID-root program that also has file capabilities.
1271 When such a program is executed,
1272 the process gains just the capabilities granted by the program
1273 (i.e., not all capabilities,
1274 as would occur when executing a set-user-ID-root program
1275 that does not have any associated file capabilities).
1276 .PP
1277 Note that one can assign empty capability sets to a program file,
1278 and thus it is possible to create a set-user-ID-root program that
1279 changes the effective and saved set-user-ID of the process
1280 that executes the program to 0,
1281 but confers no capabilities to that process.
1282 .\"
1283 .SS Capability bounding set
1284 The capability bounding set is a security mechanism that can be used
1285 to limit the capabilities that can be gained during an
1286 .BR execve (2).
1287 The bounding set is used in the following ways:
1288 .IP * 2
1289 During an
1290 .BR execve (2),
1291 the capability bounding set is ANDed with the file permitted
1292 capability set, and the result of this operation is assigned to the
1293 thread's permitted capability set.
1294 The capability bounding set thus places a limit on the permitted
1295 capabilities that may be granted by an executable file.
1296 .IP *
1297 (Since Linux 2.6.25)
1298 The capability bounding set acts as a limiting superset for
1299 the capabilities that a thread can add to its inheritable set using
1300 .BR capset (2).
1301 This means that if a capability is not in the bounding set,
1302 then a thread can't add this capability to its
1303 inheritable set, even if it was in its permitted capabilities,
1304 and thereby cannot have this capability preserved in its
1305 permitted set when it
1306 .BR execve (2)s
1307 a file that has the capability in its inheritable set.
1308 .PP
1309 Note that the bounding set masks the file permitted capabilities,
1310 but not the inheritable capabilities.
1311 If a thread maintains a capability in its inheritable set
1312 that is not in its bounding set,
1313 then it can still gain that capability in its permitted set
1314 by executing a file that has the capability in its inheritable set.
1315 .PP
1316 Depending on the kernel version, the capability bounding set is either
1317 a system-wide attribute, or a per-process attribute.
1318 .PP
1319 .B "Capability bounding set from Linux 2.6.25 onward"
1320 .PP
1321 From Linux 2.6.25, the
1322 .I "capability bounding set"
1323 is a per-thread attribute.
1324 (The system-wide capability bounding set described below no longer exists.)
1325 .PP
1326 The bounding set is inherited at
1327 .BR fork (2)
1328 from the thread's parent, and is preserved across an
1329 .BR execve (2).
1330 .PP
1331 A thread may remove capabilities from its capability bounding set using the
1332 .BR prctl (2)
1333 .B PR_CAPBSET_DROP
1334 operation, provided it has the
1335 .B CAP_SETPCAP
1336 capability.
1337 Once a capability has been dropped from the bounding set,
1338 it cannot be restored to that set.
1339 A thread can determine if a capability is in its bounding set using the
1340 .BR prctl (2)
1341 .B PR_CAPBSET_READ
1342 operation.
1343 .PP
1344 Removing capabilities from the bounding set is supported only if file
1345 capabilities are compiled into the kernel.
1346 In kernels before Linux 2.6.33,
1347 file capabilities were an optional feature configurable via the
1348 .B CONFIG_SECURITY_FILE_CAPABILITIES
1349 option.
1350 Since Linux 2.6.33,
1351 .\" commit b3a222e52e4d4be77cc4520a57af1a4a0d8222d1
1352 the configuration option has been removed
1353 and file capabilities are always part of the kernel.
1354 When file capabilities are compiled into the kernel, the
1355 .B init
1356 process (the ancestor of all processes) begins with a full bounding set.
1357 If file capabilities are not compiled into the kernel, then
1358 .B init
1359 begins with a full bounding set minus
1360 .BR CAP_SETPCAP ,
1361 because this capability has a different meaning when there are
1362 no file capabilities.
1363 .PP
1364 Removing a capability from the bounding set does not remove it
1365 from the thread's inheritable set.
1366 However it does prevent the capability from being added
1367 back into the thread's inheritable set in the future.
1368 .PP
1369 .B "Capability bounding set prior to Linux 2.6.25"
1370 .PP
1371 In kernels before 2.6.25, the capability bounding set is a system-wide
1372 attribute that affects all threads on the system.
1373 The bounding set is accessible via the file
1374 .IR /proc/sys/kernel/cap-bound .
1375 (Confusingly, this bit mask parameter is expressed as a
1376 signed decimal number in
1377 .IR /proc/sys/kernel/cap-bound .)
1378 .PP
1379 Only the
1380 .B init
1381 process may set capabilities in the capability bounding set;
1382 other than that, the superuser (more precisely: a process with the
1383 .B CAP_SYS_MODULE
1384 capability) may only clear capabilities from this set.
1385 .PP
1386 On a standard system the capability bounding set always masks out the
1387 .B CAP_SETPCAP
1388 capability.
1389 To remove this restriction (dangerous!), modify the definition of
1390 .B CAP_INIT_EFF_SET
1391 in
1392 .I include/linux/capability.h
1393 and rebuild the kernel.
1394 .PP
1395 The system-wide capability bounding set feature was added
1396 to Linux starting with kernel version 2.2.11.
1397 .\"
1398 .\"
1399 .\"
1400 .SS Effect of user ID changes on capabilities
1401 To preserve the traditional semantics for transitions between
1402 0 and nonzero user IDs,
1403 the kernel makes the following changes to a thread's capability
1404 sets on changes to the thread's real, effective, saved set,
1405 and filesystem user IDs (using
1406 .BR setuid (2),
1407 .BR setresuid (2),
1408 or similar):
1409 .IP 1. 3
1410 If one or more of the real, effective or saved set user IDs
1411 was previously 0, and as a result of the UID changes all of these IDs
1412 have a nonzero value,
1413 then all capabilities are cleared from the permitted, effective, and ambient
1414 capability sets.
1415 .IP 2.
1416 If the effective user ID is changed from 0 to nonzero,
1417 then all capabilities are cleared from the effective set.
1418 .IP 3.
1419 If the effective user ID is changed from nonzero to 0,
1420 then the permitted set is copied to the effective set.
1421 .IP 4.
1422 If the filesystem user ID is changed from 0 to nonzero (see
1423 .BR setfsuid (2)),
1424 then the following capabilities are cleared from the effective set:
1425 .BR CAP_CHOWN ,
1426 .BR CAP_DAC_OVERRIDE ,
1427 .BR CAP_DAC_READ_SEARCH ,
1428 .BR CAP_FOWNER ,
1429 .BR CAP_FSETID ,
1430 .B CAP_LINUX_IMMUTABLE
1431 (since Linux 2.6.30),
1432 .BR CAP_MAC_OVERRIDE ,
1433 and
1434 .B CAP_MKNOD
1435 (since Linux 2.6.30).
1436 If the filesystem UID is changed from nonzero to 0,
1437 then any of these capabilities that are enabled in the permitted set
1438 are enabled in the effective set.
1439 .PP
1440 If a thread that has a 0 value for one or more of its user IDs wants
1441 to prevent its permitted capability set being cleared when it resets
1442 all of its user IDs to nonzero values, it can do so using the
1443 .B SECBIT_KEEP_CAPS
1444 securebits flag described below.
1445 .\"
1446 .SS Programmatically adjusting capability sets
1447 A thread can retrieve and change its permitted, effective, and inheritable
1448 capability sets using the
1449 .BR capget (2)
1450 and
1451 .BR capset (2)
1452 system calls.
1453 However, the use of
1454 .BR cap_get_proc (3)
1455 and
1456 .BR cap_set_proc (3),
1457 both provided in the
1458 .I libcap
1459 package,
1460 is preferred for this purpose.
1461 The following rules govern changes to the thread capability sets:
1462 .IP 1. 3
1463 If the caller does not have the
1464 .B CAP_SETPCAP
1465 capability,
1466 the new inheritable set must be a subset of the combination
1467 of the existing inheritable and permitted sets.
1468 .IP 2.
1469 (Since Linux 2.6.25)
1470 The new inheritable set must be a subset of the combination of the
1471 existing inheritable set and the capability bounding set.
1472 .IP 3.
1473 The new permitted set must be a subset of the existing permitted set
1474 (i.e., it is not possible to acquire permitted capabilities
1475 that the thread does not currently have).
1476 .IP 4.
1477 The new effective set must be a subset of the new permitted set.
1478 .SS The securebits flags: establishing a capabilities-only environment
1479 .\" For some background:
1480 .\" see http://lwn.net/Articles/280279/ and
1481 .\" http://article.gmane.org/gmane.linux.kernel.lsm/5476/
1482 Starting with kernel 2.6.26,
1483 and with a kernel in which file capabilities are enabled,
1484 Linux implements a set of per-thread
1485 .I securebits
1486 flags that can be used to disable special handling of capabilities for UID 0
1487 .RI ( root ).
1488 These flags are as follows:
1489 .TP
1490 .B SECBIT_KEEP_CAPS
1491 Setting this flag allows a thread that has one or more 0 UIDs to retain
1492 capabilities in its permitted set
1493 when it switches all of its UIDs to nonzero values.
1494 If this flag is not set,
1495 then such a UID switch causes the thread to lose all permitted capabilities.
1496 This flag is always cleared on an
1497 .BR execve (2).
1498 .IP
1499 Note that even with the
1500 .B SECBIT_KEEP_CAPS
1501 flag set, the effective capabilities of a thread are cleared when it
1502 switches its effective UID to a nonzero value.
1503 However,
1504 if the thread has set this flag and its effective UID is already nonzero,
1505 and the thread subsequently switches all other UIDs to nonzero values,
1506 then the effective capabilities will not be cleared.
1507 .IP
1508 The setting of the
1509 .B SECBIT_KEEP_CAPS
1510 flag is ignored if the
1511 .B SECBIT_NO_SETUID_FIXUP
1512 flag is set.
1513 (The latter flag provides a superset of the effect of the former flag.)
1514 .IP
1515 This flag provides the same functionality as the older
1516 .BR prctl (2)
1517 .B PR_SET_KEEPCAPS
1518 operation.
1519 .TP
1520 .B SECBIT_NO_SETUID_FIXUP
1521 Setting this flag stops the kernel from adjusting the process's
1522 permitted, effective, and ambient capability sets when
1523 the thread's effective and filesystem UIDs are switched between
1524 zero and nonzero values.
1525 (See the subsection
1526 .IR "Effect of user ID changes on capabilities" .)
1527 .TP
1528 .B SECBIT_NOROOT
1529 If this bit is set, then the kernel does not grant capabilities
1530 when a set-user-ID-root program is executed, or when a process with
1531 an effective or real UID of 0 calls
1532 .BR execve (2).
1533 (See the subsection
1534 .IR "Capabilities and execution of programs by root" .)
1535 .TP
1536 .B SECBIT_NO_CAP_AMBIENT_RAISE
1537 Setting this flag disallows raising ambient capabilities via the
1538 .BR prctl (2)
1539 .BR PR_CAP_AMBIENT_RAISE
1540 operation.
1541 .PP
1542 Each of the above "base" flags has a companion "locked" flag.
1543 Setting any of the "locked" flags is irreversible,
1544 and has the effect of preventing further changes to the
1545 corresponding "base" flag.
1546 The locked flags are:
1547 .BR SECBIT_KEEP_CAPS_LOCKED ,
1548 .BR SECBIT_NO_SETUID_FIXUP_LOCKED ,
1549 .BR SECBIT_NOROOT_LOCKED ,
1550 and
1551 .BR SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED .
1552 .PP
1553 The
1554 .I securebits
1555 flags can be modified and retrieved using the
1556 .BR prctl (2)
1557 .B PR_SET_SECUREBITS
1558 and
1559 .B PR_GET_SECUREBITS
1560 operations.
1561 The
1562 .B CAP_SETPCAP
1563 capability is required to modify the flags.
1564 Note that the
1565 .BR SECBIT_*
1566 constants are available only after including the
1567 .I <linux/securebits.h>
1568 header file.
1569 .PP
1570 The
1571 .I securebits
1572 flags are inherited by child processes.
1573 During an
1574 .BR execve (2),
1575 all of the flags are preserved, except
1576 .B SECBIT_KEEP_CAPS
1577 which is always cleared.
1578 .PP
1579 An application can use the following call to lock itself,
1580 and all of its descendants,
1581 into an environment where the only way of gaining capabilities
1582 is by executing a program with associated file capabilities:
1583 .PP
1584 .in +4n
1585 .EX
1586 prctl(PR_SET_SECUREBITS,
1587 /* SECBIT_KEEP_CAPS off */
1588 SECBIT_KEEP_CAPS_LOCKED |
1589 SECBIT_NO_SETUID_FIXUP |
1590 SECBIT_NO_SETUID_FIXUP_LOCKED |
1591 SECBIT_NOROOT |
1592 SECBIT_NOROOT_LOCKED);
1593 /* Setting/locking SECBIT_NO_CAP_AMBIENT_RAISE
1594 is not required */
1595 .EE
1596 .in
1597 .\"
1598 .\"
1599 .SS Per-user-namespace """set-user-ID-root""" programs
1600 A set-user-ID program whose UID matches the UID that
1601 created a user namespace will confer capabilities
1602 in the process's permitted and effective sets
1603 when executed by any process inside that namespace
1604 or any descendant user namespace.
1605 .PP
1606 The rules about the transformation of the process's capabilities during the
1607 .BR execve (2)
1608 are exactly as described in the subsections
1609 .IR "Transformation of capabilities during execve()"
1610 and
1611 .IR "Capabilities and execution of programs by root" ,
1612 with the difference that, in the latter subsection, "root"
1613 is the UID of the creator of the user namespace.
1614 .\"
1615 .\"
1616 .SS Namespaced file capabilities
1617 .\" commit 8db6c34f1dbc8e06aa016a9b829b06902c3e1340
1618 Traditional (i.e., version 2) file capabilities associate
1619 only a set of capability masks with a binary executable file.
1620 When a process executes a binary with such capabilities,
1621 it gains the associated capabilities (within its user namespace)
1622 as per the rules described above in
1623 "Transformation of capabilities during execve()".
1624 .PP
1625 Because version 2 file capabilities confer capabilities to
1626 the executing process regardless of which user namespace it resides in,
1627 only privileged processes are permitted to associate capabilities with a file.
1628 Here, "privileged" means a process that has the
1629 .BR CAP_SETFCAP
1630 capability in the user namespace where the filesystem was mounted
1631 (normally the initial user namespace).
1632 This limitation renders file capabilities useless for certain use cases.
1633 For example, in user-namespaced containers,
1634 it can be desirable to be able to create a binary that
1635 confers capabilities only to processes executed inside that container,
1636 but not to processes that are executed outside the container.
1637 .PP
1638 Linux 4.14 added so-called namespaced file capabilities
1639 to support such use cases.
1640 Namespaced file capabilities are recorded as version 3 (i.e.,
1641 .BR VFS_CAP_REVISION_3 )
1642 .I security.capability
1643 extended attributes.
1644 Such an attribute is automatically created in the circumstances described
1645 above under "File capability extended attribute versioning".
1646 When a version 3
1647 .I security.capability
1648 extended attribute is created,
1649 the kernel records not just the capability masks in the extended attribute,
1650 but also the namespace root user ID.
1651 .PP
1652 As with a binary that has
1653 .BR VFS_CAP_REVISION_2
1654 file capabilities, a binary with
1655 .BR VFS_CAP_REVISION_3
1656 file capabilities confers capabilities to a process during
1657 .BR execve ().
1658 However, capabilities are conferred only if the binary is executed by
1659 a process that resides in a user namespace whose
1660 UID 0 maps to the root user ID that is saved in the extended attribute,
1661 or when executed by a process that resides in a descendant of such a namespace.
1662 .\"
1663 .\"
1664 .SS Interaction with user namespaces
1665 For further information on the interaction of
1666 capabilities and user namespaces, see
1667 .BR user_namespaces (7).
1668 .SH CONFORMING TO
1669 .PP
1670 No standards govern capabilities, but the Linux capability implementation
1671 is based on the withdrawn POSIX.1e draft standard; see
1672 .UR https://archive.org\:/details\:/posix_1003.1e-990310
1673 .UE .
1674 .SH NOTES
1675 When attempting to
1676 .BR strace (1)
1677 binaries that have capabilities (or set-user-ID-root binaries),
1678 you may find the
1679 .I \-u <username>
1680 option useful.
1681 Something like:
1682 .PP
1683 .in +4n
1684 .EX
1685 $ \fBsudo strace \-o trace.log \-u ceci ./myprivprog\fP
1686 .EE
1687 .in
1688 .PP
1689 From kernel 2.5.27 to kernel 2.6.26,
1690 .\" commit 5915eb53861c5776cfec33ca4fcc1fd20d66dd27 removed
1691 .\" CONFIG_SECURITY_CAPABILITIES
1692 capabilities were an optional kernel component,
1693 and could be enabled/disabled via the
1694 .B CONFIG_SECURITY_CAPABILITIES
1695 kernel configuration option.
1696 .PP
1697 The
1698 .I /proc/[pid]/task/TID/status
1699 file can be used to view the capability sets of a thread.
1700 The
1701 .I /proc/[pid]/status
1702 file shows the capability sets of a process's main thread.
1703 Before Linux 3.8, nonexistent capabilities were shown as being
1704 enabled (1) in these sets.
1705 Since Linux 3.8,
1706 .\" 7b9a7ec565505699f503b4fcf61500dceb36e744
1707 all nonexistent capabilities (above
1708 .BR CAP_LAST_CAP )
1709 are shown as disabled (0).
1710 .PP
1711 The
1712 .I libcap
1713 package provides a suite of routines for setting and
1714 getting capabilities that is more comfortable and less likely
1715 to change than the interface provided by
1716 .BR capset (2)
1717 and
1718 .BR capget (2).
1719 This package also provides the
1720 .BR setcap (8)
1721 and
1722 .BR getcap (8)
1723 programs.
1724 It can be found at
1725 .br
1726 .UR https://git.kernel.org\:/pub\:/scm\:/libs\:/libcap\:/libcap.git\:/refs/
1727 .UE .
1728 .PP
1729 Before kernel 2.6.24, and from kernel 2.6.24 to kernel 2.6.32 if
1730 file capabilities are not enabled, a thread with the
1731 .B CAP_SETPCAP
1732 capability can manipulate the capabilities of threads other than itself.
1733 However, this is only theoretically possible,
1734 since no thread ever has
1735 .BR CAP_SETPCAP
1736 in either of these cases:
1737 .IP * 2
1738 In the pre-2.6.25 implementation the system-wide capability bounding set,
1739 .IR /proc/sys/kernel/cap-bound ,
1740 always masks out this capability, and this can not be changed
1741 without modifying the kernel source and rebuilding.
1742 .IP *
1743 If file capabilities are disabled in the current implementation, then
1744 .B init
1745 starts out with this capability removed from its per-process bounding
1746 set, and that bounding set is inherited by all other processes
1747 created on the system.
1748 .SH SEE ALSO
1749 .BR capsh (1),
1750 .BR setpriv (1),
1751 .BR prctl (2),
1752 .BR setfsuid (2),
1753 .BR cap_clear (3),
1754 .BR cap_copy_ext (3),
1755 .BR cap_from_text (3),
1756 .BR cap_get_file (3),
1757 .BR cap_get_proc (3),
1758 .BR cap_init (3),
1759 .BR capgetp (3),
1760 .BR capsetp (3),
1761 .BR libcap (3),
1762 .BR proc (5),
1763 .BR credentials (7),
1764 .BR pthreads (7),
1765 .BR user_namespaces (7),
1766 .BR captest (8), \" from libcap-ng
1767 .BR filecap (8), \" from libcap-ng
1768 .BR getcap (8),
1769 .BR netcap (8), \" from libcap-ng
1770 .BR pscap (8), \" from libcap-ng
1771 .BR setcap (8)
1772 .PP
1773 .I include/linux/capability.h
1774 in the Linux kernel source tree