Products › lamco-dbus-tools › Use cases

Operation not permitted at connect: Keeping a program off D-Bus

Operation not permitted connecting to D-Bus, on purpose: Refuse every connection from one systemd slice to one bus at connect, with the kernel part of dbuskeep.

Programs started in one slice are refused before the bus sees them, and busctl in the slice reports:

Failed to connect to system scope bus via local transport: Operation not permitted

Here the refusal is the point: With lamco-dbus-tools, dbussee recorded the refusal beside the messages, and the kernel part of dbuskeep refused the connection by its rule.

A real run on 2026-12-02 on Debian 13 (a test VM, kernel 6.12.118, dbus-daemon 1.16.2), with a development build of lamco-dbus-tools, version 0.1.0. The bus is a private one and the slice is the run's own. run.sh ran it, and run.txt is its output as printed.

The problem

A test runner starts programs nobody has read, and they mustn't reach the developer's bus: Not to call a service, not to watch one, not even to say hello. A sandbox would do it, but these programs run as the developer, in the developer's session, and a sandbox that fits each one is more than the job is worth. What's wanted is one rule: Nothing started under this slice connects to that bus.

The kernel part can refuse a connection before the bus sees it. Here a private bus at /var/tmp/keepoff/bus.sock stands for the bus to protect, and the slice untrusted.slice of the user's service manager for where the test runner starts its programs.

The rule

The slice's cgroup, as the service manager has it:

$ systemctl --user show -p ControlGroup --value untrusted.slice
/user.slice/user-1000.slice/user@1000.service/untrusted.slice

The kernel part, run as the user with only CAP_BPF and CAP_PERFMON, as enabling the kernel part shows, with a window of two minutes, room for 50,000 events, and the rule:

$ sudo setpriv --reuid="$(id -u)" --regid="$(id -g)" --init-groups \
    --inh-caps=-all,+bpf,+perfmon --ambient-caps=-all,+bpf,+perfmon --bounding-set=-all,+bpf,+perfmon \
    --no-new-privs dbuskeep kernel --socket /var/tmp/keepoff/kernel.sock --window 120 --max-events 50000 \
    --refuse /user.slice/user-1000.slice/user@1000.service/untrusted.slice=/var/tmp/keepoff/bus.sock &
dbuskeep kernel: hooks on local stream sockets loaded, refusing 1 connections, keeping 120.00 s of events, answering at /var/tmp/keepoff/kernel.sock
$ export LAMCO_DBUS_TOOLS_KERNEL_SOCKET=/var/tmp/keepoff/kernel.sock
$ dbuskeep kernel-status
dbuskeep kernel 0.1.0 (process 6188) at /var/tmp/keepoff/kernel.sock: 1 buses watched, refusing connections by its rules, 0 events held of 0 read, 0 let go past 120 s. The ring was full 0 times; a message was due and not found 0 times, and a header was split across two buffers 0 times.

--refuse needs bpf among the kernel's active security modules, which Debian 13 has.

Outside the slice, and inside it

The same call, from the developer's shell and from a program started in the slice:

$ dbusdo --address unix:path=/var/tmp/keepoff/bus.sock call org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetId
[10bd45f589974339af55ea866b0fc2ec]
answered in 120.86 µs at the caller
$ systemd-run --user --quiet --scope --slice=untrusted -- dbusdo --address unix:path=/var/tmp/keepoff/bus.sock call org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetId
call org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetId: no connection to the address unix:path=/var/tmp/keepoff/bus.sock bus: failed to connect to `unix:path=/var/tmp/keepoff/bus.sock`: Operation not permitted (os error 1)

The second exits 1. The bus never saw it: connect itself was refused with EPERM, so no authentication began and the bus logged nothing. Any program gets the same, whatever library it uses. busctl in the slice:

Failed to connect to system scope bus via local transport: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)

Those are busctl's own words, and its guess about why is wrong: The bus it was given isn't the system bus, and no other user is involved. The cause is the rule.

What the kernel part saw

A recording with the kernel's stamps while a program outside the slice makes 50 calls and one inside it tries once:

$ dbussee --quiet --address unix:path=/var/tmp/keepoff/bus.sock record --all --kernel --no-host --duration 5 --out rec.json &
$ dbusdo --quiet --address unix:path=/var/tmp/keepoff/bus.sock call --repeat 50 org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetId
$ wait

Its kernel-sockets records, counted by kind, and the dissector's own line:

kernel-sockets: ran - 480 events from the kernel's hooks on /var/tmp/keepoff/bus.sock, each stamped by the kernel at the end of the write or read on CLOCK_MONOTONIC; 4 times where a message was due the bytes weren't one, on any bus
kernel-sockets records by member: {'lost': 4, 'message': 475, 'refused': 1}

The refusal is in the recording beside the messages, with the process and cgroup that tried, so a later reader of the capture knows that something was kept off and when. The four lost records are messages the hooks couldn't follow: Their headers came split across two buffers, which the hooks don't join (enabling the kernel part). kernel-status counts them the same way:

$ dbuskeep kernel-status
dbuskeep kernel 0.1.0 (process 6188) at /var/tmp/keepoff/kernel.sock: 1 buses watched, refusing connections by its rules, 492 events held of 492 read, 0 let go past 120 s. The ring was full 0 times; a message was due and not found 4 times, and a header was split across two buffers 4 times.

An earlier run of the same script that day had none. It depends on how the writes happen to fall.

What it shows

  • One rule keeps every program of a slice, and of the slices under it, off one bus, at connect, before the bus or any library is involved.
  • The developer's own programs, outside the slice, are untouched: The call above took 121 µs at the caller.
  • The refusal is recorded with the messages, and dbuskeep kernel-status says that rules are in force.
  • --window and --max-events bound what the part keeps: Two minutes or 50,000 events here, the oldest let go first.

The rule ends with the kernel part. Stopped with SIGTERM, its hooks are gone: /sys/fs/bpf had no entries before the run or after it, and no process of it was left.

Limits

  • The rule names a socket path. A program that reaches the same bus at another address, an abstract one or through a proxy, isn't refused by it.
  • It's for programs that run as the developer. A sandbox that should hide the bus whole, or let a program reach some names only, is Flatpak's or another sandbox's job; a calculator and what its sandbox lets it reach reads those.
  • Where bpf isn't an active security module, Ubuntu 24.04 among them, --refuse can't be used, and the kernel part stops as it starts, saying so. Before 2026-12-02 it didn't: On Ubuntu 24.04 it said it refused and let the connection through (docs/measurements/refuse-without-bpf/).

Related