OSDN Git Service

(split) LDP_man-pages: update original to v3.34.
[linuxjm/LDP_man-pages.git] / original / man7 / capabilities.7
1 .\" Copyright (c) 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" 6 Aug 2002 - Initial Creation
24 .\" Modified 2003-05-23, Michael Kerrisk, <mtk.manpages@gmail.com>
25 .\" Modified 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com>
26 .\" 2004-12-08, mtk Added O_NOATIME for CAP_FOWNER
27 .\" 2005-08-16, mtk, Added CAP_AUDIT_CONTROL and CAP_AUDIT_WRITE
28 .\" 2008-07-15, Serge Hallyn <serue@us.bbm.com>
29 .\"     Document file capabilities, per-process capability
30 .\"     bounding set, changed semantics for CAP_SETPCAP,
31 .\"     and other changes in 2.6.2[45].
32 .\"     Add CAP_MAC_ADMIN, CAP_MAC_OVERRIDE, CAP_SETFCAP.
33 .\" 2008-07-15, mtk
34 .\"     Add text describing circumstances in which CAP_SETPCAP
35 .\"     (theoretically) permits a thread to change the
36 .\"     capability sets of another thread.
37 .\"     Add section describing rules for programmatically
38 .\"     adjusting thread capability sets.
39 .\"     Describe rationale for capability bounding set.
40 .\"     Document "securebits" flags.
41 .\"     Add text noting that if we set the effective flag for one file
42 .\"     capability, then we must also set the effective flag for all
43 .\"     other capabilities where the permitted or inheritable bit is set.
44 .\" 2011-09-07, mtk/Serge hallyn: Add CAP_SYSLOG
45 .\" FIXME: Linux 3.0 added CAP_WAKE_ALARM
46 .\"
47 .TH CAPABILITIES 7 2011-09-07 "Linux" "Linux Programmer's Manual"
48 .SH NAME
49 capabilities \- overview of Linux capabilities
50 .SH DESCRIPTION
51 For the purpose of performing permission checks,
52 traditional UNIX implementations distinguish two categories of processes:
53 .I privileged
54 processes (whose effective user ID is 0, referred to as superuser or root),
55 and
56 .I unprivileged
57 processes (whose effective UID is nonzero).
58 Privileged processes bypass all kernel permission checks,
59 while unprivileged processes are subject to full permission
60 checking based on the process's credentials
61 (usually: effective UID, effective GID, and supplementary group list).
62
63 Starting with kernel 2.2, Linux divides the privileges traditionally
64 associated with superuser into distinct units, known as
65 .IR capabilities ,
66 which can be independently enabled and disabled.
67 Capabilities are a per-thread attribute.
68 .\"
69 .SS Capabilities List
70 The following list shows the capabilities implemented on Linux,
71 and the operations or behaviors that each capability permits:
72 .TP
73 .BR CAP_AUDIT_CONTROL " (since Linux 2.6.11)"
74 Enable and disable kernel auditing; change auditing filter rules;
75 retrieve auditing status and filtering rules.
76 .TP
77 .BR CAP_AUDIT_WRITE " (since Linux 2.6.11)"
78 Write records to kernel auditing log.
79 .TP
80 .B CAP_CHOWN
81 Make arbitrary changes to file UIDs and GIDs (see
82 .BR chown (2)).
83 .TP
84 .B CAP_DAC_OVERRIDE
85 Bypass file read, write, and execute permission checks.
86 (DAC is an abbreviation of "discretionary access control".)
87 .TP
88 .B CAP_DAC_READ_SEARCH
89 Bypass file read permission checks and
90 directory read and execute permission checks.
91 .TP
92 .B CAP_FOWNER
93 .PD 0
94 .RS
95 .IP * 2
96 Bypass permission checks on operations that normally
97 require the file system UID of the process to match the UID of
98 the file (e.g.,
99 .BR chmod (2),
100 .BR utime (2)),
101 excluding those operations covered by
102 .B CAP_DAC_OVERRIDE
103 and
104 .BR CAP_DAC_READ_SEARCH ;
105 .IP *
106 set extended file attributes (see
107 .BR chattr (1))
108 on arbitrary files;
109 .IP *
110 set Access Control Lists (ACLs) on arbitrary files;
111 .IP *
112 ignore directory sticky bit on file deletion;
113 .IP *
114 specify
115 .B O_NOATIME
116 for arbitrary files in
117 .BR open (2)
118 and
119 .BR fcntl (2).
120 .RE
121 .PD
122 .TP
123 .B CAP_FSETID
124 Don't clear set-user-ID and set-group-ID permission
125 bits when a file is modified;
126 set the set-group-ID bit for a file whose GID does not match
127 the file system or any of the supplementary GIDs of the calling process.
128 .TP
129 .B CAP_IPC_LOCK
130 Lock memory
131 .RB ( mlock (2),
132 .BR mlockall (2),
133 .BR mmap (2),
134 .BR shmctl (2)).
135 .TP
136 .B CAP_IPC_OWNER
137 Bypass permission checks for operations on System V IPC objects.
138 .TP
139 .B CAP_KILL
140 Bypass permission checks for sending signals (see
141 .BR kill (2)).
142 This includes use of the
143 .BR ioctl (2)
144 .B KDSIGACCEPT
145 operation.
146 .\" FIXME CAP_KILL also has an effect for threads + setting child
147 .\"       termination signal to other than SIGCHLD: without this
148 .\"       capability, the termination signal reverts to SIGCHLD
149 .\"       if the child does an exec().  What is the rationale
150 .\"       for this?
151 .TP
152 .BR CAP_LEASE " (since Linux 2.4)"
153 Establish leases on arbitrary files (see
154 .BR fcntl (2)).
155 .TP
156 .B CAP_LINUX_IMMUTABLE
157 Set the
158 .B FS_APPEND_FL
159 and
160 .B FS_IMMUTABLE_FL
161 .\" These attributes are now available on ext2, ext3, Reiserfs, XFS, JFS
162 i-node flags (see
163 .BR chattr (1)).
164 .TP
165 .BR CAP_MAC_ADMIN " (since Linux 2.6.25)"
166 Override Mandatory Access Control (MAC).
167 Implemented for the Smack Linux Security Module (LSM).
168 .TP
169 .BR CAP_MAC_OVERRIDE " (since Linux 2.6.25)"
170 Allow MAC configuration or state changes.
171 Implemented for the Smack LSM.
172 .TP
173 .BR CAP_MKNOD " (since Linux 2.4)"
174 Create special files using
175 .BR mknod (2).
176 .TP
177 .B CAP_NET_ADMIN
178 Perform various network-related operations
179 (e.g., setting privileged socket options,
180 enabling multicasting, interface configuration,
181 modifying routing tables).
182 .TP
183 .B CAP_NET_BIND_SERVICE
184 Bind a socket to Internet domain privileged ports
185 (port numbers less than 1024).
186 .TP
187 .B CAP_NET_BROADCAST
188 (Unused)  Make socket broadcasts, and listen to multicasts.
189 .TP
190 .B CAP_NET_RAW
191 Use RAW and PACKET sockets.
192 .\" Also various IP options and setsockopt(SO_BINDTODEVICE)
193 .TP
194 .B CAP_SETGID
195 Make arbitrary manipulations of process GIDs and supplementary GID list;
196 forge GID when passing socket credentials via UNIX domain sockets.
197 .TP
198 .BR CAP_SETFCAP " (since Linux 2.6.24)"
199 Set file capabilities.
200 .TP
201 .B CAP_SETPCAP
202 If file capabilities are not supported:
203 grant or remove any capability in the
204 caller's permitted capability set to or from any other process.
205 (This property of
206 .B CAP_SETPCAP
207 is not available when the kernel is configured to support
208 file capabilities, since
209 .B CAP_SETPCAP
210 has entirely different semantics for such kernels.)
211
212 If file capabilities are supported:
213 add any capability from the calling thread's bounding set
214 to its inheritable set;
215 drop capabilities from the bounding set (via
216 .BR prctl (2)
217 .BR PR_CAPBSET_DROP );
218 make changes to the
219 .I securebits
220 flags.
221 .TP
222 .B CAP_SETUID
223 Make arbitrary manipulations of process UIDs
224 .RB ( setuid (2),
225 .BR setreuid (2),
226 .BR setresuid (2),
227 .BR setfsuid (2));
228 make forged UID when passing socket credentials via UNIX domain sockets.
229 .\" FIXME CAP_SETUID also an effect in exec(); document this.
230 .TP
231 .B CAP_SYS_ADMIN
232 .PD 0
233 .RS
234 .IP * 2
235 Perform a range of system administration operations including:
236 .BR quotactl (2),
237 .BR mount (2),
238 .BR umount (2),
239 .BR swapon (2),
240 .BR swapoff (2),
241 .BR sethostname (2),
242 and
243 .BR setdomainname (2);
244 .IP *
245 perform privileged
246 .BR syslog (2)
247 operations (since Linux 2.6.37,
248 .BR CAP_SYSLOG
249 should be used to permit such operations);
250 .IP *
251 perform
252 .B IPC_SET
253 and
254 .B IPC_RMID
255 operations on arbitrary System V IPC objects;
256 .IP *
257 perform operations on
258 .I trusted
259 and
260 .I security
261 Extended Attributes (see
262 .BR attr (5));
263 .IP *
264 use
265 .BR lookup_dcookie (2);
266 .IP *
267 use
268 .BR ioprio_set (2)
269 to assign
270 .B IOPRIO_CLASS_RT
271 and (before Linux 2.6.25)
272 .B IOPRIO_CLASS_IDLE
273 I/O scheduling classes;
274 .IP *
275 forge UID when passing socket credentials;
276 .IP *
277 exceed
278 .IR /proc/sys/fs/file-max ,
279 the system-wide limit on the number of open files,
280 in system calls that open files (e.g.,
281 .BR accept (2),
282 .BR execve (2),
283 .BR open (2),
284 .BR pipe (2));
285 .IP *
286 employ
287 .B CLONE_NEWNS
288 flag with
289 .BR clone (2)
290 and
291 .BR unshare (2);
292 .IP *
293 perform
294 .B KEYCTL_CHOWN
295 and
296 .B KEYCTL_SETPERM
297 .BR keyctl (2)
298 operations;
299 .IP *
300 perform
301 .BR madvise (2)
302 .B MADV_HWPOISON
303 operation.
304 .RE
305 .PD
306 .TP
307 .B CAP_SYS_BOOT
308 Use
309 .BR reboot (2)
310 and
311 .BR kexec_load (2).
312 .TP
313 .B CAP_SYS_CHROOT
314 Use
315 .BR chroot (2).
316 .TP
317 .B CAP_SYS_MODULE
318 Load and unload kernel modules
319 (see
320 .BR init_module (2)
321 and
322 .BR delete_module (2));
323 in kernels before 2.6.25:
324 drop capabilities from the system-wide capability bounding set.
325 .TP
326 .B CAP_SYS_NICE
327 .PD 0
328 .RS
329 .IP * 2
330 Raise process nice value
331 .RB ( nice (2),
332 .BR setpriority (2))
333 and change the nice value for arbitrary processes;
334 .IP *
335 set real-time scheduling policies for calling process,
336 and set scheduling policies and priorities for arbitrary processes
337 .RB ( sched_setscheduler (2),
338 .BR sched_setparam (2));
339 .IP *
340 set CPU affinity for arbitrary processes
341 .RB ( sched_setaffinity (2));
342 .IP *
343 set I/O scheduling class and priority for arbitrary processes
344 .RB ( ioprio_set (2));
345 .IP *
346 apply
347 .BR migrate_pages (2)
348 to arbitrary processes and allow processes
349 to be migrated to arbitrary nodes;
350 .\" FIXME CAP_SYS_NICE also has the following effect for
351 .\" migrate_pages(2):
352 .\"     do_migrate_pages(mm, &old, &new,
353 .\"         capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
354 .IP *
355 apply
356 .BR move_pages (2)
357 to arbitrary processes;
358 .IP *
359 use the
360 .B MPOL_MF_MOVE_ALL
361 flag with
362 .BR mbind (2)
363 and
364 .BR move_pages (2).
365 .RE
366 .PD
367 .TP
368 .B CAP_SYS_PACCT
369 Use
370 .BR acct (2).
371 .TP
372 .B CAP_SYS_PTRACE
373 Trace arbitrary processes using
374 .BR ptrace (2);
375 apply
376 .BR get_robust_list (2)
377 to arbitrary processes.
378 .TP
379 .B CAP_SYS_RAWIO
380 Perform I/O port operations
381 .RB ( iopl (2)
382 and
383 .BR ioperm (2));
384 access
385 .IR /proc/kcore .
386 .TP
387 .B CAP_SYS_RESOURCE
388 .PD 0
389 .RS
390 .IP * 2
391 Use reserved space on ext2 file systems;
392 .IP *
393 make
394 .BR ioctl (2)
395 calls controlling ext3 journaling;
396 .IP *
397 override disk quota limits;
398 .IP *
399 increase resource limits (see
400 .BR setrlimit (2));
401 .IP *
402 override
403 .B RLIMIT_NPROC
404 resource limit;
405 .IP *
406 raise
407 .I msg_qbytes
408 limit for a System V message queue above the limit in
409 .I /proc/sys/kernel/msgmnb
410 (see
411 .BR msgop (2)
412 and
413 .BR msgctl (2)).
414 .IP *
415 use
416 .BR F_SETPIPE_SZ
417 to increase the capacity of a pipe above the limit specified by
418 .IR /proc/sys/fs/pipe-max-size .
419 .RE
420 .PD
421 .TP
422 .B CAP_SYS_TIME
423 Set system clock
424 .RB ( settimeofday (2),
425 .BR stime (2),
426 .BR adjtimex (2));
427 set real-time (hardware) clock.
428 .TP
429 .B CAP_SYS_TTY_CONFIG
430 Use
431 .BR vhangup (2).
432 .TP
433 .BR CAP_SYSLOG " (since Linux 2.6.37)"
434 Perform privileged
435 .BR syslog (2)
436 operations.
437 See
438 .BR syslog (2)
439 for information on which operations require privilege.
440 .\"
441 .SS Past and Current Implementation
442 A full implementation of capabilities requires that:
443 .IP 1. 3
444 For all privileged operations,
445 the kernel must check whether the thread has the required
446 capability in its effective set.
447 .IP 2.
448 The kernel must provide system calls allowing a thread's capability sets to
449 be changed and retrieved.
450 .IP 3.
451 The file system must support attaching capabilities to an executable file,
452 so that a process gains those capabilities when the file is executed.
453 .PP
454 Before kernel 2.6.24, only the first two of these requirements are met;
455 since kernel 2.6.24, all three requirements are met.
456 .\"
457 .SS Thread Capability Sets
458 Each thread has three capability sets containing zero or more
459 of the above capabilities:
460 .TP
461 .IR Permitted :
462 This is a limiting superset for the effective
463 capabilities that the thread may assume.
464 It is also a limiting superset for the capabilities that
465 may be added to the inheritable set by a thread that does not have the
466 .B CAP_SETPCAP
467 capability in its effective set.
468
469 If a thread drops a capability from its permitted set,
470 it can never reacquire that capability (unless it
471 .BR execve (2)s
472 either a set-user-ID-root program, or
473 a program whose associated file capabilities grant that capability).
474 .TP
475 .IR Inheritable :
476 This is a set of capabilities preserved across an
477 .BR execve (2).
478 It provides a mechanism for a process to assign capabilities
479 to the permitted set of the new program during an
480 .BR execve (2).
481 .TP
482 .IR Effective :
483 This is the set of capabilities used by the kernel to
484 perform permission checks for the thread.
485 .PP
486 A child created via
487 .BR fork (2)
488 inherits copies of its parent's capability sets.
489 See below for a discussion of the treatment of capabilities during
490 .BR execve (2).
491 .PP
492 Using
493 .BR capset (2),
494 a thread may manipulate its own capability sets (see below).
495 .\"
496 .SS File Capabilities
497 Since kernel 2.6.24, the kernel supports
498 associating capability sets with an executable file using
499 .BR setcap (8).
500 The file capability sets are stored in an extended attribute (see
501 .BR setxattr (2))
502 named
503 .IR "security.capability" .
504 Writing to this extended attribute requires the
505 .BR CAP_SETFCAP
506 capability.
507 The file capability sets,
508 in conjunction with the capability sets of the thread,
509 determine the capabilities of a thread after an
510 .BR execve (2).
511
512 The three file capability sets are:
513 .TP
514 .IR Permitted " (formerly known as " forced ):
515 These capabilities are automatically permitted to the thread,
516 regardless of the thread's inheritable capabilities.
517 .TP
518 .IR Inheritable " (formerly known as " allowed ):
519 This set is ANDed with the thread's inheritable set to determine which
520 inheritable capabilities are enabled in the permitted set of
521 the thread after the
522 .BR execve (2).
523 .TP
524 .IR Effective :
525 This is not a set, but rather just a single bit.
526 If this bit is set, then during an
527 .BR execve (2)
528 all of the new permitted capabilities for the thread are
529 also raised in the effective set.
530 If this bit is not set, then after an
531 .BR execve (2),
532 none of the new permitted capabilities is in the new effective set.
533
534 Enabling the file effective capability bit implies
535 that any file permitted or inheritable capability that causes a
536 thread to acquire the corresponding permitted capability during an
537 .BR execve (2)
538 (see the transformation rules described below) will also acquire that
539 capability in its effective set.
540 Therefore, when assigning capabilities to a file
541 .RB ( setcap (8),
542 .BR cap_set_file (3),
543 .BR cap_set_fd (3)),
544 if we specify the effective flag as being enabled for any capability,
545 then the effective flag must also be specified as enabled
546 for all other capabilities for which the corresponding permitted or
547 inheritable flags is enabled.
548 .\"
549 .SS Transformation of Capabilities During execve()
550 .PP
551 During an
552 .BR execve (2),
553 the kernel calculates the new capabilities of
554 the process using the following algorithm:
555 .in +4n
556 .nf
557
558 P'(permitted) = (P(inheritable) & F(inheritable)) |
559                 (F(permitted) & cap_bset)
560
561 P'(effective) = F(effective) ? P'(permitted) : 0
562
563 P'(inheritable) = P(inheritable)    [i.e., unchanged]
564
565 .fi
566 .in
567 where:
568 .RS 4
569 .IP P 10
570 denotes the value of a thread capability set before the
571 .BR execve (2)
572 .IP P'
573 denotes the value of a capability set after the
574 .BR execve (2)
575 .IP F
576 denotes a file capability set
577 .IP cap_bset
578 is the value of the capability bounding set (described below).
579 .RE
580 .\"
581 .SS Capabilities and execution of programs by root
582 In order to provide an all-powerful
583 .I root
584 using capability sets, during an
585 .BR execve (2):
586 .IP 1. 3
587 If a set-user-ID-root program is being executed,
588 or the real user ID of the process is 0 (root)
589 then the file inheritable and permitted sets are defined to be all ones
590 (i.e., all capabilities enabled).
591 .IP 2.
592 If a set-user-ID-root program is being executed,
593 then the file effective bit is defined to be one (enabled).
594 .PP
595 The upshot of the above rules,
596 combined with the capabilities transformations described above,
597 is that when a process
598 .BR execve (2)s
599 a set-user-ID-root program, or when a process with an effective UID of 0
600 .BR execve (2)s
601 a program,
602 it gains all capabilities in its permitted and effective capability sets,
603 except those masked out by the capability bounding set.
604 .\" If a process with real UID 0, and nonzero effective UID does an
605 .\" exec(), then it gets all capabilities in its
606 .\" permitted set, and no effective capabilities
607 This provides semantics that are the same as those provided by
608 traditional UNIX systems.
609 .SS Capability bounding set
610 The capability bounding set is a security mechanism that can be used
611 to limit the capabilities that can be gained during an
612 .BR execve (2).
613 The bounding set is used in the following ways:
614 .IP * 2
615 During an
616 .BR execve (2),
617 the capability bounding set is ANDed with the file permitted
618 capability set, and the result of this operation is assigned to the
619 thread's permitted capability set.
620 The capability bounding set thus places a limit on the permitted
621 capabilities that may be granted by an executable file.
622 .IP *
623 (Since Linux 2.6.25)
624 The capability bounding set acts as a limiting superset for
625 the capabilities that a thread can add to its inheritable set using
626 .BR capset (2).
627 This means that if a capability is not in the bounding set,
628 then a thread can't add this capability to its
629 inheritable set, even if it was in its permitted capabilities,
630 and thereby cannot have this capability preserved in its
631 permitted set when it
632 .BR execve (2)s
633 a file that has the capability in its inheritable set.
634 .PP
635 Note that the bounding set masks the file permitted capabilities,
636 but not the inherited capabilities.
637 If a thread maintains a capability in its inherited set
638 that is not in its bounding set,
639 then it can still gain that capability in its permitted set
640 by executing a file that has the capability in its inherited set.
641 .PP
642 Depending on the kernel version, the capability bounding set is either
643 a system-wide attribute, or a per-process attribute.
644 .PP
645 .B "Capability bounding set prior to Linux 2.6.25"
646 .PP
647 In kernels before 2.6.25, the capability bounding set is a system-wide
648 attribute that affects all threads on the system.
649 The bounding set is accessible via the file
650 .IR /proc/sys/kernel/cap-bound .
651 (Confusingly, this bit mask parameter is expressed as a
652 signed decimal number in
653 .IR /proc/sys/kernel/cap-bound .)
654
655 Only the
656 .B init
657 process may set capabilities in the capability bounding set;
658 other than that, the superuser (more precisely: programs with the
659 .B CAP_SYS_MODULE
660 capability) may only clear capabilities from this set.
661
662 On a standard system the capability bounding set always masks out the
663 .B CAP_SETPCAP
664 capability.
665 To remove this restriction (dangerous!), modify the definition of
666 .B CAP_INIT_EFF_SET
667 in
668 .I include/linux/capability.h
669 and rebuild the kernel.
670
671 The system-wide capability bounding set feature was added
672 to Linux starting with kernel version 2.2.11.
673 .\"
674 .PP
675 .B "Capability bounding set from Linux 2.6.25 onward"
676 .PP
677 From Linux 2.6.25, the
678 .I "capability bounding set"
679 is a per-thread attribute.
680 (There is no longer a system-wide capability bounding set.)
681
682 The bounding set is inherited at
683 .BR fork (2)
684 from the thread's parent, and is preserved across an
685 .BR execve (2).
686
687 A thread may remove capabilities from its capability bounding set using the
688 .BR prctl (2)
689 .B PR_CAPBSET_DROP
690 operation, provided it has the
691 .B CAP_SETPCAP
692 capability.
693 Once a capability has been dropped from the bounding set,
694 it cannot be restored to that set.
695 A thread can determine if a capability is in its bounding set using the
696 .BR prctl (2)
697 .B PR_CAPBSET_READ
698 operation.
699
700 Removing capabilities from the bounding set is only supported if file
701 capabilities are compiled into the kernel.
702 In kernels before Linux 2.6.33,
703 file capabilities were an optional feature configurable via the
704 CONFIG_SECURITY_FILE_CAPABILITIES
705 option.
706 Since Linux 2.6.33, the configuration option has been removed
707 and file capabilities are always part of the kernel.
708 When file capabilities are compiled into the kernel, the
709 .B init
710 process (the ancestor of all processes) begins with a full bounding set.
711 If file capabilities are not compiled into the kernel, then
712 .B init
713 begins with a full bounding set minus
714 .BR CAP_SETPCAP ,
715 because this capability has a different meaning when there are
716 no file capabilities.
717
718 Removing a capability from the bounding set does not remove it
719 from the thread's inherited set.
720 However it does prevent the capability from being added
721 back into the thread's inherited set in the future.
722 .\"
723 .\"
724 .SS Effect of User ID Changes on Capabilities
725 To preserve the traditional semantics for transitions between
726 0 and nonzero user IDs,
727 the kernel makes the following changes to a thread's capability
728 sets on changes to the thread's real, effective, saved set,
729 and file system user IDs (using
730 .BR setuid (2),
731 .BR setresuid (2),
732 or similar):
733 .IP 1. 3
734 If one or more of the real, effective or saved set user IDs
735 was previously 0, and as a result of the UID changes all of these IDs
736 have a nonzero value,
737 then all capabilities are cleared from the permitted and effective
738 capability sets.
739 .IP 2.
740 If the effective user ID is changed from 0 to nonzero,
741 then all capabilities are cleared from the effective set.
742 .IP 3.
743 If the effective user ID is changed from nonzero to 0,
744 then the permitted set is copied to the effective set.
745 .IP 4.
746 If the file system user ID is changed from 0 to nonzero (see
747 .BR setfsuid (2))
748 then the following capabilities are cleared from the effective set:
749 .BR CAP_CHOWN ,
750 .BR CAP_DAC_OVERRIDE ,
751 .BR CAP_DAC_READ_SEARCH ,
752 .BR CAP_FOWNER ,
753 .BR CAP_FSETID ,
754 .B CAP_LINUX_IMMUTABLE
755 (since Linux 2.2.30),
756 .BR CAP_MAC_OVERRIDE ,
757 and
758 .B CAP_MKNOD
759 (since Linux 2.2.30).
760 If the file system UID is changed from nonzero to 0,
761 then any of these capabilities that are enabled in the permitted set
762 are enabled in the effective set.
763 .PP
764 If a thread that has a 0 value for one or more of its user IDs wants
765 to prevent its permitted capability set being cleared when it resets
766 all of its user IDs to nonzero values, it can do so using the
767 .BR prctl (2)
768 .B PR_SET_KEEPCAPS
769 operation.
770 .\"
771 .SS Programmatically adjusting capability sets
772 A thread can retrieve and change its capability sets using the
773 .BR capget (2)
774 and
775 .BR capset (2)
776 system calls.
777 However, the use of
778 .BR cap_get_proc (3)
779 and
780 .BR cap_set_proc (3),
781 both provided in the
782 .I libcap
783 package,
784 is preferred for this purpose.
785 The following rules govern changes to the thread capability sets:
786 .IP 1. 3
787 If the caller does not have the
788 .B CAP_SETPCAP
789 capability,
790 the new inheritable set must be a subset of the combination
791 of the existing inheritable and permitted sets.
792 .IP 2.
793 (Since kernel 2.6.25)
794 The new inheritable set must be a subset of the combination of the
795 existing inheritable set and the capability bounding set.
796 .IP 3.
797 The new permitted set must be a subset of the existing permitted set
798 (i.e., it is not possible to acquire permitted capabilities
799 that the thread does not currently have).
800 .IP 4.
801 The new effective set must be a subset of the new permitted set.
802 .SS The """securebits"" flags: establishing a capabilities-only environment
803 .\" For some background:
804 .\"       see http://lwn.net/Articles/280279/ and
805 .\"       http://article.gmane.org/gmane.linux.kernel.lsm/5476/
806 Starting with kernel 2.6.26,
807 and with a kernel in which file capabilities are enabled,
808 Linux implements a set of per-thread
809 .I securebits
810 flags that can be used to disable special handling of capabilities for UID 0
811 .RI ( root ).
812 These flags are as follows:
813 .TP
814 .B SECBIT_KEEP_CAPS
815 Setting this flag allows a thread that has one or more 0 UIDs to retain
816 its capabilities when it switches all of its UIDs to a nonzero value.
817 If this flag is not set,
818 then such a UID switch causes the thread to lose all capabilities.
819 This flag is always cleared on an
820 .BR execve (2).
821 (This flag provides the same functionality as the older
822 .BR prctl (2)
823 .B PR_SET_KEEPCAPS
824 operation.)
825 .TP
826 .B SECBIT_NO_SETUID_FIXUP
827 Setting this flag stops the kernel from adjusting  capability sets when
828 the threads's effective and file system UIDs are switched between
829 zero and nonzero values.
830 (See the subsection
831 .IR "Effect of User ID Changes on Capabilities" .)
832 .TP
833 .B SECBIT_NOROOT
834 If this bit is set, then the kernel does not grant capabilities
835 when a set-user-ID-root program is executed, or when a process with
836 an effective or real UID of 0 calls
837 .BR execve (2).
838 (See the subsection
839 .IR "Capabilities and execution of programs by root" .)
840 .PP
841 Each of the above "base" flags has a companion "locked" flag.
842 Setting any of the "locked" flags is irreversible,
843 and has the effect of preventing further changes to the
844 corresponding "base" flag.
845 The locked flags are:
846 .BR SECBIT_KEEP_CAPS_LOCKED ,
847 .BR SECBIT_NO_SETUID_FIXUP_LOCKED ,
848 and
849 .BR SECBIT_NOROOT_LOCKED .
850 .PP
851 The
852 .I securebits
853 flags can be modified and retrieved using the
854 .BR prctl (2)
855 .B PR_SET_SECUREBITS
856 and
857 .B PR_GET_SECUREBITS
858 operations.
859 The
860 .B CAP_SETPCAP
861 capability is required to modify the flags.
862
863 The
864 .I securebits
865 flags are inherited by child processes.
866 During an
867 .BR execve (2),
868 all of the flags are preserved, except
869 .B SECBIT_KEEP_CAPS
870 which is always cleared.
871
872 An application can use the following call to lock itself,
873 and all of its descendants,
874 into an environment where the only way of gaining capabilities
875 is by executing a program with associated file capabilities:
876 .in +4n
877 .nf
878
879 prctl(PR_SET_SECUREBITS,
880         SECBIT_KEEP_CAPS_LOCKED |
881         SECBIT_NO_SETUID_FIXUP |
882         SECBIT_NO_SETUID_FIXUP_LOCKED |
883         SECBIT_NOROOT |
884         SECBIT_NOROOT_LOCKED);
885 .fi
886 .in
887 .SH "CONFORMING TO"
888 .PP
889 No standards govern capabilities, but the Linux capability implementation
890 is based on the withdrawn POSIX.1e draft standard; see
891 .IR http://wt.xpilot.org/publications/posix.1e/ .
892 .SH NOTES
893 Since kernel 2.5.27, capabilities are an optional kernel component,
894 and can be enabled/disabled via the CONFIG_SECURITY_CAPABILITIES
895 kernel configuration option.
896
897 The
898 .I /proc/PID/task/TID/status
899 file can be used to view the capability sets of a thread.
900 The
901 .I /proc/PID/status
902 file shows the capability sets of a process's main thread.
903
904 The
905 .I libcap
906 package provides a suite of routines for setting and
907 getting capabilities that is more comfortable and less likely
908 to change than the interface provided by
909 .BR capset (2)
910 and
911 .BR capget (2).
912 This package also provides the
913 .BR setcap (8)
914 and
915 .BR getcap (8)
916 programs.
917 It can be found at
918 .br
919 .IR http://www.kernel.org/pub/linux/libs/security/linux-privs .
920
921 Before kernel 2.6.24, and since kernel 2.6.24 if
922 file capabilities are not enabled, a thread with the
923 .B CAP_SETPCAP
924 capability can manipulate the capabilities of threads other than itself.
925 However, this is only theoretically possible,
926 since no thread ever has
927 .BR CAP_SETPCAP
928 in either of these cases:
929 .IP * 2
930 In the pre-2.6.25 implementation the system-wide capability bounding set,
931 .IR /proc/sys/kernel/cap-bound ,
932 always masks out this capability, and this can not be changed
933 without modifying the kernel source and rebuilding.
934 .IP *
935 If file capabilities are disabled in the current implementation, then
936 .B init
937 starts out with this capability removed from its per-process bounding
938 set, and that bounding set is inherited by all other processes
939 created on the system.
940 .SH "SEE ALSO"
941 .BR capget (2),
942 .BR prctl (2),
943 .BR setfsuid (2),
944 .BR cap_clear (3),
945 .BR cap_copy_ext (3),
946 .BR cap_from_text (3),
947 .BR cap_get_file (3),
948 .BR cap_get_proc (3),
949 .BR cap_init (3),
950 .BR capgetp (3),
951 .BR capsetp (3),
952 .BR credentials (7),
953 .BR pthreads (7),
954 .BR getcap (8),
955 .BR setcap (8)
956 .PP
957 .I include/linux/capability.h
958 in the kernel source