Section 21.14

Linux Security Modules: an enforcement-hook framework

What it is

Linux Security Modules (LSM) is a framework inside the Linux kernel that lets a security module, a separately written part of the kernel, decide whether the kernel may perform an operation on one of its internal objects, such as a file, a process or a network packet. The kernel calls the module through hooks, calls placed just before such operations, and gives objects opaque security fields that the module manages. Chris Wright, Crispin Cowan, Stephen Smalley, James Morris and Greg Kroah-Hartman described it in 2002.

Basis of this account

This account rests on the paper “Linux Security Modules: General Security Support for the Linux Kernel”, in the proceedings of the 11th USENIX Security Symposium, San Francisco, 5 to 9 August 2002, in the copy that USENIX, the Advanced Computing Systems Association, hosts (https://www.usenix.org/legacy/event/sec02/full_papers/wright/wright.pdf). The abstract and Sections 1 to 3 were read in full, Sections 4 and 5 in part, and Sections 6 to 8 not at all. The kernel’s own overview, “Linux Security Modules: General Security Hooks for Linux” by Stephen Smalley, Timothy Fraser and Chris Vance (https://docs.kernel.org/security/lsm.html), was read in full; that page warns that the interfaces it describes are outdated. The companion studies of hook placement that the paper cites were not read. Nothing was run.

What the paper claims

The authors make these claims.

How it works

The framework has four parts.

The paper’s example is vfs_mkdir, the kernel function that creates a directory. After the kernel’s own permission checks, a call to the module’s mkdir hook asks whether the process may create this directory in this parent directory; an error stops the operation. After the directory exists, a post_mkdir hook lets the module set the security field of the new directory’s inode. The kernel’s overview records that LSM entered the mainstream kernel in December 2003. Its description of the present arrangement differs from the paper: each hook keeps a list of module functions, called in the order set by the configuration option CONFIG_LSM, so several modules can be active together, which the overview calls a close approximation of general module stacking.

A hook framework raises the question whether its hooks are enough. Call an operation security-sensitive if a policy may need to control it, such as reading a file’s data, sending a signal to a process or loading a kernel module. The hook set mediates completely, in the sense of the principle of complete mediation §1.16, when every execution path that performs a security-sensitive operation passes first through a hook that authorizes that same operation, for the same subject, on the object that is then used. The LSM authors state that they tested LSM kernels by booting and running them and did not test systematically. They cite companion work by Antony Edwards, Trent Jaeger and Xiaolan Zhang at the IBM (International Business Machines) T. J. Watson Research Center on verifying hook placement, including a static analysis §3.1 with CQUAL, a tool that checks type qualifiers, extra labels attached to the types of a C program.

The general method for showing that a hook set is adequate has four steps.

  1. List the security-sensitive operations: the accesses to protected objects that a policy may need to control.

  2. For each operation, find every path that reaches it in the control-flow graph §3.13, the graph whose nodes are program steps and whose edges are the possible transfers of control between them.

  3. Show that a hook authorizing that operation dominates it: every path from the program’s entry to the operation passes through the hook.

  4. Show that the hook checks the right things and that its answer is still valid at the use: the object that is then used, with no second lookup by name in between; the requesting subject; a permission that covers the operation, since a read check does not cover a write; and no other thread able to change the object or its label before the use.

Static analysis, which reasons about the source code without running it, can cover all paths of the model it builds. Tests and runtime tracing cover only the paths that run, but catch places where the model is wrong.

Worked example

This example is invented, although each route it lists exists in Linux. A module must stop process P from reading the contents of the file /secret. Suppose it implements only the hook that runs when a file is opened. Count the routes by which P can read the contents.

  1. P opens /secret and reads it. The open hook runs: covered.

  2. P opens the same file through a hard link §13.10, a second name for the same file. The hook still runs, because LSM checks the inode, not the name: covered. A check on the path string /secret at the system-call interface would miss this route.

  3. Another process that was allowed to open /secret sends P its open file descriptor §15.21 over a local socket, a communication channel between processes on one machine. P opens nothing: not covered.

  4. P opened /secret before the policy changed and keeps reading through the same descriptor. The open check ran under the old policy: not covered.

The open hook alone covers 2 of the 4 routes. LSM’s hook set has the pieces for the other 2: a hook that controls the receipt of open file descriptors over socket communication covers route 3, and file hooks on read and write, which the paper says let a module revalidate permission on use to support dynamic policy changes, cover route 4. With them, 4 of 4 routes are covered.

One more route shows the limit of checks on use. If P maps an open file into its memory with mmap, later reads are ordinary memory loads that no hook sees. The only possible check is when the mapping is made, and withdrawing access afterwards means removing the mapping.

What this account does not establish

The paper does not claim that its hooks mediate every security-sensitive operation. Its authors tested LSM kernels only by booting and running them, and point to companion work for verification. Complete mediation by a hook set has to be shown; the framework does not provide it.

Most hooks are restrictive. Apart from the few permissive capability hooks, a module can tighten the kernel’s discretionary access control but cannot grant what DAC denies, and it is not consulted when the kernel denies.

Placing hooks after name resolution removes races between a check and the kernel’s own lookup, but not every race. A module that reads its own security data without locking can decide on a label while another thread changes it, and kernel code that looks an object up again after the hook can check one object and use another.

The measurements were made in 2002 on Linux 2.5.15, and the interface has changed since. The kernel’s overview, which itself warns that its interfaces are outdated, is the only source here for the present arrangement; this account does not describe the current list of hooks.

Questions answered

What makes a hook set adequate?

A hook set is adequate when it mediates completely: every execution path that performs a security-sensitive operation, meaning an operation that the policy may need to control, first passes through a hook that authorizes exactly that operation, for the subject that requests it, on the object that is then used, and nothing can change the object or the policy’s answer between the check and the use. Four conditions follow.

  1. Every path. Alternative routes to the same effect, such as reading a file through a descriptor received from another process or through a memory mapping, each need a check. A correct check on one convenient path does not govern a path that never calls it.

  2. The right arguments. The hook sees the actual kernel object after the kernel has resolved names and handles, so the object checked is the object used, and it sees enough context to decide.

  3. No gap in time. No window lets another thread, or a policy change, make the earlier decision wrong; where access continues over time, as with an open file, the hook checks again on each use.

  4. Lifecycle hooks. Objects receive their security labels when created and release them when freed, so every check has a label to work with.

Adequacy is shown, not assumed. List the security-sensitive operations; show with static analysis over the control-flow graph that a matching hook precedes each one on every path; and use tests and runtime tracing to catch places where the analysis models the code wrongly. The Linux Security Modules paper (Wright and colleagues, 2002) placed its hooks just before kernel objects are accessed for the reasons in the second condition, and left systematic verification of its hook set to companion work.

Related entries: Probe and hook (§14.26), Reference monitor (§1.17), Complete mediation (§1.16), Authorization (§1.7), Time-of-check to time-of-use (TOCTOU) (§9.26), Static analysis (§3.1), Control-flow graph (CFG) (§3.13), Flask: separating security policy from enforcement mechanisms (§21.12). Further reading: [217, 218].