Section 2.41

Software fault isolation (SFI)

Definition

Software fault isolation (SFI) is a technique that confines untrusted machine code to its own region of memory inside an address space, the range of addresses a running program uses, that it shares with trusted code, without relying on hardware memory protection. A rewriter inserts a check or an address-forcing step before every store and every computed jump, and a validator confirms, before the code runs, that every such instruction is guarded and that no jump can skip a guard. Wahbe, Lucco, Anderson and Graham introduced SFI in 1993.

The idea

A program that loads a plug-in written by someone else, such as a database running user code, can run the plug-in in a separate process. The hardware then keeps the plug-in out of the host’s memory, but every call between them crosses a process boundary, which is slow, and a microcontroller §6.1 often has no memory management unit §6.9 (MMU), the hardware that gives each process its own address space, at all. Running the plug-in inside the host’s address space is fast, but one stray store from the plug-in can overwrite the host. SFI keeps the speed and restores the separation in software: the plug-in’s own instructions are made unable to produce an address outside its region.

An inserted check is just more instructions, and that is the weak point. If the plug-in can jump directly to a store that follows its check, the check never runs. SFI therefore solves two problems together: guard every dangerous instruction, and make the guards impossible to skip. A turnstile in front of a door is a fair picture with one condition attached: it protects the door only if every side entrance is bricked up. In SFI, the side entrances are jumps.

How it works

The method of Wahbe and colleagues works in this order.

  1. Lay out a fault domain. The module gets a code region and a data region. Each is 2k2^{k} bytes long and starts at a multiple of 2k2^{k}, so every address inside a region has the same upper bits, called the segment identifier.

  2. Guard every store. Either check the address, comparing its upper bits with the segment identifier and stopping the module if they differ, which the paper calls segment matching; or force the address into the region without checking, which it calls address sandboxing §2.30: a bitwise AND clears the upper bits and a bitwise OR sets them to the segment identifier. Forcing never stops the module; a bad address becomes a wrong address inside the module’s own region.

  3. Use a dedicated register. The guarded address goes into a register reserved for this purpose, and the store uses that register, not the register the module computed. The validator checks that no other instruction writes the dedicated register. A jump straight to the store then gains nothing: the store uses whatever the dedicated register holds, and only guard sequences ever put a value there.

  4. Guard computed jumps the same way. A jump whose target is computed at run time, including a return from a function, whose return address the module can overwrite on its stack, is forced into the code region through a dedicated code register. For every jump with a fixed target, the validator checks before the code runs that the target lies inside the code region.

  5. Keep code and guards out of reach. Module stores land only in the data region, so the module cannot rewrite its own validated code.

  6. Leave only through gates. Calls into the host go through fixed entry stubs, trusted routines that check their arguments.

On a processor whose instructions all have one length and alignment §4.22, every aligned word is an instruction start. On x86, instructions vary in length, so a jump into the middle of one decodes different instructions that the validator never saw. Native Client (NaCl), a Google sandbox for native code, in its 32-bit x86 version (Yee and others, 2009) [285], divides the code into 32-byte bundles: no instruction crosses a bundle boundary, every computed jump target is forced to a multiple of 32, and each forcing instruction sits in the same bundle as the jump it guards. A computed jump therefore lands only at a bundle start, never between a guard and its use, and the validator checks that every fixed jump target is an instruction start it decoded.

Worked example

Take 32-bit addresses and a data region of 216=65,5362^{16} = 65{,}536 bytes from 0x20010000 to 0x2001FFFF; its segment identifier is the upper half, 0x2001. The rewriter replaces a 4-byte store of register v to the address in register a with three instructions that use the dedicated register d:

1: and d, a, 0x0000FFFC
2: or d, d, 0x20010000
3: store word [d], v

The mask 0x0000FFFC keeps the low 16 bits except the last two, so the forced address is also a multiple of 4.

The module sets a to 0x20000400, an address in the host’s data. Instruction 1 keeps 0x00000400; instruction 2 sets the upper half and gives 0x20010400. The store writes the bytes 0x20010400 to 0x20010403, inside the module’s region, and the host’s data is untouched. With a = 0xFFFFFFFF, instruction 1 keeps 0x0000FFFC, instruction 2 gives 0x2001FFFC, and the 4 bytes written end at 0x2001FFFF, the last byte of the region. Had the mask been 0x0000FFFF, the address would be 0x2001FFFF, and the store would also write 0x20020000 to 0x20020002, 3 bytes past the region. Forcing alignment closes that gap. So does a guard zone: at least 3 bytes after the region that no other code uses, often left unmapped so that a write there faults.

Now the module tries to skip the guard: it computes the address of instruction 3 and jumps there. The jump is itself forced into the code region, so it can reach instruction 3. But d still holds the value left by the last guard sequence that ran, or the address inside the region that the loader put there at the start, so the store again writes inside the region. To make d hold 0x20000400, the module would need an instruction other than a guard pair that writes d, and the validator rejected every such instruction. The only moment d lies outside the region is just after instruction 1, and the next instruction is always instruction 2.

Stated precisely

Let the data region be [base,base+2k)[\mathrm{base}, \mathrm{base} + 2^{k}), where base\mathrm{base} is a multiple of 2k2^{k}, and let m=2k−1m = 2^{k} - 1, the number whose low kk bits are all ones. For every address aa, the value (a&m)|base(a \mathbin{\&} m) \mathbin{|} \mathrm{base} lies in the region, where &\& is bitwise AND and || is bitwise OR: its low kk bits are some value from 0 to 2k−12^{k} - 1, and its upper bits equal those of base\mathrm{base}, whose low kk bits are zero.

The validator accepts a module only if four conditions hold.

  1. Every store addresses memory through a dedicated data register, plus at most a constant offset no larger than the guard zone, and every computed jump goes through a dedicated code register.

  2. The only instructions that write a dedicated register are guard pairs: an AND with mm immediately followed by an OR with the region’s base.

  3. Every jump with a fixed target lands on an instruction start inside the code region.

  4. The code region and the data region do not overlap.

If, in addition, the loader starts each dedicated register at an address inside its region, then by induction over the executed instructions, at every instruction boundary except the one just after the AND of a guard pair, each dedicated register holds an address in its own region. Hence every store writes within the data region and its guard zone, and every jump stays in the code region or enters a host stub.

The argument assumes that the processor executes instructions as documented, that interrupts §6.11 save and restore registers exactly, that the validator is correct, that the host stubs check their own arguments, and that nothing else modifies the module’s code after validation.

Limits and common mistakes

Reads are a separate choice. The 1993 implementation guarded stores and jumps but not loads, so a module could read any memory; the paper notes that guarding loads as well is possible at extra cost. Such SFI protects integrity §1.9, not confidentiality §1.8.

Forcing hides bugs. A wild address becomes a wrong address inside the region, and the module silently corrupts its own data. Segment matching reports the offending instruction instead.

SFI confines addresses and nothing else. It bounds neither running time nor host resources, and it does not catch a use-after-free §4.6 inside the module’s own region. Timing and cache side channels §1.29 remain.

The rewriter and the compiler need not be trusted, because the validator checks their output. The validator and the host stubs must be correct; a stub that trusts a pointer from the module undoes the confinement.

A bytecode §2.4 virtual machine §2.3 avoids the jump-around problem by construction. Guest jumps are offsets between guest instructions, and the interpreter’s §2.13 checks live in host code that no guest jump can name. What remains is to check that every guest jump lands on the start of a guest instruction, for example never inside the second half of a 16-byte eBPF §2.1 (extended Berkeley Packet Filter) instruction. WebAssembly §19.5.6 gets the same effect from structured control flow, in which a jump can only name an enclosing block.

Questions answered

How is control prevented from jumping around an inserted check?

Software fault isolation, which confines untrusted machine code by inserting address guards before its stores and jumps, prevents jumps around a guard with three measures that work together, all confirmed by a validator before the code runs.

  1. The guard’s result sits where the guarded instruction reads it. The checked or forced address goes into a dedicated register that only guard sequences may write, and every store or computed jump uses that register. Jumping past the guard straight to the store gains nothing, because the register still holds an address that an earlier guard forced into the region.

  2. Jump targets are restricted. A jump with a fixed target is checked before the code runs. A computed jump, including a function return, is itself guarded: forced into the code region and, where instructions vary in length as on x86, onto the start of an aligned bundle, with the guard in the same bundle as the jump, so that no target lies between them.

  3. Code and guards cannot be changed. The module’s stores are confined to its data region, so it cannot rewrite validated code, and no unvalidated instruction can write a dedicated register.

Calls out of the module go only through fixed host entry points that check their arguments. In a bytecode virtual machine the problem does not arise in this form: guest jumps can reach only guest instructions, and the checks live in host code that the guest cannot name.

Related entries: Instruction encoding (§2.7), Runtime bounds check (§2.27), Refinement (§3.32), Sandbox (§2.30), Sandbox escape (§2.31), Memory protection unit (MPU) (§6.8), Memory management unit (MMU) (§6.9), WebAssembly validation, linear memory, and host imports (§19.5.6). Further reading: [70, 164, 285].