Setting
A sensor gateway is a small device that collects readings from sensors and passes them on. In this invented example the gateway also accepts a small decision program, delivered through a phone from a publisher, the one party authorized to change the gateway’s rules. The gateway’s own software, the host, runs the decision program, the guest, inside a virtual machine §2.3 (VM): a part of the host that carries out the guest’s instructions one at a time and checks each memory access. The guest can call helper functions §2.19, host routines that the host chooses to offer to it.
Two situations arise. In the first, the VM gives the guest scratch memory §2.24, an 8-byte working region with bytes numbered 0 to 7, and reuses the same region for every run without clearing it. In one run the guest writes bytes 4 and 5 and then reads 4 bytes starting at byte 4. In the second, inside the host, an updater thread, one of several independently scheduled sequences of execution that share the host’s memory, frees a 16-byte record at address 0x20001000. A callback §14.22, a host routine registered earlier to run later, still holds that address. The memory allocator then gives the same 16 bytes to a new record , and the callback runs. Every access in both situations lands inside a region that exists.
Short answer
Both situations are unsafe although every access is in range, because a bounds check answers only where an access lands. Two further questions need answers: were the bytes being read actually written during the current use of the memory, and is the object still alive, meaning allocated and not yet freed, when it is used? Uninitialized bytes hold leftovers that can mislead a decision or leak earlier data. A use-after-free §4.6 touches whatever now occupies the freed memory. Initialization and lifetime each need their own tracking.
Follow-up questions
Why is an in-range read still unsafe?
An in-range read is still unsafe because being in range says where the bytes are, not what they hold. Take scratch memory of 8 bytes, numbered 0 to 7, that a virtual machine (VM) gives a downloaded program as working memory and reuses from run to run without clearing. A 4-byte read starting at byte 4 covers bytes 4, 5, 6 and 7. Since and the region has 8 bytes, the read lies inside the region and any bounds check passes. If this run wrote only bytes 4 and 5, bytes 6 and 7 still hold whatever an earlier run left there.
Two kinds of harm follow.
Wrong decisions. The value read is half this run’s data and half leftovers, so the program’s decision can depend on an earlier request. Anyone who can shape the earlier run, for instance by sending a crafted request, can steer the value.
Leaks. If the program sends the value out, in a reply or a log entry, it discloses bytes from an earlier run, which may belong to another user or contain secret data.
Registers raise the same problem: a register read before it is written holds a leftover from whatever ran before. In the C language, a local variable that was never assigned has an indeterminate value, and reading it can make the behavior of the whole program undefined.
What are two possible defenses?
The two defenses are to give every byte a defined value before each run, or to prove before the run that no byte is read before it is written. Both concern a downloaded program running in a virtual machine (VM) that gives it working memory, for example 8 bytes numbered 0 to 7.
Initialize at entry. Before each run the VM sets every byte of the working memory, and every register the program may read, to a defined value, usually zero. For 8 bytes this costs 8 byte writes per run; the cost grows with the size of the region. Afterwards every in-range read returns either a value this run wrote or zero. Leftovers can no longer leak, although the program can still compute with a zero it did not mean to read.
Track initialization before the run. A verifier, a checker that examines the whole program before it runs, records for each byte whether it is definitely written, meaning written on every path through the program that can reach the current instruction. It rejects any program that may read a byte outside that set. Where two paths join, the set is the intersection: the bytes written on both paths.
An example of the second defense: one branch writes bytes 4, 5 and 6, the other writes bytes 4 and 5. After the branches join, the definitely written set is . A 4-byte read starting at byte 4 needs all of ; bytes 6 and 7 are missing, so the verifier rejects the program. Counting byte 6 as written because one branch wrote it would be wrong, since on the other branch byte 6 still holds a leftover. The read needs every byte it covers, not only its first byte.
The first defense costs time on every run. The second costs one analysis and rejects some harmless programs. For example, a program tests the same condition twice: it writes byte 6 only if the first test succeeds and reads byte 6 only if the second test succeeds, so the read never happens without the write. An analysis that treats the two tests as unrelated still sees a path on which byte 6 is read unwritten, and rejects the program.
What is temporal safety §2.29?
Temporal memory safety is the property that every access to an object happens during the object’s lifetime: after the object was allocated, that is reserved for use, and before it was freed, that is given back for reuse. Spatial memory safety §2.28 asks whether an access lands inside the object; temporal memory safety asks whether the object still exists when the access happens.
A use-after-free breaks it. In a gateway’s software, an updater thread, one of several independently scheduled sequences of execution sharing memory, frees a 16-byte record at address 0x20001000. A callback, a routine registered earlier to run later, still holds that address; a pointer that outlives its object like this is a dangling pointer §4.1. The memory allocator then gives the same 16 bytes to a new record . When the callback writes 4 bytes at offset 8 of what it takes to be , it changes bytes 8 to 11 of . A bounds check passes, since and the record has 16 bytes, and the address belongs to a live object. The address is the same; the object is not. Equal address bits do not bring back.
The defense protects the object from the moment a user obtains its address until that user’s last access. Common methods:
an ownership rule: one owner frees the object, and anyone who borrows the address must stop using it before the owner frees it;
reference counting §9.22: the object keeps a count of current holders and is freed only when the count reaches zero;
a grace period §9.21, as in read-copy update §9.20 (RCU): the updater publishes a replacement and frees the old object only after every reader that could still hold the old address has finished;
a lock, which only one holder at a time can own, that the updater takes before freeing and the user holds from obtaining the address through the last use.
Why do copy-only helpers §2.22 help?
A copy-only helper gives a downloaded program a copy of host data instead of a pointer into host memory, so the program never holds a host address that could later go stale. A helper function is a routine of the device’s own software, the host, that the downloaded program, the guest, may call. A copy-only helper copies a stated number of bytes from a host object into the guest’s own memory, or the other way, and then returns. Three benefits follow.
Lifetime questions stay inside the helper. The host object needs to stay alive only while the helper runs, a short stretch of host code that can hold a lock throughout, so that no other code frees or changes the object meanwhile. After the return the guest has only its copy, in memory that belongs to the guest.
Guest accesses stay checkable. The copy lies in the guest’s own region, which the virtual machine (VM) running the guest already checks. No host region, with a lifetime of its own, enters the guest’s view.
Less authority. The guest receives a snapshot of the bytes it may see, not a handle that reaches the live object.
The helper itself is native code and needs its own argument. During the copy the source object must be alive and readable, the destination must be guest memory at least as long as the copy, and the length must be exact. Copying 8 bytes into a 4-byte destination is a buffer overflow §4.4 inside the helper, whatever the interface promises. If another thread can change the source during the copy, the helper must hold a lock, or use another method, to produce a coherent snapshot §9.23, a copy that matches the source at one single moment; otherwise the copy can mix old and new bytes. A copy is correct as of the moment it was taken and says nothing about the source afterwards.
Is clearing guest memory enough for information-flow safety?
No. Clearing the working memory of the downloaded program, the guest, closes one route by which information can leak, and the other routes stay open. An information-flow property states which observer may learn what, for example: nothing that the gateway reveals to a phone may depend on readings that belong to another user. Information-flow control §1.41 is the discipline of tracking and enforcing such properties. Clearing the guest’s working memory before each run stops bytes of one run from being read in the next. It does nothing about these routes:
a helper function, a host routine the guest may call, that returns secret data on purpose, such as another user’s reading, which the guest then passes on;
guest persistent state §2.36, data that the host keeps for the guest from one run to the next, which carries history across runs by design;
the verdict itself: an
ALLOWorDENYthat depends on a secret reveals something about it;time, a side channel §1.29: if a run takes longer when a secret bit is 1, an observer who times the reply learns the bit, and radio activity whose timing or volume depends on a secret leaks in the same way;
host memory outside the guest’s region, such as the host’s own stack, which also holds leftovers that a careless helper can copy out.
So the order of work is: name the observer, what the observer can see and what it may learn; then check every route in scope. Clearing memory is required for one route and proves nothing about the others.
Related entries: Scratch memory (§2.24), Temporal memory safety (§2.29), Confidentiality (§1.8), Object lifetime (§4.1).