fence flat IMDS fails closed when the agent leaves supervise (H1-M4)
The flat-tier IMDS fence keyed its drop entirely on MainPID being in the supervise leaf. A flat child (uid carlos, delegated cgroupfs) can write the agent PID into a sibling leaf and move it out of supervise; at the next 5 s refresh the script saw 'not in supervise', flushed the table to empty and installed no drop, removing its own fence (fail open). The internal/cgroup PID-move fallback is the matching residual: it starts a flat tenant in the agent's own (IMDS-exempt) supervise leaf and only then moves it, so for the start-to-move window the tenant reaches IMDS and can leave a grandchild behind. Fence (staged under infra-proposed/, not applied in place): a PID latch in /run tells a same-PID exit from supervise (a child moved the agent out: H1-M4) from a restart (new MainPID) or startup (never latched). The child case fails CLOSED -- the refresh keeps the drop the previous cycle installed, no nft call. Startup/restart keep the empty-table carve-out so the edge is never locked out of its own credentials while it comes up. internal/cgroup: a Manager.fencedSpawn flag (set from CARLOS_IMDS_FLAT_FENCE) makes both flat start-and-move fallback sites refuse (ErrIMDSFenceFailClosed) rather than run a tenant through the exempt leaf; the shim, which places the child before its code runs, stays the only path a flat tenant starts through. Un-fenced boxes keep Ruling 2's fallback. Test TestHunt_H1_M4_FenceFailsClosedOutsideSupervise pins both sides. Durable fix remains no untrusted code on the flat tier (#370 nested-everywhere); this mitigates, not replaces.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
4 files changed,
+346
−1
infra-proposed/carloku-infra/fence-flat-imds.CHANGES.md+52 −0infra-proposed/carloku-infra/fence-flat-imds.sh+167 −0internal/cgroup/cgroup.go+62 −1internal/cgroup/cgroup_test.go+65 −0
diff --git a/infra-proposed/carloku-infra/fence-flat-imds.CHANGES.md b/infra-proposed/carloku-infra/fence-flat-imds.CHANGES.md| new file mode 100644 |
| index 000000000..e1956f997 |
| --- /dev/null |
| +++ b/infra-proposed/carloku-infra/fence-flat-imds.CHANGES.md |
| @@ -0,0 +1,52 @@ |
| +# Proposed change to `carloku-infrastructure/scripts/fence-flat-imds.sh` (H1-M4) |
| + |
| +This is a STAGED replacement. Do not edit the live repo in place; apply this to |
| +`carloku-infrastructure` via its own review/merge, then redeploy the fence by |
| +SSM as usual. The file here is a full copy of the script with the box-side |
| +refresh rewritten; the SSM wrapper and the systemd unit are unchanged. |
| + |
| +## What changes, by line (against the live script at hunt snapshot) |
| + |
| +Line numbers below are the LIVE script's. |
| + |
| +- **Header comment, lines 12-16.** Reworded: a stale table still fails open for |
| + lockout-safety (startup/restart), but an agent that was confirmed in |
| + `supervise` and then LEFT it with the *same PID* now fails CLOSED. Names the |
| + `/run/carlos-imds-flat.confirmed` PID latch that tells the two apart. |
| + |
| +- **New line after 57 (`sup=$svc/supervise`).** Add `state=/run/carlos-imds-flat.confirmed` |
| + plus its comment. |
| + |
| +- **Lines 59-86 (the whole `{ … } > "$f"; nft -f "$f"` block).** Replaced. The |
| + old block always emitted `flush table` and only re-added the drop rules when |
| + the agent was confirmed in `supervise`; when it was not, the table flushed to |
| + empty — the drop removed, IMDS open to the flat tier (fail OPEN). The new |
| + block computes a three-way `mode` first: |
| + - `install` — agent confirmed in `supervise` (old positive branch, verbatim |
| + rules + chown/chmod), **plus** `printf '%s\n' "$main" > "$state"` to latch |
| + the confirmed PID. |
| + - `retain` — `$state` holds a PID equal to the current non-zero MainPID, but |
| + the agent is no longer in `supervise`. Same PID ⇒ not a restart (systemd |
| + would report a new MainPID) ⇒ a flat child moved the agent out (H1-M4). |
| + `exit 0` with **no nft call**, so the live table (the drop installed by the |
| + previous cycle) is retained: IMDS stays shut. This is the fix. |
| + - `open` — MainPID is 0, or a new PID not yet in `supervise` (startup or |
| + restart). Emits the empty-table flush exactly as the old code did: the |
| + deliberate carve-out, so the edge's own main process keeps its credentials |
| + while it comes up. A stale latch from a prior PID is dropped first. |
| + |
| +## Why the PID latch (not just "agent not in supervise -> keep drop") |
| + |
| +A blanket "keep the drop whenever the agent is absent from supervise" would |
| +lock the edge out of its own credentials on every restart: the restarted agent |
| +spends its startup window under `carlos-edge.service` (not yet in `supervise`), |
| +where the retained drop would deny it IMDS. The latch distinguishes the two |
| +cases by PID identity — a restart gets a new MainPID (carve-out, fail open), a |
| +child-driven move keeps the same MainPID (fail closed). `/run` is tmpfs, so the |
| +latch resets each boot and the first-boot startup window is always carve-out. |
| + |
| +## Proof |
| + |
| +Dash trace of the decision (mocking the supervise check and `nft`) is in the |
| +task report, all four cases: install / retain (H1-M4) / startup-open / |
| +restart-open. |
diff --git a/infra-proposed/carloku-infra/fence-flat-imds.sh b/infra-proposed/carloku-infra/fence-flat-imds.sh| new file mode 100644 |
| index 000000000..7d6151427 |
| --- /dev/null |
| +++ b/infra-proposed/carloku-infra/fence-flat-imds.sh |
| @@ -0,0 +1,167 @@ |
| +#!/bin/bash |
| +# 🤖 Keep flat-tier children of the edge (sidecars, flat and foreign exec |
| +# routes) away from the instance metadata service (security review |
| +# 2026-10-23, finding B1). Flat children run as uid carlos inside |
| +# carlos-edge.service's cgroup, so the carlos_imds uid gate |
| +# (harden-host.sh) lets them through; this table drops IMDS for every socket |
| +# in that cgroup except the edge agent's own `supervise` leaf (whose |
| +# cgroup.procs root takes over, so a child cannot move itself in) and an |
| +# explicit exempt list (empty). |
| +# |
| +# nft resolves a cgroup path to an id when the rule loads, and the edge |
| +# recreates its cgroups when it restarts, so a loop service reloads the table every |
| +# 5 s. A STALE table (an edge restart, or the agent still coming up) fails |
| +# open for lockout-safety: the drop is only installed once the edge's main |
| +# process is confirmed in `supervise`, so the edge can never be locked out of |
| +# its own credentials during startup or a restart. |
| +# |
| +# But an agent that was confirmed in `supervise` and then LEFT it with the |
| +# same PID is not a restart (systemd would report a new MainPID) — it is a |
| +# flat child that moved the agent out of `supervise` to make this refresh tear |
| +# its own drop down (hunt H1-M4). That case now fails CLOSED: the refresh keeps |
| +# the drop the previous cycle installed, so IMDS stays shut to the flat tier. |
| +# The PID latch (/run/carlos-imds-flat.confirmed) is what tells the two apart. |
| +# |
| +# Install on a box (via SSM send-command with this file as the payload), or: |
| +# fence-flat-imds.sh <aws-profile> <region> <instance-id> |
| +# Exempt cgroups (operator-owned flat apps that need the box role) go in |
| +# EXEMPT below, as names under carlos-edge.service. |
| +set -euo pipefail |
| + |
| +if [ $# -ge 3 ]; then |
| + profile=$1; region=$2; instance=$3 |
| + d=$(mktemp -d) |
| + sed -n '/^# ---- box-side ----$/,$p' "$0" > "$d/payload.sh" |
| + jq -n --rawfile s "$d/payload.sh" '{commands: ($s | split("\n"))}' > "$d/params.json" |
| + cmdid=$(aws --profile "$profile" --region "$region" ssm send-command --instance-ids "$instance" \ |
| + --document-name AWS-RunShellScript --parameters "file://$d/params.json" \ |
| + --comment "fence-flat-imds" --query Command.CommandId --output text) |
| + for i in $(seq 1 24); do sleep 5 |
| + s=$(aws --profile "$profile" --region "$region" ssm get-command-invocation --command-id "$cmdid" \ |
| + --instance-id "$instance" --query Status --output text 2>/dev/null || echo Pending) |
| + [ "$s" = Success ] || [ "$s" = Failed ] && break |
| + done |
| + echo "status: $s" |
| + aws --profile "$profile" --region "$region" ssm get-command-invocation --command-id "$cmdid" \ |
| + --instance-id "$instance" --query '[StandardOutputContent,StandardErrorContent]' --output text | tail -20 |
| + rm -rf "$d" |
| + [ "$s" = Success ] |
| + exit 0 |
| +fi |
| + |
| +# ---- box-side ---- |
| +set -e |
| +cat > /usr/local/sbin/carlos-imds-flat-refresh <<'REFRESH' |
| +#!/bin/bash |
| +# Reload table inet carlos_imds_flat (installed by fence-flat-imds.sh). |
| +set -euo pipefail |
| +# Empty since 2026-10-24: the forge app was exempt at first, but its binary |
| +# links no AWS client and never touches IMDS, and any exempt cgroup is one a |
| +# flat child (same uid, delegated cgroupfs) can move itself into. |
| +EXEMPT="" |
| +cg=/sys/fs/cgroup |
| +svc=carlos.slice/carlos-edge.service |
| +sup=$svc/supervise |
| +# state latches the PID we last confirmed in supervise this boot. /run is |
| +# tmpfs, so the latch clears on reboot and the startup carve-out applies afresh |
| +# each boot. It lets this refresh tell a flat child moving the agent OUT of |
| +# supervise (same PID, now absent — H1-M4) from the agent's own startup or a |
| +# restart (a new PID, or none, not yet moved in). |
| +state=/run/carlos-imds-flat.confirmed |
| +main=$(systemctl show -p MainPID --value carlos-edge.service 2>/dev/null || echo 0) |
| + |
| +if [ -d "$cg/$sup" ] && [ "$main" != 0 ] && grep -qx "0::/$sup" "/proc/$main/cgroup" 2>/dev/null; then |
| + mode=install |
| +elif [ -f "$state" ] && [ "$main" != 0 ] && [ "$(cat "$state" 2>/dev/null)" = "$main" ]; then |
| + mode=retain |
| +else |
| + mode=open |
| +fi |
| + |
| +case "$mode" in |
| +retain) |
| + # H1-M4 fail closed. The SAME process we confirmed in supervise this boot is |
| + # still MainPID but has LEFT supervise; a restart would have a new MainPID, |
| + # so a flat child moved the agent out to tear this drop down. Leave the live |
| + # table exactly as the last cycle installed it — the drop stays, IMDS stays |
| + # shut to the flat tier. No nft call: skipping the cycle retains the current |
| + # ruleset. The old script flushed to an empty table here (fail open). |
| + exit 0 |
| + ;; |
| +open) |
| + # Startup / restart carve-out (preserved). MainPID is 0 (stopped), or a NEW |
| + # process that has not yet moved into supervise. Keep the table empty so the |
| + # edge's own main process keeps its credentials while it comes up. Drop a |
| + # stale latch from a prior PID so the next confirmation re-latches the new one. |
| + if [ "$main" != 0 ] && [ -f "$state" ] && [ "$(cat "$state" 2>/dev/null)" != "$main" ]; then |
| + rm -f "$state" |
| + fi |
| + f=$(mktemp) |
| + trap 'rm -f "$f"' EXIT |
| + { |
| + echo "table inet carlos_imds_flat {}" |
| + echo "flush table inet carlos_imds_flat" |
| + } > "$f" |
| + nft -f "$f" |
| + ;; |
| +install) |
| + # The agent writes supervise/cgroup.procs once, to move itself in |
| + # (internal/cgroup/cgroup.go setupAt). After that nothing legitimate |
| + # needs to, so root takes the file: a flat child running as carlos |
| + # can no longer migrate itself into the exempt leaf. Moving OUT of |
| + # supervise (the start-and-move fallback) needs only the destination's |
| + # and the common ancestor's cgroup.procs, which stay carlos-owned — but |
| + # internal/cgroup now fails that fallback closed while the fence is on. |
| + chown root:root "$cg/$sup/cgroup.procs" "$cg/$sup/cgroup.threads" 2>/dev/null || true |
| + chmod 0644 "$cg/$sup/cgroup.procs" "$cg/$sup/cgroup.threads" 2>/dev/null || true |
| + f=$(mktemp) |
| + trap 'rm -f "$f"' EXIT |
| + { |
| + echo "table inet carlos_imds_flat {}" |
| + echo "flush table inet carlos_imds_flat" |
| + echo "table inet carlos_imds_flat {" |
| + echo " chain out {" |
| + echo " type filter hook output priority -1; policy accept;" |
| + for a in "$sup" $(for e in $EXEMPT; do [ -d "$cg/$svc/$e" ] && echo "$svc/$e"; done); do |
| + echo " ip daddr 169.254.169.254 socket cgroupv2 level 3 \"$a\" accept" |
| + echo " ip6 daddr fd00:ec2::254 socket cgroupv2 level 3 \"$a\" accept" |
| + done |
| + echo " ip daddr 169.254.169.254 socket cgroupv2 level 2 \"$svc\" counter drop" |
| + echo " ip6 daddr fd00:ec2::254 socket cgroupv2 level 2 \"$svc\" counter drop" |
| + echo " }" |
| + echo "}" |
| + } > "$f" |
| + nft -f "$f" |
| + # Latch the confirmed PID so a later same-PID exit from supervise is read as |
| + # the H1-M4 escape (retain), not a restart (open). |
| + printf '%s\n' "$main" > "$state" |
| + ;; |
| +esac |
| +REFRESH |
| +chmod 0755 /usr/local/sbin/carlos-imds-flat-refresh |
| + |
| +# A long-running loop rather than a 5 s timer: a timer writes two journal |
| +# lines per run (about 17,000 a day); the loop logs only nft errors. |
| +systemctl disable --now carlos-imds-flat.timer >/dev/null 2>&1 || true |
| +rm -f /etc/systemd/system/carlos-imds-flat.timer |
| +cat > /etc/systemd/system/carlos-imds-flat.service <<'UNIT' |
| +[Unit] |
| +Description=Keep the IMDS fence for flat-tier edge children current (reload every 5 s) |
| +After=nftables.service |
| + |
| +[Service] |
| +Type=simple |
| +ExecStart=/bin/sh -c 'while :; do /usr/local/sbin/carlos-imds-flat-refresh; sleep 5; done' |
| +Restart=always |
| +RestartSec=5 |
| + |
| +[Install] |
| +WantedBy=multi-user.target |
| +UNIT |
| + |
| +systemctl daemon-reload |
| +systemctl enable carlos-imds-flat.service >/dev/null 2>&1 |
| +systemctl restart carlos-imds-flat.service |
| +sleep 2 |
| +echo "service: $(systemctl is-active carlos-imds-flat.service) timer: $(systemctl is-enabled carlos-imds-flat.timer 2>&1 | head -1)" |
| +echo "fence:"; nft list table inet carlos_imds_flat |
diff --git a/internal/cgroup/cgroup.go b/internal/cgroup/cgroup.go| index db5faf516..ba3f9ec7a 100644 |
| --- a/internal/cgroup/cgroup.go |
| +++ b/internal/cgroup/cgroup.go |
| @@ -174,6 +174,22 @@ func ParseSelfCgroup(data string) (string, bool) { |
| type Manager struct { |
| root string |
| logf func(string, ...any) |
| + // fencedSpawn is true when the agent runs in a cgroup the flat-tier IMDS |
| + // fence (carloku-infrastructure scripts/fence-flat-imds.sh) exempts from |
| + // its IMDS drop — the `supervise` leaf setupAt moves us into. When it is, |
| + // the flat start's PID-move fallback (startAndMoveIn) must NOT run: it |
| + // starts the tenant in the agent's own cgroup and only then moves it, so |
| + // for the start-to-move window the tenant runs in the exempt leaf, reaches |
| + // IMDS, and can fork a grandchild that STAYS there after the move — a |
| + // persistent escape (hunt H1-M4 / the start-and-move race). With the fence |
| + // active there is no race-free way to keep an unplaced tenant out of the |
| + // exempt leaf, so such a start fails closed. The shim, which places the |
| + // child before any of its code runs, stays the only path a flat tenant |
| + // starts through. Set in the production setup() from CARLOS_IMDS_FLAT_FENCE |
| + // (the box sets it when the fence is deployed); false on an un-fenced box |
| + // and in tests, where the window exposes no exempt leaf and the fallback |
| + // keeps its Ruling 2 behaviour. |
| + fencedSpawn bool |
| // locks holds each host's start lock (hostLock), made on first use. |
| locksMu sync.Mutex |
| locks map[string]*sync.Mutex |
| @@ -240,6 +256,16 @@ func setup(logf func(string, ...any), retain bool) *Manager { |
| logf("agent: exec-child accounting disabled (no delegated subtree — unit file without Delegate=?): %v", err) |
| return nil |
| } |
| + // When the box has the flat-tier IMDS fence deployed (fence-flat-imds.sh), |
| + // it sets CARLOS_IMDS_FLAT_FENCE on carlos-edge.service so the agent knows |
| + // its own `supervise` leaf is IMDS-exempt. A flat start that cannot be |
| + // placed by the shim then fails closed rather than running a tenant in that |
| + // exempt leaf through the PID-move fallback (H1-M4). Un-fenced boxes leave |
| + // it unset and keep the fallback's Ruling 2 behaviour. |
| + if os.Getenv("CARLOS_IMDS_FLAT_FENCE") != "" { |
| + m.fencedSpawn = true |
| + logf("agent: flat-tier IMDS fence active — a flat start the shim cannot place fails closed (H1-M4)") |
| + } |
| logf("agent: exec-child accounting armed (delegated subtree %s)", m.Root()) |
| return m |
| } |
| @@ -333,6 +359,14 @@ func (m *Manager) sweepStale() { |
| // child at once, so nothing is started. |
| var ErrKilledCgroupKept = errors.New("cgroup: a cgroup that took cgroup.kill could not be made fresh before a spawn") |
| +// ErrIMDSFenceFailClosed is a flat start refused because the shim could not |
| +// place the child in its own (IMDS-dropped) cgroup before exec and the agent |
| +// runs in a cgroup the IMDS fence exempts (fencedSpawn): the PID-move fallback |
| +// would run the tenant in that exempt `supervise` leaf for the start-to-move |
| +// window, where it reaches IMDS and can leave a grandchild behind (H1-M4). The |
| +// start is refused rather than run through the exempt leaf: fail closed. |
| +var ErrIMDSFenceFailClosed = errors.New("cgroup: flat start refused: the IMDS fence is active and the child could not be placed before exec, so the start-and-move fallback would run it in the agent's IMDS-exempt cgroup (H1-M4)") |
| + |
| // startCmd is the package's one spawn seam: every exec.Cmd.Start, the |
| // shim's own included, goes through it, so a test can fake a start without |
| // a process. |
| @@ -511,6 +545,9 @@ func (m *Manager) startIn(host, dir string, limit Limit, required bool, build fu |
| return nil, fmt.Errorf("%w: %s: %w", ErrLimitNotApplied, filepath.Base(dir), err) |
| } |
| if !required && !shimAvailable() { |
| + if m.fencedSpawn { |
| + return nil, m.refuseFencedFlatStart(host, dir, nil) |
| + } |
| cmd, _, err := m.startAndMoveIn(host, dir, build, before) |
| return cmd, err |
| } |
| @@ -546,7 +583,14 @@ func (m *Manager) startIn(host, dir string, limit Limit, required bool, build fu |
| return nil, fmt.Errorf("%w: %s: %w", ErrLimitNotApplied, host, err) |
| } |
| // The flat start alone, after a placement failure the shim proved |
| - // happened before exec. |
| + // happened before exec. With the IMDS fence active the PID-move fallback |
| + // would run the tenant in the agent's exempt `supervise` leaf for the |
| + // start-to-move window, so it fails closed instead (H1-M4): the shim is |
| + // the only path a flat tenant runs through, and it never runs tenant code |
| + // outside the IMDS-dropped target cgroup. |
| + if m.fencedSpawn { |
| + return nil, m.refuseFencedFlatStart(host, dir, err) |
| + } |
| m.logf("cgroup: %s: placement before exec failed (%v) — using the PID-move fallback for this spawn", host, err) |
| cmd, _, err = m.startAndMoveIn(host, dir, build, before) |
| return cmd, err |
| @@ -681,6 +725,23 @@ func (m *Manager) applyLimit(host, dir string, limit Limit, required bool) error |
| return first |
| } |
| +// refuseFencedFlatStart answers a flat start the shim could not place while |
| +// the IMDS fence is active (fencedSpawn): the PID-move fallback is not run, so |
| +// no tenant runs in the agent's exempt `supervise` leaf (H1-M4). The dir this |
| +// spawn made is removed, as every other refused start removes its own, so the |
| +// sampler never reports a ghost host. cause, when non-nil, is the placement |
| +// failure that would have triggered the fallback; it is logged, not wrapped |
| +// into the returned error, so a member reads one stable condition. |
| +func (m *Manager) refuseFencedFlatStart(host, dir string, cause error) error { |
| + _ = os.Remove(dir) |
| + if cause != nil { |
| + m.logf("cgroup: %s: placement before exec failed (%v) and the IMDS fence is active — flat start refused, not run through the exempt leaf (H1-M4)", host, cause) |
| + } else { |
| + m.logf("cgroup: %s: no placement shim in this process and the IMDS fence is active — flat start refused, not run through the exempt leaf (H1-M4)", host) |
| + } |
| + return fmt.Errorf("%w: %s", ErrIMDSFenceFailClosed, host) |
| +} |
| + |
| func (m *Manager) startAndMove(host string, build func() *exec.Cmd) (*exec.Cmd, error) { |
| cmd, _, err := m.startAndMoveIn(host, m.hostDir(host), build, nil) |
| return cmd, err |
diff --git a/internal/cgroup/cgroup_test.go b/internal/cgroup/cgroup_test.go| index 5511c202a..11ee4812b 100644 |
| --- a/internal/cgroup/cgroup_test.go |
| +++ b/internal/cgroup/cgroup_test.go |
| @@ -171,6 +171,71 @@ func fakeStart(t *testing.T, fail func(cmd *exec.Cmd) error) *[]*exec.Cmd { |
| func buildTrue() *exec.Cmd { return exec.Command("/bin/true") } |
| +// TestHunt_H1_M4_FenceFailsClosedOutsideSupervise pins the fail-closed |
| +// invariant for the flat start's PID-move fallback. When the agent runs in a |
| +// cgroup the flat-tier IMDS fence exempts (fencedSpawn — the `supervise` leaf, |
| +// per fence-flat-imds.sh), a flat start the shim could not place before exec |
| +// must NOT fall back to start-and-move: that fallback starts the tenant in the |
| +// agent's own (exempt) cgroup and only then moves it, so for the window the |
| +// tenant runs where IMDS is reachable and can leave a grandchild behind that |
| +// stays there after the move (hunt H1-M4). The start fails closed: nothing |
| +// runs, the host dir is removed, and the caller gets ErrIMDSFenceFailClosed. |
| +// The child is never the mover — it never runs through the exempt leaf at all. |
| +// |
| +// The control half shows the fallback is unchanged on an un-fenced box |
| +// (fencedSpawn false): the window exposes no exempt leaf there, so Ruling 2's |
| +// PID-move fallback still runs. The full end-to-end proof (a real tenant held |
| +// out of IMDS) needs a box with the fence and a delegated cgroup subtree; this |
| +// asserts the pure start-path decision a unit binary can reach. |
| +func TestHunt_H1_M4_FenceFailsClosedOutsideSupervise(t *testing.T) { |
| + root := newDelegatedRoot(t) |
| + m, err := SetupAt(root, t.Logf) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + quietOOM(t) |
| + // The shim placed the child and before agreed, then the shim died before |
| + // exec: the real flat start would reach its PID-move fallback here. |
| + placementFailed := &placementError{errors.New("the shim exited before exec")} |
| + stubPlacement(t, func(_ *Manager, _ string, _ *exec.Cmd, before func() error) (bool, error) { |
| + if before != nil { |
| + if err := before(); err != nil { |
| + return true, err |
| + } |
| + } |
| + return false, placementFailed |
| + }) |
| + calls := fakeStart(t, func(*exec.Cmd) error { return nil }) |
| + |
| + // Fenced: the fallback would run the tenant in the exempt leaf, so the |
| + // start fails closed and nothing runs. |
| + m.fencedSpawn = true |
| + cmd, err := m.Start("fenced.example.com", Limit{}, buildTrue) |
| + if cmd != nil || !errors.Is(err, ErrIMDSFenceFailClosed) { |
| + t.Fatalf("fenced Start = %v, %v; want no command and ErrIMDSFenceFailClosed", cmd, err) |
| + } |
| + if errors.Is(err, placementFailed) || isPlacementError(err) { |
| + t.Fatalf("the fenced refusal %v leaks the placement failure", err) |
| + } |
| + if len(*calls) != 0 { |
| + t.Fatalf("fenced start ran the command %d times; a fenced fallback must run nothing", len(*calls)) |
| + } |
| + if _, statErr := os.Stat(m.hostDir("fenced.example.com")); !os.IsNotExist(statErr) { |
| + t.Fatalf("host dir after a fenced refusal: %v, want it removed (no ghost host)", statErr) |
| + } |
| + |
| + // Un-fenced control: the same placement failure still falls back to the |
| + // PID-move start (Ruling 2), so an un-fenced box is not regressed. |
| + m.fencedSpawn = false |
| + cmd, err = m.Start("open.example.com", Limit{}, buildTrue) |
| + if cmd == nil || err != nil { |
| + t.Fatalf("un-fenced Start = %v, %v; want the PID-move fallback to start the command", cmd, err) |
| + } |
| + if got := readFixture(t, filepath.Join(m.hostDir("open.example.com"), "cgroup.procs")); got != strconv.Itoa(os.Getpid()) { |
| + t.Fatalf("un-fenced cgroup.procs = %q, want the child's PID moved in", got) |
| + } |
| +} |
| + |
| // TestStartNilManagerSpawnsPlainly: the nil manager is the no-delegation |
| // state — one plain start, no SysProcAttr, no filesystem writes. |
| func TestStartNilManagerSpawnsPlainly(t *testing.T) { |