Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Secrecy and Noninterference

These notes are in part adapted from Security Foundations, as well as the survey paper Language-Based Information-Flow Security.

Using the program logic we have seen in class, you can prove that a given program can compute the right answer for all possible inputs. However, just because a program can compute the right answer does not mean that it doesn’t have any bugs!

Let’s see an example in C-looking pseudocode.

string PlaceOrder(string item, string address, string card) { 
    string receipt = FormatReceipt(item, address);
    Charge(card, Price(item));
    WriteFile(card, "~/orders.log");
    return receipt;
} 

The above piece of code takes in an item item, a shipping address address, and a credit card number card, charges the card for the item, and returns a receipt for the order. However, it also writes the credit card number to disk in a place where any other person (or concurrently running process) could read it. While PlaceOrder returns what it should, it should not count as a correct ordering function!

First, let’s see that Hoare logic on its own cannot prevent this issue. Let’s think of a specification for PlaceOrder, just focusing on the return value. \[ \{\mathsf{True}\}\ \mathsf{PlaceOrder}(\mathsf{item}, \mathsf{address}, \mathsf{card})\ \{\mathsf{return} = \mathsf{FormatReceipt}(\mathsf{item}, \mathsf{address})\} \] This specification of the return value is correct from a functional perspective, but not from a security perspective.

OK, we could try to fix it. Let’s try one fix, which prevents ~/orders.log from being written, by ensuring that the disk content at ~/orders.log remains unchanged: \[ \forall v.\ \{\mathsf{Disk}(\mathtt{\sim/orders.log}) = v\}\ \mathsf{PlaceOrder}(\mathsf{item}, \mathsf{address}, \mathsf{card})\ \{\mathsf{return} = \mathsf{FormatReceipt}(\mathsf{item}, \mathsf{address}) \land \mathsf{Disk}(\mathtt{\sim/orders.log}) = v\} \] However, of course, this does not prevent PlaceOrder from just writing to some other location on the disk. So maybe we should instead ban all writes to the disk: \[ \forall D.\ \{\mathsf{Disk} = D\}\ \mathsf{PlaceOrder}(\mathsf{item}, \mathsf{address}, \mathsf{card})\ \{\mathsf{return} = \mathsf{FormatReceipt}(\mathsf{item}, \mathsf{address}) \land \mathsf{Disk} = D\} \] But this precludes any legitimate write to the disk; for example, we might want PlaceOrder to write the receipt to a file on the disk:

string PlaceOrder(string item, string address, string card) { 
    string receipt = FormatReceipt(item, address);
    Charge(card, Price(item));
    WriteFile(receipt, "~/receipt.txt");
    return receipt;
}

How do we allow “good” writes to happen while preventing “bad” writes? The answer, which we will discuss today, is to track dependencies.

Security for IMP

Let’s think of this problem for our simple imperative language, IMP. Below is an abstracted version of our function from before: \[ \begin{array}{l} \mathit{receipt} := \mathit{item} + \mathit{address}; \\ \mathit{file} := \mathit{card}; \end{array} \] For us to analyze this example, we need to name which variables should be secret and which should be public. For this example, let’s say that \(\mathit{item}\), \(\mathit{address}\), \(\mathit{receipt}\), and \(\mathit{file}\) are public, but \(\mathit{card}\) is secret. This program is insecure because there is an information flow from the secret variable \(\mathit{card}\) to the public variable \(\mathit{file}\). That is, reading \(\mathit{file}\) will tell us what \(\mathit{card}\) is. We will now investigate what it means to prevent insecure information flows in programs.

We need a condition on programs which tells us that you can’t deduce the values of secret variables from the values of public variables. First, let’s consider the following requirement: no public variables can be equal to a secret variable. This requirement isn’t quite good enough, due to the following program: \[ \begin{array}{l} \mathit{receipt} := \mathit{item} + \mathit{address}; \\ \mathit{file} := \mathit{card} + 1; \end{array} \] While \(\mathit{file}\) is never equal to \(\mathit{card}\), an attacker can deduce the value of \(\mathit{card}\) by subtracting 1 from \(\mathit{file}\).

Instead, we need to capture the idea that no public variable can depend on a secret variable. We capture dependency by considering two executions of the program, which the same public inputs but different secret inputs. If the public outputs change in the two executions, then we know there is a dependency from the secret inputs to the public outputs.

Let’s see this in action with our example program. Let’s take two states: \[ \sigma_1 = \{\mathit{item} \mapsto 1, \mathit{address} \mapsto 2, \mathit{receipt} \mapsto 3, \mathit{file} \mapsto 4, \mathit{card} \mapsto 5\} \] Now, since the public inputs have to be the same in both executions, we set the second state as: \[ \sigma_2 = \{\mathit{item} \mapsto 1, \mathit{address} \mapsto 2, \mathit{receipt} \mapsto 3, \mathit{file} \mapsto 4, \mathit{card} \mapsto 6\} \]

Now, we can run the program to receive two final states: \[ \sigma_1’ = \{\mathit{item} \mapsto 1, \mathit{address} \mapsto 2, \mathit{receipt} \mapsto 3, \mathit{file} \mapsto 5, \mathit{card} \mapsto 5\} \] \[ \sigma_2’ = \{\mathit{item} \mapsto 1, \mathit{address} \mapsto 2, \mathit{receipt} \mapsto 3, \mathit{file} \mapsto 7, \mathit{card} \mapsto 6\} \]

Since the value of \(\mathit{file}\) changed at the end, we know that \(\mathit{file}\) depended on a secret input. If we instead changed the second line to \(\mathit{file} := \mathit{item};\), however, then the final states would be: \[ \sigma_1’ = \{\mathit{item} \mapsto 1, \mathit{address} \mapsto 2, \mathit{receipt} \mapsto 3, \mathit{file} \mapsto 1, \mathit{card} \mapsto 5\} \] \[ \sigma_2’ = \{\mathit{item} \mapsto 1, \mathit{address} \mapsto 2, \mathit{receipt} \mapsto 3, \mathit{file} \mapsto 1, \mathit{card} \mapsto 6\} \] Since the value of \(\mathit{file}\) did not change, we know that \(\mathit{file}\) does not depend on a secret input.

Defining Security. This brings us to our definition of security, which is called noninterference. To define noninterference for general programs, we need a notion of a labeling, \(\Gamma\), which assigns a security level to each variable in the program. For now, we will have two security levels: public and secret. Hence, \(\Gamma\) will have type \(\mathsf{Var} \to \{public, secret\}\).

Now, we need to know how to talk about the two parallel states, \(\sigma_1\) and \(\sigma_2\). We need them to agree on public outputs, but (possibly) differ on secret outputs. To that end, we introduce the following definition: \[ \sigma_1 \approx_\Gamma \sigma_2 := \forall x. \Gamma(x) = public \implies \sigma_1(x) = \sigma_2(x). \] \[ \sigma_1 \approx_L \sigma_2 := \forall x. \Gamma(x) = public \implies \sigma_1(x) = \sigma_2(x). \] We say that \(\sigma_1\) and \(\sigma_2\) are indistinguishable under \(\Gamma\) if \(\sigma_1 \approx_\Gamma \sigma_2\). Two states are indistinguishable if they agree on all public variables. Now that we have defined indistinguishability, we can define noninterference:

Definition (Noninterference). Let \(\Gamma\) be a labeling. An IMP statement \(s\) is noninterferent if, for all \(\sigma_1\) and \(\sigma_2\) such that \(\sigma_1 \approx_\Gamma \sigma_2\), if \(\langle \sigma_1, s \rangle \to^* \langle \sigma_1’, \mathsf{skip} \rangle\) and \(\langle \sigma_2, s \rangle \to^* \langle \sigma_2’, \mathsf{skip} \rangle\), then \(\sigma_1’ \approx_\Gamma \sigma_2’\).

You can think of noninterference as a property on two parallel executions of the program, where they begin from two indistinguishable states. If both executions finish, then the two final states must also be indistinguishable. This intuition shows why Hoare logic on its own is not enough: Hoare logic (as we have seen in class) only reasons about one execution at a time, whereas noninterference requires two.

Examples: Assignment

Throughout the below examples, we will write \(x_P\) for a public variable \(x\), and \(y_S\) for a secret variable \(y\).

Is the following program noninterferent? \[ \begin{array}{l} x_P := a_P + b_P; \\ y_S := z_S + 1; \end{array} \]

Yes, because, while we assign to a secret variable, we only use secrets to do so.

What about this one? \[ \begin{array}{l} x_P := a_P + b_P; \\ y_S := x_P + 1; \end{array} \] Still yes! You are allowed to move public values into secret variables; just not the other way around.

What about this? \[ \begin{array}{l} x_S := y_S + z_P; \\ a_S := x_S + 1; \\ b_P := 2 \cdot a_S; \end{array} \] No, because there is a dependency between \(x_S\) and \(b_P\).

This all seems to imply a rule: assignments can flow from public to secret, but not vice versa.

Examples: Control Flow

So far, we have only seen how noninterference works for simple programs consisting of assignments. Now, let’s see what noninterference means for control flow statements, such as conditionals and loops.

First, let’s consider the following program: \[ \begin{array}{l} \mathsf{if}\ x_P > 0\ \mathsf{then} \\ \quad y_S := 1 \\ \mathsf{else} \\ \quad y_S := 2 \end{array} \] Is this program noninterferent? Yes, because the condition is based on a public variable. Thus, the two parallel runs of the program will follow the same control flow, and either both assign \(y_S := 1\) or both assign \(y_S := 2\).

For the same reason, the following programs is also noninterferent: \[ \begin{array}{l} x_P := 0; \\ \mathsf{while}\ x_P < y_P\ \mathsf{do} \\ \quad z_S := z_S + 1; \end{array} \] We are performing a loop, but the loop condition is based on public variables.

OK, so does that mean we can only branch on a public expression? Not so! This is also fine: \[ \begin{array}{l} y_P := z_P \cdot 2; \\ \mathsf{if}\ y_S > 0\ \mathsf{then} \\ \quad x_S := y_S \\ \mathsf{else} \\ \quad x_S := -y_S \\ y_P := y_P + 1 \end{array} \] Thus, we can branch on secret variables as well; however, the below program is not noninterferent: \[ \begin{array}{l} \mathsf{if}\ y_S > 0\ \mathsf{then} \\ \quad x_P := 1 \\ \mathsf{else} \\ \quad x_P := 2 \end{array} \] Why is this? Because there is an indirect control-flow dependency from the secret variable \(y_S\) to the public variable \(x_P\). Considering booleans as integers still, the above program is equivalent to: \[ \begin{array}{l} x_P := 1 \cdot (y_S > 0) + 2 \cdot (y_S \leq 0); \end{array} \] which is not allowed by our rule about assignment.

This brings us to our second rule: you cannot assign to public variables inside of a secret branch.

Defending Noninterference

Now that we have a definition of noninterference, we can now define a type system for statements which can guarantee noninterference. What does this mean for the real world? It means that, given an untrusted piece of code (even one written by a coding agent), we can statically check and hence trust that it won’t misuse our secret data. This is a very powerful idea! The type system we are presenting is an information flow type system, because it tracks information flows throughout the program.

Since we are defining a type system, we first need to define our types. We want to track public and secret data, so our types are going to be security labels:

\[ L ::= \mathsf{public} \mid \mathsf{secret} \]

Now, we need to define our type system. Let’s recap the grammar of IMP we wish to capture:

\[ \begin{array}{rcl} e & ::= & n \mid x \mid e_1 \oplus e_2 \mid \ominus e \\ s & ::= & x := e \mid s_1; s_2 \mid \mathsf{if}\ e\ \mathsf{then}\ s_1\ \mathsf{else}\ s_2 \mid \mathsf{while}\ e\ \mathsf{do}\ s \end{array} \] Above, \(e_1 \oplus e_2\) and \(\ominus e\) represent arbitrary binary and unary operations, respectively.

Because our grammar has two subgrammars for expressions and statements, we will define separate typing judgments for each.

Typing Expressions. Expressions will be typed in a manner similar to how we typed arithmetic expressions earlier in the class. The judgment will look like \(\Gamma \vdash e : L\), where \(\Gamma : \mathsf{Var} \to \{ \mathsf{public}, \mathsf{secret} \}\) is a labeling.

To define the typing rules, we will introduce one more piece of notation: the join \(L_1 \sqcup L_2\), which represents the least upper bound of the two labels \(L_1\) and \(L_2\). For our simple set of labels \({\mathsf{public}, \mathsf{secret}}\), the join is defined as follows: \[ L_1 \sqcup L_2 := \begin{cases} \mathsf{public} & \text{if } L_1 = \mathsf{public} \text{ and } L_2 = \mathsf{public} \\ \mathsf{secret} & \text{otherwise} \end{cases} \]

We can now define the typing rules for expressions: \[ \dfrac{}{\Gamma \vdash n : \tau}\ \text{(T-Const)} \qquad \dfrac{}{\Gamma \vdash x : \Gamma(x)}\ \text{(T-Var)} \]

\[ \dfrac{\Gamma \vdash e_1 : L_1 \qquad \Gamma \vdash e_2 : L_2}{\Gamma \vdash e_1 \oplus e_2 : L_1 \sqcup L_2}\ \text{(T-BinOp)} \qquad \dfrac{\Gamma \vdash e : L}{\Gamma \vdash \ominus e : L}\ \text{(T-UnOp)} \]

Typing Statements. We are now ready to type statements. Since statements only are run for their side effects but do not themselves output values, our judgment for statements does not have an output type. Furthermore, because we need to track control-flow dependencies (since you can’t assign to public variables in a secret branch), our judgment also needs to include a program counter label which represents the current security level of control flow. Putting these ideas together, our judgment will look like \(\Gamma; \mathsf{pc} \vdash s\), where \(\mathsf{pc}\) is the current program counter label. Initially \(\mathsf{pc}\) will begin at \(\mathsf{public}\), but can grow when we branch on secrets.

To define the typing rules for statements, we need one extra ingredient: a notion of flows-to, written \(L_1 \sqsubseteq L_2\), which represents that information is allowed to flow from label \(L_1\) to label \(L_2\). For our simple set of labels \({\mathsf{public}, \mathsf{secret}}\), the flows-to relation is defined as follows: \[ L_1 \sqsubseteq L_2 := \begin{cases} \mathsf{false} & \text{if } L_1 = \mathsf{secret} \text{ and } L_2 = \mathsf{public} \\ \mathsf{true} & \text{otherwise} \end{cases} \]

We now can define the typing rules for statements:

\[ \dfrac{}{\Gamma; \mathsf{pc} \vdash \mathsf{skip}}\ \text{(T-Skip)} \qquad \dfrac{\Gamma \vdash e : L \qquad \mathsf{pc} \sqsubseteq \Gamma(x) \qquad L \sqsubseteq \Gamma(x)}{\Gamma; \mathsf{pc} \vdash x := e}\ \text{(T-Assign)} \]

\[ \dfrac{\Gamma; \mathsf{pc} \vdash s_1 \qquad \Gamma; \mathsf{pc} \vdash s_2}{\Gamma; \mathsf{pc} \vdash s_1; s_2}\ \text{(T-Seq)} \]

\[ \dfrac{\Gamma \vdash e : L \qquad \Gamma; \mathsf{pc} \sqcup L \vdash s_1 \qquad \Gamma; \mathsf{pc} \sqcup L \vdash s_2}{\Gamma; \mathsf{pc} \vdash \mathsf{if}\ e\ \mathsf{then}\ s_1\ \mathsf{else}\ s_2}\ \text{(T-If)} \]

\[ \dfrac{\Gamma \vdash e : L \qquad \Gamma; \mathsf{pc} \sqcup L \vdash s}{\Gamma; \mathsf{pc} \vdash \mathsf{while}\ e\ \mathsf{do}\ s}\ \text{(T-While)} \]

The rules for \(\mathsf{skip}\) and sequencing are straightforward. For assignment, we need to perform two checks: one, that the label of the expression can flow to the label of the variable being assigned (to rule out direct flows from secret to public), and two, that the current program counter label also flows to the label of the variable being assigned (to rule out implicit flows from the control flow to the variable). The rules for control flow statements grow the program counter label by the label of the guard.

Soundness. While we will formally prove soundness of the type system, we can state it:

Theorem. If \(\Gamma; \mathsf{public} \vdash s\), then \(s\) satisfies noninterference under \(\Gamma\).

Incompleteness of Information Flow Type Systems

It is worth mentioning that there are some programs which are semantically noninterferent but are not accepted by the type system. Indeed, since IMP is Turing-complete, it is undecidable to automatically check whether a given program is noninterferent. Consider this program: \[ \begin{array}{l} s; \\ \mathsf{if}\ e\ \mathsf{then}\ x_P := y_S\ \mathsf{else}\ \mathsf{skip} \end{array} \] where the variable \(y_S\) is not used in the program \(s\). This program is noninterferent if and only if, after running \(s\), the value of \(e\) is equal to zero, which is undecidable.

While this might be a contrived example, there are some more interesting (and realistic) programs which are noninterferent but do not pass our type system. Here is an example: \[ \begin{array}{l} x_P := y_S; \\ z_S := x_P; \\ x_P := 0; \end{array} \] Here, we temporarily make use of the public variable \(x_P\) to store the value of the secret variable \(y_S\) before copying it to the secret variable \(z_S\). The above program is equivalent to the program \[ \begin{array}{l} z_S := y_S; \\ x_P := 0; \end{array} \] which is noninterferent, so our first program must be as well. While this example might seem a bit contrived, it stands in for a real use-case: shared memory usage. Suppose we have a high-security process which needs to use memory that is shared between multiple threads. Naively, this is insecure, since another thread can read the contents of the shared memory, which might contain secrets. However, if the high-security process makes sure to zeroize the memory after releasing access to the shared resource, then the shared resource can safely hold secret data temporarily.