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 , 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 . 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 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 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 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 tagged. Four questions remain, and each needs its own check.
Is the object new? The phone can resend a recorded genuine object, such as object 41, without knowing , and its tag is still valid. Only a comparison of its sequence number with the last one accepted exposes this replay attack §1.26, the resending of a recorded valid message: 41 is not greater than 42.
Is the program safe to run? Object 43 is genuine but reads 4 bytes from byte 7 of an 8-byte buffer, whose bytes are numbered 0 to 7. Only byte remains from byte 7 onwards, and , so the read leaves the buffer. Whatever the object’s tag says, a verifier, a checker that examines the whole program before the program runs, must reject the program, or a runtime bounds check §2.27, a test made just before each memory access, must refuse the read.
Is the request permitted? The gateway must decide whether this operation, on this gateway, is one that a holder of may request: the header must name this gateway, and the operation must be one that the key may authorize. This decision is authorization §1.7, and the tag does not make it.
Did the publisher write it? Object 44 carries a valid tag made with a stolen key. No check on the object can tell the thief from the publisher. This question is answered outside the object, by protecting and by replacing it once it is known to be stolen; meanwhile the memory checks at least keep the thief’s program inside its own memory.
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 , 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.
Authenticated and memory-unsafe. The genuine publisher ships a program with a bug: it reads 4 bytes starting at byte 7 of an 8-byte buffer. The buffer’s bytes are numbered 0 to 7, so the read needs bytes 7, 8, 9 and 10, and only byte 7 exists. The tag is genuine, and the read is still out of bounds. The virtual machine must refuse it, for example with a runtime bounds check that compares the remaining byte with the 4 bytes requested.
Authenticated, memory-safe and malicious. An attacker who stole ships a program that answers
DENYto every request. It touches only its own buffer and finishes at once, so every memory check passes, and the owner can no longer change any setting. A second such program answersALLOWto a request that must be refused, such as switching off a tamper alarm. Memory safety holds in both cases.
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 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 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 , which sets each bit of the result to 1 exactly when the two input bits differ. For one byte, the ciphertext byte is , where is the plaintext byte and the keystream byte, and decryption computes . 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 , and replaces by , because . The gateway decrypts . 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 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:
, representation: the bytes parse in the expected format. Every length field fits within the bytes received, every field lies in its allowed range and the format version is known.
, authentication: the tag recomputed with over exactly the covered bytes equals the received tag. The comparison takes the same time wherever the first differing byte lies, which is called a constant-time comparison §4.33, so its timing does not reveal how much of a forged tag was right.
, permission: the header names this gateway, and the operation is one that a holder of may request.
, freshness: the sequence number is greater than the last one accepted, which the gateway keeps in storage that survives power loss.
, semantic and resource admission: the program passes the verifier, or will run under runtime bounds checks; it fits the memory set aside for it; it calls only permitted helper functions §2.19, routines of the gateway’s own software that a program may call; and it runs within an instruction budget §2.32, a limit on the number of instructions per run.
The gateway accepts exactly when all five hold: where means “and”. The replayed object 41 shows why no single check can stand in for the others: , , and are true, but asks whether , which is false, so accept is false. The buggy object 43 fails 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 .
Order the checks so that unauthenticated bytes reach as little code as possible: first only enough of to locate the tag and the covered bytes safely, then , then the rest of , then and , and last , 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 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 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].