|
| 1 | +#!/usr/bin/env bash |
| 2 | +# ============================================================================= |
| 3 | +# NFTBan v1.100 PR-P2-4 — CI exec-trace assertion helper |
| 4 | +# ============================================================================= |
| 5 | +# SPDX-License-Identifier: MPL-2.0 |
| 6 | +# meta:name="ci-exec-trace-assert" |
| 7 | +# meta:type="script" |
| 8 | +# meta:version="1.100.0" |
| 9 | +# meta:owner="Antonios Voulvoulis <contact@nftban.com>" |
| 10 | +# meta:created_date="2026-04-20" |
| 11 | +# meta:description="Wrap a dry-run command under strace and fail if any forbidden mutation process was spawned" |
| 12 | +# meta:inventory.files="scripts/ci-exec-trace-assert.sh" |
| 13 | +# meta:inventory.binaries="/usr/bin/strace" |
| 14 | +# meta:inventory.env_vars="" |
| 15 | +# meta:inventory.config_files="" |
| 16 | +# meta:inventory.systemd_units="" |
| 17 | +# meta:inventory.network="" |
| 18 | +# meta:inventory.privileges="root" |
| 19 | +# ============================================================================= |
| 20 | +# |
| 21 | +# Usage: ci-exec-trace-assert.sh <command args...> |
| 22 | +# |
| 23 | +# Runs the given command under `strace -f -e trace=execve`, captures |
| 24 | +# every process spawn, then asserts no forbidden mutation binary was |
| 25 | +# invoked. Passes through the wrapped command's exit code on success; |
| 26 | +# exits non-zero if any forbidden execve is detected OR if the wrapped |
| 27 | +# command itself failed. |
| 28 | +# |
| 29 | +# Contract (PR-P2-4, frozen 2026-04-20): |
| 30 | +# - Only fires on execve syscalls — does not depend on Go-level mocks |
| 31 | +# - Independent of source-grep patterns (catches dynamically- |
| 32 | +# constructed commands that grep cannot see) |
| 33 | +# - Minimal surface — focused grep pass on the trace file |
| 34 | +# - Degrades gracefully: if strace is unavailable, the command runs |
| 35 | +# unwrapped with a CI warning (never silently weakens) |
| 36 | +# |
| 37 | +# Falsifiability: every regex in FORBIDDEN below matches a specific |
| 38 | +# execve shape the test expects to NEVER see during a dry-run. Any |
| 39 | +# match = gate failure. The dry-run paths must be observational at |
| 40 | +# the process-spawning level, not just the Go-function-call level. |
| 41 | +# |
| 42 | +# ============================================================================= |
| 43 | +set -Eeuo pipefail |
| 44 | + |
| 45 | +if [[ $# -lt 1 ]]; then |
| 46 | + echo "usage: $0 <command args...>" >&2 |
| 47 | + exit 2 |
| 48 | +fi |
| 49 | + |
| 50 | +# Graceful degrade: if strace isn't present (should not happen in CI |
| 51 | +# after the explicit install step, but possible on developer laptops), |
| 52 | +# run the command unwrapped. Emit a GitHub-Actions warning so CI logs |
| 53 | +# flag the missing coverage. |
| 54 | +if ! command -v strace >/dev/null 2>&1; then |
| 55 | + echo "::warning::strace not available — exec-trace assertion skipped for this invocation" |
| 56 | + exec "$@" |
| 57 | +fi |
| 58 | + |
| 59 | +TRACE=$(mktemp /tmp/ci-exec-trace.XXXXXX) |
| 60 | +trap 'rm -f "$TRACE"' EXIT |
| 61 | + |
| 62 | +# Wrap the caller's command. -f follows forks (so Go subprocess spawns |
| 63 | +# are caught); -e trace=execve restricts to the one syscall that spawns |
| 64 | +# a new binary. Output goes to $TRACE, the actual command runs against |
| 65 | +# the real terminal so its output is still visible in the CI log. |
| 66 | +set +e |
| 67 | +strace -f -e trace=execve -o "$TRACE" -- "$@" |
| 68 | +rc=$? |
| 69 | +set -e |
| 70 | + |
| 71 | +# FORBIDDEN patterns — each is an extended regex; a match = gate |
| 72 | +# failure. Every pattern targets a specific mutation-flavored invocation |
| 73 | +# shape. Read-only calls (nft list, systemctl is-active, iptables-save) |
| 74 | +# do NOT match and are allowed. |
| 75 | +FORBIDDEN=( |
| 76 | + # nft with mutation verbs. Read-only "nft list ..." does not match. |
| 77 | + 'execve\("[^"]*", \["nft", "(add|create|delete|flush)"' |
| 78 | + # systemctl lifecycle verbs anywhere in argv. Read-only |
| 79 | + # "is-active"/"is-enabled"/"status"/"show" do not match. |
| 80 | + 'execve\("[^"]*", \["systemctl"[^]]*"(start|stop|restart|reload|enable|disable|mask|unmask)"' |
| 81 | + # External firewall binaries — any invocation is mutation intent |
| 82 | + # (these tools have no read-only subcommands our dry-run could |
| 83 | + # legitimately need). Match on argv[0]. |
| 84 | + 'execve\("[^"]*", \["ufw"' |
| 85 | + 'execve\("[^"]*", \["firewall-cmd"' |
| 86 | + 'execve\("[^"]*", \["iptables-restore"' |
| 87 | + 'execve\("[^"]*", \["ip6tables-restore"' |
| 88 | + # CSF invocations with destructive flags. |
| 89 | + 'execve\("[^"]*", \["csf"[^]]*"(-e|-x|--enable|--disable)"' |
| 90 | + # Package-manager mutation verbs. |
| 91 | + 'execve\("[^"]*", \["apt-get"[^]]*"(remove|purge)"' |
| 92 | + 'execve\("[^"]*", \["dnf"[^]]*"(remove|erase)"' |
| 93 | + 'execve\("[^"]*", \["rpm"[^]]*"-e"' |
| 94 | + 'execve\("[^"]*", \["dpkg"[^]]*"(--remove|--purge)"' |
| 95 | + # User/group deletion. |
| 96 | + 'execve\("[^"]*", \["userdel"' |
| 97 | + 'execve\("[^"]*", \["groupdel"' |
| 98 | +) |
| 99 | + |
| 100 | +fail=0 |
| 101 | +for pat in "${FORBIDDEN[@]}"; do |
| 102 | + if grep -nE "$pat" "$TRACE" >/dev/null 2>&1; then |
| 103 | + echo "::error::G3-EXEC-TRACE FAIL: forbidden mutator spawned during dry-run" |
| 104 | + echo " pattern: $pat" |
| 105 | + echo " matched execve calls (up to 5):" |
| 106 | + grep -nE "$pat" "$TRACE" | head -5 | sed 's/^/ /' |
| 107 | + fail=1 |
| 108 | + fi |
| 109 | +done |
| 110 | + |
| 111 | +if (( fail > 0 )); then |
| 112 | + echo "::error::G3-EXEC-TRACE: dry-run spawned one or more forbidden mutation binaries (see patterns above)" |
| 113 | + exit 1 |
| 114 | +fi |
| 115 | + |
| 116 | +# Propagate the wrapped command's exit code unchanged. The trace |
| 117 | +# assertion is purely additive — it neither rescues a failing command |
| 118 | +# nor masks a passing one. |
| 119 | +exit $rc |
0 commit comments