Capabilities(7)

๐Ÿ’– capabilities(7) โ€” a Miscellaneous Information Manual page, but make it make sense ๐Ÿ’–

okay bestie, sit down, hydrate, because this is the glow-up of the traditional Unix permission model and you're gonna want notes. โœจ


๐ŸŽ€ NAME ๐ŸŽ€

capabilities โ€” overview of Linux capabilities ๐ŸŒธ


โœจ DESCRIPTION โœจ

so. for the purpose of permission checks, traditional UNIX implementations split all processes into exactly two little categories, and it's giving "binary" in the most 1970s way possible:

  • privileged processes โ€” these are the ones whose effective user ID is 0, aka superuser aka root ๐Ÿ‘‘. They literally bypass every single kernel permission check. no vibe check at all. they just walk in.
  • unprivileged processes โ€” effective UID is nonzero. these ones get the full permission checking treatment, based on the process's credentials (usually: effective UID, effective GID, and the supplementary group list). every door has a lock and every lock gets rattled. ๐Ÿ”

and like, that's fine, but it means root is one giant everything-pass and that is not the girlboss energy we want. ๐Ÿšฉ

Starting with Linux 2.2, Linux said "not me doing this anymore" and divided the privileges traditionally associated with superuser into distinct lil units, known as capabilities, which can be independently enabled and disabled. so you don't have to hand someone the entire kingdom just because they need to open one gate. ๐Ÿ’…

tiny but important note: capabilities are a per-thread attribute. per thread. not per process, bestie. write that down. ๐Ÿ“

๐Ÿฆ‹ Capabilities list ๐Ÿฆ‹

here's the whole roster of capabilities implemented on Linux, plus the operations or behaviors each one permits. think of it as the group chat member list, except every member has a very specific superpower and absolutely no chill. ๐Ÿซถ

  • CAP_AUDIT_CONTROL (since Linux 2.6.11) โ€” turn kernel auditing on and off; change auditing filter rules; retrieve auditing status and filtering rules. basically the audit settings admin. ๐Ÿ—‚๏ธ
  • CAP_AUDIT_READ (since Linux 3.16) โ€” lets you read the audit log via a multicast netlink socket. ๐Ÿ‘€
  • CAP_AUDIT_WRITE (since Linux 2.6.11) โ€” write records into the kernel auditing log. โœ๏ธ
  • CAP_BLOCK_SUSPEND (since Linux 3.5) โ€” employ features that can block system suspend (epoll(7) EPOLLWAKEUP, /proc/sys/wake_lock). "no sleep mode, we're busy." โฐ
  • CAP_BPF (since Linux 5.8) โ€” employ privileged BPF operations; see bpf(2) and bpf-helpers(7).

this capability was added in Linux 5.8 to separate out BPF functionality from the overloaded CAP_SYS_ADMIN capability. (yes CAP_SYS_ADMIN is getting called out repeatedly in this doc. it's a whole theme. ๐Ÿ”ฅ)

  • CAP_CHECKPOINT_RESTORE (since Linux 5.9) โ€”
    • Update /proc/sys/kernel/ns_last_pid (see pid_namespaces(7));
    • employ the set_tid feature of clone3(2);
    • read the contents of the symbolic links in /proc/pid/map_files for other processes.

this capability was added in Linux 5.9 to separate out checkpoint/restore functionality from the overloaded CAP_SYS_ADMIN capability. ๐Ÿ’พ

  • CAP_CHOWN โ€” make arbitrary changes to file UIDs and GIDs (see chown(2)). who owns what? you decide. ๐Ÿท๏ธ
  • CAP_DAC_OVERRIDE โ€” bypass file read, write, and execute permission checks. (DAC is an abbreviation of "discretionary access control".) so like, the file said no and you said "anyway." ๐Ÿšช
  • CAP_DAC_READ_SEARCH โ€”
    • bypass file read permission checks and directory read and execute permission checks;
    • invoke open_by_handle_at(2);
    • use the linkat(2) AT_EMPTY_PATH flag to create a link to a file referred to by a file descriptor.
  • CAP_FOWNER โ€”
    • bypass permission checks on operations that normally require the filesystem UID of the process to match the UID of the file (e.g., chmod(2), utime(2)), excluding those operations covered by CAP_DAC_OVERRIDE and CAP_DAC_READ_SEARCH;
    • set inode flags (see FS_IOC_SETFLAGS(2const)) on arbitrary files;
    • set Access Control Lists (ACLs) on arbitrary files;
    • ignore directory sticky bit on file deletion;
    • modify user extended attributes on sticky directory owned by any user;
    • specify O_NOATIME for arbitrary files in open(2) and fcntl(2).
  • CAP_FSETID โ€”
    • don't clear set-user-ID and set-group-ID mode bits when a file is modified;
    • set the set-group-ID bit for a file whose GID does not match the filesystem or any of the supplementary GIDs of the calling process.
  • CAP_IPC_LOCK โ€”
  • CAP_IPC_OWNER โ€” bypass permission checks for operations on System V IPC objects. ๐Ÿงฑ
  • CAP_KILL โ€” bypass permission checks for sending signals (see kill(2)). this includes use of the ioctl(2) KDSIGACCEPT operation. ๐Ÿ’€
  • CAP_LEASE (since Linux 2.4) โ€” establish leases on arbitrary files (see fcntl(2)). ๐Ÿ“œ
  • CAP_LINUX_IMMUTABLE โ€” set the FS_APPEND_FL and FS_IMMUTABLE_FL inode flags (see FS_IOC_SETFLAGS(2const)). ๐ŸงŠ
  • CAP_MAC_ADMIN (since Linux 2.6.25) โ€”
    • for the Smack Linux Security Module (LSM), allow MAC configuration or state changes;
    • for the SELinux LSM, allow setting undefined security contexts on inodes (since Linux 2.6.27).
  • CAP_MAC_OVERRIDE (since Linux 2.6.25) โ€” override Mandatory Access Control (MAC). implemented for the Smack LSM. โœ‹
  • CAP_MKNOD (since Linux 2.4) โ€” create special files using mknod(2). ๐Ÿ„
  • CAP_NET_ADMIN โ€” perform various network-related operations:
    • interface configuration;
    • administration of IP firewall, masquerading, and accounting;
    • modify routing tables;
    • bind to any address for transparent proxying;
    • set type-of-service (TOS);
    • clear driver statistics;
    • set promiscuous mode;
    • enabling multicasting;
    • use setsockopt(2) to set the following socket options: SO_DEBUG, SO_MARK, SO_PRIORITY (for a priority outside the range 0 to 6), SO_RCVBUFFORCE, and SO_SNDBUFFORCE.
  • CAP_NET_BIND_SERVICE โ€” bind a socket to Internet domain privileged ports (port numbers less than 1024). the low-numbered-port club. ๐ŸŽŸ๏ธ
  • CAP_NET_BROADCAST โ€” (Unused) make socket broadcasts, and listen to multicasts. literally labeled unused, bestie, she's on the roster but she's not getting minutes. ๐Ÿช‘
  • CAP_NET_RAW โ€”
    • use RAW and PACKET sockets;
    • bind to any address for transparent proxying.
  • CAP_PERFMON (since Linux 5.8) โ€” employ various performance-monitoring mechanisms, including:
    • call perf_event_open(2);
    • employ various BPF operations that have performance implications;
    • read the contents of /proc/pid/maps, /proc/pid/smaps, and /proc/pid/smaps_rollup for all processes.

this capability was added in Linux 5.8 to separate out performance monitoring functionality from the overloaded CAP_SYS_ADMIN capability. see also the kernel source file Documentation/admin-guide/perf-security.rst. ๐Ÿ“Š

  • CAP_SETGID โ€”
    • make arbitrary manipulations of process GIDs and supplementary GID list;
    • forge GID when passing socket credentials via UNIX domain sockets;
    • write a group ID mapping in a user namespace (see user_namespaces(7)).
  • CAP_SETFCAP (since Linux 2.6.24) โ€” set arbitrary capabilities on a file.

since Linux 5.12, this capability is also needed to map user ID 0 in a new user namespace; see user_namespaces(7) for details.

  • CAP_SETPCAP โ€” okay this one is a whole two-timeline situation, pay attention:
    • if file capabilities are supported (i.e., since Linux 2.6.24): add any capability from the calling thread's bounding set to its inheritable set; drop capabilities from the bounding set (via prctl(2) PR_CAPBSET_DROP); make changes to the securebits flags.
    • if file capabilities are not supported (i.e., before Linux 2.6.24): grant or remove any capability in the caller's permitted capability set to or from any other process. (this property of CAP_SETPCAP is not available when the kernel is configured to support file capabilities, since CAP_SETPCAP has entirely different semantics for such kernels.)
so yeah โ€” same name, completely different powers depending on your kernel config. do not assume. ๐Ÿ˜ญ
  • CAP_SETUID โ€”
  • CAP_SYS_ADMIN โ€” note: this capability is overloaded; see Notes to kernel developers below. ๐Ÿ‘€
bestie. if you take one thing from part 1, take this: CAP_SYS_ADMIN is the "and also everything else" girl and the kernel devs keep apologizing for her. she will come up again. and again. ๐Ÿซ 

  • do a whole bunch of system administration operations, like quotactl(2), mount(2), umount(2), pivot_root(2), swapon(2), swapoff(2), sethostname(2), and setdomainname(2) โ€” yeah that whole little friend group ๐Ÿซถ
  • do the privileged syslog(2) operations (ngl since Linux 2.6.37 you should be using CAP_SYSLOG for that instead, like, that's the one you want)
  • run the VM86_REQUEST_IRQ vm86(2) command
  • access the exact same checkpoint/restore functionality that CAP_CHECKPOINT_RESTORE governs โ€” but bestie, that weaker capability is the preferred one for it, so like, use that one ๐Ÿ’…
  • do the same BPF operations that CAP_BPF governs (again though โ€” the weaker CAP_BPF is preferred for accessing that functionality, not this one ๐Ÿ‘‘)
  • use the same performance monitoring mechanisms that CAP_PERFMON governs (and yes, same deal: the weaker CAP_PERFMON is preferred for that ๐ŸŽ€)
  • do IPC_SET and IPC_RMID operations on any System V IPC objects you feel like
  • override the RLIMIT_NPROC resource limit
  • mess with trusted and security extended attributes (peep xattr(7))
  • use lookup_dcookie(2)
  • use ioprio_set(2) to assign IOPRIO_CLASS_RT and โ€” before Linux 2.6.25 โ€” IOPRIO_CLASS_IDLE I/O scheduling classes
  • forge a PID when passing socket credentials over UNIX domain sockets ๐Ÿ˜ญ
  • exceed /proc/sys/fs/file-max (that's the system-wide cap on how many files can be open) in system calls that open files, like accept(2), execve(2), open(2), pipe(2)
  • use the CLONE_* flags that create new namespaces with clone(2) and unshare(2) โ€” but heads up, since Linux 3.8, creating user namespaces doesn't need any capability โœจ
  • access privileged perf event info
  • call setns(2) (this one needs CAP_SYS_ADMIN in the target namespace, fyi)
  • call fanotify_init(2)
  • do the privileged KEYCTL_CHOWN and KEYCTL_SETPERM keyctl(2) operations
  • do the madvise(2) MADV_HWPOISON operation
  • use the TIOCSTI ioctl(2) to shove characters into the input queue of a terminal that isn't the caller's controlling terminal (lowkey unhinged, that's the point ๐Ÿ’€)
  • use the obsolete nfsservctl(2) system call
  • use the obsolete bdflush(2) system call
  • do all kinds of privileged block-device ioctl(2) operations
  • do all kinds of privileged filesystem ioctl(2) operations
  • do privileged ioctl(2) operations on the /dev/random device (see random(4))
  • install a seccomp(2) filter without having to first set the no_new_privs thread attribute
  • change the allow/deny rules for device control groups
  • use the ptrace(2) PTRACE_SECCOMP_GET_FILTER operation to dump the tracee's seccomp filters
  • use the ptrace(2) PTRACE_SETOPTIONS operation to suspend the tracee's seccomp protections, aka the PTRACE_O_SUSPEND_SECCOMP flag
  • do administrative operations on soooo many device drivers
  • change autogroup nice values by writing to /proc/pid/autogroup (see sched(7))

๐ŸŒˆ CAP_SYS_BOOT ๐ŸŒˆ

Use reboot(2) and kexec_load(2). That's it, that's the capability ๐Ÿ’–

๐ŸŒธ CAP_SYS_CHROOT ๐ŸŒธ

๐Ÿฆ‹ CAP_SYS_MODULE ๐Ÿฆ‹

  • Load and unload kernel modules (peep init_module(2) and delete_module(2)) โ€” very main character energy, be careful;
  • before Linux 2.6.25: drop capabilities from the system-wide capability bounding set.

๐Ÿ’… CAP_SYS_NICE ๐Ÿ’…

๐ŸŽ€ CAP_SYS_PACCT ๐ŸŽ€

Use acct(2).

๐Ÿ‘‘ CAP_SYS_PTRACE ๐Ÿ‘‘

  • Trace arbitrary processes using ptrace(2);
  • inspect sensitive info from other processes via /proc (like reading /proc/pid/maps, /proc/pid/mem, or reading the symlinks /proc/pid/exe, /proc/pid/fd/*);
  • apply get_robust_list(2) to arbitrary processes;
  • transfer data to or from the memory of arbitrary processes using process_vm_readv(2) and process_vm_writev(2);
  • inspect processes using kcmp(2);
  • do other privileged process-inspection and debugging operations. (Check the uses of the ptrace_may_access() kernel function, iykyk.)

๐Ÿ”ฅ CAP_SYS_RAWIO ๐Ÿ”ฅ

  • Do I/O port operations (iopl(2) and ioperm(2));
  • access /proc/kcore;
  • use the FIBMAP ioctl(2) operation;
  • open devices for accessing x86 model-specific registers (MSRs, see msr(4));
  • update /proc/sys/vm/mmap_min_addr;
  • create memory mappings at addresses below whatever /proc/sys/vm/mmap_min_addr says;
  • map files in /proc/bus/pci;
  • open /dev/mem and /dev/kmem;
  • do various SCSI device commands;
  • do certain operations on hpsa(4) and cciss(4) devices;
  • do a whole range of device-specific operations on other devices.

โญ CAP_SYS_RESOURCE โญ

  • Use reserved space on ext2 filesystems;
  • make ioctl(2) calls that control ext3 journaling;
  • override disk quota limits;
  • increase resource limits (see setrlimit(2));
  • override the RLIMIT_NPROC resource limit;
  • override the maximum number of consoles when allocating a console;
  • override the maximum number of keymaps;
  • allow more than 64hz interrupts from the real-time clock;
  • raise the msg_qbytes limit for a System V message queue above the limit in /proc/sys/kernel/msgmnb (see msgop(2) and msgctl(2));
  • let the RLIMIT_NOFILE resource limit on the number of "in-flight" file descriptors get bypassed when you pass file descriptors to another process over a UNIX domain socket (see unix(7));
  • override the /proc/sys/fs/pipe-size-max limit when setting a pipe's capacity with the F_SETPIPE_SZ fcntl(2) command;
  • use F_SETPIPE_SZ to boost a pipe's capacity above the limit in /proc/sys/fs/pipe-max-size;
  • override the /proc/sys/fs/mqueue/queues_max, /proc/sys/fs/mqueue/msg_max, and /proc/sys/fs/mqueue/msgsize_max limits when creating POSIX message queues (see mq_overview(7));
  • use the prctl(2) PR_SET_MM operation;
  • set /proc/pid/oom_score_adj to a value lower than whatever was last set by a process with CAP_SYS_RESOURCE.

๐Ÿงš CAP_SYS_TIME ๐Ÿงš

Set the system clock (settimeofday(2), stime(2), adjtimex(2)); set the real-time (hardware) clock. Not me casually time-traveling โฐ

๐Ÿ’œ CAP_SYS_TTY_CONFIG ๐Ÿ’œ

Use vhangup(2); use various privileged ioctl(2) operations on virtual terminals.

๐ŸŒŸ CAP_SYSLOG (since Linux 2.6.37) ๐ŸŒŸ

  • Do privileged syslog(2) operations. See syslog(2) for the deets on which operations actually need privilege.
  • View kernel addresses exposed via /proc and other interfaces when /proc/sys/kernel/kptr_restrict has the value 1. (See the discussion of kptr_restrict in proc(5).)

๐ŸŒˆ CAP_WAKE_ALARM (since Linux 3.0) ๐ŸŒˆ

Trigger something that's gonna wake the system up (set CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM timers). Rise and shine bestie โ˜€๏ธ


๐Ÿ› ๏ธ Past and current implementation ๐Ÿ› ๏ธ

Okay so a full implementation of capabilities needs all of this to be true:

  • For every privileged operation, the kernel has to check whether the thread has the required capability in its effective set.
  • The kernel has to provide system calls that let a thread's capability sets be changed and retrieved.
  • The filesystem has to support attaching capabilities to an executable file, so a process gains those capabilities when that file gets executed.

Before Linux 2.6.24, only the first two of these requirements were met. Since Linux 2.6.24, all three are met. glow up โœจ

๐Ÿ‘ฉโ€๐Ÿ’ป Notes to kernel developers ๐Ÿ‘ฉโ€๐Ÿ’ป

If you're adding a new kernel feature that should be governed by a capability, pls consider these points first:

  • The whole goal of capabilities is to chop the power of superuser into pieces, so that if a program holding one or more capabilities gets compromised, its power to damage the system is less than the same program running with root privilege.
  • You get to choose: either make a brand-new capability for your new feature, or associate the feature with one of the existing capabilities. To keep the capability set at a manageable size, the second option is preferable โ€” unless there are compelling reasons to go with the first one. (There's also a technical limit: the size of capability sets is currently limited to 64 bits. periodt.)
  • To figure out which existing capability best fits your new feature, review the list of capabilities above and find the "silo" your feature slides into. One approach: check whether there are other features requiring capabilities that will always be used alongside your new feature. If your new feature is useless without those other features, use the same capability as those other features. ๐Ÿ’–
  • Do NOT pick CAP_SYS_ADMIN if there is literally any way around it!! ๐Ÿ˜ญ A huge chunk of the capability checks that already exist are all tied up with this one (peep that partial list up there). It can lowkey be called "the new root" โ€” because on one hand it hands over a wildly wide range of powers, and on the other, that same giant scope is exactly why so many privileged programs need it. Please don't make the problem worse, bestie. The only new features that should get attached to CAP_SYS_ADMIN are ones that very closely match what's already going on in that silo. Periodt.
  • If you've genuinely figured out that you do need a brand-new capability for your feature, then don't make it or name it as a "single-use" capability. Likeโ€ฆ the addition of the super-specific CAP_SYS_PACCT was probably a mistake, ngl. Instead, try to figure out and name your new capability as a broader silo that other related future use cases could also fit into. ๐Ÿ‘‘

Thread capability sets ๐Ÿงตโœจ

Every thread has the following capability sets, and each one contains zero or more of the capabilities from above:

Permitted ๐Ÿ’–

This is the limiting superset for the effective capabilities that the thread is allowed to assume. It's also a limiting superset for the capabilities that a thread can add to its inheritable set โ€” but only if that thread does not have the CAP_SETPCAP capability sitting in its effective set.

Heads up: if a thread drops a capability from its permitted set, it can never get that capability back. Like, ever. (Unless it execve(2)s either a set-user-ID-root program, or a program whose associated file capabilities grant that capability.) So be careful, okay? ๐Ÿ˜ฌ

Inheritable ๐ŸŒธ

This is a set of capabilities that gets preserved across an execve(2). Inheritable capabilities stay inheritable when you execute any program, and they get added to the permitted set when you execute a program that has the corresponding bits set in the file inheritable set.

And because inheritable capabilities generally do not survive an execve(2) when you're running as a non-root user, applications that want to run helper programs with elevated capabilities should consider using ambient capabilities instead โ€” more on those below. ๐Ÿ‘‡

Effective โญ

This is the set of capabilities that the kernel actually uses to run permission checks for the thread.

Bounding (per-thread since Linux 2.6.25) ๐ŸŽ€

The capability bounding set is a mechanism you can use to limit the capabilities that get gained during an execve(2).

Since Linux 2.6.25, this is a per-thread capability set. On older kernels, the capability bounding set was a system-wide attribute shared by every thread on the system. (Retro vibes, honestly. ๐Ÿ’€)

For the full tea, see Capability bounding set below.

Ambient (since Linux 4.3) ๐Ÿฆ‹

This is a set of capabilities that get preserved across an execve(2) of a program that is not privileged. The ambient capability set obeys this invariant: no capability can ever be ambient if it isn't both permitted and inheritable. That's the rule, no exceptions.

The ambient capability set can be modified directly using prctl(2). Ambient capabilities are automatically lowered if either the corresponding permitted or inheritable capability gets lowered. They move as a unit, basically. ๐Ÿ’…

Executing a program that changes UID or GID because of the set-user-ID or set-group-ID bits โ€” or executing a program that has any file capabilities set โ€” will clear the ambient set. Ambient capabilities get added to the permitted set and assigned to the effective set when execve(2) is called. And if ambient capabilities cause a process's permitted and effective capabilities to increase during an execve(2), that does not trigger the secure-execution mode described in ld.so(8).


A child created via fork(2) inherits copies of its parent's capability sets. For the details on how execve(2) messes with capabilities, see Transformation of capabilities during execve() below. ๐Ÿ”„

Using capset(2), a thread can manipulate its own capability sets โ€” see Programmatically adjusting capability sets below.

And since Linux 3.2, the file /proc/sys/kernel/cap_last_cap exposes the numerical value of the highest capability supported by the running kernel. You can use that to figure out the highest bit that may be set in a capability set. ๐ŸŒˆ


File capabilities ๐Ÿ“โœจ

Since Linux 2.6.24, the kernel supports associating capability sets with an executable file using setcap(8). These file capability sets are stored in an extended attribute (see setxattr(2) and xattr(7)) named security.capability. Writing to this extended attribute requires the CAP_SETFCAP capability. The file capability sets, together with the capability sets of the thread, decide what capabilities a thread has after an execve(2).

The three file capability sets are:

Permitted (formerly known as forced) ๐Ÿ’œ

These capabilities are automatically permitted to the thread, no matter what the thread's inheritable capabilities are.

Inheritable (formerly known as allowed) ๐Ÿซถ

This set gets ANDed with the thread's inheritable set to determine which inheritable capabilities end up enabled in the permitted set of the thread after the execve(2).

Effective โšก

This one isn't a set at all โ€” it's just a single bit. If this bit is set, then during an execve(2) all of the new permitted capabilities for the thread also get raised in the effective set. If this bit is not set, then after an execve(2), none of the new permitted capabilities is in the new effective set.

Enabling the file effective capability bit implies that any file permitted or inheritable capability that causes a thread to acquire the corresponding permitted capability during an execve(2) (see Transformation of capabilities during execve() below) will also acquire that capability in its effective set. So when you're assigning capabilities to a file (setcap(8), cap_set_file(3), cap_set_fd(3)), if you specify the effective flag as enabled for any capability, then the effective flag must also be specified as enabled for all other capabilities for which the corresponding permitted or inheritable flag is enabled. Group chat rule: it's all or nothing. ๐Ÿ™ˆ

File capability extended attribute versioning ๐Ÿท๏ธโœจ

To allow for extensibility, the kernel supports a scheme that encodes a version number inside the security.capability extended attribute that's used to implement file capabilities. These version numbers are internal to the implementation, and not directly visible to user-space applications. To date, these are the supported versions:

VFS_CAP_REVISION_1

This was the original file capability implementation, and it supported 32-bit masks for file capabilities.

VFS_CAP_REVISION_2 (since Linux 2.6.25)

This version allows file capability masks that are 64 bits in size, and it was necessary because the number of supported capabilities grew past 32. (Growth era. ๐ŸŒฑ) The kernel transparently keeps supporting the execution of files that have 32-bit version 1 capability masks, but when it's adding capabilities to files that didn't have capabilities before, or modifying the capabilities of existing files, it automatically uses the version 2 scheme (or possibly the version 3 scheme, described below).

VFS_CAP_REVISION_3 (since Linux 4.14)

Version 3 file capabilities exist to support namespaced file capabilities (described below).

Just like version 2 file capabilities, version 3 capability masks are 64 bits in size. But on top of that, the root user ID of the namespace is encoded in the security.capability extended attribute. (A namespace's root user ID is the value that user ID 0 inside that namespace maps to in the initial user namespace.)

Version 3 file capabilities are designed to coexist with version 2 capabilities โ€” meaning that on a modern Linux system, some files might have version 2 capabilities while others have version 3 capabilities. It's giving mixed era, and that's fine. ๐Ÿ’…


Before Linux 4.14, the only kind of file capability extended attribute that could be attached to a file was a VFS_CAP_REVISION_2 attribute. Since Linux 4.14, which version of the security.capability extended attribute gets attached to a file depends on the circumstances in which the attribute was created.

Starting with Linux 4.14, a security.capability extended attribute is automatically created as (or converted to) a version 3 (VFS_CAP_REVISION_3) attribute if both of these are true:

  • The thread writing the attribute resides in a noninitial user namespace. (More precisely: the thread resides in a user namespace other than the one from which the underlying filesystem was mounted.)
  • The thread has the CAP_SETFCAP capability over the file inode โ€” meaning that (a) the thread has the CAP_SETFCAP capability in its own user namespace, and (b) the UID and GID of the file inode have mappings in the writer's user namespace.

When a VFS_CAP_REVISION_3 security.capability extended attribute is created, the root user ID of the creating thread's user namespace gets saved in the extended attribute. ๐Ÿ’พ

By contrast, creating or modifying a security.capability extended attribute from a privileged (CAP_SETFCAP) thread that resides in the namespace where the underlying filesystem was mounted (this normally means the initial user namespace) automatically results in the creation of a version 2 (VFS_CAP_REVISION_2) attribute.

So note that creating a version 3 security.capability extended attribute is automatic. That is to say: when a user-space application writes (setxattr(2)) a security.capability attribute in the version 2 format, the kernel will automatically create a version 3 attribute if the attribute is created in the circumstances described above. Correspondingly, when a version 3 security.capability attribute is retrieved (getxattr(2)) by a process that resides inside a user namespace that was created by the root user ID (or a descendant of that user namespace), the returned attribute is (automatically) simplified to appear as a version 2 attribute โ€” i.e., the returned value is the size of a version 2 attribute and does not include the root user ID. These automatic translations mean that no changes are required to user-space tools (e.g., setcap(1) and getcap(1)) in order for those tools to be used to create and retrieve version 3 security.capability attributes. Slay, honestly. ๐Ÿ‘‘

Okay so bestie, real talk: a file can have either a version 2 or a version 3 security.capability extended attribute associated with it, but not both โ€” it's giving exclusive relationship energy. ๐Ÿ’… Creation or modification of the security.capability extended attribute will automatically modify the version according to the circumstances in which the extended attribute is created or modified. No cap.


โœจ Transformation of capabilities during execve() โœจ

During an execve(2), the kernel calculates the new capabilities of the process using the following algorithm:

P'(ambient)     = (file is privileged) ? 0 : P(ambient)

P'(permitted)   = (P(inheritable) & F(inheritable)) |
                  (F(permitted) & P(bounding)) | P'(ambient)

P'(effective)   = F(effective) ? P'(permitted) : P'(ambient)

P'(inheritable) = P(inheritable)    [i.e., unchanged]

P'(bounding)    = P(bounding)       [i.e., unchanged]

where:

  • P() denotes the value of a thread capability set before the execve(2)
  • P'() denotes the value of a thread capability set after the execve(2)
  • F() denotes a file capability set

Note the following details relating to the above capability transformation rules:

  • The ambient capability set is present only since Linux 4.3. When determining the transformation of the ambient set during execve(2), a privileged file is one that has capabilities or has the set-user-ID or set-group-ID bit set.
  • Prior to Linux 2.6.25, the bounding set was a system-wide attribute shared by all threads. That system-wide value was employed to calculate the new permitted set during execve(2) in the same manner as shown above for P(bounding).
Note: during the capability transitions described above, file capabilities may be ignored (treated as empty) for the same reasons that the set-user-ID and set-group-ID bits are ignored; see execve(2). File capabilities are similarly ignored if the kernel was booted with the no_file_caps option.
Note: according to the rules above, if a process with nonzero user IDs performs an execve(2) then any capabilities that are present in its permitted and effective sets will be cleared. For the treatment of capabilities when a process with a user ID of zero performs an execve(2), see Capabilities and execution of programs by root below.

๐Ÿฆ‹ Safety checking for capability-dumb binaries ๐Ÿฆ‹

A capability-dumb binary is an application that has been marked to have file capabilities, but has not been converted to use the libcap(3) API to manipulate its capabilities. (In other words, this is a traditional set-user-ID-root program that has been switched to use file capabilities, but whose code has not been modified to understand capabilities.) For such applications, the effective capability bit is set on the file, so that the file permitted capabilities are automatically enabled in the process effective set when executing the file. The kernel recognizes a file which has the effective capability bit set as capability-dumb for the purpose of the check described here.

When executing a capability-dumb binary, the kernel checks if the process obtained all permitted capabilities that were specified in the file permitted set, after the capability transformations described above have been performed. (The typical reason why this might not occur is that the capability bounding set masked out some of the capabilities in the file permitted set.) If the process did not obtain the full set of file permitted capabilities, then execve(2) fails with the error EPERM. This prevents possible security risks that could arise when a capability-dumb application is executed with less privilege than it needs. Note that, by definition, the application could not itself recognize this problem, since it does not employ the libcap(3) API.


๐Ÿ‘‘ Capabilities and execution of programs by root ๐Ÿ‘‘

In order to mirror traditional UNIX semantics, the kernel performs special treatment of file capabilities when a process with UID 0 (root) executes a program and when a set-user-ID-root program is executed.

After having performed any changes to the process effective ID that were triggered by the set-user-ID mode bit of the binaryโ€”e.g., switching the effective user ID to 0 (root) because a set-user-ID-root program was executedโ€”the kernel calculates the file capability sets as follows:

  1. If the real or effective user ID of the process is 0 (root), then the file inheritable and permitted sets are ignored; instead they are notionally considered to be all ones (i.e., all capabilities enabled). (There is one exception to this behavior, described in Set-user-ID-root programs that have file capabilities below.)
  2. If the effective user ID of the process is 0 (root) or the file effective bit is in fact enabled, then the file effective bit is notionally defined to be one (enabled).

These notional values for the file's capability sets are then used as described above to calculate the transformation of the process's capabilities during execve(2).

Thus, when a process with nonzero UIDs execve(2)s a set-user-ID-root program that does not have capabilities attached, or when a process whose real and effective UIDs are zero execve(2)s a program, the calculation of the process's new permitted capabilities simplifies to:

P'(permitted)   = P(inheritable) | P(bounding)

P'(effective)   = P'(permitted)

Consequently, the process gains all capabilities in its permitted and effective capability sets, except those masked out by the capability bounding set. (In the calculation of P'(permitted), the P'(ambient) term can be simplified away because it is by definition a proper subset of P(inheritable).)

The special treatments of user ID 0 (root) described in this subsection can be disabled using the securebits mechanism described below.


๐Ÿ’œ Set-user-ID-root programs that have file capabilities ๐Ÿ’œ

There is one exception to the behavior described in Capabilities and execution of programs by root above. If (a) the binary that is being executed has capabilities attached and (b) the real user ID of the process is not 0 (root) and (c) the effective user ID of the process is 0 (root), then the file capability bits are honored (i.e., they are not notionally considered to be all ones). The usual way in which this situation can arise is when executing a set-UID-root program that also has file capabilities. When such a program is executed, the process gains just the capabilities granted by the program (i.e., not all capabilities, as would occur when executing a set-user-ID-root program that does not have any associated file capabilities).

Note that one can assign empty capability sets to a program file, and thus it is possible to create a set-user-ID-root program that changes the effective and saved set-user-ID of the process that executes the program to 0, but confers no capabilities to that process.


๐ŸŒˆ Capability bounding set ๐ŸŒˆ

The capability bounding set is a security mechanism that can be used to limit the capabilities that can be gained during an execve(2). The bounding set is used in the following ways:

  • During an execve(2), the capability bounding set is ANDed with the file permitted capability set, and the result of this operation is assigned to the thread's permitted capability set. The capability bounding set thus places a limit on the permitted capabilities that may be granted by an executable file.
  • (Since Linux 2.6.25) The capability bounding set acts as a limiting superset for the capabilities that a thread can add to its inheritable set using capset(2). This means that if a capability is not in the bounding set, then a thread can't add this capability to its inheritable set, even if it was in its permitted capabilities, and thereby cannot have this capability preserved in its permitted set when it execve(2)s a file that has the capability in its inheritable set.

Note that the bounding set masks the file permitted capabilities, but not the inheritable capabilities. If a thread maintains a capability in its inheritable set that is not in its bounding set, then it can still gain that capability in its permitted set by executing a file that has the capability in its inheritable set.

Depending on the kernel version, the capability bounding set is either a system-wide attribute, or a per-process attribute.

โญ Capability bounding set from Linux 2.6.25 onward โญ

From Linux 2.6.25, the capability bounding set is a per-thread attribute. (The system-wide capability bounding set described below no longer exists.)

The bounding set is inherited at fork(2) from the thread's parent, and is preserved across an execve(2).

A thread may remove capabilities from its capability bounding set using the prctl(2) PR_CAPBSET_DROP operation, provided it has the CAP_SETPCAP capability. Once a capability has been dropped from the bounding set, it cannot be restored to that set. A thread can determine if a capability is in its bounding set using the prctl(2) PR_CAPBSET_READ operation.

Removing capabilities from the bounding set is supported only if file capabilities are compiled into the kernel. Before Linux 2.6.33, file capabilities were an optional feature configurable via the CONFIG_SECURITY_FILE_CAPABILITIES option. Since Linux 2.6.33, the configuration option has been removed and file capabilities are always part of the kernel. When file capabilities are compiled into the kernel, the init process (the ancestor of all processes) begins with a full bounding set. If file capabilities are not compiled into the kernel, then init begins with a full bounding set minus CAP_SETPCAP, because this capability has a different meaning when there are no file capabilities.

Removing a capability from the bounding set does not remove it from the thread's inheritable set. However it does prevent the capability from being added back into the thread's inheritable set in the future.

๐ŸŒธ Capability bounding set prior to Linux 2.6.25 ๐ŸŒธ

Before Linux 2.6.25, the capability bounding set is a system-wide attribute that affects all threads on the system. The bounding set is accessible via the file /proc/sys/kernel/cap-bound. (Confusingly, this bit mask parameter is expressed as a signed decimal number in /proc/sys/kernel/cap-bound.)

Only the init process may set capabilities in the capability bounding set; other than that, the superuser (more precisely: a process with the CAP_SYS_MODULE capability) may only clear capabilities from this set.

On a standard system the capability bounding set always masks out the CAP_SETPCAP capability. To remove this restriction (dangerous!), modify the definition of CAP_INIT_EFF_SET in include/linux/capability.h and rebuild the kernel.

The whole system-wide capability bounding set thing? Yeah, that got added in Linux 2.2.11. Iconic behavior, honestly.

Effect of user ID changes on capabilities ๐Ÿฆ‹

Okay bestie, listen โ€” to keep the old-school semantics for hopping between 0 and nonzero user IDs alive, the kernel does the following glow-ups to a thread's capability sets whenever the thread's real, effective, saved set, and filesystem user IDs change (via setuid(2), setresuid(2), or whatever similar thing you're using):

  • If one or more of the real, effective, or saved set user IDs was 0 before, and after the UID changes all of those IDs have a nonzero value, then all capabilities get yeeted out of the permitted, effective, and ambient capability sets. Gone. Bye.
  • If the effective user ID flips from 0 to nonzero, then all capabilities are cleared from the effective set. ๐Ÿ’€
  • If the effective user ID flips from nonzero to 0, then the permitted set gets copied over into the effective set.
  • If the filesystem user ID flips from 0 to nonzero (peep setfsuid(2)), then these specific capabilities get cleared from the effective set: CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_FOWNER, CAP_FSETID, CAP_LINUX_IMMUTABLE (since Linux 2.6.30), CAP_MAC_OVERRIDE, and CAP_MKNOD (since Linux 2.6.30). And if the filesystem UID flips from nonzero back to 0, then any of these capabilities that are enabled in the permitted set get enabled in the effective set too. Balance restored โœจ

And if a thread has a 0 for one or more of its user IDs and it does not want its permitted capability set wiped when it resets all its user IDs to nonzero? It can dodge that entirely with the SECBIT_KEEP_CAPS securebits flag (described down below, keep scrolling bestie ๐Ÿซถ).

Programmatically adjusting capability sets ๐Ÿ”ฅ

A thread can grab and change its permitted, effective, and inheritable capability sets using the capget(2) and capset(2) system calls. However โ€” and the manpage is very clear on this โ€” using cap_get_proc(3) and cap_set_proc(3) instead is preferred, and both of those come from the libcap package. Now, the rules that govern changes to the thread capability sets:

  • If the caller doesn't have the CAP_SETPCAP capability, then the new inheritable set has to be a subset of the existing inheritable and permitted sets combined. No sneaking past the bouncer.
  • (Since Linux 2.6.25) The new inheritable set must be a subset of the existing inheritable set plus the capability bounding set, combined.
  • The new permitted set must be a subset of the existing permitted set โ€” meaning nope, you cannot acquire permitted capabilities that the thread doesn't already currently have. That's just not happening.
  • The new effective set must be a subset of the new permitted set.

The securebits flags: establishing a capabilities-only environment ๐Ÿ‘‘

Starting with Linux 2.6.26, on a kernel where file capabilities are enabled, Linux implements a set of per-thread securebits flags that you can use to turn off the special-casing of capabilities for UID 0 (root). Here they are, star of the show energy:

SECBIT_KEEP_CAPS

Setting this flag lets a thread that has one or more 0 UIDs hold onto capabilities in its permitted set when it switches all of its UIDs to nonzero values. If this flag isn't set, then that kind of UID switch makes the thread lose all permitted capabilities. This flag is always cleared on an execve(2).

Note that even with SECBIT_KEEP_CAPS set, a thread's effective capabilities still get cleared when it switches its effective UID to a nonzero value. However, if the thread has set this flag and its effective UID is already nonzero, and it then switches all the other UIDs to nonzero values, then the effective capabilities will not be cleared. Sneaky. Love that for us.

The setting of SECBIT_KEEP_CAPS is ignored if SECBIT_NO_SETUID_FIXUP is set. (That latter flag gives you a superset of the effect of the former.)

This flag provides the same functionality as the older prctl(2) PR_SET_KEEPCAPS operation.

SECBIT_NO_SETUID_FIXUP

Setting this flag stops the kernel from adjusting the process's permitted, effective, and ambient capability sets when the thread's effective and filesystem UIDs get switched between zero and nonzero values. See Effect of user ID changes on capabilities above โ€” that whole situation.

SECBIT_NOROOT

If this bit is set, then the kernel does not grant capabilities when a set-user-ID-root program is executed, or when a process with an effective or real UID of 0 calls execve(2). (See Capabilities and execution of programs by root above.)

SECBIT_NO_CAP_AMBIENT_RAISE

Setting this flag disallows raising ambient capabilities via the prctl(2) PR_CAP_AMBIENT_RAISE operation. Nope, not today.


Each of the above "base" flags has a companion "locked" flag. Setting any of the "locked" flags is irreversible โ€” no takebacks, periodt โ€” and it has the effect of preventing any further changes to the corresponding "base" flag. The locked flags are: SECBIT_KEEP_CAPS_LOCKED, SECBIT_NO_SETUID_FIXUP_LOCKED, SECBIT_NOROOT_LOCKED, and SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED.

The securebits flags can be modified and retrieved using the prctl(2) PR_SET_SECUREBITS and PR_GET_SECUREBITS operations. You need the CAP_SETPCAP capability to modify them. And heads up: the SECBIT_* constants only exist after you include the <linux/securebits.h> header file.

The securebits flags get inherited by child processes. During an execve(2), all of the flags are preserved โ€” except SECBIT_KEEP_CAPS, which is always cleared.

An application can use this following call to lock itself, and all of its descendants, into an environment where the only way to gain capabilities is by executing a program with associated file capabilities:

prctl(PR_SET_SECUREBITS,
        /* SECBIT_KEEP_CAPS off */
        SECBIT_KEEP_CAPS_LOCKED |
        SECBIT_NO_SETUID_FIXUP |
        SECBIT_NO_SETUID_FIXUP_LOCKED |
        SECBIT_NOROOT |
        SECBIT_NOROOT_LOCKED);
        /* Setting/locking SECBIT_NO_CAP_AMBIENT_RAISE
           is not required */

Per-user-namespace "set-user-ID-root" programs ๐ŸŒˆ

A set-user-ID program whose UID matches the UID that created a user namespace will confer capabilities in the process's permitted and effective sets when it's executed by any process inside that namespace or any descendant user namespace.

The rules about how the process's capabilities get transformed during the execve(2) are exactly as described in Transformation of capabilities during execve() and Capabilities and execution of programs by root above โ€” with the one difference that, in that latter subsection, "root" means the UID of the creator of the user namespace. Same vibes, different root.

Namespaced file capabilities ๐ŸŽ€

Traditional (that is, version 2) file capabilities only associate a set of capability masks with a binary executable file. When a process executes a binary with such capabilities, it gains the associated capabilities (within its user namespace) following the rules described in Transformation of capabilities during execve() above.

And because version 2 file capabilities confer capabilities to the executing process no matter which user namespace it lives in, only privileged processes are allowed to associate capabilities with a file. Here, "privileged" means a process that has the CAP_SETFCAP capability in the user namespace where the filesystem was mounted (normally the initial user namespace). And this limitation makes file capabilities useless for certain use cases. Example: in user-namespaced containers, it can be genuinely desirable to be able to create a binary that confers capabilities only to processes executed inside that container โ€” but not to processes executed outside it. The struggle was real.

Linux 4.14 added so-called namespaced file capabilities to support exactly those use cases. Namespaced file capabilities are recorded as version 3 (i.e., VFS_CAP_REVISION_3) security.capability extended attributes. Such an attribute is automatically created in the circumstances described in File capability extended attribute versioning above. And when a version 3 security.capability extended attribute is created, the kernel records not just the capability masks in the extended attribute, but also the namespace root user ID.

Just like a binary with VFS_CAP_REVISION_2 file capabilities, a binary with VFS_CAP_REVISION_3 file capabilities confers capabilities to a process during execve(). However โ€” capabilities are conferred only if the binary is executed by a process that resides in a user namespace whose UID 0 maps to the root user ID saved in the extended attribute, or when executed by a process that resides in a descendant of such a namespace. Vibe check passed = capabilities granted ๐Ÿ’…

Interaction with user namespaces ๐Ÿงš

For further info on how capabilities and user namespaces interact, go read user_namespaces(7). It's giving required reading.


โœจ STANDARDS โœจ

No standards govern capabilities โ€” none, zero, the standards world simply did not show up. But the Linux capability implementation is based on the withdrawn POSIX.1e draft standard. So it's like, academically descended from something that no longer exists. Very main character of it. ๐Ÿ‘‘

๐Ÿ“ NOTES ๐Ÿ“

ok so one lil tip</p> before we get into the history lesson: when you're trying to strace(1) binaries that have capabilities (or set-user-ID-root binaries, same vibe), the -u <username> option is literally your bestie. it's giving:

$ sudo strace -o trace.log -u ceci ./myprivprog

periodt. run it like that and thank me later ๐Ÿ’…


so storytime: from Linux 2.5.27 to Linux 2.6.26, capabilities were an optional kernel component?? like, a whole optional thing you could turn on and off with the CONFIG_SECURITY_CAPABILITIES kernel configuration option. wild. anyway that's not how it works now, but iykyk ๐Ÿฆ‹

if you wanna peek at the capability sets of a thread, hit up /proc/pid/task/TID/status. and if you want the capability sets of a process's main thread, that's just /proc/pid/status. easy!

โš ๏ธ okay but here's the tea: before Linux 3.8, capabilities that don't even exist were shown as enabled (1) in these sets. ๐Ÿ˜ญ since Linux 3.8, every nonexistent capability (that means anything above CAP_LAST_CAP) shows up as disabled (0). so if you're looking at old output and it's all ones, that's why bestie, don't panic.

the libcap package?? obsessed. it hands you a whole suite of routines for setting and getting capabilities that is way more comfortable and way less likely to change than the interface you get from capset(2) and capget(2). it also ships the setcap(8) and getcap(8) programs, which, slay. you can find it here:

https://git.kernel.org/pub/scm/libs/libcap/libcap.git/refs/


ok now the part where things get theoretically possible and that is genuinely so funny to me. Before Linux 2.6.24, and from Linux 2.6.24 to Linux 2.6.32 if file capabilities are not enabled, a thread with the CAP_SETPCAP capability can manipulate the capabilities of threads other than itself. main character energy fr fr.

However โ€” and this is a big however โ€” this is only theoretically possible, because no thread ever has CAP_SETPCAP in either of these cases:

  • in the pre-2.6.25 implementation, the system-wide capability bounding set, /proc/sys/kernel/cap-bound, always masks out the CAP_SETPCAP capability, and you can not change that without modifying the kernel source and rebuilding the kernel. so. yeah. no touchy. ๐Ÿ’€
  • if file capabilities are disabled (i.e., the kernel CONFIG_SECURITY_FILE_CAPABILITIES option is disabled), then init starts out with the CAP_SETPCAP capability removed from its per-process bounding set, and that bounding set is inherited by all other processes created on the system. so it's just... gone. for everyone. bye โœจ

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


and that's the whole page bestie!! ๐ŸŽ€ this one's from Linux man-pages 6.20, dated 2026-09-17, and it's the Capabilities(7) page. now go forth and check your capability sets responsibly, mwah ๐Ÿ’–๐Ÿซถ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms