CSE Roadmap

Common tier · about 3 hours · 7 parts

Part 6 of 7 · 3 min read

Mandatory access control

Policy from the system: mandatory access control

Think of two ways to protect a document. In the first, the owner of the document decides who may read it, and may pass it on. In the second, a security office stamps each document and each person, and a rule table decides, and the owner cannot override it. The analogy stops in one place: the office has a person to settle a dispute, and the kernel needs the rules written beforehand.

In plain words: The mode bits and user IDs of page 2 are discretionary access control (DAC): the owner of a file decides who gets access, and root bypasses it. Mandatory access control (MAC) adds a policy set by the administrator, which applies to every process, including root, and which the owner cannot change. Linux provides MAC through Linux security modules (LSMs). SELinux labels every process and file with a type and allows an access only if a policy rule lists it, for example that a web-server type may read web-content files and may not read home directories. AppArmor attaches a profile to a program, listing by path the files and capabilities that it may use. Both check after the normal DAC check, so the final answer is the AND of both: an access needs to pass the permission bits and the MAC policy.

A worked example (made up to show the idea). A web server runs as the user www and is taken over by an attacker. DAC allows www to read its own files, and /home/alice/.ssh/id_rsa is mode 0600 and owned by Alice, so DAC already refuses. Suppose Alice had set the file to 0644 by mistake. DAC now allows the read. A SELinux policy that allows the web-server type to read only web-content labels still refuses it, so one error is caught by the other layer. The test machine had no LSM listed, so no output is shown here.

The key lines. MAC rules say what a program may touch, not what a user may touch, which suits a service that is taken over: it cannot widen its own reach. The price is a policy to write and maintain.

Mandatory access control

Mandatory access control (MAC) is an access policy that the system administrator sets and the kernel enforces on every process, so that neither the owner of a resource nor the process itself can override it. It works in addition to discretionary checks.

Example: an SELinux rule that lets the web-server type read web-content files and nothing else.

Interview angle

A common question is "How would you confine an untrusted program on Linux?" A good answer combines a dropped user and capabilities, resource limits, a seccomp filter, a new root or mount namespace, and a MAC profile. It says which layer covers which gap.

Check yourself: 1. What is the difference between DAC and MAC? 2. What two checks must an access pass on a system with SELinux? 3. Why does MAC suit a service that may be taken over?

Words you will meet

Word Meaning
Sandbox A restricted environment that limits the damage of a flaw.
rlimit A per-process cap on a resource, with a soft and a hard value.
seccomp A kernel filter on a process's system calls.
Attack surface The kernel entry points a process can try.
chroot Change the root directory of a process.
MAC / LSM Admin-set policy for all processes / the kernel framework for it.

Quick notes

  • A sandbox assumes a take-over and limits what follows. Combine layers.
  • rlimit: soft is enforced, hard is the ceiling. Lowering the hard limit is a one-way door.
  • seccomp: a filter answers allow, error or kill for each system call. It cannot be removed.
  • chroot: changes the file view only. Call chdir("/") afterwards. It is not a complete boundary.
  • MAC (SELinux, AppArmor) applies a policy to every process and works in addition to the permission bits.