Products › lamco-dbus-tools › Docs
dbuskeep kernel: eBPF stamps at every D-Bus socket
dbuskeep kernel, the kernel part: eBPF hooks that stamp each D-Bus message at every socket it crosses, who gets the events, the refusal, and two capabilities.
Describes version 0.4.1. The source of truth is kernel/dbus_hooks.bpf.c, src/kernel/ and
src/lens/kernel_legs.rs.
The kernel part works at layer 7. Hooks in the kernel stamp each D-Bus message at every socket it crosses, which is the only way to
tell the time a call spent in the bus from the time it spent in the service. It's optional, separate and
privileged: Every other program works without it, none loads anything into the kernel, and a build without
the kernel feature has the command but says it hasn't the part.
What it hooks
| Hook | Where | What it does |
|---|---|---|
fentry and fexit |
unix_stream_sendmsg |
Each write to a local stream socket: At the start, the buffers are noted when the socket is a bus's; at the end, each message that starts in the bytes written is reported |
fentry and fexit |
unix_stream_recvmsg |
The same for each read |
| LSM | unix_stream_connect |
Only with --refuse: A connection from a listed cgroup to a listed bus is refused with EPERM |
A socket is a bus's when its own address, or its peer's, is a path or abstract name the service was told of. The bus's side of a connection is bound to the bus's address, and a client's peer is that side, so both ends of every connection to the bus are found. The socket's own address is looked up first, then its peer's: A client's socket can have an address of its own, as sd-bus's does, since the kernel gives one to a socket that asks for its peer's credentials before connecting. A socket with an address that isn't a bus's, of a length a bus has, costs two lookups.
Every write and read on every local stream socket of the machine passes the hooks while the service runs, including those of programs that have nothing to do with D-Bus. That's the cost of this layer, measured in the measurements of 2026-12-01. A socket whose address is of a length no watched bus has is passed without its address being copied or looked up: The hooks keep a table of the watched buses' address lengths (before and after).
What an event holds
For each message that starts in the bytes a process wrote or read on a bus socket:
| Field | Meaning |
|---|---|
direction |
sent or received |
pid |
The process that wrote or read |
cgroup |
Its cgroup's id |
sock, peer |
The kernel's addresses of the socket and its peer: Used only to pair the two ends of a connection |
offset |
Where in that direction's bytes the message starts, from when the hooks first saw the socket |
serial, reply_serial, type, flags |
From the message's header. reply_serial is read only when the whole header is in one write or read: A reader that takes a header in pieces gives 0 there, so an answer is joined to its call by the writing side's stamp |
length |
Header and body |
It has two stamps on the kernel's CLOCK_MONOTONIC, the clock the recorder stamps with: When the write or
read began, and when it ended. Nothing of a message's content is kept: Not its body, its path, its interface
or its member. Those are in the recorder's own records of the same messages, when it's listening.
A message is found by its fixed 16-byte header and the length it gives, so the hooks follow where the next
header of each direction of each socket starts. A connection's first bytes are the authentication lines; the
messages start after the last line ending in a call's first bytes. A connection already running when the
hooks were loaded is taken up at the first write or read that starts with a valid header. Where a header is
due and isn't there, that direction isn't followed again until one is, and the event says so (lost). A
header split across two buffers of one write is skipped and counted.
Who gets what
The hooks see every bus they're told of, so the service decides who gets which events:
| Bus | Its events go to |
|---|---|
The system bus (/run/dbus/system_bus_socket) |
Root and the group lamco-dbus-monitor, as its messages do from dbuskeep run |
| A bus whose socket is a path | The user who owns the socket, and root. Where the service can't look at the socket, as the unit can't inside a user's /run/user/UID, the user of the bus's own process: The one process at an end of every connection seen from both ends, the asker's own set aside, its user read from /proc/PID/status. Until the bus's events show which process that is, events is refused with why |
| A bus whose socket is abstract | Root only: An abstract socket has no owner to check |
A recorder tells the service to watch its bus (watch) before it starts listening and asks for that bus's
events of its own span when it stops (events). A bus stays watched until the service stops. The hooks hold 64 buses, so a user other than root may add at most 8; one asking for more is refused and told so, and root may add more or restart the part. Run against the loaded part on Fedora 44: Eight granted, the ninth refused (docs/measurements/watch-limit/).
The refusal
--refuse CGROUP=SOCKET refuses connections to the bus at SOCKET from the cgroup CGROUP, its path under
/sys/fs/cgroup, and from every cgroup under it. The refusal happens before the bus sees the connection, so
the program gets EPERM from connect and the bus nothing. It's per connection, not per message: The bus
already decides per message, and answers properly.
It needs bpf among the kernel's active security modules (cat /sys/kernel/security/lsm). Debian 13 and
Fedora 45 have it; Ubuntu 24.04 hasn't. There the kernel lets the refusal attach and never calls it, so the
service reads the list before it loads anything and, with --refuse, stops as it starts and says why, rather
than run without its rule (run on Ubuntu 24.04, docs/measurements/refuse-without-bpf/).
A refusal by any security module before it in the chain stands.
Privilege
It needs exactly two capabilities: CAP_BPF and CAP_PERFMON. The kernel counts the LSM program among the tracing
programs, so the refusal needs nothing more. Root is never needed, and the measurements ran the service as an
ordinary user given those two. packaging/systemd/system/dbuskeep-kernel.service runs it so, with every
other capability out of its bounding set.
The kernel must describe its own types (/sys/kernel/btf/vmlinux), and the hooks need the kernel's
trampolines: x86_64 from kernel 5.5, 64-bit ARM from 6.0. The 64-bit ARM programs have run only under emulation.
Commands
| Command | What it does |
|---|---|
dbuskeep kernel [--window S] [--max-events N] [--refuse CGROUP=SOCKET]... [--socket PATH] |
Load the hooks and keep events for --window seconds (600), at most --max-events (2,000,000) |
dbuskeep kernel-status |
Its process, buses watched, events held and read, and three counts: Ring full, header not found, header split |
dbussee record --kernel ... |
Also take this bus's events for the recording's span, as kernel-sockets records |
dbustell timing REC --caller ACTS --split |
Each call split at every socket it crossed |
LAMCO_DBUS_TOOLS_KERNEL_SOCKET names the service's socket in place of
/run/lamco-dbus-tools/keep/kernel.sock.
The split
How to time a call shows it in use. Each call is followed through eight stamps, the end of each write or read:
- The caller writes the call.
- The bus reads it.
- The bus writes it to the service.
- The service reads it.
- The service writes its answer.
- The bus reads that.
- The bus writes it to the caller.
- The caller reads it.
The seven gaps sum to the kernel's span of the call. dbusdo stamps before it writes and after it reads, so
that span lies inside the caller's: The caller's figure is the kernel's span, from the start of its write to
the end of its read, and its own time. A call whose stamps fall outside the caller's span was joined wrongly,
and is counted and left out.
A message becomes readable at some moment during its write, and each stamp is the end of a call, so a read can end before the write that fed it. Each gap from a write to its read is known to within that write's own length, which the output states.
Why it's built so
- Hooks, not a module: A module needs signing under Secure Boot, taints the kernel and can't be delivered on several immutable distributions.
- The in-kernel programs are C: The toolkit has no
unsafe, and programs written in Rust for the kernel need it at every helper. They're licensed GPL-2.0 OR MIT, which the kernel's GPL-only helpers require; the rest of the toolkit stays MIT or Apache-2.0. - The loader is aya 0.14.0: It's the last release that builds with this toolkit's Rust. aya has no attach
point for
cgroup/connect_unix, the other way to refuse a connection, which would also needCAP_NET_ADMIN; the LSM hook keeps the part to two capabilities. - One object for both architectures: Both are little-endian, the programs read their arguments the same way on each, and every kernel type is relocated against the running kernel's when it loads.