Section 20.12

Tree 12: “The HMAC is valid; why verify the program?”

Setting

A sensor gateway is a small device that collects readings from sensors and passes them on. The gateway in this invented example decides each management request, such as a request to change one of its settings, by running a small decision program that answers ALLOW or DENY. The program is bytecode §2.4: instructions for a processor that exists only as a definition, not as hardware. Part of the gateway’s software, a virtual machine §2.3 (VM), carries out those instructions one at a time while the gateway keeps serving its other work. A publisher, the one party authorized to change the gateway’s policy, writes the programs. A phone carries each program from the publisher to the gateway, and the phone can read, keep, change or resend any bytes it carries.

Each program travels as an update object: a header, then the program bytes, then a tag. The header names the target gateway, the operation (install a new program) and a sequence number, which the publisher increases by 1 for every new object. The tag is a 32-byte value computed with HMAC §5.8 (hash-based message authentication code), a function of a secret key and a message whose result only holders of the key can compute. The publisher and the gateway share the key KaK_a, the active message-authentication key. The tag covers the header and the program bytes. The gateway has already installed object number 42.

Three objects now arrive, and the tag on each one is valid under KaK_a. Object 41 is a genuine object from the publisher that the phone recorded weeks ago and now sends again. Object 43 is new and genuine, but its program has a bug: it reads 4 bytes starting at byte 7 of an 8-byte input buffer, a region of memory that holds the program’s input. Object 44 was written by an attacker who stole a copy of KaK_a from the publisher’s computer. A colleague asks why the gateway should check anything more once the HMAC is valid.

Short answer

A valid tag proves only that a holder of the key KaK_a produced exactly the covered bytes and that nobody has changed them since. It does not prove that the object is new, since a recorded old object keeps its valid tag; that the program is safe, since a key holder can ship a bug; or that the publisher wrote it, since a stolen key makes valid tags too. So the gateway also checks format, sequence number, permission and memory behavior, all on one unchanging copy of the bytes.

Follow-up questions

First answer

The gateway must verify the program because the tag answers only one of the questions that decide whether to install it. In this setting a gateway receives decision programs from a publisher through a phone, and each program carries a tag computed with HMAC (a keyed function whose result only holders of the key can compute) under the key KaK_a that the publisher and the gateway share. A valid tag establishes integrity §1.9 and authenticity of the covered bytes: they are exactly the bytes that some holder of KaK_a tagged. Four questions remain, and each needs its own check.

All checks must examine the same bytes that later run. If the gateway verifies the tag on one buffer and then installs from a buffer that the phone can still overwrite, every check is void; this error is called time-of-check to time-of-use §9.26 (TOCTOU). The gateway therefore copies the object once into memory that only it can change, checks that copy and installs that copy. Accepting the object is still not installing it: the program becomes active only after it has been stored and committed, and the publisher learns that only from a confirmation sent after the commit.

Can an authenticated malicious program be memory-safe?

Yes: being authenticated and being memory-safe are independent properties, so an authenticated program can be memory-safe and malicious at once, and an authenticated program can also break memory rules. Authentication §1.6, here a valid tag computed with HMAC (a keyed function whose result only key holders can compute) under the shared key KaK_a, says who produced the bytes. Memory safety says that every read and write of the program stays inside the memory regions that the gateway gave it. The decision program runs inside the gateway’s virtual machine, the part of the gateway’s software that carries out the program’s instructions and can refuse an illegal one.

The two kinds of check therefore protect different things. A runtime bounds check, or a verifier that examines the program before it runs, protects the gateway’s memory from the program; neither looks at what the verdicts mean. A program that is memory-safe but gives harmful verdicts is stopped only by other means: keeping KaK_a out of an attacker’s hands, review and testing of each program against the owner’s intended policy, and limits on which settings any program may allow.

Can an unauthenticated safe program still be refused?

Yes, and the gateway should refuse it: the owner requires that only the publisher can change the policy, and a safe program from someone else still changes the policy. Safety asks what a program can do when it runs: whether it stays inside its memory, finishes in time and calls only the routines it is allowed to call. Authorization asks who may make this change, on this gateway, now. In this setting the publisher is the only party authorized to change the gateway’s policy, and a tag computed with HMAC (a keyed function whose result only holders of the shared key KaK_a can compute) is how the gateway recognizes the publisher’s objects.

Take an outsider’s program with no valid tag that passes every memory check and answers ALLOW to every request. Running it is memory-safe, and it still hands control of the gateway’s policy to the outsider. Even a program that behaves exactly like the installed one must be refused, because accepting it would establish that outsiders can install programs, and the next one need not be harmless. Requiring both safety and authorization is therefore consistent: each rules out a different bad outcome.

A refusal on these grounds says nothing about the program’s safety. Refused means not authorized; it does not mean unsafe.

The order of the checks also matters. The gateway should check the tag before it runs the verifier, the checker that analyzes the whole program before the program runs and can take much processor time. Otherwise anyone can make the gateway spend that time analyzing junk, a form of denial of service §1.28, and any defect in the verifier can be reached by an outsider who holds no key.

Does encryption replace the tag?

No: encryption keeps content secret, a tag detects changes, and encryption alone does not detect changes. Encryption gives confidentiality §1.8, meaning that only holders of the key can read the content. A tag, computed with a message authentication code §5.7 such as HMAC under a key that the publisher and the gateway share, gives integrity: any change to the covered bytes makes the tag check fail.

Counter mode (CTR) §15.5 shows why encryption alone is not enough. Counter mode is a common way to encrypt with a block cipher §15.3. It produces a stream of bytes that depends on the key, called the keystream, and combines each byte of the message with one keystream byte by exclusive or (XOR), written ⊕\oplus, which sets each bit of the result to 1 exactly when the two input bits differ. For one byte, the ciphertext byte is c=p⊕kc = p \oplus k, where pp is the plaintext byte and kk the keystream byte, and decryption computes c⊕k=pc \oplus k = p. Let byte 12 of the plaintext be the number of the target gateway, 0x05, and let the attacker want 0x07 there. The attacker knows the layout and the current value but not kk, and replaces cc by c⊕𝟶𝚡𝟶𝟸c \oplus \mathtt{0x02}, because 𝟶𝚡𝟶𝟻⊕𝟶𝚡𝟶𝟽=𝟶𝚡𝟶𝟸\mathtt{0x05} \oplus \mathtt{0x07} = \mathtt{0x02}. The gateway decrypts (c⊕𝟶𝚡𝟶𝟸)⊕k=p⊕𝟶𝚡𝟶𝟸=𝟶𝚡𝟶𝟻⊕𝟶𝚡𝟶𝟸=𝟶𝚡𝟶𝟽(c \oplus \mathtt{0x02}) \oplus k = p \oplus \mathtt{0x02} = \mathtt{0x05} \oplus \mathtt{0x02} = \mathtt{0x07}. Decryption reports no error, and the gateway reads the attacker’s value.

Encrypting the radio link between the phone and the gateway does not replace the tag either. The phone is one end of that link: it receives the plaintext, can change it and encrypts the changed bytes again. Link encryption protects against bystanders on the radio channel, not against the phone.

When the program must be both secret and protected against change, use AEAD §5.16 (authenticated encryption with associated data): one algorithm §14.1 that encrypts the program and computes a tag over the ciphertext and over header fields sent unencrypted, the associated data, under a key that only the publisher and the gateway hold. RFC 5116 defines a standard interface for such algorithms. A valid AEAD tag still answers only the question of who produced the bytes, so the checks of freshness, permission and program safety stay.

How should the acceptance predicate look?

The acceptance predicate should require every independent check to pass, all on one stable copy of the object: accept only an object that is well formed, authentic, permitted, fresh and admissible. A predicate is a condition that is either true or false. In this setting a gateway receives update objects, each a header, a decision program and a tag computed with HMAC (a keyed function whose result only holders of the shared key KaK_a can compute), and the gateway has already accepted the object with sequence number 42. Name five checks, each true or false for the object at hand:

The gateway accepts exactly when all five hold: accept=R∧A∧P∧F∧S,\mathrm{accept} = R \land A \land P \land F \land S, where ∧\land means “and”. The replayed object 41 shows why no single check can stand in for the others: RR, AA, PP and SS are true, but FF asks whether 41>4241 > 42, which is false, so accept is false. The buggy object 43 fails SS if the gateway checks programs with a verifier; with runtime bounds checks instead, it is accepted and its faulty read is refused during each run. An outsider’s object fails AA.

Order the checks so that unauthenticated bytes reach as little code as possible: first only enough of RR to locate the tag and the covered bytes safely, then AA, then the rest of RR, then PP and FF, and last SS, the most expensive. All five run on one copy of the object that nothing else can change between the checks and installation.

Acceptance is not activation. After acceptance the gateway stores the program and commits it, the single step that makes the new program the one to run. It must record the new sequence number in the same atomic step as the commit, a step that either happens completely or not at all. Suppose the stored number is 50, a good object 52 arrives, and the phone holds an older genuine object 51. If the gateway raised the stored number to 52 first and power failed before the commit, object 52 would be lost, and a resend would be rejected because 52>5252 > 52 is false. If it committed first and power failed before raising the number, program 52 would be active with 50 stored, and object 51 would pass FF and replace the newer program. For the same reason, an acknowledgment sent when the object arrives proves only arrival; the publisher must not read it as proof that the program is active.

Related entries: Bytecode authenticity versus safety (§2.43), Message authentication code (MAC) (§5.7), Authenticated encryption (AEAD) (§5.16), Replay attack (§1.26), Authorization (§1.7), Runtime bounds check (§2.27), eBPF verifier (§3.3), Counter mode (CTR) (§15.5), Time-of-check to time-of-use (TOCTOU) (§9.26), Monotonic counter (§8.28). Further reading: [67].