test: run mutation testing on the security core and catch what it missed

Merged
KI
25d ago
+777-28

Problem

Each threat in the threat model has a test, and since M10 those claims are also checked against generated cases. Nothing measures whether the tests would notice a bug in the code that enforces them. This is the third item of M10 in the roadmap: mutation testing of lib.rs, containment.rs, broker.rs, broker_sys.rs, and fsgate-broker with confine.rs, on Linux and macOS.

Change

  • .cargo/mutants.toml points cargo-mutants 27.1.0 at those six files.
    • It runs cargo test --all-targets, which leaves out the doctests. They only build the documentation's examples, and rebuilding them doubled the time each mutant took. Leaving out tests can only turn a caught mutant into a missed one, never the reverse.
  • .github/workflows/mutants.yml runs every mutant on ubuntu-latest (with experimental-intercept) and on macos-latest.
    • It runs on demand, and on a pull request that changes its own setup.
    • Each job compares its missed mutants, without line numbers, with .github/mutants/missed-<os>.txt, and fails if they differ. A new miss needs a test or a reason in the list. A listed mutant the tests now catch leaves the list.
    • A mutant that times out counts as caught, as cargo-mutants counts it.
    • cargo-mutants does not read cfg, so a mutant in code a platform does not build passes every test there. Each job leaves out the ones it can name without a line number, and the list names the rest.
  • The lists give the reason each listed mutant cannot change behavior on that platform: 6 on macOS and 29 on Linux.
  • New tests for what the runs missed.
    • Confinement:
      • A plain lock() applies every restriction the kernel has.
      • --direct <dir>:l lets the worker list the directory and not read it.
      • A worker named without a path is found on PATH, past an entry where it is missing and one where it is not executable.
      • Only a missing runtime path is skipped; one that cannot be opened for another reason stops the worker.
    • The rights and restrictions as sets: a union with overlap, set difference, is_empty, and the names Debug prints, which the RightsExceeded message uses.
    • A plain EACCES keeps its errno; only an escape becomes EXDEV.
    • The broker:
      • At its 1 MiB limit, a request and a response of exactly that size are sent and served, and a frame one byte longer than its prefix fails the connection.
      • A hang-up between requests ends serve_conn with Ok.
      • A relative path comes back as NotAbsolute, and a failed connect names its remedy.
      • serve returns when its listener fails, and choosing peers keeps the connection limit.
      • FSGATE_BROKER_FD=2 is refused.
    • The interception mode refuses --unconfined, --direct, and --env, each on its own, and its plan names WRITE and CREATE as unused, each on its own.
  • Test fixes.
    • The serve tests' raw client now hangs up its side after writing. A broker that wrongly answers then fails the test instead of leaving it waiting.
    • The confined worker's stray-descriptor test planted one stray, above the worker's connection. It now plants one on each side, at 3 and at 900, so leaving the descriptors below the connection open fails it.
  • The roadmap ticks the item with the run below.

What it found

The first macOS run, before any new test, missed 26 of 267 mutants. The first Linux run, with the tests for those added, missed 28 of 353. Two of the misses let a lock or a worker hold more than asked for:

  • A plain lock() restricting nothing beyond files. That happens if LockOptions::requested combines what was asked for with & instead of |. The restriction tests each require the restriction they check, which applies it whatever is requested, so none noticed.
  • --direct <dir>:l giving execute instead of list. That happens without its match arm.

The rest change no grant or rule. They are:

  • operators and Debug output of the sets;
  • off-by-one changes at the frame limit;
  • error remedies and errno labels;
  • option builders that drop earlier settings;
  • serve not returning;
  • how the worker is found on PATH.

The listed ones are:

  • code the platform does not build;
  • ^ for | on distinct flags;
  • checks that only a kernel below Landlock ABI v3 reaches, which the kernels job's boot without Landlock covers;
  • a descriptor number the broker never uses.

It also found a flaky coverage check in the model test, fixed in #79. That flake once made a missed mutant look caught in a local run.

After the worker's supervisor and socket filter

#75 and #76 merged while this was open, and the Linux run grew to 420 mutants. The new misses:

  • The stray-descriptor test checked one side. With > changed to == in close_others_at_exec, every descriptor below the worker's connection stayed open and the test still passed. The test now catches it (above).
  • Equivalent in the socket filter. BPF_LD and BPF_W are both 0, so ^ for either | in load, and & between the two, build the same instruction. The same goes for ^ on the distinct flags in answer, jump, PROGRAM, and the supervisor's wait options.
  • refuses_unix_connects returning false. Below Linux 7.1, as on the runner, the answer is already false. On 7.1 and later the mutant installs the filter where Landlock already refuses the same connects, which the kernels job's 7.3 boot tests.
  • The filter's install for architectures with no filter, which x86_64 does not build.
  • The stub confine module in fsgate-broker.rs, which only systems other than Linux build. #75 added Supervisor::start and Running::finish to it, so the Linux exclude now leaves out everything under confine:: in that file, not just confine::command.

One run hit a flaky test: a_program_a_confined_worker_leaves_running_ends_with_it (from #75) reported "the program never ran" under an equivalent mutant, which made it look caught and failed the comparison. The worker prints its pids and exits at once, and the broker then ends the program's group, so the test's is_running check can come too late. It is not fixed here.

Verification

CI on 5373307, after merging main with #75, #76, and #77, both mutation jobs passed with the misses matching the lists:

PlatformMutantsCaughtTimed outMissedDid not buildTime
The runner's Linux 7.0 (Landlock ABI v8), with experimental-intercept42028182910216 min
macOS 26 (Darwin 25.6, arm64)278196667010 min

Every timeout leaves a client waiting for an answer that never comes, or a worker that should have been confined still running. The other 11 checks passed on 5373307, including all four kernels and arm64. The last commit only updates the roadmap.

Locally, on macOS 26.2 with Rust 1.99.1:

  • cargo fmt --all --check and cargo clippy --all-targets -- -D warnings: clean.
  • cargo clippy --target x86_64-unknown-linux-gnu -p fsgate --all-targets -- -D warnings, with and without --features experimental-intercept, and cargo +1.85.0 check for both targets: clean. These are type-checks, not runs.
  • cargo test --locked: 121 passed. The Linux-only tests ran only in CI.
  • actionlint 1.7.12 (with shellcheck) and zizmor 1.32.0 on the workflow: no findings.

Not verified:

  • Timeouts. A mutant that only times out is counted as caught.
  • New misses. The workflow does not run when the code changes, only when its setup does, or on demand, so a later change can add a miss that the next run reports.
  • The descriptor-number reason. It rests on std reopening closed standard streams at startup, which I did not test.
  • The kernels job. It runs the regular tests, not mutants, so the reasons that name it are about its tests, not a mutation run there.

Documentation and release

The roadmap item is ticked with the run above. The workflow, the config, and the lists explain themselves. Test and CI only, so no changelog entry and no release.