]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/capabilities.7
rtnetlink.7: Various tweaks to Jan Moskyto Matejka's patch
[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 .IP *
486 Modify autogroup nice values by writing to
487 .IR /proc/[pid]/autogroup
488 (see
489 .BR sched (7)).
490 .RE
491 .PD
492 .TP
493 .B CAP_SYS_BOOT
494 Use
495 .BR reboot (2)
496 and
497 .BR kexec_load (2).
498 .TP
499 .B CAP_SYS_CHROOT
500 .RS
501 .PD 0
502 .IP * 2
503 Use
504 .BR chroot (2);
505 .IP *
506 change mount namespaces using
507 .BR setns (2).
508 .PD
509 .RE
510 .TP
511 .B CAP_SYS_MODULE
512 .RS
513 .PD 0
514 .IP * 2
515 Load and unload kernel modules
516 (see
517 .BR init_module (2)
518 and
519 .BR delete_module (2));
520 .IP *
521 in kernels before 2.6.25:
522 drop capabilities from the system-wide capability bounding set.
523 .PD
524 .RE
525 .TP
526 .B CAP_SYS_NICE
527 .PD 0
528 .RS
529 .IP * 2
530 Raise process nice value
531 .RB ( nice (2),
532 .BR setpriority (2))
533 and change the nice value for arbitrary processes;
534 .IP *
535 set real-time scheduling policies for calling process,
536 and set scheduling policies and priorities for arbitrary processes
537 .RB ( sched_setscheduler (2),
538 .BR sched_setparam (2),
539 .BR sched_setattr (2));
540 .IP *
541 set CPU affinity for arbitrary processes
542 .RB ( sched_setaffinity (2));
543 .IP *
544 set I/O scheduling class and priority for arbitrary processes
545 .RB ( ioprio_set (2));
546 .IP *
547 apply
548 .BR migrate_pages (2)
549 to arbitrary processes and allow processes
550 to be migrated to arbitrary nodes;
551 .\" FIXME CAP_SYS_NICE also has the following effect for
552 .\" migrate_pages(2):
553 .\" do_migrate_pages(mm, &old, &new,
554 .\" capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
555 .\"
556 .\" Document this.
557 .IP *
558 apply
559 .BR move_pages (2)
560 to arbitrary processes;
561 .IP *
562 use the
563 .B MPOL_MF_MOVE_ALL
564 flag with
565 .BR mbind (2)
566 and
567 .BR move_pages (2).
568 .RE
569 .PD
570 .TP
571 .B CAP_SYS_PACCT
572 Use
573 .BR acct (2).
574 .TP
575 .B CAP_SYS_PTRACE
576 .PD 0
577 .RS
578 .IP * 2
579 Trace arbitrary processes using
580 .BR ptrace (2);
581 .IP *
582 apply
583 .BR get_robust_list (2)
584 to arbitrary processes;
585 .IP *
586 transfer data to or from the memory of arbitrary processes using
587 .BR process_vm_readv (2)
588 and
589 .BR process_vm_writev (2);
590 .IP *
591 inspect processes using
592 .BR kcmp (2).
593 .RE
594 .PD
595 .TP
596 .B CAP_SYS_RAWIO
597 .PD 0
598 .RS
599 .IP * 2
600 Perform I/O port operations
601 .RB ( iopl (2)
602 and
603 .BR ioperm (2));
604 .IP *
605 access
606 .IR /proc/kcore ;
607 .IP *
608 employ the
609 .B FIBMAP
610 .BR ioctl (2)
611 operation;
612 .IP *
613 open devices for accessing x86 model-specific registers (MSRs, see
614 .BR msr (4));
615 .IP *
616 update
617 .IR /proc/sys/vm/mmap_min_addr ;
618 .IP *
619 create memory mappings at addresses below the value specified by
620 .IR /proc/sys/vm/mmap_min_addr ;
621 .IP *
622 map files in
623 .IR /proc/bus/pci ;
624 .IP *
625 open
626 .IR /dev/mem
627 and
628 .IR /dev/kmem ;
629 .IP *
630 perform various SCSI device commands;
631 .IP *
632 perform certain operations on
633 .BR hpsa (4)
634 and
635 .BR cciss (4)
636 devices;
637 .IP *
638 perform a range of device-specific operations on other devices.
639 .RE
640 .PD
641 .TP
642 .B CAP_SYS_RESOURCE
643 .PD 0
644 .RS
645 .IP * 2
646 Use reserved space on ext2 filesystems;
647 .IP *
648 make
649 .BR ioctl (2)
650 calls controlling ext3 journaling;
651 .IP *
652 override disk quota limits;
653 .IP *
654 increase resource limits (see
655 .BR setrlimit (2));
656 .IP *
657 override
658 .B RLIMIT_NPROC
659 resource limit;
660 .IP *
661 override maximum number of consoles on console allocation;
662 .IP *
663 override maximum number of keymaps;
664 .IP *
665 allow more than 64hz interrupts from the real-time clock;
666 .IP *
667 raise
668 .I msg_qbytes
669 limit for a System V message queue above the limit in
670 .I /proc/sys/kernel/msgmnb
671 (see
672 .BR msgop (2)
673 and
674 .BR msgctl (2));
675 .IP *
676 allow the
677 .B RLIMIT_NOFILE
678 resource limit on the number of "in-flight" file descriptors
679 to be bypassed when passing file descriptors to another process
680 via a UNIX domain socket (see
681 .BR unix (7));
682 .IP *
683 override the
684 .I /proc/sys/fs/pipe-size-max
685 limit when setting the capacity of a pipe using the
686 .B F_SETPIPE_SZ
687 .BR fcntl (2)
688 command;
689 .IP *
690 use
691 .BR F_SETPIPE_SZ
692 to increase the capacity of a pipe above the limit specified by
693 .IR /proc/sys/fs/pipe-max-size ;
694 .IP *
695 override
696 .I /proc/sys/fs/mqueue/queues_max
697 limit when creating POSIX message queues (see
698 .BR mq_overview (7));
699 .IP *
700 employ the
701 .BR prctl (2)
702 .B PR_SET_MM
703 operation;
704 .IP *
705 set
706 .IR /proc/[pid]/oom_score_adj
707 to a value lower than the value last set by a process with
708 .BR CAP_SYS_RESOURCE .
709 .RE
710 .PD
711 .TP
712 .B CAP_SYS_TIME
713 Set system clock
714 .RB ( settimeofday (2),
715 .BR stime (2),
716 .BR adjtimex (2));
717 set real-time (hardware) clock.
718 .TP
719 .B CAP_SYS_TTY_CONFIG
720 Use
721 .BR vhangup (2);
722 employ various privileged
723 .BR ioctl (2)
724 operations on virtual terminals.
725 .TP
726 .BR CAP_SYSLOG " (since Linux 2.6.37)"
727 .RS
728 .PD 0
729 .IP * 2
730 Perform privileged
731 .BR syslog (2)
732 operations.
733 See
734 .BR syslog (2)
735 for information on which operations require privilege.
736 .IP *
737 View kernel addresses exposed via
738 .I /proc
739 and other interfaces when
740 .IR /proc/sys/kernel/kptr_restrict
741 has the value 1.
742 (See the discussion of the
743 .I kptr_restrict
744 in
745 .BR proc (5).)
746 .PD
747 .RE
748 .TP
749 .BR CAP_WAKE_ALARM " (since Linux 3.0)"
750 Trigger something that will wake up the system (set
751 .B CLOCK_REALTIME_ALARM
752 and
753 .B CLOCK_BOOTTIME_ALARM
754 timers).
755 .\"
756 .SS Past and current implementation
757 A full implementation of capabilities requires that:
758 .IP 1. 3
759 For all privileged operations,
760 the kernel must check whether the thread has the required
761 capability in its effective set.
762 .IP 2.
763 The kernel must provide system calls allowing a thread's capability sets to
764 be changed and retrieved.
765 .IP 3.
766 The filesystem must support attaching capabilities to an executable file,
767 so that a process gains those capabilities when the file is executed.
768 .PP
769 Before kernel 2.6.24, only the first two of these requirements are met;
770 since kernel 2.6.24, all three requirements are met.
771 .\"
772 .SS Notes to kernel developers
773 When adding a new kernel feature that should be governed by a capability,
774 consider the following points.
775 .IP * 3
776 The goal of capabilities is divide the power of superuser into pieces,
777 such that if a program that has one or more capabilities is compromised,
778 its power to do damage to the system would be less than the same program
779 running with root privilege.
780 .IP *
781 You have the choice of either creating a new capability for your new feature,
782 or associating the feature with one of the existing capabilities.
783 In order to keep the set of capabilities to a manageable size,
784 the latter option is preferable,
785 unless there are compelling reasons to take the former option.
786 (There is also a technical limit:
787 the size of capability sets is currently limited to 64 bits.)
788 .IP *
789 To determine which existing capability might best be associated
790 with your new feature, review the list of capabilities above in order
791 to find a "silo" into which your new feature best fits.
792 One approach to take is to determine if there are other features
793 requiring capabilities that will always be used along with the new feature.
794 If the new feature is useless without these other features,
795 you should use the same capability as the other features.
796 .IP *
797 .IR Don't
798 choose
799 .B CAP_SYS_ADMIN
800 if you can possibly avoid it!
801 A vast proportion of existing capability checks are associated
802 with this capability (see the partial list above).
803 It can plausibly be called "the new root",
804 since on the one hand, it confers a wide range of powers,
805 and on the other hand,
806 its broad scope means that this is the capability
807 that is required by many privileged programs.
808 Don't make the problem worse.
809 The only new features that should be associated with
810 .B CAP_SYS_ADMIN
811 are ones that
812 .I closely
813 match existing uses in that silo.
814 .IP *
815 If you have determined that it really is necessary to create
816 a new capability for your feature,
817 don't make or name it as a "single-use" capability.
818 Thus, for example, the addition of the highly specific
819 .BR CAP_SYS_PACCT
820 was probably a mistake.
821 Instead, try to identify and name your new capability as a broader
822 silo into which other related future use cases might fit.
823 .\"
824 .SS Thread capability sets
825 Each thread has the following capability sets containing zero or more
826 of the above capabilities:
827 .TP
828 .IR Permitted
829 This is a limiting superset for the effective
830 capabilities that the thread may assume.
831 It is also a limiting superset for the capabilities that
832 may be added to the inheritable set by a thread that does not have the
833 .B CAP_SETPCAP
834 capability in its effective set.
835 .IP
836 If a thread drops a capability from its permitted set,
837 it can never reacquire that capability (unless it
838 .BR execve (2)s
839 either a set-user-ID-root program, or
840 a program whose associated file capabilities grant that capability).
841 .TP
842 .IR Inheritable
843 This is a set of capabilities preserved across an
844 .BR execve (2).
845 Inheritable capabilities remain inheritable when executing any program,
846 and inheritable capabilities are added to the permitted set when executing
847 a program that has the corresponding bits set in the file inheritable set.
848 .IP
849 Because inheritable capabilities are not generally preserved across
850 .BR execve (2)
851 when running as a non-root user, applications that wish to run helper
852 programs with elevated capabilities should consider using
853 ambient capabilities, described below.
854 .TP
855 .IR Effective
856 This is the set of capabilities used by the kernel to
857 perform permission checks for the thread.
858 .TP
859 .IR Bounding " (per-thread since Linux 2.6.25)"
860 The capability bounding set is a mechanism that can be used
861 to limit the capabilities that are gained during
862 .BR execve (2).
863 .IP
864 Since Linux 2.6.25, this is a per-thread capability set.
865 In older kernels, the capability bounding set was a system wide attribute
866 shared by all threads on the system.
867 .IP
868 .IP
869 For more details on the capability bounding set, see below.
870 .TP
871 .IR Ambient " (since Linux 4.3)"
872 .\" commit 58319057b7847667f0c9585b9de0e8932b0fdb08
873 This is a set of capabilities that are preserved across an
874 .BR execve (2)
875 of a program that is not privileged.
876 The ambient capability set obeys the invariant that no capability
877 can ever be ambient if it is not both permitted and inheritable.
878 .IP
879 The ambient capability set can be directly modified using
880 .BR prctl (2).
881 Ambient capabilities are automatically lowered if either of
882 the corresponding permitted or inheritable capabilities is lowered.
883 .IP
884 Executing a program that changes UID or GID due to the
885 set-user-ID or set-group-ID bits or executing a program that has
886 any file capabilities set will clear the ambient set.
887 Ambient capabilities are added to the permitted set and
888 assigned to the effective set when
889 .BR execve (2)
890 is called.
891 If ambient capabilities cause a process's permitted and effective
892 capabilities to increase during an
893 .BR execve (2),
894 this does not trigger the secure-execution mode described in
895 .BR ld.so (8).
896 .PP
897 A child created via
898 .BR fork (2)
899 inherits copies of its parent's capability sets.
900 See below for a discussion of the treatment of capabilities during
901 .BR execve (2).
902 .PP
903 Using
904 .BR capset (2),
905 a thread may manipulate its own capability sets (see below).
906 .PP
907 Since Linux 3.2, the file
908 .I /proc/sys/kernel/cap_last_cap
909 .\" commit 73efc0394e148d0e15583e13712637831f926720
910 exposes the numerical value of the highest capability
911 supported by the running kernel;
912 this can be used to determine the highest bit
913 that may be set in a capability set.
914 .\"
915 .SS File capabilities
916 Since kernel 2.6.24, the kernel supports
917 associating capability sets with an executable file using
918 .BR setcap (8).
919 The file capability sets are stored in an extended attribute (see
920 .BR setxattr (2)
921 and
922 .BR xattr (7))
923 named
924 .IR "security.capability" .
925 Writing to this extended attribute requires the
926 .BR CAP_SETFCAP
927 capability.
928 The file capability sets,
929 in conjunction with the capability sets of the thread,
930 determine the capabilities of a thread after an
931 .BR execve (2).
932 .PP
933 The three file capability sets are:
934 .TP
935 .IR Permitted " (formerly known as " forced ):
936 These capabilities are automatically permitted to the thread,
937 regardless of the thread's inheritable capabilities.
938 .TP
939 .IR Inheritable " (formerly known as " allowed ):
940 This set is ANDed with the thread's inheritable set to determine which
941 inheritable capabilities are enabled in the permitted set of
942 the thread after the
943 .BR execve (2).
944 .TP
945 .IR Effective :
946 This is not a set, but rather just a single bit.
947 If this bit is set, then during an
948 .BR execve (2)
949 all of the new permitted capabilities for the thread are
950 also raised in the effective set.
951 If this bit is not set, then after an
952 .BR execve (2),
953 none of the new permitted capabilities is in the new effective set.
954 .IP
955 Enabling the file effective capability bit implies
956 that any file permitted or inheritable capability that causes a
957 thread to acquire the corresponding permitted capability during an
958 .BR execve (2)
959 (see the transformation rules described below) will also acquire that
960 capability in its effective set.
961 Therefore, when assigning capabilities to a file
962 .RB ( setcap (8),
963 .BR cap_set_file (3),
964 .BR cap_set_fd (3)),
965 if we specify the effective flag as being enabled for any capability,
966 then the effective flag must also be specified as enabled
967 for all other capabilities for which the corresponding permitted or
968 inheritable flags is enabled.
969 .\"
970 .SS File capability extended attribute versioning
971 To allow extensibility,
972 the kernel supports a scheme to encode a version number inside the
973 .I security.capability
974 extended attribute that is used to implement file capabilities.
975 These version numbers are internal to the implementation,
976 and not directly visible to user-space applications.
977 To date, the following versions are supported:
978 .TP
979 .BR VFS_CAP_REVISION_1
980 This was the original file capability implementation,
981 which supported 32-bit masks for file capabilities.
982 .TP
983 .BR VFS_CAP_REVISION_2 " (since Linux 2.6.25)"
984 .\" commit e338d263a76af78fe8f38a72131188b58fceb591
985 This version allows for file capability masks that are 64 bits in size,
986 and was necessary as the number of supported capabilities grew beyond 32.
987 The kernel transparently continues to support the execution of files
988 that have 32-bit version 1 capability masks,
989 but when adding capabilities to files that did not previously
990 have capabilities, or modifying the capabilities of existing files,
991 it automatically uses the version 2 scheme
992 (or possibly the version 3 scheme, as described below).
993 .TP
994 .BR VFS_CAP_REVISION_3 " (since Linux 4.14)"
995 .\" commit 8db6c34f1dbc8e06aa016a9b829b06902c3e1340
996 Version 3 file capabilities are provided
997 to support namespaced file capabilities (described below).
998 .IP
999 As with version 2 file capabilities,
1000 version 3 capability masks are 64 bits in size.
1001 But in addition, the root user ID of namespace is encoded in the
1002 .I security.capability
1003 extended attribute.
1004 (A namespace's root user ID is the value that user ID 0
1005 inside that namespace maps to in the initial user namespace.)
1006 .IP
1007 Version 3 file capabilities are designed to coexist
1008 with version 2 capabilities;
1009 that is, on a modern Linux system,
1010 there may be some files with version 2 capabilities
1011 while others have version 3 capabilities.
1012 .PP
1013 Before Linux 4.14,
1014 the only kind of file capability extended attribute
1015 that could be attached to a file was a
1016 .B VFS_CAP_REVISION_2
1017 attribute.
1018 Since Linux 4.14,
1019 the version of the
1020 .I security.capability
1021 extended attribute that is attached to a file
1022 depends on the circumstances in which the attribute was created.
1023 .PP
1024 Starting with Linux 4.14, a
1025 .I security.capability
1026 extended attribute is automatically created as (or converted to)
1027 a version 3
1028 .RB ( VFS_CAP_REVISION_3 )
1029 attribute if both of the following are true:
1030 .IP (1) 4
1031 The thread writing the attribute resides in a noninitial user namespace.
1032 (More precisely: the thread resides in a user namespace other
1033 than the one from which the underlying filesystem was mounted.)
1034 .IP (2)
1035 The thread has the
1036 .BR CAP_SETFCAP
1037 capability over the file inode,
1038 meaning that (a) the thread has the
1039 .B CAP_SETFCAP
1040 capability in its own user namespace;
1041 and (b) the UID and GID of the file inode have mappings in
1042 the writer's user namespace.
1043 .PP
1044 When a
1045 .BR VFS_CAP_REVISION_3
1046 .I security.capability
1047 extended attribute is created, the root user ID of the creating thread's
1048 user namespace is saved in the extended attribute.
1049 .PP
1050 By contrast, creating or modifying a
1051 .I security.capability
1052 extended attribute from a privileged
1053 .RB ( CAP_SETFCAP )
1054 thread that resides in the
1055 namespace where the underlying filesystem was mounted
1056 (this normally means the initial user namespace)
1057 automatically results in the creation of a version 2
1058 .RB ( VFS_CAP_REVISION_2 )
1059 attribute.
1060 .PP
1061 Note that the creation of a version 3
1062 .I security.capability
1063 extended attribute is automatic.
1064 That is to say, when a user-space application writes
1065 .RB ( setxattr (2))
1066 a
1067 .I security.capability
1068 attribute in the version 2 format,
1069 the kernel will automatically create a version 3 attribute
1070 if the attribute is created in the circumstances described above.
1071 Correspondingly, when a version 3
1072 .I security.capability
1073 attribute is retrieved
1074 .RB ( getxattr (2))
1075 by a process that resides inside a user namespace that was created by the
1076 root user ID (or a descendant of that user namespace),
1077 the returned attribute is (automatically)
1078 simplified to appear as a version 2 attribute
1079 (i.e., the returned value is the size of a version 2 attribute and does
1080 not include the root user ID).
1081 These automatic translations mean that no changes are required to
1082 user-space tools (e.g.,
1083 .BR setcap (1)
1084 and
1085 .BR getcap (1))
1086 in order for those tools to be used to create and retrieve version 3
1087 .I security.capability
1088 attributes.
1089 .PP
1090 Note that a file can have either a version 2 or a version 3
1091 .I security.capability
1092 extended attribute associated with it, but not both:
1093 creation or modification of the
1094 .I security.capability
1095 extended attribute will automatically modify the version
1096 according to the circumstances in which the extended attribute is
1097 created or modified.
1098 .\"
1099 .SS Transformation of capabilities during execve()
1100 .PP
1101 During an
1102 .BR execve (2),
1103 the kernel calculates the new capabilities of
1104 the process using the following algorithm:
1105 .PP
1106 .in +4n
1107 .EX
1108 P'(ambient) = (file is privileged) ? 0 : P(ambient)
1109
1110 P'(permitted) = (P(inheritable) & F(inheritable)) |
1111 (F(permitted) & P(bounding)) | P'(ambient)
1112
1113 P'(effective) = F(effective) ? P'(permitted) : P'(ambient)
1114
1115 P'(inheritable) = P(inheritable) [i.e., unchanged]
1116
1117 P'(bounding) = P(bounding) [i.e., unchanged]
1118 .EE
1119 .in
1120 .PP
1121 where:
1122 .RS 4
1123 .IP P() 6
1124 denotes the value of a thread capability set before the
1125 .BR execve (2)
1126 .IP P'()
1127 denotes the value of a thread capability set after the
1128 .BR execve (2)
1129 .IP F()
1130 denotes a file capability set
1131 .RE
1132 .PP
1133 Note the following details relating to the above capability
1134 transformation rules:
1135 .IP * 3
1136 The ambient capability set is present only since Linux 4.3.
1137 When determining the transformation of the ambient set during
1138 .BR execve (2),
1139 a privileged file is one that has capabilities or
1140 has the set-user-ID or set-group-ID bit set.
1141 .IP *
1142 Prior to Linux 2.6.25,
1143 the bounding set was a system-wide attribute shared by all threads.
1144 That system-wide value was employed to calculate the new permitted set during
1145 .BR execve (2)
1146 in the same manner as shown above for
1147 .IR P(bounding) .
1148 .PP
1149 .IR Note :
1150 during the capability transitions described above,
1151 file capabilities may be ignored (treated as empty) for the same reasons
1152 that the set-user-ID and set-group-ID bits are ignored; see
1153 .BR execve (2).
1154 File capabilities are similarly ignored if the kernel was booted with the
1155 .I no_file_caps
1156 option.
1157 .PP
1158 .IR Note :
1159 according to the rules above,
1160 if a process with nonzero user IDs performs an
1161 .BR execve (2)
1162 then any capabilities that are present in
1163 its permitted and effective sets will be cleared.
1164 For the treatment of capabilities when a process with a
1165 user ID of zero performs an
1166 .BR execve (2),
1167 see below under
1168 .IR "Capabilities and execution of programs by root" .
1169 .\"
1170 .SS Safety checking for capability-dumb binaries
1171 A capability-dumb binary is an application that has been
1172 marked to have file capabilities, but has not been converted to use the
1173 .BR libcap (3)
1174 API to manipulate its capabilities.
1175 (In other words, this is a traditional set-user-ID-root program
1176 that has been switched to use file capabilities,
1177 but whose code has not been modified to understand capabilities.)
1178 For such applications,
1179 the effective capability bit is set on the file,
1180 so that the file permitted capabilities are automatically
1181 enabled in the process effective set when executing the file.
1182 The kernel recognizes a file which has the effective capability bit set
1183 as capability-dumb for the purpose of the check described here.
1184 .PP
1185 When executing a capability-dumb binary,
1186 the kernel checks if the process obtained all permitted capabilities
1187 that were specified in the file permitted set,
1188 after the capability transformations described above have been performed.
1189 (The typical reason why this might
1190 .I not
1191 occur is that the capability bounding set masked out some
1192 of the capabilities in the file permitted set.)
1193 If the process did not obtain the full set of
1194 file permitted capabilities, then
1195 .BR execve (2)
1196 fails with the error
1197 .BR EPERM .
1198 This prevents possible security risks that could arise when
1199 a capability-dumb application is executed with less privilege that it needs.
1200 Note that, by definition,
1201 the application could not itself recognize this problem,
1202 since it does not employ the
1203 .BR libcap (3)
1204 API.
1205 .\"
1206 .SS Capabilities and execution of programs by root
1207 .\" See cap_bprm_set_creds(), bprm_caps_from_vfs_cap() and
1208 .\" handle_privileged_root() in security/commoncap.c (Linux 5.0 source)
1209 In order to mirror traditional UNIX semantics,
1210 the kernel performs special treatment of file capabilities when
1211 a process with UID 0 (root) executes a program and
1212 when a set-user-ID-root program is executed.
1213 .PP
1214 After having performed any changes to the process effective ID that
1215 were triggered by the set-user-ID mode bit of the binary\(eme.g.,
1216 switching the effective user ID to 0 (root) because
1217 a set-user-ID-root program was executed\(emthe
1218 kernel calculates the file capability sets as follows:
1219 .IP 1. 3
1220 If the real or effective user ID of the process is 0 (root),
1221 then the file inheritable and permitted sets are ignored;
1222 instead they are notionally considered to be all ones
1223 (i.e., all capabilities enabled).
1224 (There is one exception to this behavior, described below in
1225 .IR "Set-user-ID-root programs that have file capabilities" .)
1226 .IP 2.
1227 If the effective user ID of the process is 0 (root) or
1228 the file effective bit is in fact enabled,
1229 then the file effective bit is notionally defined to be one (enabled).
1230 .PP
1231 These notional values for the file's capability sets are then used
1232 as described above to calculate the transformation of the process's
1233 capabilities during
1234 .BR execve (2).
1235 .PP
1236 Thus, when a process with nonzero UIDs
1237 .BR execve (2)s
1238 a set-user-ID-root program that does not have capabilities attached,
1239 or when a process whose real and effective UIDs are zero
1240 .BR execve (2)s
1241 a program, the calculation of the process's new
1242 permitted capabilities simplifies to:
1243 .PP
1244 .in +4n
1245 .EX
1246 P'(permitted) = P(inheritable) | P(bounding)
1247
1248 P'(effective) = P'(permitted)
1249 .EE
1250 .in
1251 .PP
1252 Consequently, the process gains all capabilities in its permitted and
1253 effective capability sets,
1254 except those masked out by the capability bounding set.
1255 (In the calculation of P'(permitted),
1256 the P'(ambient) term can be simplified away because it is by
1257 definition a proper subset of P(inheritable).)
1258 .PP
1259 The special treatments of user ID 0 (root) described in this subsection
1260 can be disabled using the securebits mechanism described below.
1261 .\"
1262 .\"
1263 .SS Set-user-ID-root programs that have file capabilities
1264 There is one exception to the behavior described under
1265 .IR "Capabilities and execution of programs by root" .
1266 If (a) the binary that is being executed has capabilities attached and
1267 (b) the real user ID of the process is
1268 .I not
1269 0 (root) and
1270 (c) the effective user ID of the process
1271 .I is
1272 0 (root), then the file capability bits are honored
1273 (i.e., they are not notionally considered to be all ones).
1274 The usual way in which this situation can arise is when executing
1275 a set-UID-root program that also has file capabilities.
1276 When such a program is executed,
1277 the process gains just the capabilities granted by the program
1278 (i.e., not all capabilities,
1279 as would occur when executing a set-user-ID-root program
1280 that does not have any associated file capabilities).
1281 .PP
1282 Note that one can assign empty capability sets to a program file,
1283 and thus it is possible to create a set-user-ID-root program that
1284 changes the effective and saved set-user-ID of the process
1285 that executes the program to 0,
1286 but confers no capabilities to that process.
1287 .\"
1288 .SS Capability bounding set
1289 The capability bounding set is a security mechanism that can be used
1290 to limit the capabilities that can be gained during an
1291 .BR execve (2).
1292 The bounding set is used in the following ways:
1293 .IP * 2
1294 During an
1295 .BR execve (2),
1296 the capability bounding set is ANDed with the file permitted
1297 capability set, and the result of this operation is assigned to the
1298 thread's permitted capability set.
1299 The capability bounding set thus places a limit on the permitted
1300 capabilities that may be granted by an executable file.
1301 .IP *
1302 (Since Linux 2.6.25)
1303 The capability bounding set acts as a limiting superset for
1304 the capabilities that a thread can add to its inheritable set using
1305 .BR capset (2).
1306 This means that if a capability is not in the bounding set,
1307 then a thread can't add this capability to its
1308 inheritable set, even if it was in its permitted capabilities,
1309 and thereby cannot have this capability preserved in its
1310 permitted set when it
1311 .BR execve (2)s
1312 a file that has the capability in its inheritable set.
1313 .PP
1314 Note that the bounding set masks the file permitted capabilities,
1315 but not the inheritable capabilities.
1316 If a thread maintains a capability in its inheritable set
1317 that is not in its bounding set,
1318 then it can still gain that capability in its permitted set
1319 by executing a file that has the capability in its inheritable set.
1320 .PP
1321 Depending on the kernel version, the capability bounding set is either
1322 a system-wide attribute, or a per-process attribute.
1323 .PP
1324 .B "Capability bounding set from Linux 2.6.25 onward"
1325 .PP
1326 From Linux 2.6.25, the
1327 .I "capability bounding set"
1328 is a per-thread attribute.
1329 (The system-wide capability bounding set described below no longer exists.)
1330 .PP
1331 The bounding set is inherited at
1332 .BR fork (2)
1333 from the thread's parent, and is preserved across an
1334 .BR execve (2).
1335 .PP
1336 A thread may remove capabilities from its capability bounding set using the
1337 .BR prctl (2)
1338 .B PR_CAPBSET_DROP
1339 operation, provided it has the
1340 .B CAP_SETPCAP
1341 capability.
1342 Once a capability has been dropped from the bounding set,
1343 it cannot be restored to that set.
1344 A thread can determine if a capability is in its bounding set using the
1345 .BR prctl (2)
1346 .B PR_CAPBSET_READ
1347 operation.
1348 .PP
1349 Removing capabilities from the bounding set is supported only if file
1350 capabilities are compiled into the kernel.
1351 In kernels before Linux 2.6.33,
1352 file capabilities were an optional feature configurable via the
1353 .B CONFIG_SECURITY_FILE_CAPABILITIES
1354 option.
1355 Since Linux 2.6.33,
1356 .\" commit b3a222e52e4d4be77cc4520a57af1a4a0d8222d1
1357 the configuration option has been removed
1358 and file capabilities are always part of the kernel.
1359 When file capabilities are compiled into the kernel, the
1360 .B init
1361 process (the ancestor of all processes) begins with a full bounding set.
1362 If file capabilities are not compiled into the kernel, then
1363 .B init
1364 begins with a full bounding set minus
1365 .BR CAP_SETPCAP ,
1366 because this capability has a different meaning when there are
1367 no file capabilities.
1368 .PP
1369 Removing a capability from the bounding set does not remove it
1370 from the thread's inheritable set.
1371 However it does prevent the capability from being added
1372 back into the thread's inheritable set in the future.
1373 .PP
1374 .B "Capability bounding set prior to Linux 2.6.25"
1375 .PP
1376 In kernels before 2.6.25, the capability bounding set is a system-wide
1377 attribute that affects all threads on the system.
1378 The bounding set is accessible via the file
1379 .IR /proc/sys/kernel/cap-bound .
1380 (Confusingly, this bit mask parameter is expressed as a
1381 signed decimal number in
1382 .IR /proc/sys/kernel/cap-bound .)
1383 .PP
1384 Only the
1385 .B init
1386 process may set capabilities in the capability bounding set;
1387 other than that, the superuser (more precisely: a process with the
1388 .B CAP_SYS_MODULE
1389 capability) may only clear capabilities from this set.
1390 .PP
1391 On a standard system the capability bounding set always masks out the
1392 .B CAP_SETPCAP
1393 capability.
1394 To remove this restriction (dangerous!), modify the definition of
1395 .B CAP_INIT_EFF_SET
1396 in
1397 .I include/linux/capability.h
1398 and rebuild the kernel.
1399 .PP
1400 The system-wide capability bounding set feature was added
1401 to Linux starting with kernel version 2.2.11.
1402 .\"
1403 .\"
1404 .\"
1405 .SS Effect of user ID changes on capabilities
1406 To preserve the traditional semantics for transitions between
1407 0 and nonzero user IDs,
1408 the kernel makes the following changes to a thread's capability
1409 sets on changes to the thread's real, effective, saved set,
1410 and filesystem user IDs (using
1411 .BR setuid (2),
1412 .BR setresuid (2),
1413 or similar):
1414 .IP 1. 3
1415 If one or more of the real, effective or saved set user IDs
1416 was previously 0, and as a result of the UID changes all of these IDs
1417 have a nonzero value,
1418 then all capabilities are cleared from the permitted, effective, and ambient
1419 capability sets.
1420 .IP 2.
1421 If the effective user ID is changed from 0 to nonzero,
1422 then all capabilities are cleared from the effective set.
1423 .IP 3.
1424 If the effective user ID is changed from nonzero to 0,
1425 then the permitted set is copied to the effective set.
1426 .IP 4.
1427 If the filesystem user ID is changed from 0 to nonzero (see
1428 .BR setfsuid (2)),
1429 then the following capabilities are cleared from the effective set:
1430 .BR CAP_CHOWN ,
1431 .BR CAP_DAC_OVERRIDE ,
1432 .BR CAP_DAC_READ_SEARCH ,
1433 .BR CAP_FOWNER ,
1434 .BR CAP_FSETID ,
1435 .B CAP_LINUX_IMMUTABLE
1436 (since Linux 2.6.30),
1437 .BR CAP_MAC_OVERRIDE ,
1438 and
1439 .B CAP_MKNOD
1440 (since Linux 2.6.30).
1441 If the filesystem UID is changed from nonzero to 0,
1442 then any of these capabilities that are enabled in the permitted set
1443 are enabled in the effective set.
1444 .PP
1445 If a thread that has a 0 value for one or more of its user IDs wants
1446 to prevent its permitted capability set being cleared when it resets
1447 all of its user IDs to nonzero values, it can do so using the
1448 .B SECBIT_KEEP_CAPS
1449 securebits flag described below.
1450 .\"
1451 .SS Programmatically adjusting capability sets
1452 A thread can retrieve and change its permitted, effective, and inheritable
1453 capability sets using the
1454 .BR capget (2)
1455 and
1456 .BR capset (2)
1457 system calls.
1458 However, the use of
1459 .BR cap_get_proc (3)
1460 and
1461 .BR cap_set_proc (3),
1462 both provided in the
1463 .I libcap
1464 package,
1465 is preferred for this purpose.
1466 The following rules govern changes to the thread capability sets:
1467 .IP 1. 3
1468 If the caller does not have the
1469 .B CAP_SETPCAP
1470 capability,
1471 the new inheritable set must be a subset of the combination
1472 of the existing inheritable and permitted sets.
1473 .IP 2.
1474 (Since Linux 2.6.25)
1475 The new inheritable set must be a subset of the combination of the
1476 existing inheritable set and the capability bounding set.
1477 .IP 3.
1478 The new permitted set must be a subset of the existing permitted set
1479 (i.e., it is not possible to acquire permitted capabilities
1480 that the thread does not currently have).
1481 .IP 4.
1482 The new effective set must be a subset of the new permitted set.
1483 .SS The securebits flags: establishing a capabilities-only environment
1484 .\" For some background:
1485 .\" see http://lwn.net/Articles/280279/ and
1486 .\" http://article.gmane.org/gmane.linux.kernel.lsm/5476/
1487 Starting with kernel 2.6.26,
1488 and with a kernel in which file capabilities are enabled,
1489 Linux implements a set of per-thread
1490 .I securebits
1491 flags that can be used to disable special handling of capabilities for UID 0
1492 .RI ( root ).
1493 These flags are as follows:
1494 .TP
1495 .B SECBIT_KEEP_CAPS
1496 Setting this flag allows a thread that has one or more 0 UIDs to retain
1497 capabilities in its permitted set
1498 when it switches all of its UIDs to nonzero values.
1499 If this flag is not set,
1500 then such a UID switch causes the thread to lose all permitted capabilities.
1501 This flag is always cleared on an
1502 .BR execve (2).
1503 .IP
1504 Note that even with the
1505 .B SECBIT_KEEP_CAPS
1506 flag set, the effective capabilities of a thread are cleared when it
1507 switches its effective UID to a nonzero value.
1508 However,
1509 if the thread has set this flag and its effective UID is already nonzero,
1510 and the thread subsequently switches all other UIDs to nonzero values,
1511 then the effective capabilities will not be cleared.
1512 .IP
1513 The setting of the
1514 .B SECBIT_KEEP_CAPS
1515 flag is ignored if the
1516 .B SECBIT_NO_SETUID_FIXUP
1517 flag is set.
1518 (The latter flag provides a superset of the effect of the former flag.)
1519 .IP
1520 This flag provides the same functionality as the older
1521 .BR prctl (2)
1522 .B PR_SET_KEEPCAPS
1523 operation.
1524 .TP
1525 .B SECBIT_NO_SETUID_FIXUP
1526 Setting this flag stops the kernel from adjusting the process's
1527 permitted, effective, and ambient capability sets when
1528 the thread's effective and filesystem UIDs are switched between
1529 zero and nonzero values.
1530 (See the subsection
1531 .IR "Effect of user ID changes on capabilities" .)
1532 .TP
1533 .B SECBIT_NOROOT
1534 If this bit is set, then the kernel does not grant capabilities
1535 when a set-user-ID-root program is executed, or when a process with
1536 an effective or real UID of 0 calls
1537 .BR execve (2).
1538 (See the subsection
1539 .IR "Capabilities and execution of programs by root" .)
1540 .TP
1541 .B SECBIT_NO_CAP_AMBIENT_RAISE
1542 Setting this flag disallows raising ambient capabilities via the
1543 .BR prctl (2)
1544 .BR PR_CAP_AMBIENT_RAISE
1545 operation.
1546 .PP
1547 Each of the above "base" flags has a companion "locked" flag.
1548 Setting any of the "locked" flags is irreversible,
1549 and has the effect of preventing further changes to the
1550 corresponding "base" flag.
1551 The locked flags are:
1552 .BR SECBIT_KEEP_CAPS_LOCKED ,
1553 .BR SECBIT_NO_SETUID_FIXUP_LOCKED ,
1554 .BR SECBIT_NOROOT_LOCKED ,
1555 and
1556 .BR SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED .
1557 .PP
1558 The
1559 .I securebits
1560 flags can be modified and retrieved using the
1561 .BR prctl (2)
1562 .B PR_SET_SECUREBITS
1563 and
1564 .B PR_GET_SECUREBITS
1565 operations.
1566 The
1567 .B CAP_SETPCAP
1568 capability is required to modify the flags.
1569 Note that the
1570 .BR SECBIT_*
1571 constants are available only after including the
1572 .I <linux/securebits.h>
1573 header file.
1574 .PP
1575 The
1576 .I securebits
1577 flags are inherited by child processes.
1578 During an
1579 .BR execve (2),
1580 all of the flags are preserved, except
1581 .B SECBIT_KEEP_CAPS
1582 which is always cleared.
1583 .PP
1584 An application can use the following call to lock itself,
1585 and all of its descendants,
1586 into an environment where the only way of gaining capabilities
1587 is by executing a program with associated file capabilities:
1588 .PP
1589 .in +4n
1590 .EX
1591 prctl(PR_SET_SECUREBITS,
1592 /* SECBIT_KEEP_CAPS off */
1593 SECBIT_KEEP_CAPS_LOCKED |
1594 SECBIT_NO_SETUID_FIXUP |
1595 SECBIT_NO_SETUID_FIXUP_LOCKED |
1596 SECBIT_NOROOT |
1597 SECBIT_NOROOT_LOCKED);
1598 /* Setting/locking SECBIT_NO_CAP_AMBIENT_RAISE
1599 is not required */
1600 .EE
1601 .in
1602 .\"
1603 .\"
1604 .SS Per-user-namespace """set-user-ID-root""" programs
1605 A set-user-ID program whose UID matches the UID that
1606 created a user namespace will confer capabilities
1607 in the process's permitted and effective sets
1608 when executed by any process inside that namespace
1609 or any descendant user namespace.
1610 .PP
1611 The rules about the transformation of the process's capabilities during the
1612 .BR execve (2)
1613 are exactly as described in the subsections
1614 .IR "Transformation of capabilities during execve()"
1615 and
1616 .IR "Capabilities and execution of programs by root" ,
1617 with the difference that, in the latter subsection, "root"
1618 is the UID of the creator of the user namespace.
1619 .\"
1620 .\"
1621 .SS Namespaced file capabilities
1622 .\" commit 8db6c34f1dbc8e06aa016a9b829b06902c3e1340
1623 Traditional (i.e., version 2) file capabilities associate
1624 only a set of capability masks with a binary executable file.
1625 When a process executes a binary with such capabilities,
1626 it gains the associated capabilities (within its user namespace)
1627 as per the rules described above in
1628 "Transformation of capabilities during execve()".
1629 .PP
1630 Because version 2 file capabilities confer capabilities to
1631 the executing process regardless of which user namespace it resides in,
1632 only privileged processes are permitted to associate capabilities with a file.
1633 Here, "privileged" means a process that has the
1634 .BR CAP_SETFCAP
1635 capability in the user namespace where the filesystem was mounted
1636 (normally the initial user namespace).
1637 This limitation renders file capabilities useless for certain use cases.
1638 For example, in user-namespaced containers,
1639 it can be desirable to be able to create a binary that
1640 confers capabilities only to processes executed inside that container,
1641 but not to processes that are executed outside the container.
1642 .PP
1643 Linux 4.14 added so-called namespaced file capabilities
1644 to support such use cases.
1645 Namespaced file capabilities are recorded as version 3 (i.e.,
1646 .BR VFS_CAP_REVISION_3 )
1647 .I security.capability
1648 extended attributes.
1649 Such an attribute is automatically created in the circumstances described
1650 above under "File capability extended attribute versioning".
1651 When a version 3
1652 .I security.capability
1653 extended attribute is created,
1654 the kernel records not just the capability masks in the extended attribute,
1655 but also the namespace root user ID.
1656 .PP
1657 As with a binary that has
1658 .BR VFS_CAP_REVISION_2
1659 file capabilities, a binary with
1660 .BR VFS_CAP_REVISION_3
1661 file capabilities confers capabilities to a process during
1662 .BR execve ().
1663 However, capabilities are conferred only if the binary is executed by
1664 a process that resides in a user namespace whose
1665 UID 0 maps to the root user ID that is saved in the extended attribute,
1666 or when executed by a process that resides in a descendant of such a namespace.
1667 .\"
1668 .\"
1669 .SS Interaction with user namespaces
1670 For further information on the interaction of
1671 capabilities and user namespaces, see
1672 .BR user_namespaces (7).
1673 .SH CONFORMING TO
1674 .PP
1675 No standards govern capabilities, but the Linux capability implementation
1676 is based on the withdrawn POSIX.1e draft standard; see
1677 .UR https://archive.org\:/details\:/posix_1003.1e-990310
1678 .UE .
1679 .SH NOTES
1680 When attempting to
1681 .BR strace (1)
1682 binaries that have capabilities (or set-user-ID-root binaries),
1683 you may find the
1684 .I \-u <username>
1685 option useful.
1686 Something like:
1687 .PP
1688 .in +4n
1689 .EX
1690 $ \fBsudo strace \-o trace.log \-u ceci ./myprivprog\fP
1691 .EE
1692 .in
1693 .PP
1694 From kernel 2.5.27 to kernel 2.6.26,
1695 .\" commit 5915eb53861c5776cfec33ca4fcc1fd20d66dd27 removed
1696 .\" CONFIG_SECURITY_CAPABILITIES
1697 capabilities were an optional kernel component,
1698 and could be enabled/disabled via the
1699 .B CONFIG_SECURITY_CAPABILITIES
1700 kernel configuration option.
1701 .PP
1702 The
1703 .I /proc/[pid]/task/TID/status
1704 file can be used to view the capability sets of a thread.
1705 The
1706 .I /proc/[pid]/status
1707 file shows the capability sets of a process's main thread.
1708 Before Linux 3.8, nonexistent capabilities were shown as being
1709 enabled (1) in these sets.
1710 Since Linux 3.8,
1711 .\" 7b9a7ec565505699f503b4fcf61500dceb36e744
1712 all nonexistent capabilities (above
1713 .BR CAP_LAST_CAP )
1714 are shown as disabled (0).
1715 .PP
1716 The
1717 .I libcap
1718 package provides a suite of routines for setting and
1719 getting capabilities that is more comfortable and less likely
1720 to change than the interface provided by
1721 .BR capset (2)
1722 and
1723 .BR capget (2).
1724 This package also provides the
1725 .BR setcap (8)
1726 and
1727 .BR getcap (8)
1728 programs.
1729 It can be found at
1730 .br
1731 .UR https://git.kernel.org\:/pub\:/scm\:/libs\:/libcap\:/libcap.git\:/refs/
1732 .UE .
1733 .PP
1734 Before kernel 2.6.24, and from kernel 2.6.24 to kernel 2.6.32 if
1735 file capabilities are not enabled, a thread with the
1736 .B CAP_SETPCAP
1737 capability can manipulate the capabilities of threads other than itself.
1738 However, this is only theoretically possible,
1739 since no thread ever has
1740 .BR CAP_SETPCAP
1741 in either of these cases:
1742 .IP * 2
1743 In the pre-2.6.25 implementation the system-wide capability bounding set,
1744 .IR /proc/sys/kernel/cap-bound ,
1745 always masks out the
1746 .B CAP_SETPCAP
1747 capability, and this can not be changed
1748 without modifying the kernel source and rebuilding the kernel.
1749 .IP *
1750 If file capabilities are disabled (i.e., the kernel
1751 .B CONFIG_SECURITY_FILE_CAPABILITIES
1752 option is disabled), then
1753 .B init
1754 starts out with the
1755 .B CAP_SETPCAP
1756 capability removed from its per-process bounding
1757 set, and that bounding set is inherited by all other processes
1758 created on the system.
1759 .SH SEE ALSO
1760 .BR capsh (1),
1761 .BR setpriv (1),
1762 .BR prctl (2),
1763 .BR setfsuid (2),
1764 .BR cap_clear (3),
1765 .BR cap_copy_ext (3),
1766 .BR cap_from_text (3),
1767 .BR cap_get_file (3),
1768 .BR cap_get_proc (3),
1769 .BR cap_init (3),
1770 .BR capgetp (3),
1771 .BR capsetp (3),
1772 .BR libcap (3),
1773 .BR proc (5),
1774 .BR credentials (7),
1775 .BR pthreads (7),
1776 .BR user_namespaces (7),
1777 .BR captest (8), \" from libcap-ng
1778 .BR filecap (8), \" from libcap-ng
1779 .BR getcap (8),
1780 .BR netcap (8), \" from libcap-ng
1781 .BR pscap (8), \" from libcap-ng
1782 .BR setcap (8)
1783 .PP
1784 .I include/linux/capability.h
1785 in the Linux kernel source tree