Vulnerability management for NixOS
nixray docs database.md
14 kB · 255 lines
Markdown
at main

Database layout (PostgreSQL ≥ 14) #

Authoritative DDL: src/nixray/adapters/postgres/migrations/.

            ┌──────────────┐          ┌──────────────┐
            │     cve      │          │   package    │
            │ id (PK)      │          │ id (PK)      │
            │ description  │          │ pname        │
            │ cvss_v3      │          │ version      │
            │ first_seen_at│          │ UQ(pname,ver)│
            │ updated_at   │          └──────┬───────┘
            └──┬────────┬──┘                 │
               │        │                    │
   ┌───────────┴──┐  ┌──┴───────────────────┴──┐       ┌───────────────────────┐
   │ suppression  │  │        finding          │       │         scan          │
   │ id (PK)      │  │ id (PK)                 │       │ id (PK)               │
   │ cve_id (FK)  │  │ cve_id (FK)             │       │ started_at            │
   │ pname?       │  │ package_id (FK)         │       │ finished_at?          │
   │ version?     │  │ UQ(cve_id, package_id)  │       │ status (enum)         │
   │ reason       │  └──┬───────────────────┬──┘       │ target                │
   │ created_by/at│     │                   │          │ system_store_path?    │
   │ expires_at?  │     │                   │          │ nixos_version?        │
   │ revoked_*?   │     │                   │          │ scanner_version?      │
   └──────────────┘     │                   │          │ finding_count?        │
                        │                   │          │ error?                │
          ┌─────────────┴────────┐   ┌──────┴──────────┴───┐                  │
          │    observation       │   │   exposure_period    │                  │
          │ scan_id  (PK,FK) ────┼──▶│ id (PK)              │                  │
          │ finding_id (PK,FK)   │   │ finding_id (FK)      │                  │
          │ derivation_paths[]   │   │ opened_scan_id (FK) ─┼──────────────────┘
          └──────────────────────┘   │ closed_scan_id? (FK) │
                                     │ period tstzrange     │
                                     │ EXCLUDE overlapping  │
                                     └──────────────────────┘

Tables #

host #

A machine nixray keeps history for (migration 0007). name is unique and hostname-like (CHECK). Only the history belongs to a host (scan, and through it observation, plus exposure_period); findings, CVEs and every decision (suppression, confirmation, external_triage, finding_judgement) are about a package and apply on all hosts. Migration 0007 assigns the existing history to one host named by NIXRAY_HOST (default: the machine's hostname) at migration time.

scan #

One row per scanner run, success or failure.

column type notes
id bigint identity PK
host_id bigint FK host; UNIQUE (id, host_id) lets periods reference both
started_at timestamptz observation time of all findings of this scan
finished_at timestamptz null NULL ⇔ status = 'running' (CHECK)
status scan_status enum running/succeeded/failed
target text e.g. --system or a path
system_store_path text null /run/current-system resolved → correlates with deployments
nixos_version text null
scanner_version text null vulnix version
finding_count integer null denormalised summary; set iff succeeded (CHECK)
runtime_count integer null how many of them are runtime findings; null for scans before migration 0005
error text null required when failed (CHECK)

Partial index on (host_id, started_at) for status = 'succeeded' (trend queries).

cve #

Reference data from NVD as delivered by vulnix. Upserted on every scan because NVD re-scores and re-describes CVEs over time. id is checked against ^CVE-[0-9]{4}-[0-9]{4,}$; cvss_v3 numeric(3,1) in [0,10], nullable.

package #

(pname, version) as split by vulnix. Unique pair.

finding #

Identity of "package P in version V is affected by CVE C" – the unit of triage and exposure. Unique (cve_id, package_id).

observation – immutable facts #

"Scan S observed finding F (through derivations D, in scope X)". PK (scan_id, finding_id), derivation_paths text[] because one name/version may come from several .drvs, scope finding_scope (runtime/buildtime, added by migration 0005; earlier observations were set to runtime). Indexed on finding_id. Source of truth for the trend and for rebuilding periods. processes text[] (running processes, by systemd unit, that had the package loaded) and restart_needed boolean (only they still had it, not the current system) were added by migration 0015 (ADR-17); earlier observations have none and false.

exposure_period – derived, maintained on ingest #

period tstzrange = [opened_at, closed_at), upper bound unbounded while open. A period lies on one host: host_id with FKs (opened_scan_id, host_id) and (closed_scan_id, host_id) → scan (id, host_id), so only that host's scans open and close it.

  • EXCLUDE USING gist (host_id WITH =, finding_id WITH =, period WITH &&) (needs btree_gist): periods of a finding on a host never overlap.
  • Partial unique index (host_id, finding_id) WHERE upper_inf(period): at most one open period per finding and host.
  • CHECK closed_scan_id IS NULL ⇔ upper_inf(period).

suppression – false-positive rules (append-only) #

column notes
cve_id FK cve, required
pname null = every package
version null = every version; CHECK version IS NULL OR pname IS NOT NULL
reason required, CHECK at least 10 non-blank characters
created_by, created_at audit
expires_at optional review date; expired rule no longer suppresses
revoked_at, revoked_by, revoke_reason all-or-nothing CHECK; rows are never deleted

Partial unique index on (cve_id, coalesce(pname,''), coalesce(version,'')) WHERE revoked_at IS NULL – one live rule per scope; creating a new rule for the same scope revokes ("supersedes") the previous one in the same transaction.

A finding is suppressed iff a rule exists with revoked_at IS NULL, (expires_at IS NULL OR expires_at > now()), same cve_id, and matching or null pname/version. This rule lives only in the domain (Suppression.suppresses); the database stores facts, not interpretations (see architecture ADR-4).

A trigger (suppression_append_only) rejects DELETE and any UPDATE other than setting the revocation once, so the audit trail cannot be rewritten even with direct SQL access by the application role.

confirmation – review decisions (append-only, revocable) #

"CVE C really affects package P (any version)", recorded from the review inbox.

column notes
cve_id FK cve
pname package name, required
note optional, ≤ 2000 characters
created_by, created_at audit
revoked_at, revoked_by, revoke_reason all-or-nothing CHECK; reason ≥ 10 characters

Partial unique index (cve_id, pname) WHERE revoked_at IS NULL: one active confirmation per scope; confirming again revokes ("supersedes") the previous one in the same transaction. A trigger (confirmation_append_only) rejects DELETE and every UPDATE other than setting the revocation once. Migration 0003 resolved duplicates from before that rule by keeping the newest.

A finding needs review iff it is open, not suppressed and has no active confirmation for its (cve_id, pname) (rule in the domain, ADR-4).

external_triage – cache of the NixOS security tracker #

One row per CVE (cve_id primary key, no foreign key: the tracker may know CVEs before nixray does) with outcome, basis (human/automatic), the linked (affected) and removed (excluded) attribute names, explanation, issue, url (CHECK: http(s) only, so a hostile value cannot become a javascript: link) and fetched_at. Rows are replaced on refresh; this is a cache of external data, not an audit record. See tracker.md.

finding_judgement – judgements by a language model #

One row per finding (finding_id primary key and foreign key) with applicability (affected/not_affected/uncertain), the model's reasoning (1–2000 characters), model and judged_at. Like external_triage it is advice, replaced when a finding is judged again, not an audit record. See judgement.md.

known_exploited – CISA's Known Exploited Vulnerabilities catalog #

One row per CVE in the catalog (cve_id primary key, no foreign key: most of them are not on any host) with date_added. Replaced as a whole by nixray kev-sync (migration 0008, ADR-12).

epss – FIRST's EPSS scores #

One row per scored CVE (cve_id primary key, no foreign key) with score, the probability of exploitation in the next 30 days (CHECK: 0–1). Replaced as a whole by nixray epss-sync (migration 0009, ADR-12).

One row per CVE and collection ((cve_id, source) primary key, no foreign key) with source (exploit_db/metasploit/nuclei) and url (CHECK: https only, so a hostile value cannot become a javascript: link). Each source's rows are replaced as a whole by nixray exploit-sync (migration 0010, ADR-12).

cve_record – cache of the CVE Program's records #

One row per CVE (cve_id primary key, no foreign key) with state (published/rejected/unknown), the best score cvss, its cvss_version (3.0/3.1/4.0) and cvss_source (the CNA's short name or CISA-ADP), all three set together (CHECK), cwes, CISA's SSVC decision points (ssvc_exploitation none/poc/active, ssvc_automatable, ssvc_technical_impact partial/total), published (the record's publication date, for the refresh policy) and fetched_at. Replaced on refresh by nixray cve-sync (migration 0011, ADR-13).

euvd_entry – cache of ENISA's EUVD #

One row per CVE (cve_id primary key, no foreign key) with euvd_id (null: not in the EUVD, cached as well), exploited_since (only with an euvd_id, CHECK) and fetched_at. Replaced on refresh by nixray euvd-sync (migration 0012, ADR-14).

debian_verdict – the Debian security tracker's verdicts #

One row per CVE and Debian source package (unique, no foreign key), with status (not_for_us/rejected/not_affected/minor/fixed/open) and detail (Debian's reason, or the fixed Debian version). package is null exactly for a verdict about the CVE itself (not_for_us, rejected; CHECK). Replaced as a whole by nixray debian-sync (migration 0013, ADR-15).

upstream_check and upstream_affected – what a newer nixpkgs fixes #

One upstream_check row per run of nixray upstream-check and host (host_id): checked_at (when it started), target (e.g. nixpkgs=github:NixOS/nixpkgs/nixos-25.11), system_drv (the evaluated system's derivation) and error (null: succeeded). A successful check has one upstream_affected row per (cve_id, pname) vulnix still reported; the version is the upstream one, so there is no link to finding. Facts only: fix available since is derived when reading (migration 0014, ADR-16).

How the views are derived #

The read side loads flat rows and lets the domain compute (ADR-4):

  • Trend: for every successful scan time t, count the exposure periods with lower <= t < upper. Because a period is only closed by a successful scan that no longer observed the finding, this equals "findings observed by that scan". Implemented as a sweep (count_open_at) in O((n+m) log n).
  • Exposure of a CVE: union of the periods of all its findings (touching periods merge, e.g. an upgrade to a still-affected version), open periods last until now.

The same answers are available in SQL for ad-hoc analysis, e.g.:

SELECT f.cve_id,
       range_agg(tstzrange(lower(p.period), coalesce(upper(p.period), now()))) AS exposed
FROM exposure_period p JOIN finding f ON f.id = p.finding_id
GROUP BY f.cve_id;

Roles & privileges (NixOS module) #

The database is owned by the nixray role, connected via Unix socket with peer authentication – no password exists. Both services (nixray-scan, nixray-web) run as the nixray system user. Migrations run in nixray-migrate.service before both.

Retention #

Facts are small (≈ findings × scans). At 2 scans/day and ~600 findings this is ≈ 440k observation rows per year, comfortably within Postgres' range. Upstream checks add about as many upstream_affected rows (one per still-affected CVE and package name, twice a day) per host. scan rows can be deleted (ON DELETE CASCADE on observations) if retention is ever needed; periods reference scans by id with ON DELETE RESTRICT so history of exposure cannot be destroyed accidentally.