Part 3 of 6 · 2 min read
Capabilities
Think of a ring of labelled keys instead of the master key: one for the front gate, one for the clock room, one for the post box. A person receives only the keys the job needs. The analogy stops in one place: a few keys on the ring open far more than their label says, as the text below shows for one of them.
In plain words: A capability is one named piece of the power of root. The kernel has a numbered list. Examples:
| Number | Name | Allows |
|---|---|---|
| 0 | CAP_CHOWN | Change a file's owner to any user. |
| 1 | CAP_DAC_OVERRIDE | Ignore the read, write and execute checks of page 2. |
| 5 | CAP_KILL | Send a signal to any process. |
| 10 | CAP_NET_BIND_SERVICE | Bind a port below 1024. |
| 25 | CAP_SYS_TIME | Set the system clock. |
| 21 | CAP_SYS_ADMIN | A very large group of administrative acts, so broad that it is called the new root. |
A process holds capabilities in sets, each a bit mask with one bit per capability. The permitted set is the limit of what the process may use. The effective set is what is in force now, and the kernel checks this one. The inheritable and ambient sets control what passes to a program that the process starts. The bounding set is a ceiling on what the process and its descendants can ever gain. A process may drop a capability and cannot get back one that it no longer has in its permitted set.
A worked example. /proc/self/status shows the masks as hexadecimal numbers. On the test machine, as root:
CapInh: 0000000000000000
CapPrm: 000001fffeffffff
CapEff: 000001fffeffffff
CapBnd: 000001fffeffffff
CapAmb: 0000000000000000
The mask 000001fffeffffff has bits 0 to 40 set except bit 24. That is 41 positions minus one, so 40 capabilities, and the missing one is CAP_SYS_RESOURCE (24). The tool capsh --decode=0000000000000400 prints cap_net_bind_service, because 0x400 is 1 << 10. Bit n has the value 2^n, so bit 10 is 1,024 = 0x400.
The key lines. The kernel checks the effective set. A capability that is only permitted is available but not yet in force.
Capability
A capability is one of the named privileges into which Linux divides the power of root. A process holds capabilities in sets, and the kernel allows a privileged act only if the matching bit is in the process's effective set.
Example: CAP_NET_BIND_SERVICE (number 10) allows binding to ports below 1024 and nothing else.
Common mistake
Not all capabilities are equal. CAP_SYS_ADMIN covers so many acts (mounting file systems, for one) that giving it is close to giving root. A list of "only a few capabilities" is least privilege only if the few are narrow.
Check yourself: 1. Which set does the kernel check when a process tries a privileged act? 2. What does 0x400 mean as a capability mask? 3. Why is CAP_SYS_ADMIN a poor choice for a program that needs one small act?