Section 1.16

Complete mediation

Definition

Complete mediation is a security design principle: every access to every protected object must be checked for authority each time the access happens. An object is a protected resource, such as a file, a memory region or a device setting; an access is any operation on it; authority is the right to perform that operation. The principle rules out two things: a route to the object that skips the check, and reliance on an earlier decision after the facts behind it have changed. Saltzer and Schroeder stated it in 1975 [233].

The idea

Unix-like operating systems check a file’s permissions when a program opens the file, and then hand the program a file descriptor §15.21, a small number that the program uses for every later read and write. The reads are not checked again. If the file’s owner removes the program’s permission one second after the open, the program keeps reading through the descriptor it already holds. The decision taken at open time still governs accesses that happen after the facts behind it changed. Checking once at open time saves a permission check on every read and write, but it is a known departure from complete mediation, and withdrawing access to a file that is already open needs a separate mechanism.

A building that checks badges only at the front door is a fair picture of incomplete mediation. A side door, a loading dock that nobody watches and a badge that keeps working for a week after its owner left all reach the inside without a current check. Complete mediation means a check at every door, against today’s list. The picture stops at the doors. In software the unwatched routes are harder to see: a second function that performs the same operation, an error path, a reused number that now names a different object, or work queued during one call that runs after the call has returned.

How it works

Mediating access to an object completely takes five steps.

  1. List the protected effects: what must not happen without authority, such as reading a secret, writing a setting or running code.

  2. List every route to each effect: the normal function, other functions that do the same thing, error and clean-up paths, debugging interfaces, interrupt handlers §6.12, hardware that writes memory on its own through direct memory access §4.36, and work queued to run later.

  3. Check on every route, before the effect, or send every route through one point that checks.

  4. Bind the decision to what is used: the subject, meaning the party that asks; the operation; the object itself, rather than its name or number; and the policy, meaning the rules in force.

  5. Say what invalidates a remembered decision. A decision kept in a cache, or carried by a handle, stays valid only until the object is released, its identifier is reused, the policy changes or the subject’s authority is withdrawn. At each of those events the remembered decision is discarded.

A decision also holds only for the content that was checked. If the object can change between the check and the use, the program must either check at the moment of use, or copy the object into memory that nothing else can change and use that copy. Otherwise it has a time-of-check to time-of-use §9.26 (TOCTOU) error.

Four cases defeat a single check on the normal path, and each needs its own answer.

Creation.

Creating an object is itself an access, to the directory, table or memory pool that will hold the object, and must be checked. The new object must start with its own protection, not with whatever protection its storage had before.

Reuse.

Identifiers are recycled: handle numbers, file descriptor numbers, table slots and memory addresses. A decision stored under a number attaches itself to whatever object holds that number next.

Aliases.

One object can have several names: a symbolic link §13.10 and its target; the paths reports/../keys and keys, which name the same file when reports is an ordinary directory; two pointers to one buffer. A check made through one name must hold for the object reached through any name, so the check belongs on the object, not on the name.

Later side effects.

A callback §14.22, a queued task or a deferred write runs after the call that checked it has returned, possibly after the object or the policy has changed. The check must still hold at the moment the effect happens.

Worked example

A system keeps open objects in a table of 8 slots, numbered 0 to 7, and gives programs handles, numbers that name a slot. A cache remembers decisions by handle, so that repeated reads are not checked again.

  1. Object A, a public file, is placed in slot 7, and its handle is 7. A program asks to read handle 7. The check allows the read, and the cache stores “handle 7: read allowed”.

  2. A is closed, and slot 7 becomes free.

  3. Object B, a file of secret keys, is opened by another part of the system and placed in slot 7. Its handle is also 7.

  4. The program asks to read handle 7 again. The cache answers “allowed”, and the program reads B, which no decision ever allowed.

The usual repair adds a generation number: a counter stored with each slot that increases every time the slot is reused. Each handle is built from both parts, handle=generation×8+slot\text{handle} = \text{generation} \times 8 + \text{slot}, so the 3 lowest bits of the handle hold the slot (0 to 7) and the bits above them hold the generation.

The generation number has a fixed width, so it eventually returns to 0, which is called counter wraparound §6.21. With a 16-bit handle, 3 bits hold the slot and 16−3=1316 - 3 = 13 bits hold the generation, which can therefore take 213=8,1922^{13} = 8{,}192 values. Slot 7 is now at generation 5. After 8,191 more reuses, 8,192 in all since handle 39 was issued, generation 4 comes round again, and a program that still holds the stale handle 39 reaches a new object. Retiring a slot before its generation wraps closes that gap completely. Discarding every cache entry for a slot whenever the slot is freed closes only the mediation part: every access through a reused handle then receives a fresh check, but the stale handle 39 still names whatever object occupies the slot.

Stated precisely

Write an access as (s,a,o,t)(s, a, o, t): subject ss performs operation aa on object oo at time tt. Complete mediation holds when, for every access that takes effect in any run of the system, a check allowed exactly ss, aa and oo, and either the check ran at time tt, or it ran at an earlier time t0t_0 and none of the following changed between t0t_0 and tt:

The principle relies on three further assumptions. The check computes the policy’s answer correctly. The identity of ss is genuine, which needs authentication §1.6. And no subject can skip or alter the checking code or its data, which are two of the requirements of a reference monitor §1.17.

Limits and common mistakes

Complete mediation says that every access is checked, not that the checks are right. A complete set of checks that enforces a wrong policy, or computes the policy’s answer wrongly, protects nothing.

Checks placed at selected points mediate only the operations that pass those points. Hooks on the main request handler do not mediate what an error path, a debugging command or a background task does to the same object.

Completeness is a claim about every route, so one forgotten route defeats it, and testing the normal route cannot show it. Showing complete mediation needs a list of all routes and an argument that the list is complete, for example that the object can be reached only through one interface.

Caching §14.20 decisions is allowed, but Saltzer and Schroeder warned that proposals to save time by remembering the result of an authority check must be examined skeptically. A cache keeps the principle only if every entry records exactly what the decision covered, namely the subject, the operation, the object’s lasting identity and the policy version, and only if the entry is discarded when the object is released, its identifier is reused, the policy changes or the subject’s authority is withdrawn.

Carrying authority in a handle, as Unix file descriptors do and as capability security §1.40 does by design, is a deliberate trade: the check at creation stands in for later checks. The design must then say how authority is withdrawn, because a later change of permissions does not reach handles already issued.

A refused request must have no effect either. If an error path performs part of the operation before the refusal, for example writing a record into protected storage or reserving memory, the refusal comes too late for that part.

Questions answered

Do checks cover creation, reuse, aliases, and later side effects?

Checks must cover all four, because complete mediation requires every access to a protected object to be checked against the authority in force when the access happens, and each of the four cases creates an access that a single check on the normal path misses. Creation is an access to the place that will hold the new object, and the new object must start with its own protection. Reuse lets a decision stored under a recycled number, such as a handle or a file descriptor, cover a different object; decisions must be bound to the object’s lasting identity, for example a slot number together with a generation counter, or discarded when the object is released. Aliases let one object be reached through several names, such as a symbolic link and its target, so the check must apply to the object reached, not to the name used. Later side effects, such as callbacks, queued work and deferred writes, run after the checking call has returned, so the check must still hold when the effect happens.

To answer the question for a real system, list every route to each protected effect, including other functions that perform it, error paths, debugging interfaces and work that runs later. For each route, name the check that covers it and the events that would invalidate that check. A single visible check on the normal path is not evidence that the other routes are covered.

Related entries: Reference monitor (§1.17), Structural bytecode admission (§2.26), Temporal memory safety (§2.29), Time-of-check to time-of-use (TOCTOU) (§9.26), Counter wraparound (§6.21), Capability security (§1.40). Further reading: [233].