From 274de4b41b737016dd3ec7d6187909425b2d077c Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Tue, 27 Oct 2026 00:34:15 -0500 Subject: [PATCH] fix: clean up only accounts created by clean-user harness Guard account removal after successful creation, avoid clock-only candidate names, and fail closed if temporary setup fails. Add mocked cleanup regressions to the existing hello-sim host target. --- mk/dx-sim.mk | 1 + tests/dx/clean_user_cleanup_test.sh | 99 +++++++++++++++++++++++++++++ tools/runtime/clean-user-hello.sh | 20 ++++-- 3 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 tests/dx/clean_user_cleanup_test.sh diff --git a/mk/dx-sim.mk b/mk/dx-sim.mk index ece28b29..f03e13f1 100644 --- a/mk/dx-sim.mk +++ b/mk/dx-sim.mk @@ -4,4 +4,5 @@ # line and that two runs give the same bits. Host only: --sim never opens a device. .PHONY: test-hello-sim test-hello-sim: $(OUT_DIR)/hello_gpu $(OUT_DIR)/hello_gpu_control + bash tests/dx/clean_user_cleanup_test.sh sh tests/dx/hello_sim_test.sh $(OUT_DIR)/hello_gpu $(OUT_DIR)/hello_gpu_control diff --git a/tests/dx/clean_user_cleanup_test.sh b/tests/dx/clean_user_cleanup_test.sh new file mode 100644 index 00000000..236b91d1 --- /dev/null +++ b/tests/dx/clean_user_cleanup_test.sh @@ -0,0 +1,99 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# Host-only orchestration tests. All privilege, network and GPU-entry commands +# are mocked; no real user account, sudo, network request or device is used. +set -euo pipefail +root=$(cd "$(dirname "$0")/../.." && pwd) +script=${1:-$root/tools/runtime/clean-user-hello.sh} +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir "$work/bin" +cat > "$work/bin/sudo" <<'MOCK' +#!/usr/bin/env bash +set -eu +printf '%s\n' "$*" >> "$MOCK_CALLS" +case "$1" in + useradd) exit "$MOCK_USERADD_RC" ;; + -u) exit 0 ;; # Never execute the requested login shell. + test) exit 1 ;; # No real trace was produced. + pkill|userdel|rm) exit 0 ;; # Record only; never execute. + *) echo UNEXPECTED_PRIVILEGE >> "$MOCK_CALLS"; echo "unexpected privileged command: $1" >&2; exit 99 ;; +esac +MOCK +cat > "$work/bin/curl" <<'MOCK' +#!/usr/bin/env bash +echo curl >> "$MOCK_CALLS" +exit 7 # Stop after account creation, before README/GPU execution. +MOCK +cat > "$work/bin/flock" <<'MOCK' +#!/usr/bin/env bash +echo UNEXPECTED_GPU_ENTRY >> "$MOCK_CALLS" +exit 99 +MOCK +cat > "$work/bin/getent" <<'MOCK' +#!/usr/bin/env bash +exit 2 # The mock account is absent after successful mock deletion. +MOCK +cat > "$work/bin/date" <<'MOCK' +#!/usr/bin/env bash +printf '%s\n' 010203 # Make clock-based names collide deterministically. +MOCK +real_mktemp=$(command -v mktemp) +cat > "$work/bin/mktemp" <<'MOCK' +#!/usr/bin/env bash +if [ "${MOCK_MKTEMP_FAIL:-0}" = 1 ]; then exit 1; fi +exec "$REAL_MKTEMP" "$@" +MOCK +chmod +x "$work/bin/"* +pass=0; fail=0 +check() { + local name=$1; shift + if "$@"; then echo "PASS $name"; pass=$((pass + 1)) + else echo "FAIL $name"; fail=$((fail + 1)); fi +} +no_account_cleanup() { ! grep -Eq '^(pkill|userdel) ' "$1"; } +no_privilege() { [ ! -s "$1" ]; } +run_case() { + local name=$1 add_rc=$2 temp_fail=${3:-0} + mkdir -p "$work/$name/tmp" + : > "$work/$name/calls" + set +e + env PATH="$work/bin:/usr/bin:/bin" \ + REAL_MKTEMP="$real_mktemp" MOCK_CALLS="$work/$name/calls" \ + MOCK_USERADD_RC="$add_rc" MOCK_MKTEMP_FAIL="$temp_fail" \ + TMPDIR="$work/$name/tmp" CUDAPP_BOARD= \ + bash "$script" main "$work/$name/run.log" > "$work/$name/output" 2>&1 + rc=$? + set -e + check "$name-exit-3" test "$rc" -eq 3 + check "$name-never-enters-gpu" test "$(grep -c UNEXPECTED_GPU_ENTRY "$work/$name/calls" || true)" -eq 0 + check "$name-only-expected-privilege" test "$(grep -c UNEXPECTED_PRIVILEGE "$work/$name/calls" || true)" -eq 0 +} + +# useradd exit 9 means the candidate account already exists. Its processes and +# home belong to someone else; the cleanup must never issue deletion commands. +run_case collision 9 +check existing-account-untouched no_account_cleanup "$work/collision/calls" +run_case failure 1 +check failed-creation-untouched no_account_cleanup "$work/failure/calls" + +# Successful creation followed by a network/setup failure must still remove +# precisely the owned temporary account, not leak it or use a different name. +run_case created 0 +created=$(sed -n 's/^useradd -m -s \/bin\/bash //p' "$work/created/calls") +check created-name-nonempty test -n "$created" +check owned-process-cleanup grep -Fxq "pkill -u $created" "$work/created/calls" +check owned-account-cleanup grep -Fxq "userdel -r $created" "$work/created/calls" +check one-owned-account-deletion test "$(grep -c '^userdel ' "$work/created/calls" || true)" -eq 1 + +# Even with the same UTC clock value, simultaneous/repeated candidates differ. +run_case created_again 0 +again=$(sed -n 's/^useradd -m -s \/bin\/bash //p' "$work/created_again/calls") +check distinct-candidate-names test "$created" != "$again" + +# A missing temporary directory must abort before creating any account. +run_case temp_failure 0 1 +check temp-failure-no-privilege no_privilege "$work/temp_failure/calls" + +echo "clean-user cleanup tests: $pass passed, $fail failed (mocked commands only)" +[ "$fail" -eq 0 ] diff --git a/tools/runtime/clean-user-hello.sh b/tools/runtime/clean-user-hello.sh index 85d087d4..5915ac00 100755 --- a/tools/runtime/clean-user-hello.sh +++ b/tools/runtime/clean-user-hello.sh @@ -17,26 +17,34 @@ log=${2:?usage: clean-user-hello.sh BRANCH LOG [URL]} url=${3:-https://github.com/aien-dev/CUDApp} lock=${CUDAPP_GPU_LOCK:-/tmp/aien-gb10.lock} board=${CUDAPP_BOARD:-} -U=c2stranger$(date -u +%H%M%S) +user_created=0 cd / || exit 3 run() { echo "+ $*"; "$@"; } note() { [ -n "$board" ] && echo "$(date -u +%FT%TZ) $*" >> "$board"; return 0; } -tr_dir=$(mktemp -d "${TMPDIR:-/tmp}/clean-user-trace.XXXXXX") +tr_dir=$(mktemp -d "${TMPDIR:-/tmp}/clean-user-trace.XXXXXX") || exit 3 +# Reuse the unique temporary-directory suffix instead of a clock-only name. +# useradd is the atomic ownership check: an existing account is never ours. +U=c2stranger${tr_dir##*.} exec > >(tee "$log") 2>&1 echo "# clean-user-hello: branch=$branch url=$url user=$U" echo "# date: $(date -u +%FT%TZ) host: $(uname -srm) loadavg: $(cut -d' ' -f1-3 /proc/loadavg)" echo "# operator: $(id -un) nvidia module: $(cat /sys/module/nvidia/version 2>/dev/null || echo none)" cleanup() { if sudo test -s "$tr_dir/readme.trace"; then sudo gzip -9c "$tr_dir/readme.trace" > "$log.strace.gz" && echo "# full trace of the README block kept: $log.strace.gz"; fi - echo "# --- delete the temporary user ---" - run sudo pkill -u "$U" || true - run sudo userdel -r "$U" 2>&1 | grep -v 'mail spool' || true + if [ "$user_created" -eq 1 ]; then + echo "# --- delete the temporary user ---" + run sudo pkill -u "$U" || true + run sudo userdel -r "$U" 2>&1 | grep -v 'mail spool' || true + if getent passwd "$U" >/dev/null || [ -e "/home/$U" ]; then echo "# CLEANUP FAILED: $U still present"; else echo "# user $U and /home/$U removed"; fi + else + echo "# no account created by this run; account cleanup skipped" + fi sudo rm -rf "$tr_dir" - if getent passwd "$U" >/dev/null || [ -e "/home/$U" ]; then echo "# CLEANUP FAILED: $U still present"; else echo "# user $U and /home/$U removed"; fi } trap cleanup EXIT echo "# --- create a brand-new user ---" run sudo useradd -m -s /bin/bash "$U" || exit 3 +user_created=1 # the stranger's shell: an empty environment except what login(1) sets, then the distribution's login profile S() { sudo -u "$U" env -i HOME="/home/$U" USER="$U" LOGNAME="$U" SHELL=/bin/bash TERM=dumb LANG=C.UTF-8 PATH=/usr/local/bin:/usr/bin:/bin bash -lc "cd ~; $1"; } echo "# new user:"