Section 1.3

Trust boundary

Definition

A trust boundary is a place in a system where data passes between two parts trusted to different degrees. On the less trusted side, an attacker may have chosen the data. On the more trusted side, the system relies on the data to make a decision or to change something it protects. The checks that justify that reliance belong at the boundary, where data enters the more trusted part. A trust boundary is defined by who controls the data and who relies on it, not by a network connection.

The idea

A receiver copies the bytes of a radio packet into its own memory. The bytes now sit in local memory, but the sender still chose every one of them. Copying changed where the bytes are. It did not change who controls their content. If the receiver now uses one of those bytes as a position in a table, the sender is choosing where the receiver reads or writes.

A border crossing is a fair picture. A traveler is checked at the border and given a stamp, and inside the country officials rely on the stamp without checking again. Software works the same way: the checks are concentrated at the crossing, and the code behind the crossing relies on them. The picture stops being accurate in one respect. A real system has several crossings in a row. Each one establishes one more fact about the same data, and data that has passed one crossing has not passed the later ones.

How it works

Follow one input through the parts that handle it, and write down what each part has established when it finishes.

  1. Transport, the part that carries the message, delivers a sequence of bytes of a known, limited length. Established: the length. Nothing is known about the content.

  2. The authentication §1.6 check verifies a cryptographic tag, a short value that only a holder of the secret key could have computed over exactly these bytes. To find the tag it reads only the framing, such as a length field. Established: these exact bytes come from a key holder. Not established: that they are well formed, or that the key holder is allowed to ask for this.

  3. The parser splits the authenticated bytes into fields and rejects any message that does not match the format. Established: the message is well formed.

  4. The authorization §1.7 check decides whether this sender may perform this operation on this target now. Established: the request is permitted.

  5. The action changes something the system protects.

Each check stands at a trust boundary: the steps after the check rely on a property of the data that the steps before it could not rely on. The steps can be function calls inside one program. A boundary is guarded correctly when two things hold. No part relies on a property before the step that establishes it has run. And the bytes that were checked are the same bytes that are later used.

In a threat model §1.2, a written account of what an attacker can do to a system, trust boundaries are drawn as lines on a diagram of the system’s data flows.

Worked example

A message consists of a 4-byte field named count, followed by as many 16-byte records as count states. An attacker sends a message with count set to 100,000,000. Decoding the field is harmless: reading 4 bytes as a number damages nothing.

Suppose the next line of the program reserves memory for the records, and the authentication check comes after that line. The program asks for 100,000,000×16=1,600,000,000100{,}000{,}000 \times 16 = 1{,}600{,}000{,}000 bytes, which is about 1.49 GiB. A small device has nowhere near that much memory. The request fails, and a program that does not expect the failure stops: one message from a sender who holds no key has caused a denial of service §1.28. The authentication check would have rejected the message, but it runs too late. The program relied on count as an amount it could afford when only the format of count had been checked. It crossed a trust boundary without a check.

The repair is to check count before using it. Of a message of nn bytes, the count field takes 4, so at most ⌊(n−4)/16⌋\lfloor (n - 4) / 16 \rfloor records fit, where ⌊x⌋\lfloor x \rfloor means xx rounded down to a whole number. A 256-byte message has 256−4=252256 - 4 = 252 bytes left, and 252/16=15.75252 / 16 = 15.75, so it holds at most 15 records. Any count above 15 is rejected before memory is reserved, and the largest request is 15×16=24015 \times 16 = 240 bytes.

A second mistake happens after a correct check. The program verifies the tag over the bytes in one buffer, a region of memory. Before the action runs, another part of the program overwrites that buffer, or the action reads a different buffer. The action now relies on a check that was made on other bytes. This is a time-of-check to time-of-use §9.26 (TOCTOU) error. The repair is to copy the data once into memory that nothing else can change, check that copy, and use that same copy.

Stated precisely

Describe trust as the set of properties that have been established for a value, not as a single yes-or-no flag. For a received message such as the one above, the properties are, in the order in which they are established: its length is within a limit; it is authentic, meaning it comes from a holder of the key; it is well formed; it is authorized, meaning the sender may request this operation on this target; and, if it contains code, the code has been validated as safe to run. A message can have the first three properties and lack the last two.

A trust boundary is any point where the system begins to rely on one of these properties for data that a less trusted part supplied. The boundary is guarded correctly when both of the following hold.

  1. Every route by which the program can reach that point first passes the check that establishes the property.

  2. The bytes that were checked are the bytes that are used.

Limits and common mistakes

Authentic does not mean harmless. A message from a legitimate key holder can still ask for something that must be refused, carry a value that is out of range, or be an old message sent again, which is called a replay attack §1.26.

A trusted part of a system still handles untrusted data. The framing code that finds the tag runs on bytes an attacker chose, and the parser runs on bytes that any key holder, honest or not, chose, so both must be safe for every possible input.

A later check cannot undo an earlier effect. If memory was exhausted, a secret was sent or a record was stored before the check ran, rejecting the message afterwards reverses none of it.

Trust boundaries are not only at the network. A function call, a shared buffer, a file, or a stored record read back after a restart is a trust boundary whenever the part that wrote the data is trusted less than the part that reads it.

Data also crosses a trust boundary on the way out, and what a more trusted part sends out can reveal a secret. The steps above cover incoming data only.

Storing checked and unchecked data side by side in one data structure erases the boundary, because later code cannot tell which values were checked. The Common Weakness Enumeration (CWE), a public catalog of software and hardware weaknesses, lists this mistake as CWE-501, trust boundary violation.

A system rarely has one trust boundary. The useful question about a value is which property has been established and by which check, not whether the value is trusted.

Questions answered

Where does attacker-controlled data first become trusted state?

Attacker-controlled data first becomes trusted state at the first instruction that uses the attacker’s value to decide something or to change something the system protects. Typical first uses are a length or count that sizes a memory request or a copy, a position or identifier that selects a table entry or a function, a value written to permanent storage, and bytes that are run as code or installed as settings.

To find that instruction, start where the data enters the system and follow it forward. The first such use is a trust boundary: the point where the system starts to rely on data that a less trusted party chose. Then ask which check must come before that use, and whether that check covered exactly the bytes that are used. Expect to find several such points, one for each property the system relies on, not a single point for the whole system.

Related entries: Authentication (§1.6), Authorization (§1.7), Observation versus enforcement (§1.20), Deserialization (§4.29), Time-of-check to time-of-use (TOCTOU) (§9.26). Further reading: [222, 223].