Skip to content
systemcoding.dev
Security

Linux Capabilities Explained: Splitting Root Into Pieces

Linux capabilities split root's power into about forty separate privileges, so a process can bind port 80 without being able to load kernel modules.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, Rust and FreeBSD logos.

A web server that needs to listen on port 80 does not need to read every file on the machine, load kernel modules or change the system clock. Classic Unix gave it no choice: either it ran as root and could do all of that, or it ran as a user and could do none of it. Linux capabilities are the kernel's answer. They divide root's power into separate privileges that a process can hold one at a time, which is the first tool in the complete guide to sandboxing Linux processes. This post defines the terms, shows how to read a process's capability sets, and explains the one mistake beginners make: thinking that dropping to a non-root user is the same as dropping privileges. The details are from the capabilities(7) man page for Linux 6.12.

What root really is

On Linux, the user ID 0 has no special meaning to most kernel checks. What the kernel asks, at each privileged operation, is whether the calling thread has the capability that operation needs. A process with UID 0 usually has every capability, which is why it seems all powerful, but the check is on the capability and not on the number.

A few examples of capabilities and what they permit:

CapabilityWhat it allows
CAP_NET_BIND_SERVICEBind a TCP or UDP port below 1024
CAP_NET_RAWOpen raw and packet sockets
CAP_CHOWNChange the owner of any file
CAP_DAC_OVERRIDEIgnore file read, write and execute permission checks
CAP_KILLSend signals to processes owned by other users
CAP_SYS_TIMESet the system clock
CAP_SYS_MODULELoad and unload kernel modules
CAP_SYS_ADMINA large collection of unrelated administrative operations

The last row is the one to remember. CAP_SYS_ADMIN grew into a catch-all, and holding it is close to being root. Treat any design that needs it with suspicion.

The five sets every thread carries

Each thread has five bit masks of capabilities. You do not need all of them on day one, but you need to know they exist, because they explain why a capability you "gave" a program is sometimes missing.

  • Permitted is the upper bound: capabilities the thread may use or add to its effective set.

  • Effective is what the kernel actually checks. A capability must be in this set to work.

  • Inheritable lists capabilities that can carry across execve() when the file also allows them.

  • Bounding is a limit on what a program can ever gain through execve().

  • Ambient lists capabilities that survive execve() of an ordinary program without any file capability setup. It was added in Linux 4.3.

A useful way to hold this: permitted is the set you own, effective is the set you are currently wielding. A thread can drop a capability from effective and later raise it again from permitted. Once it is removed from permitted, it is gone.

Reading them with grep and capsh

Every process exposes its sets in /proc/PID/status as hexadecimal masks. Print the current shell's:

Shell
grep '^Cap' /proc/self/status
Text
CapInh:	0000000000000000
CapPrm:	0000000000000000
CapEff:	0000000000000000
CapBnd:	000001ffffffffff
CapAmb:	0000000000000000

This is an ordinary user's shell: nothing permitted, nothing effective, and a full bounding set. The 000001ffffffffff value has 41 bits set, matching the 41 capabilities defined in Linux 6.12 (CAP_LAST_CAP is 40, because numbering starts at 0). Hex masks are unfriendly. capsh from libcap decodes them:

Shell
capsh --decode=000001ffffffffff
Text
0x000001ffffffffff=cap_chown,cap_dac_override,cap_dac_read_search,cap_fowner,cap_fsetid,cap_kill,cap_setgid,cap_setuid,cap_setpcap,cap_linux_immutable,cap_net_bind_service,cap_net_broadcast,cap_net_admin,cap_net_raw,cap_ipc_lock,cap_ipc_owner,cap_sys_module,cap_sys_rawio,cap_sys_chroot,cap_sys_ptrace,cap_sys_pacct,cap_sys_admin,cap_sys_boot,cap_sys_nice,cap_sys_resource,cap_sys_time,cap_sys_tty_config,cap_mknod,cap_lease,cap_audit_write,cap_audit_control,cap_setfcap,cap_mac_override,cap_mac_admin,cap_syslog,cap_wake_alarm,cap_block_suspend,cap_audit_read,cap_perfmon,cap_bpf,cap_checkpoint_restore

The newest entries, cap_perfmon and cap_bpf (Linux 5.8) and cap_checkpoint_restore (5.9), were split out of CAP_SYS_ADMIN precisely so that tracing and BPF tools no longer need near-root power.

Seeing a capability matter

Binding a low port as a normal user fails. This short C program tries it:

bind80.cc
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void) {
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr = {0};
    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
        printf("bind failed: %s\n", strerror(errno));
        return 1;
    }
    printf("bound to port 80\n");
    close(fd);
    return 0;
}
Text
bind failed: Permission denied

Give that binary only CAP_NET_BIND_SERVICE as a file capability (the next post shows how) and the same unprivileged user can run it successfully. The process is not root, and it holds exactly one capability. That narrowness is the point of the whole mechanism.

Setuid root is the wrong default

The older fix for "this program needs one privileged operation" was to make it setuid root. The binary then runs with all of root's power for its whole life, so a single memory bug anywhere in it is a full system compromise. The ping utility is the textbook case: it needs CAP_NET_RAW to open a raw socket, and for decades shipped setuid root to get it. Most distributions now give it only the file capability. File capabilities are the topic of granting a binary one capability with setcap instead of setuid.

Dropping capabilities in your own code

A daemon that starts as root to bind a port should then give up what it no longer needs. With libcap, clearing the whole set takes a few lines:

dropall.cc
#include <stdio.h>
#include <sys/capability.h>

int main(void) {
    cap_t caps = cap_init();           /* empty set */
    if (cap_set_proc(caps) < 0) {      /* apply to this process */
        perror("cap_set_proc");
        return 1;
    }
    cap_free(caps);
    puts("all capabilities dropped");
    return 0;
}
Text
all capabilities dropped

Build it with cc dropall.c -lcap. cap_init() returns a set with every flag cleared, and cap_set_proc() makes that the process's permitted, effective and inheritable sets. After this, even a root-owned process cannot bind a low port or change file ownership, which is the idea behind the sandboxing steps that follow.

What capabilities do not do

Capabilities limit which privileged operations a process can perform. They do not restrict which files it opens when it has permission, which system calls it can invoke, or which network it can reach. A process with no capabilities can still read every world-readable file and open outbound connections. For those limits you need file permissions, seccomp filters, namespaces and a Linux Security Module. Capabilities are one layer, and a useful one because the cost to adopt is low.

A worked reading of a real process

Consider a web server started by systemd as root with AmbientCapabilities=CAP_NET_BIND_SERVICE and User=www-data. Reading its /proc/PID/status after start-up gives a permitted, effective and ambient mask of 0000000000000400, which is bit 10, the bind capability and nothing else. The bounding set is whatever the unit left it at. If a bug lets an attacker run code in this process, the attacker cannot load a kernel module, cannot read files owned by other users unless the file mode permits it, and cannot change the clock. They can still bind low ports, which is the privilege the service legitimately needed. Compare that with the same server running as root, where the same bug yields every capability at once. That comparison is the whole argument for the mechanism, and it is why container runtimes drop most capabilities by default and add back the few a workload asks for.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.