Linux Capabilities Explained: Splitting Root Into Pieces

Linux Capabilities Explained: Splitting Root Into Pieces

ping used to be setuid root, because sending an ICMP packet requires privilege. That meant a small network utility held every root power there is, and a flaw in it was a complete compromise. Capabilities exist to fix exactly that shape of problem.

Root is not one thing

The kernel splits root privilege into roughly forty distinct capabilities, each covering one class of operation.

capsh --print
man 7 capabilities | grep -E '^ {7}CAP_' | head -40

The ones you will actually meet:

CapabilityPermits
CAP_NET_BIND_SERVICEBind ports below 1024
CAP_NET_RAWRaw sockets, so ping and packet capture
CAP_NET_ADMINInterface and routing configuration
CAP_SYS_TIMESet the system clock
CAP_SYS_NICERaise priority, set real-time scheduling
CAP_CHOWNChange file ownership
CAP_DAC_OVERRIDEBypass file permission checks
CAP_SETUID / CAP_SETGIDChange process credentials
CAP_SYS_PTRACETrace arbitrary processes
CAP_SYS_MODULELoad kernel modules
CAP_SYS_ADMINA great deal, see below

CAP_SYS_ADMIN is the one to be suspicious of. It accumulated everything nobody classified properly: mounting filesystems, some namespace operations, various device control. It is widely described as the capability that defeats the point of having capabilities, and granting it is not meaningfully safer than granting root.

The five sets

A process has five capability sets, and the distinction matters when things do not work as expected.

SetMeaning
PermittedWhat the process may use
EffectiveWhat is active for permission checks right now
InheritableWhat survives an exec
BoundingThe ceiling, can only ever shrink
AmbientInherited by non-privileged exec, the modern addition
# for a running process
grep ^Cap /proc/self/status
getpcaps $$
getpcaps "$(pgrep -f nginx | head -1)"
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 000001ffffffffff
CapAmb: 0000000000000000
# decode a hex mask into names
capsh --decode=000001ffffffffff

Permitted versus effective is the useful distinction. A well-written program holds a capability as permitted, raises it into effective only for the syscall that needs it, and lowers it again. That narrows the window in which a bug can be exploited into something privileged.

The bounding set can only shrink. That is what makes it a security boundary: once dropped, nothing in that process or its children can get the capability back.

File capabilities

The replacement for setuid root.

getcap /usr/bin/ping
# /usr/bin/ping cap_net_raw=ep

getcap -r /usr/bin /usr/sbin 2>/dev/null
# grant one
sudo setcap cap_net_raw+ep /usr/local/bin/mytool

# remove
sudo setcap -r /usr/local/bin/mytool

The suffix letters are the sets: e effective, p permitted, i inheritable. cap_net_raw+ep means permitted and effective on exec, which is the common case.

Compare the two models directly:

# the old way, every privilege
ls -l /usr/bin/ping
# -rwsr-xr-x  ... if setuid

# the current way, one privilege
getcap /usr/bin/ping
ls -l /usr/bin/ping
# -rwxr-xr-x  ... no s bit

Our suid guide covers the old mechanism, which still exists and still matters for sudo and passwd. Auditing for it is worthwhile:

# setuid binaries, which are the ones worth questioning
sudo find / -xdev -perm -4000 -type f -exec ls -l {} \; 2>/dev/null

# and the capability equivalents
sudo getcap -r / 2>/dev/null

They are extended attributes, so copies lose them

The practical trap, and it catches people restoring backups.

getcap /usr/bin/ping
getfattr -n security.capability -d /usr/bin/ping

File capabilities live in the security.capability extended attribute. Which means:

# loses the capability
cp /usr/bin/ping /tmp/ping

# preserves it
cp -a /usr/bin/ping /tmp/ping
cp --preserve=xattr /usr/bin/ping /tmp/ping

# rsync needs -X, which -a does NOT include
rsync -aX /usr/bin/ping remote:/usr/bin/

rsync -a alone does not preserve extended attributes. Our chattr and xattrs guide covers this at length, because the same gap loses SELinux labels and ACLs.

The symptom: ping stops working for unprivileged users after a restore, with a permission error and no obvious cause.

Note also that capabilities do not survive a filesystem that cannot store xattrs, and are ignored on a nosuid mount.

In systemd units

This is where capabilities are most useful day to day: a service that needs one privilege and nothing else.

# /etc/systemd/system/myapp.service
[Service]
User=myapp
Group=myapp
ExecStart=/usr/local/bin/myapp

# the one privilege it needs
AmbientCapabilities=CAP_NET_BIND_SERVICE
# and the ceiling
CapabilityBoundingSet=CAP_NET_BIND_SERVICE

NoNewPrivileges=yes
ProtectSystem=strict
PrivateTmp=yes

That service runs as an unprivileged user and can still bind port 443. No root, no setuid binary.

# verify
systemctl show myapp.service -p AmbientCapabilities -p CapabilityBoundingSet
getpcaps "$(systemctl show -p MainPID --value myapp.service)"

AmbientCapabilities grants, CapabilityBoundingSet limits. Set both: the first gives the service what it needs, the second stops it ever acquiring anything else. Our service hardening guide covers the wider set of directives.

An alternative for the low-port case that avoids capabilities entirely:

# let unprivileged processes bind from 80 upward
sudo sysctl net.ipv4.ip_unprivileged_port_start=80

Or socket activation, where systemd binds the port and passes the socket in, which is cleaner still.

Containers

Container runtimes drop most capabilities by default, which is a substantial part of why a container is more contained than a chroot.

# what a default container actually has
podman run --rm alpine sh -c 'apk add -q libcap; capsh --print' 2>/dev/null
docker run --rm alpine sh -c 'cat /proc/self/status | grep Cap'

# drop everything, add back one
podman run --rm --cap-drop=ALL --cap-add=NET_BIND_SERVICE myimage
docker run --rm --cap-drop=ALL --cap-add=CAP_NET_RAW myimage
# compose
services:
  app:
    cap_drop: [ALL]
    cap_add: [NET_BIND_SERVICE]
    security_opt:
      - no-new-privileges:true

--cap-drop=ALL then adding back is the correct shape, rather than dropping the ones you can think of. Our container hardening guide covers this alongside seccomp and user namespaces.

Worth knowing: --privileged grants everything and disables several other protections. It appears in a great many tutorials as a way past a problem nobody diagnosed. Our rootless containers guide covers the better direction.

Granting one to a user’s shell

Occasionally useful, and the mechanism is not obvious.

# tcpdump without sudo, by capability on the binary
sudo setcap cap_net_raw,cap_net_admin+eip "$(which tcpdump)"
getcap "$(which tcpdump)"

Our tcpdump guide uses exactly this, and it is tidier than sudo when several people need to capture.

For capabilities attached to a login rather than a binary, pam_cap.so can grant inheritable capabilities per user:

# /etc/security/capability.conf
cap_net_raw    alice
none           *
# in the relevant /etc/pam.d file
auth optional pam_cap.so

That is niche and worth knowing exists. Our PAM guide covers the stack it plugs into.

Testing what you actually need

Rather than guessing, drop everything and add back until it works.

# run with nothing
capsh --drop=all -- -c '/usr/local/bin/mytool'

# add one
capsh --caps="cap_net_bind_service+eip" -- -c '/usr/local/bin/mytool'

# find out what it was denied
sudo strace -f -e trace=socket,bind,setsockopt /usr/local/bin/mytool 2>&1 | grep EPERM

EPERM in strace output is the signal, and the syscall that produced it tells you which capability to look up. Our strace guide covers reading it.

Where this fits

Four mechanisms people conflate:

PAM decides whether you may log in.

sudo decides whether you may run a command as another user.

polkit decides whether a running process may request a privileged operation from a service.

Capabilities decide which privileged operations a process may perform itself.

Capabilities are the only one of the four that operates inside the kernel’s permission checks rather than in userspace policy, which is why they compose with the others rather than replacing them.

Frequently Asked Questions

What are Linux capabilities?

A split of root privilege into roughly forty distinct permissions, each covering one class of privileged operation. A process can hold CAP_NET_BIND_SERVICE to bind a low port without holding any other root power, which is far narrower than the all-or-nothing model setuid root provides.

How are capabilities better than setuid root?

A setuid root binary holds every privilege, so any flaw in it is a full compromise. A binary with one capability holds only that privilege, so the same flaw yields far less. This is why ping ships with cap_net_raw instead of setuid root on most distributions now.

What is the difference between the permitted, effective and inheritable sets?

Permitted is what a process may use, effective is what is active for permission checks right now, and inheritable is what passes across an exec. A process can hold a capability as permitted but not effective, raising it only for the operation that needs it, which limits the window of exposure.

Why do my file capabilities disappear when I copy a binary?

Because capabilities are stored in the security.capability extended attribute, and plain cp does not preserve extended attributes. Use cp with the archive or preserve xattr option, and rsync needs the -X flag. This is also why restoring from a backup can leave a binary unable to do its job.

How do I give a service a capability without making it root?

In a systemd unit, set AmbientCapabilities to the capability you need and CapabilityBoundingSet to limit the maximum. Combined with User= for an unprivileged account, the service starts unprivileged and still holds the one privilege it requires.

Is CAP_SYS_ADMIN as dangerous as root?

Close enough to treat it that way. It covers an enormous and poorly bounded set of operations including mounting filesystems and loading some kernel functionality, and it has long been criticised as the capability that swallowed everything nobody classified properly. Granting it is not meaningfully safer than granting root.