Products › lamco-dbus-tools › Interfaces
unix_stream_connect and the bpf LSM: Keeping a program off a bus
unix_stream_connect with the bpf LSM refuses a cgroup's D-Bus connections before the bus sees them, and lamco-dbus-tools does it with dbuskeep kernel --refuse.
Sometimes the right answer is that a program shouldn't reach a bus at all: Not to call a service, not to watch one, not even to say hello. Test runners that start programs nobody has read are the usual case. A bus's policy decides message by message once a program is connected, and a sandbox has to be built to fit each program. The Linux kernel's bpf security module offers a third way: A program on the unix_stream_connect hook that refuses the connection itself. lamco-dbus-tools uses it in dbuskeep kernel --refuse, which keeps every program in one control group off one bus.
What it is
Linux security modules (LSMs) are hooks at the points where the kernel decides whether an operation may go ahead. unix_stream_connect is the hook called when a process connects a local stream socket to another, which is how every client reaches a D-Bus bus. The kernel's documentation on LSM BPF programs describes how a BPF program can be attached to such a hook and return an error to refuse the operation.
The condition that catches people: A BPF program on a security hook runs only where bpf is among the kernel's active security modules, listed in /sys/kernel/security/lsm. On a kernel without it, the kernel lets the program attach and never calls it, so a tool that doesn't check would say it refused and refuse nothing. The repository's reference records that Debian 13 and Fedora 45 have bpf active and Ubuntu 24.04 hasn't.
A refusal at this hook happens before the bus sees the connection. The program gets EPERM from connect, and the bus sees nothing: No connection, no Hello, no log line of its own. It's per connection, not per message, and it stacks with the other modules: A refusal by any security module before it in the chain stands.
Errors you'll meet
The program gets no D-Bus error, since it never reached the bus. What it prints is its library's report of a failed connect. As recorded from busctl, refused by the kernel part: Failed to connect to system scope bus via local transport: Operation not permitted.
How lamco-dbus-tools reads it
dbuskeep kernel --refuse CGROUP=SOCKET refuses connections to the bus at SOCKET from the control group CGROUP, its path under /sys/fs/cgroup, and from every control group under it. It may be given more than once. It reads /sys/kernel/security/lsm before it loads anything, and where bpf isn't active it stops as it starts and says why, rather than run without its rule. The refusal needs nothing beyond the kernel part's two capabilities, CAP_BPF and CAP_PERFMON. dbuskeep kernel-status says whether it refuses any connections. dbussee and dbusdo, run from outside the control group, show the bus still answering everyone else.
dbussee --address unix:path=/var/tmp/keepoff/bus.sock check # the bus, seen from outside the slice
dbusdo --address unix:path=/var/tmp/keepoff/bus.sock call org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetId # still answered here
dbuskeep kernel --refuse /user.slice/user-1000.slice/user@1000.service/app.slice/untrusted.slice=/run/user/1000/bus # keep the slice off the user's bus
dbuskeep kernel-status # loaded, and refusing connections by its rules
In practice the kernel part is loaded first and the other programs run while it holds the rule.
Where it has been run
The README's run record lists dbuskeep kernel --refuse where the kernel doesn't enable the bpf security module, on Ubuntu 24.04 with kernel 6.8, on 2026-12-02: It loaded and said it refused, and let the connection through, as the kernel attached the refusal and never called it. It's been fixed since: It now stops as it starts and says why. The use case "A program kept off a bus" was run from its own recording, among the use cases run between 2026-11-30 and 2026-12-02.
Related
- eBPF on the bus's sockets, the rest of the kernel part.
- A program kept off a bus, run for real.
- The kernel-level part: dbuskeep kernel.
- LSM BPF programs, in the kernel's documentation.