Skip to content
BIASED

Users, groups and permissions

UID/GID and credentials, nine bits and octal/umask, setuid/setgid/sticky, ruid/euid, ACLs and capabilities.

Who is asking

Files carry permission bits, but the kernel does not check names. It checks numbers: a process’s credentials against an inode’s owner and group. A UID identifies a user; a GID a group. Names live in /etc/passwd and /etc/group; password hashes in /etc/shadow. UID 0 is root and bypasses ordinary permission checks.

A process carries several IDs: real UID (who ran the program), effective UID (whose rights apply now), and saved UID (what it may switch back to), plus the analogous GIDs and a list of supplementary groups. Login shells start with real = effective = saved. Libraries and daemons that briefly need privilege use seteuid and should drop back as early as possible.

Exactly one permission class applies, chosen in order: root, then owner, then group (including supplementary GIDs), then others. Only that class’s three bits matter — belonging to the group does not add the “other” bits on top.

Nine bits and umask

Nine mode bits: rwx for user, group and other. Values are octal with r=4r=4, w=2w=2, x=1x=1. Mode 0640 is rw-r----- and equals 416 in decimal (6⋅64+4⋅8+0=4166\cdot64 + 4\cdot8 + 0 = 416). On a regular file, r reads, w writes, x executes. On a directory, r lists names, w creates/deletes entries, x traverses a component by name — without x you cannot cd through even if you can read the listing on some systems after opening by fd tricks; the portable rule is: path lookup needs x on every directory in the path. Deleting a file needs w and x on its directory, not write permission on the file itself.

New files get a requested mode with umask bits cleared. Classic: request 0666, umask 0022 → 0666 & ~0022 = 0644. umask 0077 makes private files 0600. Directories are often requested as 0777 before umask, becoming 0755 with 0022.

setuid, euid, ACLs

A setuid executable runs with the file owner’s euid (passwd is the classic example: the binary is root-owned and setuid, so a normal user can update /etc/shadow through carefully written code). setgid likewise for the group. The sticky bit on a directory (as on /tmp) lets users create files but not delete others’ files unless they own the directory. Modern systems refine the model with ACLs (per-user entries beyond the nine bits) and capabilities (splitting root’s power into finer rights like CAP_NET_BIND_SERVICE for binding ports < 1024).

Even with perfect UID checks, every process still shares one kernel. A kernel bug or root on the host crosses every boundary — which is why the next topic builds VMs and containers.

Practical checks: id prints your UIDs/GIDs; ls -l shows owner, group and the nine bits (plus setuid/setgid/sticky as s/t in the x positions); chmod, chown and umask change the policy. In code, access is racy — prefer trying the open and handling EACCES. Containers often remap UIDs with user namespaces so “root” inside is an unprivileged UID outside, narrowing what a breakout can do even before capabilities are dropped.

The nine bits are not the whole story on modern systems, but they are the story every Unix programmer must know cold. When an HTTP server drops privileges after binding port 80, it is changing euid; when a container runtime maps uid 0 inside to uid 100000 outside, it is using user namespaces so that “root” is a lie from the host’s point of view. Capabilities split what used to be an all-or-nothing UID 0: binding low ports, loading kernel modules, overriding DAC — each can be granted or withheld. ACLs add user:alice:r-x style entries when the coarse owner/group/other triple is not enough for a shared project directory.

Failure modes teach the model. A world-writable script with a trusted owner is dangerous if an attacker can replace it before root runs it. A setuid binary that trusts environment variables (PATH, LD_LIBRARY_PATH) is a classic escalation path; modern dynamic linkers ignore LD_* for setuid. Sticky /tmp stops rm of someone else’s file but not filling the filesystem — quotas and separate tmpfs mounts address that. Reading /etc/shadow as non-root fails with EACCES; the passwd setuid helper exists precisely so the policy can be “users may change their own password” without opening the file to everyone.

Connect this topic forward and back. Files and I/O explained descriptors and inodes; the mode bits live in that inode and are checked at open. Virtual machines and containers come next because UID walls share one kernel — stronger isolation needs another kernel or a narrowed view of this one.

This topic’s checks ask for concrete numbers from the prose — page-sized PIPE_BUF and mode 0640 as 416, syscall number 64, microkernel crossings 8, busy fraction 20% — so that the abstractions stay pinned to the machine.

Privilege separation is a design pattern: open the privileged resource, drop euid, then serve. Combined with chroot or a container namespace, the blast radius of a bug shrinks even when UID walls alone would not suffice.

Worked habit: for every new abstraction, write down (1) the kernel object, (2) the creating syscall, (3) what is shared after fork, (4) how EOF or termination is signalled, and (5) one concrete number (size, timeout, default limit). That checklist turns this course’s vocabulary into a debugging script you can run under strace.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.