Eat.Sleep.Learn.Repeat

My way of reminding my future-self about stuff

LXD and Pihole v4 problem

Recently (2018-08-06), the PiHole team released version 4.0 of their awesome DNS/Add filtering solution. I have been using it for a few months now, in conjunction with OpenDNS as upstream. After reading the release notes, and a few happy users opinions with a seamless upgrade experience, I decided to upgraded myself as ususal.

RaspberryPi vs Ubuntu LXD Container

Being a nerd, I have (a few) 24x7 running computers at home. Being also economically and environmentally conscious, power consumption is an important factor to me. I even have a quite detailed (sub-second) power consumption monitoring solution I developed myself that will probably will be covered on a future post. PiHole is supposed to be run inside a low-power, low-cost RaspberryPi SoC computer. It consumes as low as 2W (idle), and up to 5.1W (400% cpu, 4-cores). My 24x7 home server, running Ubuntu linux server, provides some services that a RaspberryPi cannot fullfill. It also has a very constrained power consumption: around 50W tipically. So an additional 2W didn’t look like much, but why incur in the management overhead/connections/cost, when I can run PiHole inside a LXD container in the existing computer for free?

Ubuntu LXD containers

For the uninitiated, Ubuntu LXD containers are a way to run a full linux environment, isolated from the host linux environment (the one running in the bare-metal server), sharing the linux kernel and a few other dependencies. It is kind of like a VM (VMware, Hyper-V,Xen), but with less overhead and resource consumption. It also boots orders of magnitude faster than a traditional VM, as there is no need to initialize the OS kernel, nor any of the virtual hardware. LXD is developed and maintained by Canonical/Ubuntu, and is quite simple to setup and use (light reading required). So, It looked to me like the perfect match to run PiHole. The result: perfectly running 24x7 PiHole, with almost 0% additional overhead, both power/hardware/€€. And it was also almost maintenance-free… until today. :(

PiHole v4.0, upgrade failed

PiHole v4.0 upgrade script completed fine, but the new DNS Server daemon (pihole-FTL) will not automatically come up. After some complains from family members about the non-functioning internet at home, I rolled back the Pihole LXD container to the pre-upgrade snapshot and we were back up-and-running. Always have a rollback plan! ;)

Root cause analysis

So, the main new feature of PiHole v4.0 is precisely the pihole-FTL DNS daemon (based on dnsmasq). And it aws the new daemon not automatically staring, so it was either a failing startup script, or a carshing daemon. PiHole status check showed this:

root@pihole:/usr/bin# pihole status
  [✗] DNS service is NOT running

Looking at systemctl status, showed daemon as “active (exited)”:

root@pihole:/usr/bin# systemctl status pihole-FTL
● pihole-FTL.service - LSB: pihole-FTL daemon
   Loaded: loaded (/etc/init.d/pihole-FTL; bad; vendor preset: enabled)
   Active: active (exited) since Wed 2018-08-08 09:41:12 UTC; 3min 24s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 9366 ExecStop=/etc/init.d/pihole-FTL stop (code=exited, status=0/SUCCESS)
  Process: 9435 ExecStart=/etc/init.d/pihole-FTL start (code=exited, status=0/SUCCESS)

Aug 08 09:41:12 pihole systemd[1]: Starting LSB: pihole-FTL daemon...
Aug 08 09:41:12 pihole pihole-FTL[9435]: Not running
Aug 08 09:41:12 pihole pihole-FTL[9435]: Failed to set capabilities on file `/usr/bin/pihole-FTL' (Invalid argu
Aug 08 09:41:12 pihole pihole-FTL[9435]: The value of the capability argument is not permitted for a file. Or t
Aug 08 09:41:12 pihole su[9483]: Successful su for pihole by root
Aug 08 09:41:12 pihole su[9483]: + ??? root:pihole
Aug 08 09:41:12 pihole su[9483]: pam_unix(su:session): session opened for user pihole by (uid=0)
Aug 08 09:41:12 pihole pihole-FTL[9435]: dnsmasq: failed to create listening socket for port 53: Permission den
Aug 08 09:41:12 pihole systemd[1]: Started LSB: pihole-FTL daemon.

The log line showing “failed to set capabilities on file /usr/bin/pihole-FTL” error message, is the key to solving the problem. The log also shows that the daemon is not running, because it does not have propper permissions to bind to port 53 to provide the DNS service. It turns out that in the last version of PiHole instead of running dnsmasq with root privileges (not recommended), they moved to using Linux kernel capabilities feature. This is a good thing, as it provides a more fine-grained control about what elevated capabilities are needed specifically for the daemon, instead of providing full root-level access to a network-listening daemon (definately NOT recommended).

The line from the init script that tries to provide the required permissions is:

setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN+eip "$(which pihole-FTL)"

Which is syntactically correct… so why was it failing in my machine?

The solution

It turns out that Unprivileged LXD containers cannot set linux kernel capabilities, which of course, makes sense because container and host are both sharing the kernel.

Stephane Graber, current LXD maintainer at Ubuntu, has a blog post especifically about Linux Kernel capabilities and LXD support and security implications. There is also this closed issue explaining a similar problem to this.

I could either make the full container a privileged one, or just solve the problem inside the container. I opted for the second, as it was faster, and made things no worse than before. I just set the SETUID bit on the pihole-FTL binary and it was working again.

chmod +s /usr/bin/pihole-FTL

SECURITY DISCLAIMER: I’m not advising you to do this on your machine. Having an open network-daemon running as root is NEVER recommended

Also, if you come home and are provided with free internet access to the guest-wifi, be kind and do not start fuzzing my PiHole DNS server. Now that I think of it, what an appropiate name in this context! ;)


Share