Keep the tests of both sides in test/test_symlink_safety.bats.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <github@promofaux.dev>
pihole -d logs /etc/pihole/pihole.toml through make_array_from_file,
which redacted nothing, so webserver.api.pwhash, app_pwhash and
totp_secret ended up in the debug log and in the tricorder upload
(GHSA-qfq3-88xg-765w). The only existing redaction (pwhash in
parse_file) is not used for this file.
Replace non-empty values of these keys with "<removed>". Empty values
are left as they are, so the log still shows whether one is set.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <github@promofaux.dev>
/etc/pihole and pihole.toml are controlled by the pihole user
(GHSA-5w4v-f65m-323f):
- pihole checkout wrote /etc/pihole/ftlbranch as root and chmod-ed it,
following a symlink planted there. Write it as the pihole user
instead, removing whatever is at the path first.
- The uninstaller ran rm -rf on webserver.paths.webroot/webhome and
rm -f on files.database, files.gravity and files.macvendor as root.
Only remove the web interface directory if it is a checkout of the
Pi-hole web repository, and delete the databases as the pihole user,
before that user is removed. The defaults live in /etc/pihole, which
is removed anyway.
The macvendor.db download in the installer, also part of this
advisory, is fixed together with GHSA-wp3p-45h3-2v82.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <github@promofaux.dev>
/etc/pihole and /var/log/pihole are owned by the pihole user, who can
replace any file in them with a symlink. pihole -d runs as root and read
the logs and config files there with head/tail/sed, wrote the debug log
with a plain redirect and then chmod/chown-ed it by path, so a planted
symlink made root copy any file into the (pihole-readable) debug log, or
truncate, overwrite and re-own any file (GHSA-jvg9-fm8x-2jqq).
Do that work as the pihole user via setpriv instead, as
pihole-FTL-prestart.sh and updatecheck.sh already do:
- read files below those directories as that user
- query gravity.db and pihole-FTL.db as that user (their paths come
from pihole.toml, and pihole-FTL runs as that user anyway)
- write the debug log as that user, removing whatever is at the path
first, with mode 0640; the root chmod/chown is gone
- upload it to tricorder as that user
A symlink can then only lead to what the pihole user can already reach.
The debug log is now owned by pihole:pihole instead of root:pihole.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <github@promofaux.dev>
Parse the quiet flag at the script level so flush_log() does not check a shadowed $* on manual flushes, and print the 'Rotated logs' completion line after logrotate in once mode.
Fixes#6289
Signed-off-by: Chris Sommers <chris.sommers@icloud.com>
Since the privilege drop was added to updatecheck.sh, the script re-runs
itself as the pihole user via `su`, which keeps the caller's working
directory. When started from cron the working directory is /root (mode
0700), which the pihole user cannot read. `git ls-remote` in
get_remote_hash() then fails repository discovery with "fatal: error
reading '/root/.git'", and GITHUB_CORE_HASH, GITHUB_WEB_HASH and
GITHUB_FTL_HASH are written as null.
Change to / at the start of the script so that both the root and the
re-executed pihole invocation start from a directory every user can read.
Fixespi-hole/docker-pi-hole#2084
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
`/run/pihole-FTL.pid` is owned by the `pihole` user, but root reads it. `getFTLPID()` only rejected values starting with a non-digit, so `1+a[$(cmd)]` passed and was executed by the arithmetic `[[ ... -eq ... ]]` checks in `pihole`. The logrotate `postrotate` sent `kill -USR2` to whatever the file contained, including `-1`.
1. `getFTLPID()` accepts only a plain number whose `/proc/<pid>/comm` is `pihole-FTL`.
2. The logrotate `postrotate` uses `getFTLPID()`. The installer swaps this line in existing files, which are otherwise never replaced.
Signed-off-by: DL6ER <dl6er@dl6er.de>
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>
- updatecheck.sh: reword the publish comment to refer to mv/rename(2) and
place it directly above the mv call; use chmod 0644
- pihole-FTL-prestart.sh: replace the loop/rm/no-op redirect with a single
documented su call using touch and chmod
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
The `pihole updatechecker` cron job and pihole-FTL-prestart.sh both ran as
root and created/truncated/chmod-ed /etc/pihole/versions on a fixed path
without O_NOFOLLOW. Since the pihole user owns /etc/pihole, a compromised
pihole account could replace `versions` with a symlink to any root-owned
file and have root truncate it, relax its mode, or create it. The chmod
step also allows exposing e.g. /etc/shadow. See GHSA-xch2-4qxw-g5vj.
- pihole.cron: run `updatechecker` as the pihole user (like updateGravity),
so root no longer writes into /etc/pihole from cron.
- updatecheck.sh: build the file in a mktemp'd temp file and rename() it
into place; a symlink at the target is replaced, not followed.
- pihole-FTL-prestart.sh: create/permission `versions` (and, same bug
class, `dhcp.leases`) as the pihole user via `su`, neutralising any
planted symlink. A symlink to a root-owned file then only yields EPERM.
- basic-install.sh / uninstall.sh: register the root-owned Core and Web
repos in git's system-wide safe.directory list so the now-unprivileged
updatechecker can still read local versions.
Behaviour change: /etc/pihole/versions is now consistently mode 0644 after
a service (re)start; the find in prestart previously left it 0640, against
the file's own comment and updatecheck.sh's own chmod 644.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
`pihole -f` stopped FTL with `service` to delete the last 24 hours from the database. Docker has no `service` command, so nothing was deleted there and the queries stayed in memory. `POST /api/action/flush/logs` does the same inside FTL while it keeps running: it truncates `pihole.log`, drops the queries from memory and deletes them from the database.
`PI_HOLE_SCRIPT_DIR` is no longer `readonly`, as `api.sh` sets it as well.
Signed-off-by: DL6ER <dl6er@dl6er.de>
`files.gravity` and the `files.log.*` paths come from `pihole.toml`, which the `pihole` user owns. A root `pihole -g` used them for `chown`, `mv` and database creation, and a root `pihole flush` truncated and `chmod`ed them, so the `pihole` user could take over any directory or clobber any file.
1. `gravity.sh` now re-executes itself as `pihole` when started as root. The weekly cron, the web interface and the installer already run it that way. `/etc/pihole` is handed to `pihole` first, as Docker runs gravity before the FTL prestart script does so.
2. `piholeLogFlush.sh` writes the log files as `pihole`. The rest of the manual flush still needs root to stop FTL.
Signed-off-by: DL6ER <dl6er@dl6er.de>
The template is only used for a database created from scratch, so without a
migration step the indexes, the `WITHOUT ROWID` tables and the corrected update
triggers would reach new installations only.
`20_to_21.sql` rebuilds the three junction tables and `info` through a
temporary table - an `ALTER TABLE ... RENAME` would reparse the schema while
the views still point at the table being replaced - adds the two group indexes
and replaces the triggers that matched by content. The result has the same
schema as a database created from the template.
Signed-off-by: DL6ER <dl6er@dl6er.de>
Our config moved into /etc/logrotate.d/pihole a while back, so the
system's own scheduled logrotate now picks it up too. Our nightly and
@reboot forced runs were still using a private state file though, so
the system's run and ours each rotated the same logs independently,
unaware of each other.
Drop the private state file and use logrotate's default one instead,
so both sides agree on what's already been rotated. Also clean up the
now-stale /etc/pihole/logrotate and private state file left behind by
older installs.
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
The pihole-FTL-prestart.sh and pihole-FTL-poststop.sh scripts are
executed as root by systemd (via the '+' prefix). Both previously read
the PID file path from pihole.toml via getFTLConfigValue — a file the
pihole user can write to directly. An attacker with pihole-user access
could set files.pid to an arbitrary path and trigger a service restart
to cause root to delete then recreate any file on the system, enabling
local privilege escalation.
Fix by inlining the hardcoded path /run/pihole-FTL.pid directly in
each hook, removing any dependency on user-controlled config. The same
hardening is applied to the SysV init script for consistency.
See: GHSA-6w8x-p785-6pm4
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This was most likely added accidentally (during a refactor in 2017)
Both the 2017 and current version use /opt/pihole/COL_TABLE, as defined earlier and sourced earlier in the code.
Remove PIHOLE_COLTABLE_FILE from REQUIRED_FILES since /usr/local/bin as well as /opt/pihole are never checked
Signed-off-by: darkexplosiveqwx <101737077+darkexplosiveqwx@users.noreply.github.com>
- use `--no---` and `--yes---` to make sure the strings won't match user
comments or parts of domains
- also use `-ALLOW-` and `-BLOCK-`
Also reduce the domain column to 90 characters
Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
- Domains table ("type" column): replace 0, 1, 2 and 3 with
"exact-allow", "exact-deny", "regex-allow" and "regex-deny"
- All tables: use yes/no for "enabled" columns
Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
Use bash-internal globs and parameter expasion in preference to
assignment from output of ls or basename per file/directory.
When displaying file contents, call sed once and preprocesses the entire
file (eg pihole.toml), rather than spawning a new sed process for every
line of the file.
When checking services, call awk once to extract all data for each ip:port
pair, rather than three times.
Signed-off-by: Rob Gill <rrobgill@protonmail.com>