From 2f8f88ec8e36017c80b11e0dda94a410472d37f7 Mon Sep 17 00:00:00 2001 From: cyc <54975526+ycpss91255@users.noreply.github.com> Date: Sun, 29 Nov 2026 19:19:26 +0800 Subject: [PATCH 1/4] test: listener unit HOME is a 0700 subdir of the runner home so orphan reap passes SEC-3 (#186) --- test/bats/integration/deploy_listener.bats | 6 ++++-- test/bats/integration/listener_reap.bats | 17 +++++++++++++++++ test/bats/unit/common.bats | 21 +++++++++++++++++++++ test/bats/unit/listener_deploy.bats | 15 +++++++++++---- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/test/bats/integration/deploy_listener.bats b/test/bats/integration/deploy_listener.bats index 3a26678..34a8a1c 100644 --- a/test/bats/integration/deploy_listener.bats +++ b/test/bats/integration/deploy_listener.bats @@ -533,8 +533,10 @@ EOF run _deploy [ "${status}" -eq 0 ] [ -d "${PREFIX}/runners/gpu/work" ] + [ -d "${PREFIX}/runners/gpu/home" ] local dir - for dir in "${PREFIX}/runners" "${PREFIX}/runners/gpu" "${PREFIX}/runners/gpu/work"; do + for dir in "${PREFIX}/runners" "${PREFIX}/runners/gpu" "${PREFIX}/runners/gpu/work" \ + "${PREFIX}/runners/gpu/home"; do grep -A4 -xF 'name=chown' "${CAP}" | grep -qxF "${dir}" done grep -A4 -xF 'name=chown' "${CAP}" | grep -qxF 'ci-runner:ci-runner' @@ -736,7 +738,7 @@ _assert_migration_failure_note() { [ -d "${runner_home}/work" ] [ "${runner_home}" = "${PREFIX}/runners/gpu" ] grep -qxF "ReadWritePaths=${PREFIX}/runners/%i" "${unit}" - grep -qxF "Environment=HOME=${PREFIX}/runners/%i" "${unit}" + grep -qxF "Environment=HOME=${PREFIX}/runners/%i/home" "${unit}" grep -qxF "ExecStart=${PREFIX}/bin/scaleset-listener" "${unit}" grep -qxF "WorkingDirectory=${PREFIX}/listener" "${unit}" grep -qxF "EnvironmentFile=${ETC}/%i.env" "${unit}" diff --git a/test/bats/integration/listener_reap.bats b/test/bats/integration/listener_reap.bats index 5985028..e7d6a25 100644 --- a/test/bats/integration/listener_reap.bats +++ b/test/bats/integration/listener_reap.bats @@ -83,6 +83,23 @@ inv() { [ -x "${SCRIPT}" ] } +# #186: the listener unit sets HOME to a subdir of RUNNER_HOME. reap.sh sources +# lib/common.sh, whose SEC-3 guard refuses RUNNER_HOME==HOME; with the unit's +# shape the sweep must run, and with HOME==RUNNER_HOME it must still refuse. +@test "reap.sh passes SEC-3 with HOME a subdir of RUNNER_HOME, as the listener unit sets it (#186)" { + inv orphanid orphan + HOME="${RUNNER_HOME}/home" run "${SCRIPT}" + [ "${status}" -eq 0 ] + [[ "${output}" != *"FATAL: refusing"* ]] + grep -qxF -- 'orphanid' "${RM_CAP}" +} + +@test "reap.sh still refuses to run when HOME equals RUNNER_HOME (#186)" { + HOME="${RUNNER_HOME}" run "${SCRIPT}" + [ "${status}" -ne 0 ] + [[ "${output}" == *"refusing RUNNER_HOME"* ]] +} + @test "reap.sh removes an orphan container and prunes its stale temp dir (#105/#106)" { inv orphanid orphan mkdir -p "${RUNNER_WORK_ROOT}/jit-orphan.XYZ" diff --git a/test/bats/unit/common.bats b/test/bats/unit/common.bats index af89da6..852d7c6 100644 --- a/test/bats/unit/common.bats +++ b/test/bats/unit/common.bats @@ -27,6 +27,27 @@ setup() { [ "${status}" -ne 0 ] } +@test "sourcing common.sh still refuses RUNNER_HOME equal to HOME" { + run bash -c "HOME=/x/y RUNNER_HOME=/x/y source '${LIB}'" + [ "${status}" -ne 0 ] + [[ "${output}" == *refusing* ]] +} + +# #186: the listener unit sets HOME and the environment file sets RUNNER_HOME; +# reap.sh sources common.sh under exactly that pair, so it must pass SEC-3. +# Both values are derived from the unit template so this follows the unit. +@test "sourcing common.sh passes SEC-3 with HOME and RUNNER_HOME as the listener unit sets them (#186)" { + local unit="${BATS_TEST_DIRNAME}/../../../deploy/scaleset-listener@.service" + local unit_home runner_home + unit_home=$(sed -n 's/^Environment=HOME=//p' "${unit}" | sed 's/%i/gpu/g') + runner_home=$(sed -n 's/^ReadWritePaths=//p' "${unit}" | sed 's/%i/gpu/g') + [ -n "${unit_home}" ] + [ "${runner_home}" = /opt/github-runner-listener/runners/gpu ] + run bash -c "HOME='${unit_home}' RUNNER_HOME='${runner_home}' source '${LIB}'" + [ "${status}" -eq 0 ] + [[ "${output}" != *refusing* ]] +} + @test "sourcing common.sh accepts a non-existent absolute RUNNER_HOME (first install)" { run bash -c "RUNNER_HOME=/tmp/does-not-exist-yet/runners source '${LIB}'" [ "${status}" -eq 0 ] diff --git a/test/bats/unit/listener_deploy.bats b/test/bats/unit/listener_deploy.bats index f4c32bd..b1dc0a4 100644 --- a/test/bats/unit/listener_deploy.bats +++ b/test/bats/unit/listener_deploy.bats @@ -198,14 +198,18 @@ teardown() { rm -rf "${WORK}"; } " [ "${status}" -eq 0 ] [ -d "${WORK}/opt/runners/gpu/work" ] + # The unit's HOME (#186): a private 0700 dir inside the runner home. + [ -d "${WORK}/opt/runners/gpu/home" ] + [ "$(stat -c %a "${WORK}/opt/runners/gpu/home")" = 700 ] local dir - for dir in runners runners/gpu runners/gpu/work; do + for dir in runners runners/gpu runners/gpu/work runners/gpu/home; do [[ "${output}" == *"CHOWN ci-runner:ci-runner"*"${WORK}/opt/${dir}"* ]] done + [[ "${output}" != *"CHOWN -R"* ]] } @test "listener_ensure_work_root is safe to re-run on an existing tree (idempotent)" { - mkdir -p "${WORK}/opt/runners/gpu/work/jit-leftover" + mkdir -p "${WORK}/opt/runners/gpu/work/jit-leftover" "${WORK}/opt/runners/gpu/home/.docker" run bash -c " source '${LIB}' LISTENER_PREFIX='${WORK}/opt'; LISTENER_ETC='${WORK}/etc'; listener_deploy_paths @@ -217,6 +221,7 @@ teardown() { rm -rf "${WORK}"; } [ "${status}" -eq 0 ] # Existing job directories are left alone. [ -d "${WORK}/opt/runners/gpu/work/jit-leftover" ] + [ -d "${WORK}/opt/runners/gpu/home/.docker" ] } # --- the environment file (the secret) ------------------------------------- @@ -1077,8 +1082,10 @@ teardown() { rm -rf "${WORK}"; } [ "$(sed -n 's/^ReadWritePaths=//p' "${unit}")" = '/opt/github-runner-listener/runners/%i' ] # HOME inside the writable tree: with ProtectHome the docker CLI's credential # helper (docker-credential-pass) cannot stat ~/.password-store and every - # image pull exits 126. - [ "$(sed -n 's/^Environment=HOME=//p' "${unit}")" = '/opt/github-runner-listener/runners/%i' ] + # image pull exits 126. It is a subdir, not RUNNER_HOME itself, because SEC-3 + # in lib/common.sh refuses RUNNER_HOME==HOME and orphan reaping died (#186). + [ "$(sed -n 's/^Environment=HOME=//p' "${unit}")" = '/opt/github-runner-listener/runners/%i/home' ] + [ "$(sed -n 's/^Environment=HOME=//p' "${unit}")" != "$(sed -n 's/^ReadWritePaths=//p' "${unit}")" ] for setting in \ 'User=ci-runner' 'Group=ci-runner' \ 'WorkingDirectory=/opt/github-runner-listener/listener' \ From 6f38be4a6e381e504ce9645f0642d4263b67ffe1 Mon Sep 17 00:00:00 2001 From: cyc <54975526+ycpss91255@users.noreply.github.com> Date: Sun, 29 Nov 2026 19:24:09 +0800 Subject: [PATCH 2/4] fix: give the listener unit a HOME subdir of the runner home so orphan reap passes SEC-3 (#186) Since #168 the unit set HOME to the runner home itself, so the SEC-3 guard in lib/common.sh (RUNNER_HOME must not equal HOME) aborted every orphan reap. HOME is now /runners/%i/home, which listener_ensure_work_root creates mode 0700 owned by the service user (no recursive chown). The renderer's prefix rewrite already covers the new path; lib/common.sh is unchanged. --- deploy/scaleset-listener@.service | 7 +++++-- doc/changelog/CHANGELOG.md | 6 ++++++ lib/listener-deploy.sh | 8 ++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/deploy/scaleset-listener@.service b/deploy/scaleset-listener@.service index b5aa6c3..bf15c9a 100644 --- a/deploy/scaleset-listener@.service +++ b/deploy/scaleset-listener@.service @@ -58,8 +58,11 @@ ReadWritePaths=/opt/github-runner-listener/runners/%i # HOME inside the same writable tree. ProtectHome hides /home/ci-runner, and on # a host with docker-credential-pass installed the docker CLI then fails every # image pull (cannot stat ~/.password-store) with exit 126. A HOME it can read -# turns that into "no stored credentials" and an anonymous pull. -Environment=HOME=/opt/github-runner-listener/runners/%i +# turns that into "no stored credentials" and an anonymous pull. It is a subdir +# (the deploy creates it 0700 for the service user), not RUNNER_HOME itself: +# lib/common.sh (SEC-3) refuses RUNNER_HOME==HOME, which made every orphan +# reap fail (#186). +Environment=HOME=/opt/github-runner-listener/runners/%i/home [Install] WantedBy=multi-user.target diff --git a/doc/changelog/CHANGELOG.md b/doc/changelog/CHANGELOG.md index fb2d45a..a756374 100644 --- a/doc/changelog/CHANGELOG.md +++ b/doc/changelog/CHANGELOG.md @@ -217,6 +217,12 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed +- **Orphan reaping works again** (#186): since #168 the listener unit set + `HOME` to the runner home itself, so the SEC-3 guard (RUNNER_HOME must not + equal HOME) refused every orphan sweep and stranded containers and `jit-*` + work dirs were no longer cleaned up. The unit's `HOME` is now + `/runners//home`, which the deploy creates mode 0700 for the + service user. Redeploy the listener to pick it up. - **Declared job sizes are charged and released exactly once** (#177): a job cancelled while waiting for a worker slot no longer leaves its cores and GB committed; a job admitted before the first successful host probe diff --git a/lib/listener-deploy.sh b/lib/listener-deploy.sh index 7f129b5..4d87585 100644 --- a/lib/listener-deploy.sh +++ b/lib/listener-deploy.sh @@ -309,11 +309,15 @@ listener_ensure_engine_access() { # systemd requires the writable directory to exist before starting the unit. # Own the shared parent too so the service user can traverse it. Avoid # recursive ownership changes because jobs and other types already live here. +# The unit's HOME is a private subdir of the runner home (#186): the docker CLI +# needs a HOME it can read, and SEC-3 refuses HOME==RUNNER_HOME. listener_ensure_work_root() { - local user=$1 parent="${LISTENER_PREFIX}/runners" + local user=$1 parent="${LISTENER_PREFIX}/runners" home="${LISTENER_RUNNER_HOME}/home" install -d -m 0750 "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" _chown "${user}:${user}" "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" - echo " work root: ${LISTENER_WORK_ROOT} (owned by ${user})" + install -d -m 0700 "${home}" + _chown "${user}:${user}" "${home}" + echo " work root: ${LISTENER_WORK_ROOT}, home ${home} (owned by ${user})" } # --- the environment file (the secret) -------------------------------------- From 3047dba1f1875ed2d43f48b8aa350c07728e985f Mon Sep 17 00:00:00 2001 From: cyc <54975526+ycpss91255@users.noreply.github.com> Date: Sun, 29 Nov 2026 19:26:00 +0800 Subject: [PATCH 3/4] refactor: derive the unit HOME path as LISTENER_HOME next to the other type paths (#186) listener_type_paths now owns the /home path alongside LISTENER_RUNNER_HOME and LISTENER_WORK_ROOT, so listener_ensure_work_root no longer recomputes it locally. --- lib/listener-deploy.sh | 10 ++++++---- test/bats/unit/listener_deploy.bats | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/listener-deploy.sh b/lib/listener-deploy.sh index 4d87585..8cbd385 100644 --- a/lib/listener-deploy.sh +++ b/lib/listener-deploy.sh @@ -108,6 +108,8 @@ listener_type_paths() { LISTENER_UNIT_NAME="scaleset-listener@${LISTENER_TYPE}.service" LISTENER_RUNNER_HOME="${LISTENER_PREFIX}/runners/${LISTENER_TYPE}" LISTENER_WORK_ROOT="${LISTENER_RUNNER_HOME}/work" + # The unit's Environment=HOME (#186): a subdir, never RUNNER_HOME itself. + LISTENER_HOME="${LISTENER_RUNNER_HOME}/home" } # --- the admin tool --------------------------------------------------------- @@ -312,12 +314,12 @@ listener_ensure_engine_access() { # The unit's HOME is a private subdir of the runner home (#186): the docker CLI # needs a HOME it can read, and SEC-3 refuses HOME==RUNNER_HOME. listener_ensure_work_root() { - local user=$1 parent="${LISTENER_PREFIX}/runners" home="${LISTENER_RUNNER_HOME}/home" + local user=$1 parent="${LISTENER_PREFIX}/runners" install -d -m 0750 "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" _chown "${user}:${user}" "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" - install -d -m 0700 "${home}" - _chown "${user}:${user}" "${home}" - echo " work root: ${LISTENER_WORK_ROOT}, home ${home} (owned by ${user})" + install -d -m 0700 "${LISTENER_HOME}" + _chown "${user}:${user}" "${LISTENER_HOME}" + echo " work root: ${LISTENER_WORK_ROOT}, home ${LISTENER_HOME} (owned by ${user})" } # --- the environment file (the secret) -------------------------------------- diff --git a/test/bats/unit/listener_deploy.bats b/test/bats/unit/listener_deploy.bats index b1dc0a4..8b3b4a8 100644 --- a/test/bats/unit/listener_deploy.bats +++ b/test/bats/unit/listener_deploy.bats @@ -193,6 +193,7 @@ teardown() { rm -rf "${WORK}"; } LISTENER_PREFIX='${WORK}/opt'; LISTENER_ETC='${WORK}/etc'; listener_deploy_paths LISTENER_RUNNER_HOME=\$LISTENER_PREFIX/runners/gpu LISTENER_WORK_ROOT=\$LISTENER_RUNNER_HOME/work + LISTENER_HOME=\$LISTENER_RUNNER_HOME/home _chown() { echo \"CHOWN \$*\"; } listener_ensure_work_root ci-runner " @@ -215,6 +216,7 @@ teardown() { rm -rf "${WORK}"; } LISTENER_PREFIX='${WORK}/opt'; LISTENER_ETC='${WORK}/etc'; listener_deploy_paths LISTENER_RUNNER_HOME=\$LISTENER_PREFIX/runners/gpu LISTENER_WORK_ROOT=\$LISTENER_RUNNER_HOME/work + LISTENER_HOME=\$LISTENER_RUNNER_HOME/home _chown() { :; } listener_ensure_work_root ci-runner " From 6c0de276ea0fdeba149d4527c85ba22753c620e4 Mon Sep 17 00:00:00 2001 From: cyc <54975526+ycpss91255@users.noreply.github.com> Date: Sun, 29 Nov 2026 19:31:41 +0800 Subject: [PATCH 4/4] fix: address review findings (#186) - Redeploying one type replaces the shared unit template and restarts the other active instances; create or repair each installed sibling's home/ (mode 0700, service user) before installing the new template, including inactive instances, so no listener starts with a missing HOME. - Refuse a symbolic link at the listener HOME path instead of following it and changing the target's mode and owner (listener_ensure_home). - The reap.sh regression now derives HOME from the unit template and asserts that the orphan is removed and its stale work dir pruned; the HOME equal to RUNNER_HOME case also asserts nothing is removed. - Add a dry-run test over an existing runner tree (no work/ or home/ created), keeping the original empty-prefix dry-run test. - CHANGELOG: say the redeploy creates the home dirs before restarting. --- doc/changelog/CHANGELOG.md | 4 +-- lib/listener-deploy.sh | 33 ++++++++++++++++++---- test/bats/integration/deploy_listener.bats | 25 ++++++++++++++-- test/bats/integration/listener_reap.bats | 16 +++++++++-- test/bats/unit/listener_deploy.bats | 13 +++++++++ 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/doc/changelog/CHANGELOG.md b/doc/changelog/CHANGELOG.md index a756374..de0ebc4 100644 --- a/doc/changelog/CHANGELOG.md +++ b/doc/changelog/CHANGELOG.md @@ -221,8 +221,8 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). `HOME` to the runner home itself, so the SEC-3 guard (RUNNER_HOME must not equal HOME) refused every orphan sweep and stranded containers and `jit-*` work dirs were no longer cleaned up. The unit's `HOME` is now - `/runners//home`, which the deploy creates mode 0700 for the - service user. Redeploy the listener to pick it up. + `/runners//home`. Redeploy to create these directories with + mode 0700 for the service user before restarting listeners. - **Declared job sizes are charged and released exactly once** (#177): a job cancelled while waiting for a worker slot no longer leaves its cores and GB committed; a job admitted before the first successful host probe diff --git a/lib/listener-deploy.sh b/lib/listener-deploy.sh index 8cbd385..cb20740 100644 --- a/lib/listener-deploy.sh +++ b/lib/listener-deploy.sh @@ -308,17 +308,25 @@ listener_ensure_engine_access() { # --- the runner state tree --------------------------------------------------- +# The unit's private HOME lives inside RUNNER_HOME so SEC-3 can permit reaping. +listener_ensure_home() { + local user=$1 home=$2 + if [[ -L ${home} ]]; then + printf 'FAIL: listener HOME is a symbolic link: %s\n' "${home}" >&2 + return 1 + fi + install -d -m 0700 "${home}" + _chown "${user}:${user}" "${home}" +} + # systemd requires the writable directory to exist before starting the unit. # Own the shared parent too so the service user can traverse it. Avoid # recursive ownership changes because jobs and other types already live here. -# The unit's HOME is a private subdir of the runner home (#186): the docker CLI -# needs a HOME it can read, and SEC-3 refuses HOME==RUNNER_HOME. listener_ensure_work_root() { local user=$1 parent="${LISTENER_PREFIX}/runners" install -d -m 0750 "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" _chown "${user}:${user}" "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" - install -d -m 0700 "${LISTENER_HOME}" - _chown "${user}:${user}" "${LISTENER_HOME}" + listener_ensure_home "${user}" "${LISTENER_HOME}" echo " work root: ${LISTENER_WORK_ROOT}, home ${LISTENER_HOME} (owned by ${user})" } @@ -458,7 +466,7 @@ listener_install_unit() { } listener_deploy_template() ( - local src=$1 user=$2 rendered prefix etc replacing=0 active_units unit + local src=$1 user=$2 rendered prefix etc replacing=0 active_units unit env_file type runner_home rendered=$(mktemp) trap 'rm -f -- "${rendered}"' EXIT @@ -474,7 +482,20 @@ listener_deploy_template() ( if listener_unit_current "${rendered}" "${LISTENER_UNIT_PATH}"; then echo " systemd unit: ${LISTENER_UNIT_PATH} already current, skipped" else - [[ ! -f ${LISTENER_UNIT_PATH} ]] || replacing=1 + if [[ -f ${LISTENER_UNIT_PATH} ]]; then + replacing=1 + # The shared template also changes HOME for installed sibling instances. + # Prepare their directories before installing it, including inactive units. + for env_file in "${LISTENER_ETC}"/*.env; do + [[ -f ${env_file} && ${env_file} != "${LISTENER_ENV_FILE}" ]] || continue + type=${env_file##*/} + type=${type%.env} + listener_valid_type_name "${type}" || continue + runner_home="${LISTENER_PREFIX}/runners/${type}" + [[ -d ${runner_home} ]] || continue + listener_ensure_home "${user}" "${runner_home}/home" + done + fi listener_install_unit "${rendered}" "${LISTENER_UNIT_PATH}" if (( replacing )); then active_units=$(_systemctl list-units --type=service --state=active --no-legend --plain --full --no-pager 'scaleset-listener@*.service') diff --git a/test/bats/integration/deploy_listener.bats b/test/bats/integration/deploy_listener.bats index 34a8a1c..19d992f 100644 --- a/test/bats/integration/deploy_listener.bats +++ b/test/bats/integration/deploy_listener.bats @@ -98,6 +98,9 @@ case "$1" in disable) rm -f "${STUB_STATE}/$2.enabled"; exit 0 ;; start|restart) [ "$2" = "${STUB_FAIL_START_UNIT:-}" ] && exit 1 + if [ "$2" = "${STUB_CHECK_HOME_UNIT:-}" ]; then + [ "$(stat -c %a "${STUB_CHECK_HOME_DIR}")" = 700 ] || exit 1 + fi : > "${STUB_STATE}/$2.started"; exit 0 ;; stop) rm -f "${STUB_STATE}/$2.started"; exit 0 ;; *) exit 0 ;; @@ -374,11 +377,17 @@ EOF grep -qxF 'RUNNER_MANAGED_BY=github-runner-listener-gpu' "${ETC}/gpu.env" } -@test "redeploy restarts other active instances after replacing the shared template" { +@test "redeploy prepares sibling homes before restarting active instances with the shared template" { run _deploy --skip-github [ "${status}" -eq 0 ] + local unit="${LISTENER_UNIT_DIR}/scaleset-listener@.service" + sed -i '/^Environment=HOME=/s|/home$||' "${unit}" + mkdir -p "${PREFIX}/runners/cpu/work" "${PREFIX}/runners/idle/home" + chmod 0755 "${PREFIX}/runners/idle/home" + touch "${ETC}/cpu.env" "${ETC}/idle.env" touch "${STUB_STATE}/scaleset-listener@cpu.service.started" - printf '\n# Older installed template\n' >> "${LISTENER_UNIT_DIR}/scaleset-listener@.service" + export STUB_CHECK_HOME_UNIT=scaleset-listener@cpu.service + export STUB_CHECK_HOME_DIR="${PREFIX}/runners/cpu/home" : > "${CAP}" run _deploy_host --type gpu --skip-github