fix: never run updatecheck.sh as root

updatecheck.sh writes the versions file into /etc/pihole, which is owned
by the unprivileged pihole user. When it ran as root (the installer,
`pihole -up`, `pihole checkout`, `sudo pihole updatechecker`, the Docker
start-up check, ...) the pihole user could watch the directory, swap the
script's temporary file for a symlink and have root chmod/chown/append
to whatever it pointed at. Reproduced: a root-only file ended up owned by
pihole.

- When started as root, hand over to the pihole user (`su`) and exit
  with its result. This covers every caller, including future ones, and
  the pihole user can only damage what it already owns. The "reboot"
  argument is forwarded.
- Drop the chown of the temporary file; it is no longer needed and was
  the dangerous part.
- Use `mv -fT` so that a symlink to a directory at the target path is
  replaced rather than having the temporary file moved into the
  directory.

Tests (fresh install): started as root the script must do its work as
the pihole user and write a pihole-owned 0644 versions file; a symlink
(to a file or to a directory) at the target path must be replaced rather
than followed.

Note: running as the pihole user needs git to trust the root-owned Core
and Web repositories (safe.directory). The installer sets that up (see
the following commit); other installers of this script, such as the
Docker image, need the equivalent.

Reported-by: Keith Linneman (LinnemanLabs)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This commit is contained in:
Adam Warner 2026-10-09 22:53:13 +01:00
parent a360b14b61
commit e05dc3f139
No known key found for this signature in database
2 changed files with 93 additions and 13 deletions

View file

@ -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}"

View file

@ -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
}