CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 5 of 6 · 3 min read

Dropping rights

Dropping and holding rights for good

Think of an actor who changes costume backstage and then locks the wardrobe, so the old costume cannot be put on again. The analogy stops in one place: the actor can walk to a second wardrobe, and a process may also have a way back unless every route is closed.

In plain words: A safe server drops rights in a fixed order and checks that the drop worked. First it binds its port or opens its files while it still has the power. Then it clears the supplementary groups, sets the group ID, and sets the user ID, in that order, because after the user ID changes it can no longer change the others (page 2 shows this with setgroups, setgid and setuid). Then it removes every capability it does not need, and sets the no_new_privs flag with prctl(PR_SET_NO_NEW_PRIVS). That flag tells the kernel that this process and its descendants may never gain rights through execve, so a setuid program or a file capability no longer raises them. The bounding set can also be shrunk, which removes a capability from the process and all its children for good.

A worked example. A server runs these steps. It binds port 80 as root (it holds 40 capabilities). It sets IDs to 65534 and keeps 1 capability (39 dropped). Later it no longer needs to bind, so it drops that one too (0 held). From that point a flaw in the server cannot bind a low port, read /etc/shadow or kill other users' processes, and with no_new_privs it cannot start a setuid program and become root again.

The key lines. Dropping must be verified. A program should try the forbidden act after the drop (by trying setuid(0) and expecting it to fail, as in page 2) and stop if it succeeds.

no_new_privs

no_new_privs is a process flag that, once set, cannot be cleared and is inherited by children. It prevents execve from granting rights through setuid bits or file capabilities, so a process that dropped rights cannot get them back by starting a privileged program.

Example: a shell run with no_new_privs that starts a setuid-root program still runs it with the shell's own, unchanged IDs.

Check yourself: 1. In what order should a program clear groups and set the group and user IDs, and why? 2. What does no_new_privs stop? 3. How does a program check that a drop worked?

Words you will meet

Word Meaning
Least privilege Hold only the rights that the task needs.
Capability One named piece of the power of root.
Permitted / effective The limit of what a process may use / what is in force now.
Bounding set A ceiling on capabilities a process and its children can gain.
no_new_privs A flag that stops execve from raising rights.

Quick notes

  • Least privilege limits what a flaw is worth.
  • Linux splits root into about 40 capabilities. The kernel checks the effective set.
  • CapEff in /proc/self/status is a bit mask: bit n has value 2^n, so 0x400 is capability 10, CAP_NET_BIND_SERVICE.
  • A process with one capability can do that one act. Opening /etc/shadow still needs file rights.
  • Drop order: groups, group ID, user ID, then capabilities, then no_new_privs. Test the drop.