Skip to content

Architecture

MateusBMP edited this page Nov 16, 2026 · 2 revisions

Architecture

 Desktop session                              System (systemd)
 +------------------------+  system D-Bus   +-------------------------------------------+
 | blockads GUI (PyQt6)   |<--------------->| blockadsd   user=blockads, sandboxed       |
 | blockadsctl (CLI)      |  polkit per call|  serves DNS on fds from PID 1, filters via |
 +----------+-------------+                 |  mmap'd artifacts, upstream policy, logs   |
            | systemd D-Bus: start (no pw)  +------^------------------^-----------------+
            | pkexec: firewall.conf only           | LISTEN_FDS        | manifest.json
            v                                      |                   |
  blockads-fw (root, 1 file, stdlib)   blockadsd.socket /       blockads-update (oneshot,
  + blockads-fw-emergency.sh (sh)      blockadsd-ipv6.socket    user=blockads): download
  + blockads-failsafe.service          PID 1 holds 127.0.0.1:153 originals, compile artifacts
  iptables-restore --noflush:          and [::1]:153 (UDP+TCP)   (.timer hourly / .path)
  nat BLOCKADS_DNS, filter BLOCKADS_OUT (v4 + v6)

Components

Component Runs as Does Never does
blockadsd.socket, blockadsd-ipv6.socket PID 1 Hold the privileged port 153 across daemon restarts, so no local user can grab it after a crash —
blockadsd blockads, no capabilities, full systemd sandbox DNS serving, matching, upstreams, logs and statistics, D-Bus API, health probe Parse list text, touch the firewall, use the system resolver, bind sockets
blockads-update blockads, sandboxed oneshot, MemoryMax=768M, low CPU/IO weight Download the original lists over HTTPS, compile them, swap the manifest atomically Read the query log
blockads-fw (+ blockads-fw-emergency.sh, blockads-failsafe.service) root, only from systemd hooks or pkexec Apply, verify and revert iptables rules; edit /etc/blockads-linux/firewall.conf Use the network, import third-party code, read anything blockads can write
blockads (GUI), blockadsctl desktop user Qt6 (QtWidgets) GUI and tray icon; command line Root actions, except through polkit or pkexec

Libraries: jeepney (D-Bus), dnspython (wire format only), httpx + h2 (DoH, downloads), PyQt6, and the standard library. Code is installed in a private directory, /usr/lib/blockads-linux/python/, run with python3 -I.

Interception

When blockadsd.service starts, ExecStartPost runs blockads-fw apply --verify:

  1. It locks, checks that its binaries and firewall.conf are root-owned and not writable by others, and checks that port 153 is privileged and has a listener.
  2. It feeds one iptables-restore --noflush transaction per family and table. Built-in chains are never redeclared, so the admin's policies and other rules are untouched. Our rules live in our own chains:
    • nat BLOCKADS_DNS: redirects UDP/TCP port 53 to port 153. It skips the daemon's own traffic, local destinations (local resolvers such as systemd-resolved or dnsmasq keep split DNS; their upstream queries are still caught) and bypass_cidrs (Tailscale MagicDNS by default).
    • filter BLOCKADS_OUT: rejects DNS over TLS/QUIC on port 853 (block_dot) and, optionally, WebRTC/STUN ports (block_webrtc).
  3. It verifies every rule structurally (-C, rule counts, exactly one jump) and functionally: it sends a DNS probe with a random nonce to 127.0.0.1:9, which only our redirect can answer. On any failure it reverts everything and the start fails (fail-open).
  4. It writes /run/blockads-fw/state.json for the daemon and the GUI.

IPv6 gets the same rules with ip6tables when ::1, ip6 nat and a [::1]:153 listener are all available; otherwise IPv6 is reported as degraded. Both the nft and the legacy iptables backends are supported.

blockads-fw revert runs before every start and after every stop. It removes our jumps and chains in every backend and never stops early. A POSIX sh copy (blockads-fw-emergency.sh, also copied to /run) does the same without Python, so the rules are removed even if the installed files were deleted while running.

Nothing is persisted: the rules exist only in the kernel while the service runs.

Self-healing

  • The daemon probes the redirect every 15 s, and immediately on firewalld reloads, NetworkManager changes and resume from suspend.
  • After three failed rounds it raises an alert and exits; the stop hooks revert, and systemd restarts and re-applies. Flushed rules come back within about 30 s.
  • The systemd watchdog is fed only while the event loop and an in-process query through the real socket work. A hang means kill, revert, restart.
  • After six failures in ten minutes the unit stays failed, the tray turns red and offers Retry.
  • An upstream outage never removes the rules: the upstream policy falls back to the network's DNS, still filtered.
Failure Outcome
Crash or kill -9 PID 1 keeps the socket; ExecStopPost reverts at once, then restart and re-apply
Hang Watchdog, kill, revert, restart
Rules flushed (iptables -F, nft flush ruleset, firewalld reload) Probe detects it, restart, re-apply
Upstream down, captive portal, wrong clock Fallback to the network DNS, still filtered
Port 153 taken or not privileged No rules applied; the GUI shows the cause
Python or package upgrade while running The daemon notices and restarts
Power loss mid-apply Rules and /run vanish; the next start reverts leftovers

DNS pipeline

Each query goes through, in order: validation; the health probe and the Firefox canary use-application-dns.net (always NXDOMAIN, so browsers do not switch to their own DoH); pause and trusted networks; preferred IP type; an internal allowlist (NetworkManager's connectivity check, the list hosts); the optional DoH-server blocklist; your allow and block rules; SafeSearch and YouTube Restricted; the filter lists; then the upstream. Any internal error answers SERVFAIL; the daemon never crashes on bad input.

Blocked names are answered with 0.0.0.0/:: by default, or NXDOMAIN or REFUSED.

D-Bus API and permissions

Bus name io.github.mateusbmp.BlockAds1. Every call is checked with polkit:

Action Active local user Examples
view allowed status, statistics, catalog, settings, "would this be blocked?"
view-logs allowed query log, top domains
tighten allowed add a block rule, enable a list, update now, resume
weaken admin password (kept for a few minutes) pause, disable a list, allow a domain, change the upstream, trusted networks, custom list URLs, restore a backup
firewall admin password blockads-fw set-config (through pkexec)

Inactive and remote sessions need the admin password for everything except view.

Clone this wiki locally