OSDN Git Service

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