← Back to Pull requests

fix(nri-image-policy): observe the launch environment the runtime execs #697

Closed
unknown closed this pull request without merging on Sep 24, 2026 13:00 UTC
View on GitHub
unknown unknown commented on Sep 24, 2026 05:55 UTC
Author

Fixes the admission half of #693.

What breaks

A CDI container edit that sets an environment name the image config or pod spec already set leaves the OCI spec carrying that name twice. containerd applies CDI edits through runtime-tools' generator:

// ContainerEdits.Apply
specgen := ocigen.NewFromSpec(spec)
specgen.AddMultipleProcessEnv(e.Env)

whose cache is keyed on the whole NAME=VALUE string while the lookup uses the name alone:

func createEnvCacheMap(env []string) map[string]int {
    for i, val := range env { envMap[val] = i }   // key: "NVIDIA_VISIBLE_DEVICES=all"
}
func (g *Generator) addEnv(env, key string) {
    if idx, ok := g.envMap[key]; ok { ... }        // lookup: "NVIDIA_VISIBLE_DEVICES"
    else { g.Config.Process.Env = append(...) }    // never hits -> appends
}

Reproduced directly against the version containerd v2.3.4 vendors (runtime-tools v0.9.1-0.20251114084447):

spec.Process.Env:
   PATH=/usr/bin
   NVIDIA_VISIBLE_DEVICES=all
   MODE=production
   NVIDIA_VISIBLE_DEVICES=void
DUPLICATE: NVIDIA_VISIBLE_DEVICES appears 2 times

runc hides it — prepareEnv drops duplicates keeping the last value (libcontainer/env.go) — so the container runs with the CDI value and nothing looks wrong. We do not hide it: ObserveEnv rejects duplicate names, containerEnv swallows the error, and the launch environment reads as unknown evidence, which only env: any admits.

Every CUDA base image sets NVIDIA_VISIBLE_DEVICES and the NVIDIA CDI spec rewrites it to void, so in practice every GPU workload built on one is forced onto env: any — environment completely unconstrained, the opposite of what the operator asked for.

The change

Observe those containers with the rule the runtime itself applies — keep the last value per name — then validate strictly as before.

The tolerance is scoped to containers carrying CDI evidence: the CRI CDIDevices field, or the cdi.k8s.io annotations the cdi-annotations device-list strategy uses. That is the one case where the ambiguity is not real. Everywhere else a duplicate still makes the environment unobservable, because nothing tells us which value the runtime would keep and the ambiguity must not collapse into a map and then satisfy a constrained grant — the existing TestEnvAdmissionUsesFinalStartSpec/duplicate case keeps passing unchanged.

ObserveEnv itself is untouched, so every other caller keeps the strict reading.

This restores exactness rather than relaxing it: for GPU workloads the fingerprint now describes the environment the process receives, where before they had no environment policy at all.

Tests

internal/cmds/nri-image-policy/cdi_env_693_test.go:

  • a duplicate CDI env (annotation strategy) is observable and admitted when the entry pins what runc execs

  • same via the CRI CDIDevices field

  • a duplicate outside a CDI container stays unobservable — the existing invariant

  • an injected LD_PRELOAD on top of the duplicate is still denied

  • malformed entries (no =, empty name) stay unobservable even under CDI
  • pkg/allowlist/env_test.go covers ObserveLaunchEnv directly: duplicates resolve to the runtime's value, malformed entries are still rejected, and duplicate-free input observes identically to ObserveEnv.

    Validation status

    Measured on the candidate cluster (rke2 v1.36.4+rke2r1, containerd 2.3.4-k3s1.36, device plugin v0.19.3 with cdi-annotations), busybox + 1 GPU, mounts: any:

    | pod spec | pinned value | result |
    |---|---|---|
    | — | NVIDIA_VISIBLE_DEVICES=void | admit |
    | VIS=void | void | deny |
    | VIS=all | all | deny |
    | VIS=all | void | deny |
    | VIS=all | omitted | deny |

    The three middle rows exhaust the possible single-value views, so the evidence is unusable rather than mismatched. Controls: the same collisions on a pod with no GPU admit normally, and a plain image-vs-pod-spec PATH collision behaves correctly, since containerd's own env merge deduplicates.

    What I could not do is verify the patched binary on that hardware: on cvmMode=bare-metal the plugin comes from the measured node image (/opt/nri/plugins/10-nri-image-policy, read-only erofs), so a chart-level image swap replaces the installer, not the enforcer — correct behaviour, but it means end-to-end confirmation needs a node-image build, or a gke/aks-mode cluster where nriImagePolicy.baked=false.

    Also worth doing

    Upstream createEnvCacheMap should key by name. That stops CDI emitting duplicates at the source for everyone; this change does not depend on it, and is still wanted regardless, since the plugin should describe the environment the runtime produces rather than refuse to look at it.

    Not included

    mounts: exact for GPU workloads additionally needs #642 to reach the deployed components; with both in place a GPU entry can pin env and mounts exactly, leaving only attested GPU identity free to vary. The derived values for the DeepSeek worker (109 env values, 15 CDI mount rules) are ready to apply.

    Diff: 630 lines (+472 / -158) across 7 files Reviewer: joaosa Review duration: 424m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 630 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #697"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.4% (MEDIUM) · Higher = larger blast radius
    50.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.8% · Safe to merge
    21.8%