Section 2.43

Bytecode authenticity versus safety

Definition

Bytecode §2.4 authenticity is the property that a received program was produced or approved by a holder of a known key and has not changed since; a message authentication code §5.7 (MAC) or a digital signature §5.14 over its bytes establishes it. Bytecode safety is the property that running the program cannot break the host’s execution rules, such as staying inside permitted memory, calling only permitted helper functions §2.19 and using bounded time. Authenticity concerns where the bytes came from; safety concerns what the bytes do when they run. Neither property implies the other.

The idea

A program can be authentic and unsafe. An honest publisher can ship a program with an off-by-one read; a signing key can be stolen; a publisher’s build system can be broken into. The last two have happened to native software. The Stuxnet malware, found in 2010, installed drivers signed with private keys stolen from two hardware vendors, Realtek and JMicron [289]. In the SolarWinds attack, disclosed in 2020, intruders inserted code into the vendor’s build process, and the vendor’s genuine signature then covered the malicious update [290]. Every signature verified, and none said anything about safety.

A program can also be safe and not authentic: harmless code from a stranger, which must still be refused because nobody authorized it. And it can be authentic and safe yet wrong: a filter that returns allow for every input is well behaved and useless. A wax seal on a letter is a fair picture of authenticity: it shows who sealed the letter and that nobody opened it, but it does not make the instructions inside sensible. The picture stops in one respect: with a MAC, every holder of the shared key can make the seal, so the seal does not even say which holder made it.

How it works

A receiver that installs bytecode answers the two questions with separate mechanisms, in this order.

  1. Authenticate. Verify the tag over exactly the program bytes and every field that affects how they are used, such as the target device, the program slot and the format version; the authentication coverage §5.41 entry lists such fields. Check freshness too, for example a version number higher than the installed one, so that an old program that is authentic but vulnerable cannot be sent again in a replay attack §1.26.

  2. Admit structurally. Parse the bytes and reject unknown opcodes §2.8 and jumps outside the program, which is structural bytecode admission §2.26.

  3. Verify. If a static verifier is available, prove before running that every memory access is in bounds and every helper call is on the helper allowlist §2.20.

  4. Confine at run time. Execute in a sandbox §2.30: a runtime bounds check §2.27 on each access the verifier did not prove, the helper allowlist, and an instruction budget §2.32.

  5. Map the outcome. Turn a fault into the host’s decision with a defined fault-to-verdict mapping §2.34.

Authenticating first has a merit of its own: the parser and the verifier are large, complex code, and checking the tag first means that only programs that a key holder tagged can reach them. That shrinks the attack surface §1.5; it does not remove the need for steps 2 to 5. Throughout, the bytes that were authenticated must be the bytes that run. Copy the program once into memory the sender cannot change, then check and execute that copy, so that no time-of-check to time-of-use §9.26 (TOCTOU) gap opens.

Worked example

Authentic and unsafe. A program with a valid tag reads 4 bytes at offset 14 of a 16-byte input. The input has bytes 0 to 15; the read needs bytes 14, 15, 16 and 17, of which only 16−14=216 - 14 = 2 exist. A runtime bounds check requires offset plus size to be at most the length, and here 14+4=18>1614 + 4 = 18 > 16, so the access is refused and the run ends with a fault. The valid tag did not create the 2 missing bytes.

A verified model that drifted. A verifier on a build server accepted a program against a helper contract §2.21 stating that helper 7 copies a record of at most 64 bytes into a buffer the program supplies, and it checked that the program’s buffer has 64 bytes. A later device firmware lets helper 7 copy records of up to 96 bytes. The program’s buffer now receives up to 96−64=3296 - 64 = 32 bytes too many. If the signed message names helper table version 1 and the device runs version 2, the device refuses the program before it runs. If the message does not name it, only a run-time check inside the helper, comparing the record with the buffer size the program passed, stops the overflow.

A shared key. A fleet of 10,000 devices checks programs with one MAC key. A MAC is symmetric: every party that can check a tag can also compute one. An attacker who extracts the key from one device can produce authentic programs for all 10,000. Authentication §1.6 no longer stops this attacker; the sandbox on each device still bounds what the attacker’s programs can do.

Stated precisely

Let KK be the key, PP the program bytes and CC the context fields, and let the authenticated message be m=encode(C,P)m = \mathrm{encode}(C, P), using an encoding in which two different pairs (C,P)(C, P) never give the same mm. If the MAC is unforgeable and KK is secret, an accepted tag tt on mm shows that some holder of KK computed tt over exactly mm. That is authenticity of (C,P)(C, P), and nothing more.

Write SafeΠ(P)\mathrm{Safe}_{\Pi}(P) for the statement that every run of PP, from any starting state the host’s interface allows, obeys the execution policy Π\Pi. The implication tag accepted⇒SafeΠ(P)\text{tag accepted} \Rightarrow \mathrm{Safe}_{\Pi}(P) holds only under an extra assumption HH: every key holder tags only programs that a sound verifier, using the same model of the runtime as this device, has accepted, and no key holder is compromised. HH is a statement about people and processes, and the device cannot check it. Run-time enforcement establishes SafeΠ(P)\mathrm{Safe}_{\Pi}(P) for every PP without HH, assuming only that the virtual machine §2.3 and its helpers are correct. Authenticity narrows who can choose the programs that run; enforcement bounds what any submitted program can do.

Verification off the device

Some systems run the verifier on a developer’s computer or a build server instead of on the device, because the device is too small to run it. The device then receives signed bytecode and must trust four things it cannot check: that the verifier is sound, meaning it never accepts an unsafe program; that the verifier modeled this device’s runtime correctly; that the machine running the verifier was not compromised; and that the signer signs only programs the verifier accepted. The last is a rule of process, not of mathematics.

The signature binds the verdict to the received bytes only if the signed message contains everything the verdict depended on: the exact program bytes, or a cryptographic hash §5.1 of them; the runtime and instruction-set version; the version of the helper table and its contracts; the layout and size of the input the program reads; the device class; and a version counter. The device compares each field with its own values and refuses the program if any differs.

Run-time checks on the device are still needed. Verifiers have bugs: CVE-2021-3490, an entry in the Common Vulnerabilities and Exposures (CVE) list, describes a flaw in how the Linux eBPF §2.1 (extended Berkeley Packet Filter) verifier tracked the bounds of 32-bit bitwise operations §4.50, which let accepted programs read and write kernel memory. Models drift when a firmware update changes a helper’s contract. Keys leak. Run-time bounds checks, the helper allowlist and the instruction budget limit the damage of each of these failures, which is defense in depth §1.18.

Limits and common mistakes

A MAC tag does not say which key holder made it, and a signature does not say that the signer checked the code. The authenticity versus authorship §1.38 entry treats the first point.

A tag without freshness can be replayed: an old, vulnerable program stays authentic forever.

Fields outside the authenticated message can be changed. An authentic program can be installed into the wrong slot, or onto the wrong device, if the slot and the device are not covered.

Safety is not correctness. A safe program can still make the wrong decision; only testing or a functional specification addresses that.

Safe but unauthorized code must still be refused: safety checks do not replace authorization §1.7.

Checking one copy and running another reopens every hole above.

Questions answered

Why validate and sandbox code even after its tag verifies?

Because a verified tag proves only who approved the bytes and that they have not changed, not that running them is safe. A tag is a short value computed with a secret key, by a message authentication code, or with a private key, by a digital signature; it shows that a key holder approved exactly these bytes and says nothing about what they do. The approved program can still read outside its input, call a forbidden host function or run forever, for four reasons: an honest publisher can make a mistake; a signing key can be stolen; a build system can be compromised, so that the genuine key signs malicious code; and a verifier that checked the program elsewhere can be wrong, or can have used an outdated model of the device.

Validation, which checks the program’s structure and accesses before it runs, and sandboxing, which confines it while it runs, stop all four cases on the device itself, assuming only that the virtual machine is correct. Authentication decides who may submit a program; validation and sandboxing bound what any submitted program can do, so a system needs both.

Related entries: Message authentication code (MAC) (§5.7), Digital signature (§5.14), eBPF verifier (§3.3), Replay attack (§1.26), Authentication coverage (§5.41), Structural bytecode admission (§2.26), Runtime bounds check (§2.27), Defense in depth (§1.18), Authenticity versus authorship (§1.38), Authentication does not establish safe behavior (§17.7). Further reading: [289, 290].