Algorithm Search⌘K

Algorithmcore

Volpano-Smith-Irvine type system

Also called security type system, VSI type system.

A type system that labels variables with security levels and the program counter with a level, and whose well-typed programs are termination-insensitive noninterferent.

Time
Θ(n) worst · best
Space
O(n) extra
Where
n = the number of nodes of the syntax tree of the program
Properties
not completedeterministicofflineiterativesound
Origin
Dennis Volpano, Cynthia Irvine, Geoffrey Smith, 1996

The Volpano-Smith-Irvine type system checks a program for secure information flow before it runs. Each variable has a security level, and the checker keeps a level pcpc for the program counter, so that it sees both the explicit flows of assignments and the implicit flows of branches. The checker is a single pass over the syntax tree and takes linear time. Its soundness theorem says that a well-typed program is termination-insensitive noninterferent (TINI): the public outputs of two runs that both stop do not depend on the secrets. This is the type system view of the certification of Denning and Denning, and the base of languages such as Jif.

In plain words

Each variable is public or secret. The checker reads the program and asks one question at each assignment: can secret data reach this variable here? Secret data can arrive in two ways. The assigned expression can read a secret variable. Or the assignment can be inside an if or a while whose condition reads a secret, because then the fact that the assignment ran tells something about the secret. The checker keeps a "context level" for the second way. It is public at the start, it becomes secret inside a branch on a secret, and it goes back when the branch ends. An assignment to a public variable is an error when the expression or the context is secret. If no assignment is an error, the program cannot leak its secrets through its final public values, provided it stops.

The problem

The input is a program cc in a small while language (assignments x:=ex := e, sequences, if statements and while loops), a security lattice (L,≤,⊔,⊓)(\mathcal{L}, \le, \sqcup, \sqcap) in which ℓ1≤ℓ2\ell_1 \le \ell_2 means that information may flow from ℓ1\ell_1 to ℓ2\ell_2, a level Γ(x)\Gamma(x) for each variable, and a start level pcpc (usually ⊥\bot). The output is true when pc⊢cpc \vdash c holds in the rules below, and false otherwise. Here label(e)\mathrm{label}(e) is the join of Γ(y)\Gamma(y) over the variables yy of ee.

  • x:=ex := e is typable at pp if label(e)⊔p≤Γ(x)\mathrm{label}(e) \sqcup p \le \Gamma(x).
  • c1;c2c_1; c_2 is typable at pp if both parts are typable at pp.
  • if ee then c1c_1 else c2c_2 is typable at pp if both branches are typable at p⊔label(e)p \sqcup \mathrm{label}(e).
  • while ee do c1c_1 is typable at pp if c1c_1 is typable at p⊔label(e)p \sqcup \mathrm{label}(e).

The goal of the type system is the semantic property noninterference, in its termination-insensitive form.

The algorithm

The checker walks the syntax tree with an explicit stack of pairs (command, level of the program counter), so that a deep program does not overflow a call stack.

  1. TYPECHECK(c, Γ, pc) ▷ true if pc ⊢ c
  2. 1S ← a stack that holds the pair (c, pc)
  3. 2while S is not empty do
  4. 3(d, p) ← pop(S)
  5. 4if d = skip then continue
  6. 5if d = (x := e) then
  7. 6if label(e) ⊔ p ≰ Γ(x) then return false
  8. 7if d = (d1; d2) then push (d2, p), then (d1, p)
  9. 8if d = (if e then d1 else d2) then
  10. 9q ← p ⊔ label(e); push (d2, q), then (d1, q)
  11. 10if d = (while e do d1) then push (d1, p ⊔ label(e))
  12. 11return true

Line L6 is the only test. The term label(e)\mathrm{label}(e) catches explicit flows and the term pp catches implicit flows. Lines L9 and L10 raise the program counter level for the commands inside a branch or a loop on ee. The level is part of each stack entry, so it goes back to the old value for the command after the branch: line L7 gives d2d_2 the level pp of the sequence, not the level of a branch inside d1d_1. The loop condition can be secret; line L10 accepts this, and this is the reason the result is TINI and not termination-sensitive.

Example

Take the two-point lattice L≤HL \le H, Γ(l)=Γ(m)=L\Gamma(l) = \Gamma(m) = L, Γ(h)=H\Gamma(h) = H and the program

c=(l:=l+1); (if h then h:=l else skip); (m:=h)c = (l := l + 1);\ (\mathtt{if}\ h\ \mathtt{then}\ h := l\ \mathtt{else}\ \mathtt{skip});\ (m := h)

parsed as c1;(c2;c3)c_1; (c_2; c_3). The stack is listed with its top at the right.

step popped (d,p)(d, p) check or action stack after the step
1 (c,L)(c, L) sequence (c2;c3,L),(c1,L)(c_2; c_3, L), (c_1, L)
2 (l:=l+1,L)(l := l + 1, L) L⊔L≤LL \sqcup L \le L, true (c2;c3,L)(c_2; c_3, L)
3 (c2;c3,L)(c_2; c_3, L) sequence (c3,L),(c2,L)(c_3, L), (c_2, L)
4 (c2,L)(c_2, L) if on hh, q=Hq = H (c3,L),(skip,H),(h:=l,H)(c_3, L), (\mathtt{skip}, H), (h := l, H)
5 (h:=l,H)(h := l, H) L⊔H≤HL \sqcup H \le H, true (c3,L),(skip,H)(c_3, L), (\mathtt{skip}, H)
6 (skip,H)(\mathtt{skip}, H) nothing (c3,L)(c_3, L)
7 (m:=h,L)(m := h, L) H⊔L≤LH \sqcup L \le L, false return false

At step 7 the level of the program counter is LL again, so the program would be typable if the last command were m:=lm := l. The flow from hh to mm is explicit and real, and the checker rejects the program.

Correctness

There are two claims, both proved in full below.

  1. The checker decides typability. TYPECHECK(c,Γ,pc)(c, \Gamma, pc) returns true if and only if pc⊢cpc \vdash c. The invariant of line L2 is: pc⊢cpc \vdash c holds if and only if p⊢dp \vdash d holds for every pair (d,p)(d, p) on the stack. The rules are syntax directed, so each pop replaces a pair by exactly the premises of its rule, or finds a false side condition.
  2. Soundness (Volpano, Irvine and Smith 1996). Hypotheses: the deterministic while language above, with integer variables and no output during the run, and an observer who sees the final values of the variables at levels at or below ℓ\ell. Statement: if ⊥⊢c\bot \vdash c, then for all stores s1=ℓs2s_1 =_\ell s_2, if both runs of cc stop, the final stores are ℓ\ell-equivalent. The proof uses three lemmas: an expression of level at most ℓ\ell has the same value in ℓ\ell-equivalent stores; a command typable at pp changes only variables xx with p≤Γ(x)p \le \Gamma(x) (confinement); and an induction on the two runs. The theorem says nothing about runs that do not stop, and nothing about time.

The same theorem holds for a language with output statements, when an output to a public channel needs a public expression and a public program counter. This is Theorem 1 of Askarov, Hunt, Sabelfeld and Sands (2008). Their later theorems show how much such a TINI program can still leak through termination; see TINI.

Proof: The checker decides typability, and typable programs are termination-insensitive noninterferentfull

We use the language, the rules and the pseudocode of the page. Levels are in a finite lattice (L,≤,⊔,⊓)(\mathcal{L}, \le, \sqcup, \sqcap) with least element ⊥\bot. For an expression ee, label(e)\mathrm{label}(e) is the join of Γ(y)\Gamma(y) over the variables yy of ee, and ⊥\bot if ee has no variable. The typing judgment p⊢cp \vdash c is defined by these rules, one for each form of cc:

  • p⊢skipp \vdash \mathtt{skip}.
  • p⊢x:=ep \vdash x := e if label(e)⊔p≤Γ(x)\mathrm{label}(e) \sqcup p \le \Gamma(x).
  • p⊢c1;c2p \vdash c_1; c_2 if p⊢c1p \vdash c_1 and p⊢c2p \vdash c_2.
  • p⊢if e then c1 else c2p \vdash \mathtt{if}\ e\ \mathtt{then}\ c_1\ \mathtt{else}\ c_2 if q⊢c1q \vdash c_1 and q⊢c2q \vdash c_2, where q=p⊔label(e)q = p \sqcup \mathrm{label}(e).
  • p⊢while e do c1p \vdash \mathtt{while}\ e\ \mathtt{do}\ c_1 if p⊔label(e)⊢c1p \sqcup \mathrm{label}(e) \vdash c_1.

The rule for a variable in the original system allows any type above Γ(y)\Gamma(y); the least one is label(e)\mathrm{label}(e), and a larger type only makes the side conditions harder. So these rules accept the same programs as the rules with subsumption.

Part 1. TYPECHECK(c,Γ,pc)(c, \Gamma, pc) returns true if and only if pc⊢cpc \vdash c

Invariant. At the test of line L2, pc⊢cpc \vdash c holds if and only if p⊢dp \vdash d holds for every pair (d,p)(d, p) on the stack SS.

Initialization. Line L1 puts the single pair (c,pc)(c, pc) on SS, so the two sides are the same statement.

Maintenance. Let line L3 pop (d,p)(d, p). The rules are syntax directed: for each form of dd there is exactly one rule, and p⊢dp \vdash d holds if and only if the side condition of that rule holds and each premise holds.

  • If dd is skip, p⊢dp \vdash d holds with no premise. Removing the pair keeps the invariant.
  • If dd is x:=ex := e and the side condition holds, p⊢dp \vdash d holds and the pair is removed. If the side condition fails, line L6 returns false. Then p⊢dp \vdash d is false, and by the invariant pc⊢cpc \vdash c is false, so the answer is right.
  • If dd is d1;d2d_1; d_2, line L7 replaces the pair by (d1,p)(d_1, p) and (d2,p)(d_2, p), which are exactly the premises of the rule.
  • If dd is an if statement, lines L8 and L9 replace the pair by (d1,q)(d_1, q) and (d2,q)(d_2, q) with q=p⊔label(e)q = p \sqcup \mathrm{label}(e), the premises of the rule.
  • If dd is a while loop, line L10 replaces the pair by (d1,p⊔label(e))(d_1, p \sqcup \mathrm{label}(e)), the premise of the rule.

In each case the conjunction over the stack changes from "p⊢dp \vdash d and the rest" to "the premises of dd and the rest", which is equivalent.

Termination. Each step removes one pair and adds pairs for proper subterms of its command only. So each node of the syntax tree of cc is popped at most once, and the loop stops after at most nn steps for a program of nn command nodes. When it stops without line L6, the stack is empty, the conjunction over it is true, and by the invariant pc⊢cpc \vdash c holds. Line L11 returns true.

Part 2. If ⊥⊢c\bot \vdash c, then cc is TINI for every observer level

Semantics. A store ss maps the variables to integers. We write ⟨c,s⟩⇓t\langle c, s \rangle \Downarrow t when the run of cc from ss ends in the store tt, with the usual big-step rules; a while loop with a true condition runs the body and then the loop again. For an observer level ℓ\ell, two stores are ℓ\ell-equivalent, s1=ℓs2s_1 =_\ell s_2, if s1(x)=s2(x)s_1(x) = s_2(x) for every xx with Γ(x)≤ℓ\Gamma(x) \le \ell. For the two-point lattice and ℓ=L\ell = L this is s1=Ls2s_1 =_L s_2.

Claim. If ⊥⊢c\bot \vdash c, s1=ℓs2s_1 =_\ell s_2, ⟨c,s1⟩⇓t1\langle c, s_1 \rangle \Downarrow t_1 and ⟨c,s2⟩⇓t2\langle c, s_2 \rangle \Downarrow t_2, then t1=ℓt2t_1 =_\ell t_2.

Lemma 1 (simple security). If label(e)≤ℓ\mathrm{label}(e) \le \ell and s1=ℓs2s_1 =_\ell s_2, then ee has the same value in s1s_1 and in s2s_2.

Proof. Each variable yy of ee has Γ(y)≤label(e)≤ℓ\Gamma(y) \le \mathrm{label}(e) \le \ell, so s1(y)=s2(y)s_1(y) = s_2(y). The value of ee depends only on these values.

Lemma 2 (weakening). If p⊢cp \vdash c and p′≤pp' \le p, then p′⊢cp' \vdash c.

Proof. By induction on cc. Each side condition has the form label(e)⊔p≤Γ(x)\mathrm{label}(e) \sqcup p \le \Gamma(x), and label(e)⊔p′≤label(e)⊔p\mathrm{label}(e) \sqcup p' \le \mathrm{label}(e) \sqcup p. The premises use p⊔label(e)p \sqcup \mathrm{label}(e), which also decreases when pp decreases.

Lemma 3 (confinement). If p⊢cp \vdash c and ⟨c,s⟩⇓t\langle c, s \rangle \Downarrow t, then t(x)=s(x)t(x) = s(x) for every xx with p≰Γ(x)p \not\le \Gamma(x).

Proof. By induction on the derivation of ⟨c,s⟩⇓t\langle c, s \rangle \Downarrow t. For skip, t=st = s. For x:=ex := e, the rule gives p≤label(e)⊔p≤Γ(x)p \le \mathrm{label}(e) \sqcup p \le \Gamma(x), so the only changed variable has p≤Γ(x)p \le \Gamma(x). For a sequence, both parts are typed at pp, and the induction hypothesis applies to each part. For an if statement or a while loop, the parts run are typed at q=p⊔label(e)≥pq = p \sqcup \mathrm{label}(e) \ge p; if p≰Γ(x)p \not\le \Gamma(x) then q≰Γ(x)q \not\le \Gamma(x), so the induction hypothesis for the body (at qq) and for the next round of the loop (at pp) leaves xx unchanged.

Proof of the claim. We prove the stronger statement for every pp: if p⊢cp \vdash c, s1=ℓs2s_1 =_\ell s_2, ⟨c,s1⟩⇓t1\langle c, s_1 \rangle \Downarrow t_1 and ⟨c,s2⟩⇓t2\langle c, s_2 \rangle \Downarrow t_2, then t1=ℓt2t_1 =_\ell t_2. The induction is on the sum of the sizes of the two derivations.

  1. skip. ti=sit_i = s_i.
  2. x:=ex := e. If Γ(x)≤ℓ\Gamma(x) \le \ell, then label(e)≤Γ(x)≤ℓ\mathrm{label}(e) \le \Gamma(x) \le \ell, and by Lemma 1 both runs store the same value in xx. If Γ(x)≰ℓ\Gamma(x) \not\le \ell, the change of xx is not seen at ℓ\ell. The other variables do not change.
  3. c1;c2c_1; c_2. The runs pass through stores u1u_1 and u2u_2. By the hypothesis for c1c_1, u1=ℓu2u_1 =_\ell u_2, and then by the hypothesis for c2c_2, t1=ℓt2t_1 =_\ell t_2.
  4. if ee then c1c_1 else c2c_2, with label(e)≤ℓ\mathrm{label}(e) \le \ell. By Lemma 1 both runs take the same branch, which is typed at qq, and the hypothesis for that branch gives the result.
  5. while ee do c1c_1, with label(e)≤ℓ\mathrm{label}(e) \le \ell. By Lemma 1 the condition has the same value in both runs. If it is false, ti=sit_i = s_i. If it is true, each run is the body from sis_i to uiu_i, then the loop from uiu_i to tit_i. The hypothesis for the body (typed at p⊔label(e)p \sqcup \mathrm{label}(e)) gives u1=ℓu2u_1 =_\ell u_2, and the hypothesis for the loop (a smaller derivation, typed at pp) gives t1=ℓt2t_1 =_\ell t_2.
  6. An if statement or a while loop with label(e)≰ℓ\mathrm{label}(e) \not\le \ell. Then q=p⊔label(e)≰ℓq = p \sqcup \mathrm{label}(e) \not\le \ell. If the loop condition is false in run ii, then ti=sit_i = s_i. In every other case the run of the statement from sis_i is a run of a command typed at qq (a branch, or the body followed by the loop, which is also typed at qq by Lemma 2 and the loop rule, since q⊔label(e)=qq \sqcup \mathrm{label}(e) = q). Take xx with Γ(x)≤ℓ\Gamma(x) \le \ell. Then q≰Γ(x)q \not\le \Gamma(x), since otherwise q≤Γ(x)≤ℓq \le \Gamma(x) \le \ell. By Lemma 3, ti(x)=si(x)t_i(x) = s_i(x). So t1(x)=s1(x)=s2(x)=t2(x)t_1(x) = s_1(x) = s_2(x) = t_2(x).

The claim is the case p=⊥p = \bot. The statement needs both runs to end: case 6 uses the final stores, and the type system allows a loop on a high condition, so a typable program can stop on some secrets and run forever on others. So the result is termination-insensitive noninterference, and it says nothing about time. ■\blacksquare

Complexity

The model is the word-RAM. Let nn be the number of nodes of the syntax tree. Each command node is pushed and popped at most once (proof, Part 1), and each expression is read once, by line L6, L9 or L10. So the checker performs Θ(n)\Theta(n) joins and comparisons of levels. For a fixed finite lattice, or for a lattice of subsets of at most ww categories stored as one machine word, each join and comparison takes O(1)O(1) time, so the time is Θ(n)\Theta(n) in each case. The stack holds at most one entry for each node of the tree, so the extra space is O(n)O(n).

Properties

  • Sound for TINI. Each accepted program is TINI (Correctness, claim 2).
  • Not complete. The program l:=h−hl := h - h always stores 0 and is secure, but line L6 rejects it, because label(h−h)=H\mathrm{label}(h - h) = H. The program "if hh then l:=0l := 0 else l:=0l := 0" is also secure and rejected. No sound checker is complete, because noninterference is undecidable.
  • Flow-insensitive. Γ(x)\Gamma(x) is fixed for the whole program. The program l:=h; l:=0l := h;\ l := 0 is secure but is rejected.
  • Compositional. A sequence is typable when its parts are typable at the same level, so a library can be checked one procedure at a time.
  • Static. The checker never runs the program. It needs no secret input and adds no cost at run time.

Variants and improvements

  • Termination-sensitive typing. Volpano and Smith (1997) close the termination channel by restricting loops. In the simplest form, line L10 also requires p⊔label(e)=⊥p \sqcup \mathrm{label}(e) = \bot: a loop is accepted only with a public condition and outside every secret branch. Then low-equivalent stores give the same termination behaviour, and a typable program is termination-sensitive noninterferent. This rejects many useful loops.
  • The original form. Volpano, Irvine and Smith write the types as "τ\tau var" for a variable and "τ\tau cmd" for a command, where a command of type τ\tau cmd assigns only to variables of level at least τ\tau. A branch on ee is typable when label(e)≤τ\mathrm{label}(e) \le \tau for the types of its arms. This is equivalent to the program counter form; the tests of the code check both forms against each other.
  • Output channels. Add an output statement that is typable only when the expression and the program counter are public. This is the system of Askarov et al. (2008).
  • Flow-sensitive types. Let the level of a variable change from one program point to the next, so that l:=h; l:=0l := h;\ l := 0 is accepted. The dynamic analogue is no-sensitive-upgrade.
  • Declassification. Add a construct that lowers the level of an expression on purpose; see declassification. Jif, the language of the decentralized label model, combines such a construct with labels of several owners.
  • Relational verification. Self-composition proves noninterference of a program that the type system rejects, at the price of a full program proof.

When to use it

Use the type system when the program is known before it runs and you want a guarantee for all inputs at no cost at run time. It is the standard choice in a language designed for information flow. Use a dynamic monitor when the program is known only at run time, for example a script on a web page: no-sensitive-upgrade stops the run on a possible leak, secure multi-execution runs the program once per level, and faceted execution runs it once on values with a facet per level. Use taint analysis when you only need to find explicit copies of untrusted data; it tracks explicit flows only in most tools and gives no noninterference guarantee. If the program must leak through termination or time, the type system does not help; see TINI and constant-time verification.

Pitfalls

  • No program counter at line L6. The test label(e)≤Γ(x)\mathrm{label}(e) \le \Gamma(x) without pp accepts "if hh then l:=1l := 1 else l:=0l := 0", which copies a bit of hh to ll. The right test is label(e)⊔p≤Γ(x)\mathrm{label}(e) \sqcup p \le \Gamma(x).
  • A global program counter. If line L9 raises one shared variable pcpc and never lowers it, every assignment after the first secret branch is checked at HH, and the program "if hh then h:=0h := 0 else skip; l:=1l := 1" is rejected. If the variable is lowered too early, before the second branch d2d_2 is checked, the checker becomes unsound. Keep the level in each stack entry, as in lines L7 and L9.
  • The plain level at line L10. Pushing (d1,p)(d_1, p) instead of (d1,p⊔label(e))(d_1, p \sqcup \mathrm{label}(e)) accepts "while h>lh > l do l:=l+1l := l + 1", which copies hh to ll.
  • Reading the theorem as termination-sensitive. Line L10 accepts "while h=0h = 0 do skip; l:=1l := 1". Whether this program stops depends on hh. With public output, a typable program can reveal a whole secret of NN binary digits through termination, although it needs time exponential in NN (Askarov et al. 2008). Use the termination-sensitive variant if this matters.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

security_type_system.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/security-type-system/. The authors are listed in prov/8RQ898.jsonl.

"""The Volpano-Irvine-Smith security type system for a small while language.

Programs are nested tuples:

    expression: an int (a constant), a str (a variable), or (op, e1, e2)
                with op one of "+", "-", "*", "<", "=="
    command:    ("skip",)
                ("assign", x, e)
                ("seq", c1, c2)
                ("if", e, c1, c2)
                ("while", e, c)

A security level is a frozenset of category names, ordered by inclusion.
The empty set is the bottom level L (public). The two-point lattice is
L = frozenset() <= H = frozenset({"h"}). Join is set union.
"""

from typing import Union

Level = frozenset
Expr = Union[int, str, tuple]
Cmd = tuple

BOTTOM: Level = frozenset()


def expr_level(e: Expr, gamma: dict[str, Level]) -> Level:
    """Return the join of the levels of the variables in e (BOTTOM if none).

    Precondition: every variable of e is a key of gamma.
    Cost: O(size of e) joins. Uses an explicit stack, no recursion.
    """
    level: Level = BOTTOM
    stack: list[Expr] = [e]
    while stack:
        f = stack.pop()
        if isinstance(f, bool) or isinstance(f, int):
            continue
        if isinstance(f, str):
            level = level | gamma[f]
        else:
            stack.append(f[1])
            stack.append(f[2])
    return level


def typecheck(c: Cmd, gamma: dict[str, Level], pc: Level = BOTTOM) -> bool:
    """Return True if c is typable under the variable levels gamma at program counter pc.

    The rules are those of Volpano, Irvine and Smith in the program counter
    form: x := e needs level(e) join pc <= gamma(x); a branch or a loop on e
    checks its body at pc join level(e). Typable programs are
    termination-insensitive noninterferent.
    Precondition: every variable of c is a key of gamma.
    Cost: O(n) lattice operations for a program of n syntax nodes, and O(n)
    extra space for the explicit stack.
    """
    stack: list[tuple[Cmd, Level]] = [(c, pc)]  # @line L1
    while stack:  # @line L2
        d, p = stack.pop()  # @line L3
        tag = d[0]
        if tag == "skip":  # @line L4
            continue
        if tag == "assign":  # @line L5
            if not (expr_level(d[2], gamma) | p) <= gamma[d[1]]:  # @line L6
                return False
        elif tag == "seq":  # @line L7
            stack.append((d[2], p))
            stack.append((d[1], p))
        elif tag == "if":  # @line L8
            q = p | expr_level(d[1], gamma)  # @line L9
            stack.append((d[3], q))
            stack.append((d[2], q))
        elif tag == "while":  # @line L10
            stack.append((d[2], p | expr_level(d[1], gamma)))
        else:
            raise ValueError(f"unknown command {tag!r}")
    return True  # @line L11
test_security_type_system.py 263 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/security-type-system/. The authors are listed in prov/8RQ898.jsonl.

import itertools
import random

from security_type_system import expr_level, typecheck

L = frozenset()
H = frozenset({"h"})
GAMMA = {"l": L, "m": L, "h": H}
VARS = ["l", "m", "h"]
DOMAIN = [0, 1, 2]
MOD = 256
FUEL = 200


# An independent oracle: the original "tau var / tau cmd" form of the rules.
# A command gets the type "c cmd", where c is the meet (here the
# intersection) of the levels of the variables it may assign. A branch
# on e is typable when level(e) <= the type of each arm.

TOP = None  # stands for the top of the "tau cmd" order (no assignment)


def meet(a, b):
    if a is TOP:
        return b
    if b is TOP:
        return a
    return a & b


def oracle_level(e, gamma):
    if isinstance(e, int):
        return frozenset()
    if isinstance(e, str):
        return gamma[e]
    return oracle_level(e[1], gamma) | oracle_level(e[2], gamma)


def oracle_cmd_type(c, gamma):
    """Return (ok, write level) for c."""
    tag = c[0]
    if tag == "skip":
        return True, TOP
    if tag == "assign":
        return oracle_level(c[2], gamma) <= gamma[c[1]], gamma[c[1]]
    if tag == "seq":
        ok1, w1 = oracle_cmd_type(c[1], gamma)
        ok2, w2 = oracle_cmd_type(c[2], gamma)
        return ok1 and ok2, meet(w1, w2)
    if tag == "if":
        ok1, w1 = oracle_cmd_type(c[2], gamma)
        ok2, w2 = oracle_cmd_type(c[3], gamma)
        w = meet(w1, w2)
        guard_ok = w is TOP or oracle_level(c[1], gamma) <= w
        return ok1 and ok2 and guard_ok, w
    if tag == "while":
        ok, w = oracle_cmd_type(c[2], gamma)
        guard_ok = w is TOP or oracle_level(c[1], gamma) <= w
        return ok and guard_ok, w
    raise ValueError(tag)


def oracle_typable(c, gamma):
    return oracle_cmd_type(c, gamma)[0]


# A small interpreter, used only to check noninterference by brute force.

def ev(e, s):
    if isinstance(e, int):
        return e % MOD
    if isinstance(e, str):
        return s[e]
    a, b = ev(e[1], s), ev(e[2], s)
    op = e[0]
    if op == "+":
        return (a + b) % MOD
    if op == "-":
        return (a - b) % MOD
    if op == "*":
        return (a * b) % MOD
    if op == "<":
        return int(a < b)
    return int(a == b)


def run(c, s):
    s = dict(s)
    work = [c]
    fuel = FUEL
    while work:
        fuel -= 1
        if fuel < 0:
            return None
        d = work.pop()
        tag = d[0]
        if tag == "assign":
            s[d[1]] = ev(d[2], s)
        elif tag == "seq":
            work.append(d[2])
            work.append(d[1])
        elif tag == "if":
            work.append(d[2] if ev(d[1], s) != 0 else d[3])
        elif tag == "while":
            if ev(d[1], s) != 0:
                work.append(d)
                work.append(d[2])
    return s


def low(s, gamma):
    return tuple(s[x] for x in sorted(gamma) if gamma[x] == frozenset())


def is_tini(c, gamma, variables=VARS):
    seen = {}
    for vals in itertools.product(DOMAIN, repeat=len(variables)):
        s = dict(zip(variables, vals))
        t = run(c, s)
        if t is None:
            continue
        key = low(s, gamma)
        out = low(t, gamma)
        if seen.setdefault(key, out) != out:
            return False
    return True


# Random programs of at most 10 statements.

def rand_expr(rng, variables, depth=0):
    r = rng.random()
    if depth >= 2 or r < 0.35:
        return rng.choice(variables)
    if r < 0.5:
        return rng.randrange(3)
    return (rng.choice(["+", "-", "*", "<", "=="]),
            rand_expr(rng, variables, depth + 1),
            rand_expr(rng, variables, depth + 1))


def rand_cmd(rng, variables, budget):
    """Return (command, statements used)."""
    if budget <= 1:
        return ("assign", rng.choice(variables), rand_expr(rng, variables)), 1
    r = rng.random()
    if r < 0.35:
        return ("assign", rng.choice(variables), rand_expr(rng, variables)), 1
    if r < 0.4:
        return ("skip",), 1
    if r < 0.65:
        c1, n1 = rand_cmd(rng, variables, budget // 2)
        c2, n2 = rand_cmd(rng, variables, budget - n1)
        return ("seq", c1, c2), n1 + n2
    if r < 0.88:
        c1, n1 = rand_cmd(rng, variables, (budget - 1) // 2)
        c2, n2 = rand_cmd(rng, variables, max(1, budget - 1 - n1))
        return ("if", rand_expr(rng, variables), c1, c2), 1 + n1 + n2
    c1, n1 = rand_cmd(rng, variables, budget - 1)
    return ("while", rand_expr(rng, variables), c1), 1 + n1


# Tests.

def test_worked_example():
    # l := 1; if h then m := h else skip  is rejected (m is low).
    prog = ("seq", ("assign", "l", 1),
            ("if", "h", ("assign", "m", "h"), ("skip",)))
    assert not typecheck(prog, GAMMA)
    # The same program with a high target is typable.
    gamma2 = {"l": L, "m": H, "h": H}
    assert typecheck(prog, gamma2)
    assert is_tini(prog, gamma2)


def test_explicit_and_implicit_flows_rejected():
    assert not typecheck(("assign", "l", "h"), GAMMA)
    implicit = ("if", "h", ("assign", "l", 1), ("assign", "l", 0))
    assert not typecheck(implicit, GAMMA)
    assert not is_tini(implicit, GAMMA)
    loop = ("while", "h", ("assign", "l", 1))
    assert not typecheck(loop, GAMMA)


def test_secure_flows_accepted():
    assert typecheck(("assign", "h", "l"), GAMMA)
    assert typecheck(("assign", "l", ("+", "l", "m")), GAMMA)
    assert typecheck(("if", "l", ("assign", "h", 0), ("assign", "m", 1)), GAMMA)
    assert typecheck(("skip",), GAMMA)


def test_incomplete_but_secure_program_rejected():
    # l := h - h always stores 0, so it is secure, but the type system rejects it.
    prog = ("assign", "l", ("-", "h", "h"))
    assert is_tini(prog, GAMMA)
    assert not typecheck(prog, GAMMA)


def test_high_loop_is_typable_and_only_tini():
    # The type system accepts a loop on a secret. It can diverge on some
    # secrets, so the program is TINI but not termination-sensitive.
    prog = ("seq", ("while", "h", ("skip",)), ("assign", "l", 1))
    assert typecheck(prog, GAMMA)
    assert is_tini(prog, GAMMA)
    assert run(prog, {"l": 0, "m": 0, "h": 1}) is None
    assert run(prog, {"l": 0, "m": 0, "h": 0}) is not None


def test_initial_pc():
    assert typecheck(("assign", "h", 1), GAMMA, pc=H)
    assert not typecheck(("assign", "l", 1), GAMMA, pc=H)


def test_expr_level():
    assert expr_level(3, GAMMA) == L
    assert expr_level(("+", "l", ("*", "m", 2)), GAMMA) == L
    assert expr_level(("<", "l", "h"), GAMMA) == H


def test_diamond_lattice():
    a, b = frozenset({"a"}), frozenset({"b"})
    gamma = {"x": L, "y": a, "z": b, "w": a | b}
    assert typecheck(("assign", "w", ("+", "y", "z")), gamma)
    assert not typecheck(("assign", "y", "z"), gamma)
    assert not typecheck(("if", "y", ("assign", "z", 1), ("skip",)), gamma)
    assert typecheck(("if", "y", ("assign", "w", "z"), ("assign", "y", 0)), gamma)


def test_deep_program_no_recursion():
    prog = ("assign", "l", 0)
    for _ in range(5000):
        prog = ("seq", ("assign", "l", ("+", "l", 1)), prog)
    assert typecheck(prog, GAMMA)
    deep = ("assign", "h", 0)
    for _ in range(5000):
        deep = ("if", "l", deep, ("skip",))
    assert typecheck(deep, GAMMA)


def test_random_against_oracle_and_soundness():
    rng = random.Random(8398)
    typable = 0
    for _ in range(600):
        prog, _n = rand_cmd(rng, VARS, rng.randint(1, 10))
        got = typecheck(prog, GAMMA)
        assert got == oracle_typable(prog, GAMMA), prog
        if got:
            typable += 1
            assert is_tini(prog, GAMMA), prog
    assert typable >= 100


def test_random_diamond_against_oracle():
    rng = random.Random(4242)
    a, b = frozenset({"a"}), frozenset({"b"})
    gamma = {"x": L, "y": a, "z": b, "w": a | b}
    names = sorted(gamma)
    for _ in range(500):
        prog, _n = rand_cmd(rng, names, rng.randint(1, 10))
        assert typecheck(prog, gamma) == oracle_typable(prog, gamma), prog

History

Denning and Denning (1977) gave a certification mechanism for secure flow and proved that a certified program is secure in the sense of the lattice model. Volpano, Irvine and Smith (1996) wrote the mechanism as a type system and proved soundness with respect to a semantic noninterference property, which made the guarantee precise: it covers runs that stop and does not cover termination or time. Volpano and Smith (1997) gave the version that closes the termination channel. The survey of Sabelfeld and Myers (2003) presents the program counter form used on this page and many later extensions, such as declassification, concurrency and the Jif and FlowCaml languages.

References

  1. Dennis Volpano, Cynthia Irvine and Geoffrey Smith, A sound type system for secure flow analysis, Journal of Computer Security 4(2-3), 1996, pp. 167 to 187, the typing rules for a while language and the soundness theorem
  2. Dorothy E. Denning and Peter J. Denning, Certification of programs for secure information flow, Communications of the ACM 20(7), 1977, pp. 504 to 513, Section 3, the certification mechanism that the type system recasts
  3. Andrei Sabelfeld and Andrew C. Myers, Language-based information-flow security, IEEE Journal on Selected Areas in Communications 21(1), 2003, pp. 5 to 19, the type system in program counter form and its extensions
  4. Aslan Askarov, Sebastian Hunt, Andrei Sabelfeld and David Sands, Termination-insensitive noninterference leaks more than just a bit, ESORICS 2008, LNCS 5283, pp. 333 to 348, Section 3 (a Denning-style type system with output, Fig. 3, and Theorem 1, typable programs are TINI)
  5. Dennis Volpano and Geoffrey Smith, Eliminating covert flows with minimum typings, Proceedings of the 10th IEEE Computer Security Foundations Workshop, 1997, pp. 156 to 168, the restriction of loop conditions that closes the termination channel

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example