Module 6 · Lesson 20 of 108

Policy, cost, and diagnostics for D-Bus

Restrict access to a D-Bus service, verify the policy across Linux users, assess the cost on an embedded target, and diagnose message exchanges.

  • Beginner to advanced
  • 9 min read
  • 28 min lab
What you will need

Ubuntu 24.04 in a VM or WSL2 with a running system bus, sudo access, two distinct non-root test accounts, the preceding lesson's server configured for the system bus, dbus-send, dbus-monitor, busctl, and Bustle for graphical capture review

Your place in the module
By the end you can 4 objectives
  • Configure system bus rules for name ownership and method calls.
  • Verify that an approved user receives a reply and a second user receives an access denial.
  • Compare D-Bus, Unix domain sockets, and vsomeip against product requirements.
  • Inspect a live interface and correlate a captured call with its reply.
Video coming soonPolicy, cost, and diagnostics for D-Bus
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Writing a D-Bus server and client
  • The server registers the object before requesting its well-known name.
  • A D-Bus activation descriptor maps the well-known name to a program or a systemd unit.
  • The proxy call timeout limits the client's wait for a reply.
01

Restrict name ownership and method calls

5 min read

A working server and client raise two questions the last lesson left open: who is allowed to use the bus, and is D-Bus the right weight for an embedded product, and how do you watch it in production.

Consider a device with a diagnostics program and a service that can change hardware settings. Both programs may need access to the system bus, but only the control program should be able to request a change. The local dbus-daemon manual describes two independent controls for this case. An ownership rule governs which account can claim the service name. A send rule governs which messages a caller can send to the service connection. A program can connect successfully and still be denied when it sends a particular request.

The daemon reads XML configuration with a <busconfig> root element. The standard Ubuntu system bus configuration is /usr/share/dbus-1/system.conf. Use /etc/dbus-1/system.d/ for a local policy fragment on the lab system. Inside a <policy> element, <allow> and <deny> specify the decision for a match. A context="default" policy applies before user-specific policies. Within the applicable rules, the last matching rule for the operation determines the decision. Mandatory policies are applied after user-specific policies. Review the other loaded fragments for overlapping rules whenever you need to establish the effective policy.

Separate policy decisionsServerrequestsnameCheckownershipClientsends callCheck sendpolicyExit from step 4: send deniedReturn access errorForwardpermittedcallServerrequestsnameCheckownershipClientsends callCheck sendpolicyExit from step 4: send deniedReturn access errorForwardpermittedcall
The bus applies distinct rules when the server claims its name and when a client sends a call

This example uses a dedicated test service with one read-only application method. Substitute the name, path, interface, and member from your server in the example and commands. Use a name dedicated to the lab so an existing package policy does not affect the result. The accounts embserver and embclient must exist before the bus reads the file. The example allows embserver to own the name and gives embclient permission to call GetStatus and request introspection. Save the policy as /etc/dbus-1/system.d/embeddex-status.conf with root ownership and permissions that prevent ordinary users from changing it.

xml
<busconfig>
  <policy context="default">
    <deny own="com.embeddex.Status"/>
    <deny send_destination="com.embeddex.Status"
          send_type="method_call"/>
  </policy>
  <policy user="embserver">
    <allow own="com.embeddex.Status"/>
  </policy>
  <policy user="embclient">
    <allow send_destination="com.embeddex.Status"
           send_type="method_call"
           send_path="/com/embeddex/Status"
           send_interface="com.embeddex.Status"
           send_member="GetStatus"/>
    <allow send_destination="com.embeddex.Status"
           send_type="method_call"
           send_path="/com/embeddex/Status"
           send_interface="org.freedesktop.DBus.Introspectable"
           send_member="Introspect"/>
  </policy>
</busconfig>

The attributes in each allow rule must match together. A different path or member falls outside that permission: in the test run, embclient calling org.freedesktop.DBus.Peer.Ping on the service was denied, and so was Properties.GetAll. A destination rule matches the connection that owns the named service, so addressing that connection by its unique name does not sidestep the rule. Keep this example service on its own bus connection because the destination rule can also affect other names owned by that connection. The example restricts incoming method calls to the service. Signals and permission to receive messages are governed separately. The standard system bus policy allows the requested reply to a permitted call.

A bus rule can inspect headers such as the interface and member. The service still needs to validate arguments and enforce permissions that depend on the requested operation or current device state. If two programs run as embclient, the user rule grants them the same permissions. Use a separate account when the diagnostics program and the control program require different access.

On the lab VM or WSL2 instance, ask the system bus to reread its configuration. Check that the administrative call succeeds, then start the server as embserver on the system bus and verify that it acquires the service name. If the server was already running, relaunch it in the lab so it requests the name under the new policy. Keep the server running for both client tests. Use an existing non-root login distinct from both service and client accounts in place of otheruser.

bash
sudo dbus-send --system --print-reply \
  --dest=org.freedesktop.DBus /org/freedesktop/DBus \
  org.freedesktop.DBus.ReloadConfig

sudo -u embclient dbus-send --system --print-reply \
  --dest=com.embeddex.Status /com/embeddex/Status \
  com.embeddex.Status.GetStatus

sudo -u otheruser dbus-send --system --print-reply \
  --dest=com.embeddex.Status /com/embeddex/Status \
  com.embeddex.Status.GetStatus

These commands assume the selected method takes no arguments. If your method takes arguments supported by dbus-send, supply identical typed arguments in both calls. The approved user should receive the normal method reply. The other user should receive org.freedesktop.DBus.Error.AccessDenied and a nonzero process status. In a test run on Ubuntu 24.04 (dbus 1.14, a small GDBus server returning the string ok), the approved call printed method return ... string "ok", and the other user got Error org.freedesktop.DBus.Error.AccessDenied: Rejected send message, 2 matched rules; ... with status 1. Your service, unique names and serials will differ, so record the error name and status from your own run. A timeout or a missing service fails the test because it leaves the access decision unverified. --print-reply makes dbus-send wait for the result. Trying both commands under the same Unix account, even from different terminals, checks only one identity.

02

Decide whether D-Bus fits the device

3 min read

Count the cost across the product image and across the full request. If the image needs a bus solely for this feature, account for the daemon, its dependencies, the client library, policy files, and another process to start and supervise. If the system bus already supports other platform services, measure the additional cost of the new client, server, and their traffic. Both designs still need tests for startup order, disconnects, timeouts, and behavior when the server is unavailable.

For a method exchange, the client encodes the message, the daemon checks policy and routes it, and the server decodes it. The reply must also pass through the daemon. Buffers hold messages when a receiver falls behind, and delivering a signal to several subscribers adds work for each recipient. This is a reasonable cost for occasional status queries and control operations. For high-rate samples or large payloads, test the actual message sizes and subscriber counts before choosing the transport. A device can use D-Bus for configuration and status while moving bulk data through a separate mechanism.

IPC choices for an embedded imageD-BusCustom UnixsocketvsomeipTypical useShared localservicesSmall localprotocolSOME/IP interoperabilityRoutingBus daemonDirect socketconnectionSOME/IProutingInterface workDefine typedinterfaceDefineframing andprotocolDefineservice andpayloads
The useful comparison is between deployment requirements and the work each IPC option leaves to the product

A Unix domain socket with a small custom protocol is a candidate when two local processes exchange a bounded set of messages. A pathname socket on Linux can use filesystem permissions to control connections, and a connected Unix stream socket can identify its peer using SO_PEERCRED. A stream protocol still needs message framing, a maximum request size, an error format, version handling, and tests for reconnects and incomplete reads. The kernel provides sockets and credentials. The applications implement the protocol and decide which operations the peer can perform.

vsomeip is a C++ implementation of SOME/IP, relevant when a product must interoperate with SOME/IP services, as in automotive systems. Local communication can use Unix domain sockets, and remote communication uses TCP or UDP. It brings its own runtime, routing, and configuration, with service discovery used where the deployment requires it. Its suitability for an embedded image depends on the required interoperability and measured cost. A smaller or simpler footprint than D-Bus is not guaranteed.

D-Bus is justified when several independent local services or tools need common naming, typed interfaces, policy, and signals, especially if the platform already runs the bus. Compare candidates on the board and image you intend to ship. Use the same request payload, call rate, and subscriber count for both designs. Compare installed image size, process memory at idle and under load, CPU time over the same interval, and client-observed round-trip latency. Include a burst test so a low average does not conceal slow replies. Proportional set size, or PSS, apportions shared mappings across processes and helps when comparing combined memory cost. /proc/PID/smaps_rollup reports it for a process, and /proc/PID/stat provides CPU time counters. Record the difference between two CPU readings over the test interval. Run the comparison with capture disabled so its processing and output do not affect the timing results.

03

Inspect the interface and capture traffic

3 min read

Use introspection when you need to know what the running server advertises at a particular path. As the approved account, query that path on the system bus. The policy above includes a separate allow rule for Introspect, and the server or its library must implement the introspection interface.

bash
sudo -u embclient busctl --system introspect \
  com.embeddex.Status /com/embeddex/Status

Check the interface and member spelling and the argument and result signatures against the client. Introspection is a live request, so it can fail when the object is missing or policy denies the request. A successful response describes what the service advertises. It does not prove that every advertised method is permitted for the current caller or that an earlier request reached the server.

For a particular failure, start dbus-monitor before reproducing the request. On the Ubuntu system bus, use an account permitted to monitor, typically root on the lab machine. A non-root user gets BecomeMonitor denied, and dbus-monitor then falls back to eavesdropping, which is not a reliable trace. An ordinary permitted client and a permitted monitor have different access. Keep a capture short because it can contain values from unrelated services.

bash
sudo dbus-monitor --system

In a second terminal, run the approved call. Find its method call by destination, path, interface, and member. The sender is shown as a unique name. Compare the call's serial with the response's reply_serial, and confirm that the response is addressed to that caller. Serial numbers are scoped to the sending connection. Two clients can use the same serial, so the serial alone is an ambiguous match. The message types distinguish a normal method return from an error.

A permitted call and replyClientBusServiceCall with serialForward callReply withreply_serialReply to caller1. Client → BusCall with serial2. Bus → ServiceForward call3. Service → BusReply with reply_serial4. Bus → ClientReply to caller
Match the return to the call using the caller's unique name and the reply serial

Repeat the operation as the other user and keep the client error alongside the capture. A rejected call does not reach the service: the bus answers it with an error. In a root capture on Ubuntu 24.04 the monitor showed the method call, then an error message with error_name=org.freedesktop.DBus.Error.AccessDenied addressed to the caller, and no method return from the service. Keep the client's own error as well, since it is the direct evidence of the denial. For a timeout, check the selected bus and whether the name has an owner before inspecting the server's method handling. On a noisy bus, a monitor match expression limited to calls to the service can help locate requests. That filter can omit replies because they are addressed to the caller.

For a capture you can review graphically, save the binary PCAP stream from dbus-monitor and open it in Bustle. Run the capture, reproduce the approved operation, then stop the monitor with Ctrl+C.

bash
(umask 077; sudo dbus-monitor --system --pcap > status-bus.pcap)

Use a new filename in a private directory. Bustle displays the messages over time so you can follow exchanges between connections and see where a reply is delayed. The time between call and reply in this trace includes the exchange as seen by the monitor. It does not directly measure CPU time spent in the service handler. On a deployed device, collect only the interval you need and restrict access to the trace because arguments and results can contain device data.

The next lesson, “The event loop and why it fits embedded”, explains how a service waits for bus traffic alongside its other inputs and stays responsive while it handles them.

Key takeaways

  • D-Bus name ownership and permission to send a method call are independent.
  • The effective policy includes every applicable rule loaded by the bus.
  • D-Bus adds routing, policy checks, and queues whose cost depends on the workload and the existing image.
  • Introspection describes the live interface, while a traffic capture shows actual message exchanges.
Reflect
  1. Explain to a colleague, in two sentences: configure system bus rules for name ownership and method calls.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close