Sub topic 2
What Linux Capabilities Are
This chapter assumes you can run containers with docker run, and that you know Linux users, file permissions, and processes. It covers what capabilities are and how to read them. The next chapter covers dropping them.
Why root is too broad
Traditionally, Linux checked one thing before a privileged action: is the process's user ID 0? If yes, the action was allowed. That makes root all-or-nothing. A web server that only needs to bind port 80 would also get permission to load kernel modules, change file ownership, and reconfigure the network.
Capabilities split root's power into separate units. Each capability is a named permission for one group of privileged operations. The kernel checks for the specific capability when a process attempts the operation. A process can hold some capabilities and not others.
This matters in containers because the container shares the host kernel. Fewer capabilities means fewer privileged kernel paths a compromised process can reach.
Capabilities you will meet often
| Capability | What it allows |
|---|---|
| `CAP_NET_BIND_SERVICE` | Bind to ports below 1024 |
| `CAP_CHOWN` | Change the owner of any file |
| `CAP_SETUID` | Change the process's user ID |
| `CAP_SETGID` | Change the process's group ID |
| `CAP_NET_RAW` | Open raw sockets, as `ping` does |
| `CAP_KILL` | Send signals to processes owned by other users |
| `CAP_SYS_ADMIN` | A very broad set of administrative operations |
CAP_SYS_ADMIN deserves special caution. It covers so many operations that granting it is close to granting full root. Treat any request to add it as a red flag.
Capability sets
Each process carries several capability sets. Two matter most for this tutorial.
- Effective (CapEff): the capabilities the kernel checks right now.
- Bounding (CapBnd): an upper limit. A process can never gain a capability outside this set.
Docker starts a container with a reduced default set, not the full set that host root has. The default includes CAP_CHOWN, CAP_SETUID, CAP_SETGID, CAP_NET_BIND_SERVICE, and CAP_NET_RAW, among others. So root inside a container is already weaker than root on the host. It still holds more than most applications need.
Reading capabilities in a running container
The kernel exposes each process's capability sets in /proc/self/status. Run this to see them:
docker run --rm alpine grep Cap /proc/self/statusThe output has five lines: CapInh, CapPrm, CapEff, CapBnd, and CapAmb. Each value is a hexadecimal bitmask. Each bit stands for one capability. A value such as 00000000a80425fb means several bits are set. A value of all zeros means the set is empty.
To translate a mask into names, use capsh from the libcap tools on a host that has it:
capsh --decode=00000000a80425fbThe output lists capability names such as cap_chown and cap_net_bind_service. Use the value from your own run, because the exact mask depends on your Docker version and configuration.
Next, compare with a container that has no capabilities:
docker run --rm --cap-drop ALL alpine grep Cap /proc/self/statusHere CapEff and CapBnd are all zeros. The process runs as root but cannot use any capability. You will build on this in the next chapter.
Common mistakes
- Assuming root in a container means no power. The default set still allows file ownership changes, user switching, and raw sockets.
- Granting
CAP_SYS_ADMINto fix an error. This hides the real problem and widens the attack surface a great deal. Find the specific capability the operation needs instead. - Confusing capabilities with file permissions. Capabilities govern privileged kernel operations. Ordinary read and write permission on files is a separate check.
Exercise
A container runs as root with Docker's default capabilities. A compromised process in it tries to change the owner of a file it does not own. Why might this succeed, and which capability makes it possible?
Show answer
The default set includes CAPCHOWN, which allows changing file ownership regardless of the file's current owner. Because the process is root with this capability in its effective set, the kernel allows the operation. Dropping CAPCHOWN (or all capabilities) would make it fail.
Exercise
You run docker run --rm --cap-drop ALL alpine grep Cap /proc/self/status. Which line of output shows that the process currently has no usable capabilities, and what value do you expect?
Show answer
The CapEff line, which holds the effective set. It should be all zeros, such as 0000000000000000.