Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Tests

- The lifecycle VM's capture reader (`scripts/vm/lifecycle/egress.py`) no longer
reports a capture it cannot vouch for as clean. An empty or cut-short pcap
used to read as "0 probe packets", which `run.sh` passed as contained; it is
now inconclusive, as is a non-Ethernet file, a frame snapped below its
addresses and any layout the reader does not parse (a VLAN tag, an unknown
EtherType). Every scenario must also show the guest's own SSH traffic in the
same window, so a window of replies only, or of nothing, fails instead of
passing. Non-first IPv4 fragments no longer have a port read from their
payload, and IPv6 extension headers are walked to the transport header
(O02 of the leak-testing plan).

- The zero-leak suite builds every probe on NSE 2.4.0's stimuli instead of
scripts it wrote itself; the `nse` extra now requires
`network-sandbox-engine>=2.4.0`. Each probe carries a probe ID, so the
Expand Down
2 changes: 1 addition & 1 deletion docs/security/leak-testing-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ runtime leak blocks the affected release regardless of its original priority.
| ID / priority | Extend | Experiment | Required evidence / calibration |
| :--- | :--- | :--- | :--- |
| O01 / P0 | DNS oracle and unit tests | Deliver a direct DNS request to a receiver that drops replies or returns error rcodes; add unrelated concurrent redirected DNS | External packet still means leak; timeout/error/counter alone stays inconclusive; old inference fails the regression |
| O02 / P0 | Capture readers and classifier tests | Empty, reply-only, truncated and corrupt capture; wrong NIC; absent stimulus; hidden IP family; unsupported Ethernet/IPv6 layout | No false clean verdict. Distinguish intentional snap length from missing required bytes; correct stimulus is detected in the control |
| O02 / P0 | Capture readers and classifier tests (lifecycle reader: [tests](../../tests/test_lifecycle_egress.py)) | Empty, reply-only, truncated and corrupt capture; wrong NIC; absent stimulus; hidden IP family; unsupported Ethernet/IPv6 layout | No false clean verdict. Distinguish intentional snap length from missing required bytes; correct stimulus is detected in the control |
| D01 / P1 | NSE DNS + controlled integration | UDP/TCP 53 to public and LAN resolvers, port rewrites and competing DNAT priorities, IPv4/IPv6 | Unique query/flow observed externally when unprotected; blocked or attributed to Tor when protected; mutate the relevant safeguard |
| D02 / P1 | DNS overlay / resolved integration | After managed Tor has bootstrapped, disable non-Tor resolver paths and clear caches; request fresh names through libc, direct DNS and resolved IPC | Permitted resolution still works through Tor, or documented failure is contained; restored alternative path is a calibrated leak control |
| D03 / P1 | DNS functional tests | A/AAAA/PTR and unsupported TXT/MX/DNSKEY; UDP/TCP requests; explicit per-link DNSSEC policy | Separate supported answers from safe refusal and connectivity loss; verify DNSSEC preflight refusal happens before network changes |
Expand Down
6 changes: 6 additions & 0 deletions docs/web/how-to/vm-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ the distribution's published checksum list; the list's signature is not checked)
installs TTP from the working tree, and writes `results.txt`, one capture per phase
and state snapshots into `$VM_WORK`. `scripts/vm/lifecycle/egress.py <file.pcap>`
summarises a capture by destination; it streams the file, so size is not a concern.
A capture it cannot vouch for is never summarised as clean: an empty, cut-short or
non-Ethernet file, a frame snapped below its addresses, or a layout it does not parse
(a VLAN tag, an unknown EtherType) prints `INCONCLUSIVE: <reason>` and exits 2. Each
scenario also requires the guest's own SSH traffic in the same window
(`--require-control`), so a window that saw only replies, or nothing, fails as
inconclusive instead of passing as contained.
Transient download errors receive up to three retries. Downloads remain bounded
by connection/transfer timeouts; checksum mismatches still fail the run.
Guest readiness requires successful `cloud-init` completion. A timeout or error
Expand Down
11 changes: 11 additions & 0 deletions docs/web/release-notes/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Tests

- The lifecycle VM's capture reader (`scripts/vm/lifecycle/egress.py`) no longer
reports a capture it cannot vouch for as clean. An empty or cut-short pcap
used to read as "0 probe packets", which `run.sh` passed as contained; it is
now inconclusive, as is a non-Ethernet file, a frame snapped below its
addresses and any layout the reader does not parse (a VLAN tag, an unknown
EtherType). Every scenario must also show the guest's own SSH traffic in the
same window, so a window of replies only, or of nothing, fails instead of
passing. Non-first IPv4 fragments no longer have a port read from their
payload, and IPv6 extension headers are walked to the transport header
(O02 of the leak-testing plan).

- The zero-leak suite builds every probe on NSE 2.4.0's stimuli instead of
scripts it wrote itself; the `nse` extra now requires
`network-sandbox-engine>=2.4.0`. Each probe carries a probe ID, so the
Expand Down
198 changes: 164 additions & 34 deletions scripts/vm/lifecycle/egress.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@
whole capture with scapy's rdpcap and exhausted the host's memory; nothing here
may hold more than one packet at once.

Usage: egress.py <file.pcap> [--net 10.0.2.0/24] [--hits ADDR]
Usage: egress.py <file.pcap> [--net 10.0.2.0/24] [--hits ADDR] [--require-control]
Prints one line per (destination, protocol, port) with a packet count, then a
total. The SSH control channel (guest tcp/22 to the QEMU gateway) is excluded.
total. The SSH control channel (guest tcp/22 to the QEMU gateway) is counted
separately, not as egress.

A capture that cannot support a verdict is never summarised as clean. The
script prints ``INCONCLUSIVE: <reason>`` on stderr and exits 2 when:

* the file is empty, its header is short or corrupt, or it is not Ethernet;
* a record or its frame is cut short, or its lengths are impossible;
* a frame was snapped (QEMU's ``maxlen``) below the bytes that name its
destination, or is too short or malformed to read them;
* a frame uses a layout this reader does not parse (a VLAN tag, an unknown
EtherType): a probe could be hiding in it;
* with ``--require-control``, no guest-originated SSH control packet is in the
capture, so it is not known to see the guest's egress at all (an empty
window, a capture of replies only, the wrong NIC).

Snapping above those bytes is intentional (``vm_capture_start`` keeps 128
bytes per frame) and is not a reason: the destination address is all a
verdict needs. A port that lies beyond the snap, or in a non-first fragment,
is reported as ``-``.

There is deliberately no time filter: QEMU stamps records with its own clock,
which was an hour off the host's in practice, so phases are separated by
Expand All @@ -24,12 +43,27 @@
import struct
import sys
from collections.abc import Iterator
from dataclasses import dataclass

# QEMU user networking numbers a subnet the same way every time: the gateway is
# .2, its DNS forwarder .3 and the guest's lease .15. --net selects the subnet.
GATEWAY = "10.0.2.2"
GUEST_V4 = "10.0.2.15"

LINKTYPE_ETHERNET = 1
ETHERTYPE_IPV4, ETHERTYPE_IPV6, ETHERTYPE_ARP = 0x0800, 0x86DD, 0x0806

#: IPv6 extension headers walked to reach the transport header. A Fragment
#: header (44) is walked too; Authentication (51) counts its length in 4-byte
#: units rather than 8.
_IPV6_EXTENSIONS = {0, 43, 44, 51, 60}

_MAGIC = {0xA1B2C3D4: ("<", False), 0xD4C3B2A1: (">", False), 0xA1B23C4D: ("<", True), 0x4D3CB2A1: (">", True)}


class Inconclusive(Exception):
"""The capture cannot support a verdict; the message says why."""


def use_subnet(cidr: str) -> None:
global GATEWAY, GUEST_V4
Expand All @@ -50,68 +84,164 @@ def sent_by_guest(src: str) -> bool:


def records(path: str) -> Iterator[tuple[float, bytes]]:
with open(path, "rb") as f:
"""Yield (timestamp, frame) for every record; raise Inconclusive on any damage."""
try:
f = open(path, "rb") # noqa: SIM115 - closed below, the generator owns it
except OSError as exc:
raise Inconclusive(f"{path}: cannot be read ({exc.strerror})") from None
with f:
header = f.read(24)
if len(header) < 24:
return
raise Inconclusive(
f"{path}: {'empty' if not header else 'truncated'} file header ({len(header)} of 24 bytes)"
)
magic = struct.unpack("<I", header[:4])[0]
endian = {0xA1B2C3D4: "<", 0xD4C3B2A1: ">", 0xA1B23C4D: "<", 0x4D3CB2A1: ">"}.get(magic)
if endian is None:
raise SystemExit(f"{path}: not a pcap file (magic {magic:#x})")
nano = magic in (0xA1B23C4D, 0x4D3CB2A1)
if magic not in _MAGIC:
raise Inconclusive(f"{path}: not a pcap file (magic {magic:#x})")
endian, nano = _MAGIC[magic]
major, minor, _zone, _sigfigs, snaplen, linktype = struct.unpack(endian + "HHiIII", header[4:])
if (major, minor) != (2, 4):
raise Inconclusive(f"{path}: unsupported pcap version {major}.{minor}")
if linktype != LINKTYPE_ETHERNET:
raise Inconclusive(f"{path}: link type {linktype}, not Ethernet")
index = 0
while True:
rec = f.read(16)
if len(rec) < 16:
if not rec:
return
sec, frac, incl, _orig = struct.unpack(endian + "IIII", rec)
yield sec + frac / (1e9 if nano else 1e6), f.read(incl)
index += 1
if len(rec) < 16:
raise Inconclusive(f"{path}: record {index} header cut short ({len(rec)} of 16 bytes)")
sec, frac, incl, orig = struct.unpack(endian + "IIII", rec)
if incl > orig or (snaplen and incl > snaplen):
raise Inconclusive(f"{path}: record {index} claims {incl} bytes of {orig} (snap length {snaplen})")
frame = f.read(incl)
if len(frame) < incl:
raise Inconclusive(f"{path}: record {index} frame cut short ({len(frame)} of {incl} bytes)")
yield sec + frac / (1e9 if nano else 1e6), frame


@dataclass(frozen=True)
class Packet:
src: str
dst: str
proto: str
sport: int | None
dport: int | None


_NAMES = {6: "tcp", 17: "udp", 1: "icmp", 58: "icmpv6"}


def _ports(proto: int, l4: bytes) -> tuple[int | None, int | None]:
if proto in (6, 17) and len(l4) >= 4:
sport, dport = struct.unpack("!HH", l4[:4])
return sport, dport
return None, None


def parse(frame: bytes) -> tuple[str, str, str, int | None] | None:
"""(src, dst, proto, dport) for an IPv4/IPv6 frame, else None."""
def parse(frame: bytes) -> Packet | None:
"""The addresses, protocol and ports of an IP frame; None for ARP.

Raises Inconclusive for anything that could carry an IP packet this reader
cannot read: a frame too short for its destination, a malformed header, a
VLAN tag or an EtherType it does not know.
"""
if len(frame) < 14:
return None
raise Inconclusive(f"a {len(frame)}-byte frame has no complete Ethernet header")
ethertype = struct.unpack("!H", frame[12:14])[0]
ip = frame[14:]
if ethertype == 0x0800 and len(ip) >= 20:
if ethertype == ETHERTYPE_ARP:
return None
if ethertype == ETHERTYPE_IPV4:
if len(ip) < 20:
raise Inconclusive(f"an IPv4 frame was captured with {len(ip)} of the 20 header bytes")
ihl = (ip[0] & 0x0F) * 4
proto, src, dst, l4 = ip[9], ip[12:16], ip[16:20], ip[ihl:]
elif ethertype == 0x86DD and len(ip) >= 40:
proto, src, dst, l4 = ip[6], ip[8:24], ip[24:40], ip[40:]
if ip[0] >> 4 != 4 or ihl < 20:
raise Inconclusive(f"a malformed IPv4 header (version {ip[0] >> 4}, header length {ihl})")
proto, src, dst = ip[9], ip[12:16], ip[16:20]
first_fragment = struct.unpack("!H", ip[6:8])[0] & 0x1FFF == 0
sport, dport = _ports(proto, ip[ihl:]) if first_fragment else (None, None)
elif ethertype == ETHERTYPE_IPV6:
if len(ip) < 40:
raise Inconclusive(f"an IPv6 frame was captured with {len(ip)} of the 40 header bytes")
proto, src, dst = ip[6], ip[8:24], ip[24:40]
offset, first_fragment = 40, True
while proto in _IPV6_EXTENSIONS and len(ip) >= offset + 8:
ext = ip[offset : offset + 8]
if proto == 44:
first_fragment = first_fragment and struct.unpack("!H", ext[2:4])[0] >> 3 == 0
length = 8
else:
length = (ext[1] + 2) * 4 if proto == 51 else (ext[1] + 1) * 8
proto, offset = ext[0], offset + length
# An extension chain cut by the snap length leaves the transport unknown;
# the destination, which is all a verdict needs, is already read.
if proto in _IPV6_EXTENSIONS or not first_fragment:
sport, dport = None, None
else:
sport, dport = _ports(proto, ip[offset:])
else:
return None
name = {6: "tcp", 17: "udp", 1: "icmp", 58: "icmpv6"}.get(proto, f"proto{proto}")
dport = struct.unpack("!H", l4[2:4])[0] if proto in (6, 17) and len(l4) >= 4 else None
sport = struct.unpack("!H", l4[0:2])[0] if proto in (6, 17) and len(l4) >= 4 else None
s, d = str(ipaddress.ip_address(src)), str(ipaddress.ip_address(dst))
if name == "tcp" and sport == 22 and d == GATEWAY:
return None # our own SSH control channel
return s, d, name, dport
kind = "a VLAN-tagged frame" if ethertype in (0x8100, 0x88A8) else f"EtherType {ethertype:#06x}"
raise Inconclusive(f"{kind} is not parsed by this reader and could carry a probe")
name = _NAMES.get(proto, f"proto{proto}")
return Packet(str(ipaddress.ip_address(src)), str(ipaddress.ip_address(dst)), name, sport, dport)


def is_control(packet: Packet) -> bool:
"""The guest's side of our own SSH control channel."""
return packet.proto == "tcp" and packet.sport == 22 and packet.dst == GATEWAY


def summarise(path: str) -> tuple[collections.Counter[tuple[str, str, int | None]], int]:
"""Guest-originated flows and the number of guest SSH control packets."""
flows: collections.Counter[tuple[str, str, int | None]] = collections.Counter()
control = 0
for index, (_ts, frame) in enumerate(records(path), start=1):
try:
packet = parse(frame)
except Inconclusive as exc:
raise Inconclusive(f"{path}: record {index}: {exc}") from None
if packet is None or not sent_by_guest(packet.src):
continue
if is_control(packet):
control += 1
continue
flows[(packet.dst, packet.proto, packet.dport)] += 1
return flows, control


def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("pcap")
ap.add_argument("--net", default="10.0.2.0/24", help="the QEMU user-network subnet of this capture")
ap.add_argument("--hits", metavar="ADDR", help="print only how many guest packets went to ADDR")
ap.add_argument(
"--require-control",
action="store_true",
help="inconclusive unless the guest's SSH control channel appears in the capture",
)
args = ap.parse_args()
use_subnet(args.net)

flows: collections.Counter[tuple[str, str, int | None]] = collections.Counter()
for _ts, frame in records(args.pcap):
parsed = parse(frame)
if parsed is None or not sent_by_guest(parsed[0]):
continue
_src, dst, proto, dport = parsed
flows[(dst, proto, dport)] += 1
try:
flows, control = summarise(args.pcap)
if args.require_control and not control:
raise Inconclusive(
f"{args.pcap}: no guest-originated SSH control packet, so the capture is not "
f"known to see the guest's egress (empty window, replies only, or the wrong NIC)"
)
except Inconclusive as exc:
print(f"INCONCLUSIVE: {exc}", file=sys.stderr)
return 2

if args.hits:
print(sum(n for (dst, _proto, _dport), n in flows.items() if dst == args.hits))
return 0

for (dst, proto, dport), n in sorted(flows.items(), key=lambda kv: -kv[1]):
print(f"{n:6} {proto:7} {dst}:{dport if dport is not None else '-'}")
print(f"total {sum(flows.values())} guest-originated packet(s)")
print(f"total {sum(flows.values())} guest-originated packet(s), {control} SSH control packet(s)")
return 0


Expand Down
Loading