Integrity monitoring for kernel isolation structures
README.md

fdir #

Integrity monitoring for kernel isolation structures.

A Linux process is confined by state that lives in the kernel. Its seccomp filter is a small BPF program that the kernel runs on each of the process's system calls to allow, deny or kill it. Its capability sets are bit masks of the root privileges it holds, such as binding a low port or loading a module. Its cgroups (control groups) place it in the kernel's hierarchy of process groups, with limits on memory, CPU and process count. In orbit, a charged particle can flip a bit in memory (a single-event upset) and corrupt any of this, including the memory mappings, without the process noticing.

fdir records a known-good snapshot of that state at boot and compares the live state with it on a timer you configure. The name is the spacecraft term for fault detection, isolation and recovery (FDIR).

The library reads /proc/self/maps, /proc/self/status and /proc/self/cgroup and hashes each one with SHA-256. An Eio daemon fibre re-reads them periodically and compares the hashes with the baseline. A handler then maps the anomalies to a severity (Log, Isolate, Restart, Degrade or Safe_mode), and the default one looks at how many subsystems diverged.

A baseline is only known-good over bytes that do not change on their own, and two of the three files do. status holds the resident set, the counts of descriptors and threads and the context-switch counters. maps holds the extent of the heap and one line for every anonymous mapping the allocator takes. So each file is hashed through its projection, which drops that accounting and keeps the confinement: the seccomp mode and filter count, no-new-privs, the capability sets, and which objects are mapped with which protections. A healthy process passes the check, and a process whose confinement changed fails it.

The check catches a removed filter, a changed seccomp mode, a flipped capability bit or a new mapping. It misses a flip inside a filter's BPF instructions, because /proc/self/status does not show them. The baseline hashes also sit in the process's own memory, where an upset can hit them as well. A line the projection does not recognise is kept, so an unexpected /proc layout shows up as a change instead of going unwatched.

Installation #

Install with opam:

$ opam install fdir

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install fdir

Usage #

Boot snapshot and on-demand checks #

Take a baseline early in startup. Later, compare the live state with it whenever you want to check that nothing has drifted:

let () =
  Eio_main.run @@ fun env ->
  let clock = Eio.Stdenv.clock env in
  let fs = Fdir.Procfs.live Eio.Path.(Eio.Stdenv.fs env / "/proc") in
  let baseline = Fdir.snapshot ~clock fs in
  (* ... application runs, then later: *)
  match Fdir.check ~baseline ~clock fs with
  | Fdir.Ok _ -> ()
  | Fdir.Anomaly { anomalies; _ } ->
      List.iter
        (fun a -> Fmt.epr "drift on %a@." Fdir.pp_subsystem a.Fdir.subsystem)
        anomalies

Monitor continuously #

start_daemon forks an Eio fibre that runs check on a timer over the subsystems named in the configuration. When it finds an anomaly it calls your handler, which returns a severity. The application reads that severity to decide whether to log, isolate, restart or drop to safe mode. The daemon also counts the checks it has run:

let () =
  Eio_main.run @@ fun env ->
  Eio.Switch.run @@ fun sw ->
  let clock = Eio.Stdenv.clock env in
  let fs = Fdir.Procfs.live Eio.Path.(Eio.Stdenv.fs env / "/proc") in
  let baseline = Fdir.snapshot ~clock fs in
  let config = Fdir.Config.v ~interval:30.0 () in
  let daemon =
    Fdir.start_daemon
      ~sw ~clock ~config ~baseline ~fs
      ~on_anomaly:Fdir.default_handler
      ()
  in
  (* ... application runs, then later: *)
  let stats = Fdir.stats daemon in
  Fmt.pr "%d checks, %d anomalies@." stats.Fdir.checks_total
    stats.Fdir.anomalies_total

Fdir.default_handler returns Log for one anomaly, Degrade for two and Safe_mode for three or more.

API #

Snapshot / check #

  • Fdir.snapshot ?subsystems ~clock fs: a SHA-256 snapshot of the given subsystems, all of them by default.
  • Fdir.check ?subsystems ~baseline ~clock fs: Ok, or Anomaly { anomalies; _ } listing the subsystems that diverged. It reads only the subsystems it compares, by default those the baseline covers.
  • Fdir.Procfs.live proc reads the process's own files under proc, the /proc directory; Fdir.Procfs.mock ~maps ~status ~cgroups supplies canned data for tests.

Daemon #

  • Fdir.start_daemon ~sw ~clock ~config ~baseline ~fs ~on_anomaly ()
  • Fdir.stats daemon: the checks run, the anomalies seen, the time of the last check and the most drastic severity a handler returned.
  • Fdir.Config.v ?interval ?subsystems (): the interval defaults to 30 seconds and the subsystems to all three (Memory_maps, Seccomp, Cgroups); the daemon checks exactly the subsystems named.

Severity #

A severity is one of Log, Isolate, Restart, Degrade and Safe_mode, printed by Fdir.pp_severity.