diff --git a/advanced/Scripts/updatecheck.sh b/advanced/Scripts/updatecheck.sh index 7ba8292b..80e86d36 100755 --- a/advanced/Scripts/updatecheck.sh +++ b/advanced/Scripts/updatecheck.sh @@ -10,6 +10,20 @@ set -o pipefail +# This script writes into /etc/pihole, which is owned by the unprivileged +# "pihole" user. Anything root does there by name can be redirected by a +# symlink that user plants (GHSA-xch2-4qxw-g5vj), so never do that work as root: +# when started as root (the installer, `pihole -up`, `pihole checkout`, +# `sudo pihole updatechecker`, ...) hand over to the pihole user and exit with +# its result. The pihole user can only ever damage what it already owns. +if [[ "$(id -u)" -eq 0 ]]; then + # Only the literal "reboot" argument (see below) is passed on + reboot_arg="" + [[ "${1:-}" == "reboot" ]] && reboot_arg="reboot" + su -s /bin/sh pihole -c "/opt/pihole/updatecheck.sh ${reboot_arg}" + exit "$?" +fi + function get_local_branch() { # Return active branch cd "${1}" 2>/dev/null || { echo "null"; return; } @@ -54,11 +68,10 @@ rm -f "/etc/pihole/localversions" VERSION_FILE="/etc/pihole/versions" -# Assemble the versions file in a private temporary file and atomically move it -# into place at the very end of this script. Operating on "${VERSION_FILE}" -# directly would follow a symlink that a compromised pihole user could plant in -# the pihole-owned /etc/pihole directory (GHSA-xch2-4qxw-g5vj); renaming a fresh -# file over the path replaces such a symlink instead of writing through it. +# Assemble the versions file in a temporary file next to it and move it into +# place at the very end of this script, so readers never see a half-written +# file. (Running as the pihole user, see above, the temporary file lives in a +# directory only that user can modify.) VERSION_FILE_TMP="$(mktemp "${VERSION_FILE}.XXXXXX")" || exit 1 trap 'rm -f "${VERSION_FILE_TMP}"' EXIT @@ -143,15 +156,12 @@ if [[ "${DOCKER_TAG}" ]]; then fi # Make the assembled file world-readable ("other" needs read access so any local -# user can run `pihole -v`) and, if this script happens to be running as root, -# hand ownership to the pihole user so it can be replaced on the next run. +# user can run `pihole -v`). chmod 0644 "${VERSION_FILE_TMP}" -if [ "$(id -u)" -eq 0 ]; then - chown pihole:pihole "${VERSION_FILE_TMP}" 2>/dev/null || true -fi # Publish the assembled file. mv on a single file within the same filesystem # calls rename(2), which replaces the directory entry at "${VERSION_FILE}" -# itself. A symlink planted there is therefore swapped out rather than followed -# (GHSA-xch2-4qxw-g5vj). -mv -f "${VERSION_FILE_TMP}" "${VERSION_FILE}" +# itself, so whatever was there before (even a symlink) is swapped out. -T +# makes mv treat "${VERSION_FILE}" as the file to replace, never as a directory +# to move the temporary file into. +mv -fT "${VERSION_FILE_TMP}" "${VERSION_FILE}" diff --git a/test/test_fresh_install.bats b/test/test_fresh_install.bats index 2447f8bc..66d6e0b9 100755 --- a/test/test_fresh_install.bats +++ b/test/test_fresh_install.bats @@ -125,3 +125,73 @@ EOF done <<< "${files}" done <<< "${dirs}" } + +# The tests below build on the state left behind by the install test above. +require_installed() { + [[ -x /opt/pihole/updatecheck.sh && -x /usr/bin/pihole-FTL ]] || skip "needs the fresh install from the first test" +} + +@test "fresh install: updatecheck.sh started as root writes the versions file as the pihole user" { + require_installed + rm -f /etc/pihole/versions + + run /opt/pihole/updatecheck.sh + assert_success + + run stat -c '%a %U' /etc/pihole/versions + assert_output "644 pihole" + run cat /etc/pihole/versions + assert_line --partial "FTL_VERSION=" + # The local Core version needs git to trust the (root-owned) repository for the pihole user + refute_line "CORE_VERSION=null" +} + +@test "fresh install: updatecheck.sh started as root does its work as the pihole user" { + require_installed + # Make /etc/pihole writable by root only. If the script really hands over to + # the pihole user it cannot create its temporary file there and must fail + # without writing anything; running as root it would succeed. + rm -f /etc/pihole/versions + chown root:root /etc/pihole + + run /opt/pihole/updatecheck.sh + chown pihole:pihole /etc/pihole + + assert_failure + assert [ ! -e /etc/pihole/versions ] +} + +@test "fresh install: updatecheck.sh replaces a symlink at the versions path instead of following it" { + require_installed + printf 'secret' > /root/ghsa-canary + chmod 0600 /root/ghsa-canary + rm -f /etc/pihole/versions + ln -s /root/ghsa-canary /etc/pihole/versions + + run /opt/pihole/updatecheck.sh + assert_success + + run stat -c '%a %U' /root/ghsa-canary + assert_output "600 root" + run cat /root/ghsa-canary + assert_output "secret" + assert_file_exists /etc/pihole/versions + assert [ ! -L /etc/pihole/versions ] + rm -f /root/ghsa-canary +} + +@test "fresh install: updatecheck.sh replaces a symlink to a directory at the versions path" { + require_installed + mkdir -p /root/ghsa-dir + chown pihole /root/ghsa-dir + rm -f /etc/pihole/versions + ln -s /root/ghsa-dir /etc/pihole/versions + + run /opt/pihole/updatecheck.sh + assert_success + + assert [ ! -L /etc/pihole/versions ] + assert [ -f /etc/pihole/versions ] + assert [ -z "$(ls -A /root/ghsa-dir)" ] + rm -rf /root/ghsa-dir +}