Section 1.35

Security versus functional correctness

Definition

Functional correctness means that a program does what its specification requires: for every input in the specified range, it produces the required output and effects. Security means that stated properties keep holding while an adversary chooses the inputs, their timing or the environment, for example that the program touches no memory outside its region and leaks no secret. The two overlap, but neither implies the other: a perfectly confined program can give wrong answers, and a program that answers ordinary inputs correctly can still be exploited.

The idea

A device runs a small detector program that decides, for each incoming request, whether to allow it or deny it. The detector runs inside a sandbox §2.30, an environment that runs code under restrictions so that it cannot affect anything outside an assigned region. The sandbox checks every memory access and stops the program after a fixed number of instructions. The detector must deny any request whose temperature value is above 30 degrees. Its author makes a mistake, and the detector allows every request. Every guarantee of the sandbox holds: no access outside the region, prompt termination. The verdict is still wrong, and a request that must be denied gets through.

The sandbox constrains how the program computes, not what it computes. A calculator locked in a strong box is a fair picture: the box stops the calculator from damaging anything around it, but it cannot make the calculator’s arithmetic right. The picture stops at one point: security properties can be written into the specification as well, and then one complete specification covers both kinds. The usual mistake is to establish one narrow property, such as memory safety (every memory access stays inside memory the program may use), and then speak as if the program were correct.

The gap also runs the other way. A parser, code that splits a message into its fields, that returns the right fields for every well-formed message meets its functional specification, yet a malformed message chosen by an attacker can make it write past the end of a buffer. A functional specification usually describes the inputs that are meant to arrive; security must hold for every input that an attacker can send.

How it works

Keep the two kinds of requirement apart, and name the evidence for each.

  1. Write the functional specification. For each input, state the required output and effects. For the detector: deny if the temperature is above 30, otherwise allow.

  2. Write the security requirements. Typical ones: which memory the program may read and write; how many instructions it may run; who may install it; what information it may reveal.

  3. Match each requirement with the evidence that establishes it.

    • Memory safety, meaning that every access stays inside memory the program is allowed to use, comes from a runtime bounds check §2.27 in the sandbox, a test made as the program runs that each address lies inside the allowed region, or from a verifier, a checker that analyzes the program before it is allowed to run.

    • A limit on running time comes from an instruction budget §2.32, a cap on the number of instructions executed, or from a verifier’s proof that every loop ends.

    • Authorized installation comes from authentication §1.6, for example a valid tag on the program that only a key holder can compute, together with a rule saying which key holders may install programs.

    • The correctness of the decision rule comes from tests against the specification, from a proof, or from review. None of the evidence in the first three items says anything about it.

  4. Report each property with its own evidence. A verifier’s acceptance is evidence of memory safety and termination, not of a correct decision rule.

A verifier cannot close this gap even in principle, because it is never told what the program is for. It checks properties that make sense without knowing the purpose, such as staying inside memory. Whether the program computes the intended rule depends on a specification that only the author and the testers have. For programs in a general-purpose language there is also a theoretical limit [159], Rice’s theorem §19.2.2. Call a property of the function that a program computes nontrivial if some programs have it and some do not. For every such property, no algorithm §14.1 can decide, for every program, whether that program has the property.

Worked example

The specification: for a temperature tt in degrees Celsius, return 1 (allow) if t≤30t \le 30 and return 0 (deny) if t>30t > 30. Three candidate programs run in the sandbox, which gives each one a 32-byte scratch buffer §2.24 with positions 0 to 31.

Run each program on t=20t = 20, t=30t = 30 and t=40t = 40:

Program t=20t = 20 t=30t = 30 t=40t = 40
Required 1 1 0
P1P_1 1 1 1 (wrong)
P2P_2 1 0 (wrong) 0
P3P_3 1 1 stopped

P1P_1 runs one instruction and touches no memory, so it is memory-safe and terminates, yet it allows the reading 40, which must be denied: a functional error that lets a forbidden request through, with no safety violation at all. P2P_2 is also memory-safe. It fails only at the boundary: it denies 30, which the rule allows, because it tests t≥30t \ge 30 where the rule says t>30t > 30. The input 30 exposes this error, and the inputs 20 and 40 do not. P3P_3 computes the right verdict, but at t=40t = 40 it writes to position 40 of a buffer whose last position is 31, which is 40−31=940 - 31 = 9 positions past the end. The sandbox’s bounds check stops the program, so memory stays safe, but no verdict comes back. The device’s fallback rule now decides: fail closed §1.13 (deny when no verdict arrives) or fail open §1.14 (allow). The safety mechanism turned a security violation into a functional failure.

Suppose P1P_1 arrived with a valid authentication tag from an author who is allowed to install detectors. The tag proves who sent P1P_1. It does not make P1P_1 implement the rule.

Stated precisely

Let XX be the set of inputs that a program PP is specified for, and for each input xx in XX let S(x)S(x) be the set of outputs and effects that the specification allows. PP is functionally correct with respect to SS if, for every xx in XX, PP run on xx terminates with an outcome in S(x)S(x). A security property is a condition Φ\Phi on every execution that an adversary can cause, where the adversary may choose the inputs, including inputs outside XX, and their order and timing. For example, spatial memory safety §2.28 for a region that starts at address bb and is nn bytes long is the condition that every address aa the program reads or writes satisfies b≤a<b+nb \le a < b + n.

Neither condition implies the other unless one is written into the other. A deployed detector typically needs five separate properties:

  1. authentic origin: the program came from a key holder;

  2. authorized installation: that key holder may install it on this device;

  3. memory safety: the condition Φ\Phi above;

  4. bounded running time;

  5. a correct decision rule: functional correctness with respect to the policy SS.

A program can have the first four and lack the fifth, as P1P_1 does. One input on which the program fails, such as t=40t = 40 for P1P_1, refutes the fifth property. No finite number of passing inputs proves it, unless those inputs make up all of XX.

Limits and common mistakes

A complete specification can contain both kinds of requirement. The mistake is not in the words but in assuming that evidence for one clause of a specification covers the others.

Memory safety is not security in general. A memory-safe program can still contain logic errors, authorization §1.7 errors and side channels §1.29, which are unintended leaks of secrets through timing, power use or similar effects.

Correct behavior on intended inputs is not enough. Security requires defined, safe behavior on every input an attacker can send, including malformed ones.

The specification itself can be wrong. A program that exactly implements a badly chosen threshold is functionally correct and still fails its purpose. Checking a program against its specification is called verification; checking that the specification is the right one is called validation.

A safety mechanism that stops a faulty program leaves a decision open. The system must define what happens when no verdict arrives: denying keeps the protected resource safe at the cost of availability §1.10, and allowing keeps the service running at the cost of protection.

Wrong verdicts have names. When “positive” means flagged as malicious, an allow for a request that should be denied is a false negative §1.22, and a deny for a request that should be allowed is a false positive. P1P_1 produces false negatives and P2P_2 a false positive.

Questions answered

Can a memory-safe detector return an incorrect verdict?

Yes. A memory-safe detector can return an incorrect verdict, because memory safety only guarantees that every memory access stays inside memory the program is allowed to use; it says nothing about whether the answer the program computes matches the rule it is meant to implement. A detector that must deny every request with a temperature above 30, but always returns allow, never touches memory outside its region and always finishes, so it is memory-safe; it still allows a request with temperature 40 that it must deny. A comparison that is off by one, a test on the wrong field or a wrongly chosen threshold gives the same kind of result.

The correctness of the verdict needs its own evidence: tests that include the boundary values of the rule, such as 30 and 31 here; a proof against a written specification; or review. A verifier (a checker that analyzes a program before it may run) or a sandbox (an environment that confines a running program) is evidence of memory safety and bounded running time only, and a valid authentication tag is evidence only of who supplied the detector.

Related entries: Bytecode authenticity versus safety (§2.43), eBPF verifier (§3.3), False positive and false negative (§1.22), Sandbox (§2.30), Spatial memory safety (§2.28), Runtime bounds check (§2.27), Instruction budget (fuel) (§2.32), Fail closed (§1.13), Fail open (§1.14). Further reading: [159].