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

Tutorial: a Data Diode

This tutorial walks through demo/datadiode/, a complete Fiducia project: a network data diode on seL4 Microkit. Ethernet frames enter on an untrusted (Low) network, cross one forwarding protection domain and leave on a trusted (High) network, and nothing travels back. You read the project file by file, in the order the concepts are needed, then build it, boot it in QEMU and drive it with real traffic.

host ──virtio──▶ eth_outer ──o2d──▶ dd ──d2i──▶ eth_inner ──virtio──▶ host
 (untrusted)      (Low NIC)      (diode)       (High NIC)      (trusted)

You need the tools from Installation, including its prerequisites for building images, QEMU and Python 3. The syntax of each construct is in the Fiducia Language Reference; this page links to it rather than repeat it.

1. The project

demo/datadiode/
├── fire.toml        the manifest
├── datadiode.fis    the system: regions, PDs, channels
├── eth_outer.fi     the Low NIC's PD
├── dd.fi            the diode PD
├── eth_inner.fi     the High NIC's PD
├── include/         the C headers: eth.h, which the .fi files import, and
│                    the util.h and virtio_net.h it builds on
├── src/             the C sources: eth.c, util.c, virtio_net.c
├── spec/            the queries fire verify checks
└── inject.py        the host side: sends frames, checks what comes out

Each protection domain (PD) declared in the .fis has a .fi of the same name: dd.fi implements the PD dd. A .fi imports the C headers it calls; each include/x.h is linked with its src/x.c. The project’s other C goes into one archive, from which each PD links what its C calls: eth.c calls util.c and virtio_net.c, which no .fi imports.

fire.toml names the project and says how to run it. Its [run] section is the QEMU command line: an AArch64 virt board and two virtio-net NICs on UDP sockets, which inject.py talks to. The outer NIC must be the first -device. See The Project Manifest for every field.

2. The system description

datadiode.fis describes the whole system: what each PD is, what memory exists, and how the PDs are connected. It holds no behaviour.

A struct for the frame

struct Frame {
    len: uint16;
    origin: uint8;
    data: uint8[2048];
}

const ORIGIN_OUTER: uint8 = 1;
const ORIGIN_INNER: uint8 = 2;

Frame is the value that crosses the diode. origin is the network the frame came in on, one of the two constants; the model tracks it, which is what Verify states secrecy over. A struct or constant declared in the .fis is shared by every PD and by the C code: the build writes it to include/gen/types.h, which include/eth.h includes, so C sees the same struct Frame. See Data Layer.

Memory regions

Low MemoryRegion eth_outer_output(0x200000, 0x200000);
High MemoryRegion eth_inner_input(0x200000, 0x200000);

MemoryRegion virtio_window(0x1000, 0x1000, 0xa003000);

Low MemoryRegion outer_dma(0x200000, 0x200000);
High MemoryRegion inner_dma(0x200000, 0x200000);

A region is a size, a page size and, for a device, a physical address. virtio_window is the page of QEMU’s virtio-mmio devices where both NICs sit, outer_dma and inner_dma hold each NIC’s virtio rings and buffers, and the first two back the channels below.

Each region carries a security label, Low or High. fic checks one rule over labels: a mapping of a region is labelled at least as high as the region, so High memory is never mapped into a Low context. See Information Flow. This check, like the well-formedness checks, is a placeholder: both are still under research.

Protection domains

ProtectionDomain eth_outer(99, 79);
ProtectionDomain dd(150);
ProtectionDomain eth_inner(199, 78);

A PD is a priority and, optionally, the IRQ it handles. Each NIC’s PD takes its device’s interrupt; dd touches no device.

Channels and the buffered queue

Chan o2d = eth_outer --> dd via eth_outer_output<Frame>;
Chan d2i = dd --> eth_inner via eth_inner_input<Frame>;

System = eth_outer ||| dd ||| eth_inner;

A channel connects two PDs. --> makes it one-way: eth_outer sends on o2d and dd receives, never the reverse, and no channel runs from eth_inner back towards eth_outer. That declared topology is what makes the diode one-way.

via eth_outer_output<Frame> makes o2d a buffered channel: it carries Frame values, queued in the region eth_outer_output, which is mapped into both of its PDs. A channel without via carries no value: it is a bare Microkit notification.

The queue is a ring, Lamport’s single-producer single-consumer FIFO:

  • Capacity from the region. A 16-byte header holds the two indices and a signal word, then the slots, packed. o2d’s 2 MiB region of 2052-byte Frames holds ⌊(0x200000 − 16) / 2052⌋ = 1021 frames.
  • First in, first out. The receiver takes frames in the order they were sent.
  • A send never waits. A send copies the value into the next slot and notifies the receiver if the receiver asked. If the ring is full, the value is lost and the sender goes on.
  • A receive waits while the ring is empty. When the receiver is notified, it receives until the ring is empty, so several sends behind one notification are all received. Finding the ring empty, it asks to be notified of the next send; until then, sends notify no one.

A diode wants exactly this. The Low side must never wait on the High side, and with a lossy ring it cannot: when eth_inner falls behind, d2i fills and dd drops frames, and eth_outer and dd carry on. The queue holds up to 1021 frames in flight on each hop, so a burst from the network does not drop frames as long as the PDs downstream keep up.

System lists the PDs, interleaved. The Channel Declarations reference has the precise rules.

3. The diode: dd.fi

dd is the whole diode, and the smallest PD:

@bind(microkit.init)
proc Init() {
    ready -> Skip;
}

@bind(microkit.notified)
proc Notified() {
    o2d?f -> forward -> d2i!f -> Notified();
}

evt ready: dbg("diode transport ready");

evt forward: dbg("pass o2d -> d2i");

A PD’s behaviour is a set of processes. Microkit calls two entry points on every PD, and @bind says which process each runs: microkit.init at boot, microkit.notified whenever the PD is notified.

Notified receives a frame from o2d into f (o2d?f), performs the event forward, sends f on d2i (d2i!f), and starts again. While o2d is empty, o2d?f waits: the PD returns to the kernel until eth_outer sends.

An evt is a named step of computation. Here both events only print with dbg, the lines you see on the serial console.

4. The Low NIC: eth_outer.fi

eth_outer drives a real NIC through C: imports, mappings, state, events, then processes.

import "include/eth.h" as eth;

Low Mapping vwin(virtio_window, 0x2_000_000, "rw", false);
Low Mapping dma(outer_dma, 0x2_400_000, "rw", false);

An import makes a header’s functions callable as eth.eth_poll(). A Mapping puts a region into this PD’s address space, here at fixed virtual addresses, read-write and uncached, as device memory must be. A mapping’s addresses are readable in the program: dma.paddr below is the physical address of outer_dma.

The PD’s state is a few variables, among them the frame to send:

rx: bool;
kind: int;
replied: bool;
reply_len: uint16;
frame: Frame;
nic: int;

Events call into C and update that state:

evt setup: nic = eth.eth_setup(1, dma.paddr);

evt handle_irq: eth.eth_clear_irq();

evt poll: {
    rx = eth.eth_poll();
    kind = eth.eth_pkt_kind();
}

evt stage_forward: {
    frame = eth.eth_frame(ORIGIN_OUTER);
    dbg("forward -> o2d");
}

evt send_frame.|len: uint16|: {
    eth.eth_send_frame(len);
    dbg("Packet sent to network len=", len);
}

eth_frame returns a struct Frame, the type the .fis declared, with its origin set to the argument: every frame eth_outer sends is tagged with the outer network. send_frame carries a value: send_frame.reply_len performs it with len bound to reply_len.

At boot, Init brings up the NIC and reports whether it came up, with a guarded choice: [g] P [] [h] Q runs the branch whose guard holds.

@bind(microkit.init)
proc Init() {
    setup -> ReportNic();
}

proc ReportNic() {
        [nic >= 0] nic_up -> Skip
     [] [nic < 0] nic_failed -> Skip;
}

On each interrupt, Notified handles it and starts again:

@bind(microkit.notified)
proc Notified() {
    irq?79 -> HandleRx();
    Notified();
}

proc HandleRx() {
    handle_irq -> Skip;
    Drain();
    irq!79 -> Skip;
}

proc Drain() {
    poll -> Skip;
        [rx && kind == 1] stage_forward -> o2d!frame -> Drain()
     [] [rx && (kind == 2 || kind == 3)] handle_ping -> Reply()
     [] [rx && kind == 0] Drain()
     default Skip;
}

proc Reply() {
        [replied] send_frame.reply_len -> Drain()
     [] [!replied] Drain();
}

irq?79 waits for the NIC’s interrupt and irq!79 acknowledges it; P; Q runs P, then Q. Drain reads frames until there are none: a frame for another host is copied into frame and sent on o2d, an ARP request or ping for this PD is answered on the outer network, anything else is dropped, and default ends the loop once no guard holds. Reply sends only if handle_ping built a reply: a request shorter than its headers state gets none. The send o2d!frame never waits, so a slow dd never stalls the outer NIC.

5. The High NIC: eth_inner.fi

eth_inner starts like eth_outer, with High mappings of inner_dma. Its Notified waits for either of two things:

@bind(microkit.notified)
proc Notified() {
        irq?78 -> DrainNic()
     [] d2i?f -> Deliver(f);
    Notified();
}

proc Deliver(f: Frame) {
    send_frame.f -> Skip;
}

[] without guards is an external choice: the branch whose first event happens runs. A frame on d2i is transmitted on the inner network; the NIC’s own interrupt is acknowledged, and what the inner network sends is dropped. No branch sends anything towards dd.

6. Build

$ fire build demo/datadiode/
fire: building datadiode for qemu_virt_aarch64 (debug)
  ✓ Wrote the data-layer headers to demo/datadiode/include/gen
  ✓ Parsed 1 system + 3 program files + 1 foreign module(s)
  ✓ Resolved names
  ...
  ✓ Type-checked information flow
  ✓ Checked channel dependency acyclicity (WF2)
  ✓ Built the graphs and checked them (notified: receive-headed, receives at the head only; notified and protected: guarded choices)
  ✓ Generated 3 C file(s) in demo/datadiode/build/gen
MICROKIT|CAPDL SPEC: number of root objects = 90, spec footprint = 13.32 KiB
MICROKIT|INITIAL TASK: memory size = 440 KiB
MICROKIT|LOADER: image file size = 2.69 MiB
  ✓ image: demo/datadiode/build/datadiode.img
  ...
  ✓ UPPAAL model written to demo/datadiode/build/datadiode.xta

fire build checks the project, generates C for each PD, compiles it with the imported C and builds the Microkit image; it also writes the UPPAAL model. It warns about events such as forward that only print: the model leaves dbg out, so there they do nothing. To check the project without building, run fic demo/datadiode/ --check (see the fic reference).

7. Run

Boot the image in one terminal:

$ fire run demo/datadiode/
  ✓ image up to date: demo/datadiode/build/datadiode.img
fire: launching qemu-system-aarch64
...
MON|INFO: Microkit Monitor started!
eth_inner: init
eth_inner: virtio-net up on slot 6
dd: init
dd: diode transport ready
eth_outer: init
eth_outer: virtio-net up on slot 7

and send traffic from another:

$ python3 demo/datadiode/inject.py
forward: injected 4 on outer, received 4 on inner, 4 payload-identical
ping: reply on outer
arp: reply on outer
diode: no leak (one-way upheld)
RESULT: PASS

inject.py sends four frames for a foreign host into the outer NIC and checks that they come out of the inner NIC unchanged, pings eth_outer and sends it an ARP request, and sends a frame into the inner NIC to check that it never reaches the outer one. The console shows each frame cross:

eth_outer: forward -> o2d
dd: pass o2d -> d2i
eth_inner: delivered
...
eth_outer: Packet sent to network len=58
eth_outer: Packet sent to network len=42

Exit QEMU with Ctrl-A x.

8. Verify

fire verify does two things: it proves the contracts on the project’s C with Frama-C, then checks the UPPAAL model fire build wrote against the queries in spec/.

The contracts

The model does not look inside C. What it knows of a call is the function’s ACSL contract, which for the diode is in include/eth.h:

/*@ ensures \result == 0 || \result == 1; */
bool eth_poll(void);

/*@ ensures 0 <= \result <= 3; */
int64_t eth_pkt_kind(void);

/*@ ensures \result.origin == origin; */
struct Frame eth_frame(uint8_t origin);

/*@ ensures \result == 0 || \result == 1; */
bool eth_make_reply(void);

The first two are the NIC’s input as the model sees it: on each poll a frame may or may not have arrived, of any of the four kinds, so the model explores every branch of Drain. The third says a frame carries the origin it was given, and the fourth that a request may or may not get a reply, so the model explores both branches of Reply. Frama-C’s WP proves all four on src/eth.c.

eth.c calls helpers in util.c, whose contracts in include/util.h say what each reads and writes; WP proves those on src/util.c, and that neither file’s proved functions have a runtime error. So a frame’s bytes stay inside its buffer, whatever length the device, the frame itself or the other side of the diode gives. That holds on the forward path (eth_poll, eth_frame and eth_send_frame_value) and in eth_make_reply, which answers ARP and ping. The loops need an SMT prover, Z3: fire.toml names it, and why3 must know where it is (why3 config detect; the Nix shell is set up).

One thing is assumed. fire.toml lists it under assume, and the report names it: the virtio_net.c driver, which reaches the device in ways WP does not model. eth.c’s proof uses the driver’s contracts in include/virtio_net.h as given.

eth.c copies a frame with fi_memcpy_u8, one of the memory routines fic links into every PD. fic generates their contracts in include/gen/mem.h and proves them itself, so the report lists them as EXTERNAL, not ASSUMED.

The queries

spec/datadiode.q holds seven:

A[] not deadlock

E<> dd.L2
E<> eth_inner.Deliver

A[] not (!dd.awake && q_o2d.length > 0 && !pend_o2d)
A[] not (!eth_inner.awake && q_d2i.length > 0 && !pend_d2i)

A[] not (eth_outer.frame.origin == ORIGIN_INNER || dd.f.origin == ORIGIN_INNER || (exists (i : int[0, 2]) q_o2d_val[i].origin == ORIGIN_INNER))

E<> dd.f.origin == ORIGIN_OUTER
  • No deadlock. The environment raises each NIC’s interrupt for ever, as inject.py keeps sending, so a deadlock would be a driver that never takes or never acknowledges its interrupt.
  • A frame crosses each hop. dd can reach the location after its receive on o2d, and eth_inner can reach Deliver.
  • No lost wake-up. A receiver that is waiting while its queue holds a value always has a notification pending. In the model a queue holds at most 3 values.
  • Secrecy. No Low PD, and no slot of o2d, ever holds a frame that came in on the inner network.
  • The origin travels. dd does receive a frame tagged with the outer network, so the secrecy query is not true for want of frames.
$ fire verify demo/datadiode/
fire: verifying datadiode
  ✓ Running frama-c: /usr/bin/frama-c

  ACSL: src/eth.c                                          PASS
  ACSL: src/util.c                                         PASS
  ACSL: src/virtio_net.c, 9 functions not proved           ASSUMED
      virtio_net_up, virtio_net_rx, virtio_net_tx, virtio_net_ack, ...
  ACSL: include/gen/mem.h, 6 functions proved by fic       EXTERNAL
      bcmp, fi_memcpy_u8, memcmp, memcpy, memmove, memset
  ✓ Running verifyta: /opt/uppaal/bin/verifyta
  ✓ Query file: demo/datadiode/spec/datadiode.q

  Q1  A[] not deadlock                                     PASS
  Q2  E<> dd.L2                                            PASS
  Q3  E<> eth_inner.Deliver                                PASS
  ...
  Q7  E<> dd.f.origin == ORIGIN_OUTER                      PASS

Verifying: fire verify has the options, and how a contract becomes the model’s view of a call.

Where to go next