Skip to content

Caps - #1814

Open
yubiuser wants to merge 4 commits into
developmentfrom
caps
Open

Caps#1814
yubiuser wants to merge 4 commits into
developmentfrom
caps

Conversation

@yubiuser

Copy link
Copy Markdown
Member

Improves the capability check. Inspired by #1085 (comment)

It does three things

  1. Warn about missing NET_ADMIN even when this would be the only cap that would be checked and could not be granted.
  2. Allow to start even if CAP_STR is empty. See the linked comment above. The error was wrong in the first place, as it did not check if we failed to grant the caps but if CAP_STR is empty. And it is empty if the caps are not available to the container.
  3. Split the check for the return code of setting the caps of pihole-FTL from checking the user. This should be a two-step process.

Comment thread src/bash_functions.sh Resolved
@yubiuser
yubiuser requested a review from PromoFaux April 21, 2025 14:38
@PromoFaux

Copy link
Copy Markdown
Member

@ngrigoriev - could you give this branch a go and see if it solves the issues you mentioned in #1085?

To test, please clone the repository locally, and run the following from within the directory

git checkout caps
./build.sh

https://docs.pi-hole.net/docker/build-image/#using-the-built-image

@dschaper dschaper self-assigned this Jun 6, 2025
@yubiuser

yubiuser commented Jul 1, 2025

Copy link
Copy Markdown
Member Author

@dschaper any review news?

@yubiuser
yubiuser requested a review from a team as a code owner July 2, 2025 20:42
@PromoFaux

Copy link
Copy Markdown
Member

The only way to trigger this is to set the user: element in the compose file (i.e user: pihole)

However, that causes a lot more problems than just being unable to set the caps...

pihole  |   [i] Setting up user & group for the pihole user
pihole  |   [i] PIHOLE_UID not set in environment, using default (1000)
pihole  |   [i] PIHOLE_GID not set in environment, using default (1000)
pihole  | 
pihole  |   [i] Starting FTL configuration
pihole  | chown: changing ownership of '/macvendor.db': Operation not permitted
pihole  |   [i] Assigning password defined by Environment Variable
pihole  |   [i] Starting crond for scheduled scripts. Randomizing times for gravity and update checker
pihole  | sed: can't create temp file '/crontab.txtXXXXXX': Permission denied
pihole  | sed: can't create temp file '/crontab.txtXXXXXX': Permission denied
pihole  | crontab: must be suid to work properly
pihole  | 
pihole  |   [i] Ensuring logrotate script exists in /etc/pihole
pihole  | 
pihole  |   [i] Gravity migration checks
pihole  |   [i] Existing gravity database found - schema will be upgraded if necessary
pihole  |      
pihole  | 
pihole  |   [i] pihole-FTL pre-start checks
pihole  |   [i] Setting capabilities on pihole-FTL where possible
pihole  |   [!] WARNING: No capabilities for pihole-FTL available.
pihole  |            Pi-hole functions may not work as expected.
pihole  |             Please ensure that the container has the required capabilities.
pihole  | 
pihole  | chown: changing ownership of '/var/log/pihole/FTL.log': Operation not permitted
pihole  | chown: changing ownership of '/var/log/pihole/': Operation not permitted
pihole  | chmod: changing permissions of '/var/log/pihole/': Operation not permitted
pihole  | chmod: changing permissions of '/var/log/pihole/FTL.log': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/logrotate': Operation not permitted
pihole  | install: cannot create regular file '/run/pihole-FTL.pid': Permission denied
pihole  | install: cannot create regular file '/var/log/pihole/pihole.log': Permission denied
pihole  | install: cannot create regular file '/var/log/pihole/webserver.log': Permission denied
pihole  |   [i] Starting pihole-FTL (no-daemon) as pihole
pihole  | 
pihole  | Unable to set group list for user: Operation not permitted

@yubiuser

yubiuser commented Sep 2, 2025

Copy link
Copy Markdown
Member Author

The only way to trigger this is to set the user: element in the compose file (i.e user: pihole)

You can simulate it by setting

    cap_drop:
      - CAP_CHOWN
      - NET_BIND_SERVICE
      - NET_ADMIN
      - NET_RAW
      - SYS_NICE
      - SYS_TIME

In your compose file. It will give some errors, but FTL will start.

pihole  |   [i] Setting up user & group for the pihole user
pihole  |   [i] PIHOLE_UID not set in environment, using default (1000)
pihole  |   [i] PIHOLE_GID not set in environment, using default (1000)
pihole  | 
pihole  |   [i] Starting FTL configuration
pihole  | chown: changing ownership of '/macvendor.db': Operation not permitted
pihole  |   [i] Setting FTLCONF_webserver_api_password from file
pihole  |   [i] Assigning password defined by Environment Variable
pihole  |   [i] Starting crond for scheduled scripts. Randomizing times for gravity and update checker
pihole  | 
pihole  |   [i] Ensuring logrotate script exists in /etc/pihole
pihole  | 
pihole  |   [i] Gravity migration checks
pihole  |   [i] Existing gravity database found - schema will be upgraded if necessary
pihole  |      
pihole  | 
pihole  |   [i] pihole-FTL pre-start checks
pihole  |   [i] Setting capabilities on pihole-FTL where possible
pihole  |   [!] WARNING: No capabilities for pihole-FTL available.
pihole  |            Pi-hole functions may not work as expected.
pihole  |             Please ensure that the container has the required capabilities.
pihole  | 
pihole  | chown: changing ownership of '/etc/pihole/tls_ca.crt': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/tls.pem': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/listsCache/list.1.raw.githubusercontent.com.domains.sha1': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/listsCache/list.1.raw.githubusercontent.com.domains': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/listsCache/list.1.raw.githubusercontent.com.domains.etag': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/listsCache': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/versions': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/migration_backup/adlists.list': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/migration_backup': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/pihole.toml': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.4': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.7': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.6': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.10': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.5': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.9': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.8': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups/pihole.toml.2': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/config_backups': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/logrotate': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/pihole-FTL.db': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/gravity_backups/gravity.db.1': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/gravity_backups': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/adlists.list': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/gravity_old.db': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/dnsmasq.conf': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/hosts/custom.list': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/hosts': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/gravity.db': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/tls.crt': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/dhcp.leases': Operation not permitted
pihole  | chown: changing ownership of '/etc/pihole/': Operation not permitted
pihole  | chown: changing ownership of '/var/log/pihole/FTL.log': Operation not permitted
pihole  | chown: changing ownership of '/var/log/pihole/': Operation not permitted
pihole  | install: cannot change ownership of '/run/pihole-FTL.pid': Operation not permitted
pihole  | install: cannot change ownership of '/var/log/pihole/pihole.log': Operation not permitted
pihole  | install: cannot change ownership of '/var/log/pihole/webserver.log': Operation not permitted
pihole  |   [i] Starting pihole-FTL (no-daemon) as pihole
pihole  | 
pihole  | 
pihole  | dnsmasq: cannot open log /var/log/pihole/pihole.log: Permission denied

Using the current :latest image, it will refuse to start. (Also the error message is wrong: no caps were allowed to the container - it did not even try to set them. )

pihole  |   [i] Setting capabilities on pihole-FTL where possible
pihole  |   [!] ERROR: Unable to set capabilities for pihole-FTL.
pihole  |             Please ensure that the container has the required capabilities.

I'm not saying it is a good idea to start FTL without the caps, but some users might have reasons to do so (see here)

@PromoFaux

PromoFaux commented Apr 1, 2026 •

Copy link
Copy Markdown
Member

I'm still not really sure how to test this. Rebasing on latest development changes and following the cap_drop suggestion we get the following:

pihole  | install: cannot change ownership of '/var/log/pihole/webserver.log': Operation not permitted
pihole  |   [i] Starting pihole-FTL (no-daemon) as pihole
pihole  |
pihole  |
pihole  | dnsmasq: cannot open log /var/log/pihole/pihole.log: Permission denied
pihole  |   [!] ERROR: Did not find 'FTL started' message in /var/log/pihole/FTL.log in 30 seconds, stopping container
pihole exited with code 1

@PromoFaux
PromoFaux force-pushed the caps branch 2 times, most recently from 42f9faf to 78ffcbe Compare April 1, 2026 22:14
yubiuser and others added 4 commits April 1, 2026 23:14
…ested capability

Signed-off-by: yubiuser <github@yubiuser.dev>
Signed-off-by: yubiuser <github@yubiuser.dev>
Signed-off-by: yubiuser <github@yubiuser.dev>
Co-authored-by: Adam Warner <me@adamwarner.co.uk>
Signed-off-by: yubiuser <github@yubiuser.dev>
Comment thread src/bash_functions.sh
Comment on lines +257 to +258
echo " Cannot run as non-root."
echo " If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need so many spaces here?

I suggest to use 8 spaces for indentation:

Suggested change
echo " Cannot run as non-root."
echo " If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"
echo " Cannot run as non-root."
echo " If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"

Comment thread src/bash_functions.sh
Comment on lines +263 to 265
echo " [!] WARNING: No capabilities for pihole-FTL available."
echo " Pi-hole functions may not work as expected."
echo " Please ensure that the container has the required capabilities."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.
Use 8 spaces for indentation:

Suggested change
echo " [!] WARNING: No capabilities for pihole-FTL available."
echo " Pi-hole functions may not work as expected."
echo " Please ensure that the container has the required capabilities."
echo " [!] WARNING: No capabilities for pihole-FTL available."
echo " Pi-hole functions may not work as expected."
echo " Please ensure that the container has the required capabilities."

@PromoFaux PromoFaux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the NET_ADMIN/DHCP check ahead of the CAP_STR branch is a good change. Previously, with no capabilities available at all, dhcp.active was never switched off.

Needs changing: exit 1 is no longer conditional on the user

if [[ $ret -ne 0 ]]; then
    echo "  [!] ERROR: Unable to set capabilities for pihole-FTL. "
    if [[ "${DNSMASQ_USER:-pihole}" != "root" ]]; then
        echo "            Cannot run as non-root."
        echo "            If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"
    fi
    exit 1
fi

On development the container only exits when ret != 0 and DNSMASQ_USER != root, which is what makes DNSMASQ_USER=root a working workaround. Here exit 1 sits outside the inner if, so a root user whose setcap fails (e.g. a filesystem without xattr support) now gets a container that refuses to start. The message also still tells non-root users to set DNSMASQ_USER=root, which would no longer help them.

The PR description says this change only splits the return-code check from the user check, so I assume this is unintentional. Moving exit 1 inside the non-root branch should fix it:

if [[ $ret -ne 0 ]]; then
    echo "  [!] ERROR: Unable to set capabilities for pihole-FTL."
    if [[ "${DNSMASQ_USER:-pihole}" != "root" ]]; then
        echo "        Cannot run as non-root."
        echo "        If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"
        exit 1
    fi
fi

Question: starting with an empty CAP_STR

I agree the old message was misleading, since an empty CAP_STR means the capabilities were never available, not that setting them failed. I'm less sure that continuing is helpful when the container runs as the default pihole user.

When I followed the cap_drop reproduction from the comments above on a rebased branch, the container did not start properly. It logged dnsmasq: cannot open log /var/log/pihole/pihole.log: Permission denied and then stopped after the 30 second FTL started timeout. A non-root FTL without CAP_NET_BIND_SERVICE also cannot bind port 53. So the user gets a timeout in place of the current immediate error.

Would it make sense to only warn and continue when DNSMASQ_USER=root, and keep the hard exit otherwise? If there is a concrete use case for running non-root with no capabilities (the #1085 comment?), it would be good to document that and ideally add a test, as I'm still not sure how to exercise it.

Nits

  • Indentation of the new echo lines is inconsistent (11 and 12 spaces). rdwebdesign's suggestions use 8 spaces to match the rest of the file; please apply them.
  • Trailing space in "Unable to set capabilities for pihole-FTL. ".
  • ret is never initialised. It works because an unset variable evaluates as 0 in [[ -ne ]] (and that was already the case), but ret=0 would be clearer while this block is being touched.

Base automatically changed from development to master October 30, 2026 23:45
@PromoFaux
PromoFaux changed the base branch from master to development October 30, 2026 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants