Full name: Desktop Bus (the usual reading of the name; the D-Bus specification itself uses only the short name).
Definition
D-Bus is a system for interprocess communication §19.5.5 (IPC), the exchange of messages between programs on one computer. Each program connects to a message bus, a daemon (a background program) that routes every message to its destination. A message either calls a method, a named operation, on an object that another program offers, or announces an event, called a signal, to the programs that asked for it. A Linux system runs one system bus for system services and one session bus for each logged-in user. The freedesktop.org project publishes the specification.
The idea
A user clicks a switch in the desktop settings to turn Bluetooth off. The settings program runs as that user and cannot touch the radio. The Bluetooth service runs as root, the administrator account, and can. So the settings program must ask the service, and the service must decide whether this caller may make this request.
D-Bus gives every program the same way to ask: remote objects. A service publishes objects under paths that look like file names; each object offers interfaces, which list methods, signals and properties with typed arguments. The bus in the middle routes each message and learns from the kernel which user and which process stand behind each connection. The service therefore learns who is calling from the bus, not from anything the caller writes into the message. A telephone exchange with caller identification is a fair picture, up to one point: a verified number says who is calling, not what that caller may ask for.
How it works
Addressing. A method call names four things.
The bus name of the destination, such as
org.bluez.The object path, such as
/org/bluez/hci0.The interface, such as
org.bluez.Adapter1: a named group of methods, signals and properties.The member, the name of the method, such as
StartDiscovery.
A signature describes the arguments with one code per type, for example s for a string, u for an unsigned 32-bit integer, b for a boolean, v for a variant (a value that carries its own type) and a for an array §14.5.
Messages. The header numbers four message types: method call (1), method return (2), error (3) and signal (4). A method call is answered by one method return or one error, unless the caller asked for no reply. The answer carries the serial number of the call so that the caller can match the two, and the caller may keep working meanwhile, so the exchange is asynchronous. A signal gets no answer.
Connecting and naming, in the order it happens:
A program connects to the bus, normally over a Unix domain socket, a local socket for which the kernel can report the user and the process at the other end.
The program authenticates with the SASL (Simple Authentication §1.6 and Security Layer) mechanism named
EXTERNAL: the bus takes the program’s credentials from the kernel, not from the program.The bus assigns the connection a unique name, such as
:1.57. A unique name starts with a colon, and the bus never gives it to another connection.A service may also own a well-known name, such as
org.bluez, under which callers find it.For every message, the bus itself writes the header field
SENDER: the unique name of the sending connection.A service that wants to know its caller passes that unique name to the bus method
GetConnectionCredentials. The answer holds the caller’s user ID, group IDs and process ID and, where the system provides one, a security label.
Two policies decide whether a call takes effect.
Bus policy. The bus daemon reads configuration files in XML (Extensible Markup Language). Their
<policy>blocks hold<allow>and<deny>rules, which match a message’s destination, interface, member and type, or govern who may own which names. The blocks apply in a fixed order: those for every connection (context="default"), then those for the caller’s groups, then those for the caller’s user, then theat_consoleblocks, an old mechanism for the user at the physical keyboard, and last thecontext="mandatory"blocks. Where rules overlap, the later rule wins. The system bus denies method calls and name ownership unless a rule allows them, so each system service installs a file that opens what it needs. A denied message never reaches the service, and the caller receives the errorAccessDenied. With SELinux (Security-Enhanced Linux) or AppArmor, the bus can also consult these Linux Security Modules §21.14.Service policy. The service checks whether this caller may perform this operation on this object with these arguments, by its own rules or by asking polkit, a system service that answers “may this subject perform this action” from rules set by the administrator and can ask the user for a password.
Worked example
A service owns the well-known name org.example.Radio on the system bus. Its policy file holds three blocks:
<policy user="root">
<allow own="org.example.Radio"/>
</policy>
<policy context="default">
<deny send_destination="org.example.Radio"/>
</policy>
<policy group="radio">
<allow send_destination="org.example.Radio"/>
</policy>
Alice has user ID 1000 and belongs to the group radio. Bob has user ID 1001 and does not.
Alice’s client connects, receives the unique name :1.57, and calls the method SetPower of the interface org.example.Radio1 on the object /org/example/Radio, with the signature b and the argument false. The default block matches and denies. The block for the group radio applies later, matches and allows, so the bus delivers the call with SENDER set to :1.57. Bob’s client, :1.58, makes the same call. Only the default block applies to Bob, so the call is denied with AccessDenied, and the service never sees it.
The service’s own rules allow a second method, FactoryReset, only to root, user ID 0. Alice’s client calls it with an argument that claims uid=0. The bus policy lets the call through, because group radio may send anything to this destination. The service passes :1.57 to GetConnectionCredentials and receives user ID 1000. Since , it refuses and ignores the claim in the argument. A service that believed the argument would be a confused deputy §1.25: a program that holds authority and is tricked into using it for a caller who lacks that authority.
Stated precisely
Provided that the bus daemon itself is correct and connections use Unix domain sockets, a D-Bus message bus guarantees four things.
The
SENDERfield of every delivered message holds the unique name of the connection that sent it; the specification calls this field as trustworthy as the bus itself.The credentials that
GetConnectionCredentialsreturns for a unique name are those that the kernel reported for that connection’s socket when the connection was made.A unique name is never given to a second connection to the same bus, so a decision tied to it cannot later apply to another program.
A message is delivered only if the bus policy, applied in the order above, allows it.
D-Bus guarantees nothing about the body of a message beyond its declared types, and it does not decide whether an operation is permitted. Bus policy sees only users, groups and header fields, not argument values. A correct decision in the service is a function of the caller’s credentials, obtained from the bus by unique name, together with the object, the method, the arguments and the service’s own rules. It is never a function of an identity that the caller wrote into the arguments.
Limits and common mistakes
Process IDs are reused: after a process exits, a new process can receive the same number. A service that identifies its caller by process ID, rather than by unique name, can end up checking a different process. Pass the unique name to polkit, which accepts it as a subject of the kind system-bus-name, or use the credential ProcessFD where the bus provides it: a file descriptor §15.21 that keeps referring to the original process.
A well-known name is an address, not proof of identity. On the system bus, owning a name shows only that the bus policy allowed the owner to take it; on the session bus, any program of that user may take any free name. Any permitted connection can also send a signal with any interface and member name, so a program that acts on a signal must check that its SENDER is the unique name that currently owns the expected well-known name.
A method return means only that the service finished what it does before replying. Many services reply once an operation has started and report its end later, in a signal or a changed property; this is asynchronous completion §9.28. Treat the reply as the end of the operation only if the service’s documentation says so.
The session bus applies almost no policy: any program running as the user can call any service on that user’s session bus. A sandbox §2.30 that must restrict a program’s use of D-Bus has to filter the program’s messages itself, for example through a filtering proxy.
Questions answered
Which identity and policy authorize a caller to use a method?
The identity is the caller’s connection to the bus, and two policies decide. The connection has a unique bus name, such as :1.57, which the bus daemon assigns, never reuses, and writes into the SENDER field of every message the caller sends. Behind that name stand the credentials that the bus obtained from the kernel when the caller connected: user ID, group IDs, process ID and, where available, a security label. A service obtains them by passing the unique name to the bus method GetConnectionCredentials.
The first policy is the bus policy, in the bus daemon’s XML (Extensible Markup Language) configuration files. It allows or denies each message by the caller’s user and groups and by the message’s destination, interface, member and type; on the system bus, method calls are denied unless a rule allows them. The second policy belongs to the service, which decides whether this caller may perform this operation with these arguments, by its own rules or by asking polkit, the Linux authorization §1.7 service, with the caller’s unique name as the subject. Neither decision may rest on an identity written into the message body, on a process ID looked up afterwards, or on a well-known name.
Related entries: Authorization (§1.7), Confused deputy (§1.25), BlueZ (§12.21), Asynchronous completion (§9.28), Interprocess communication and authority transfer (§19.5.5), Least privilege (§1.15), Linux Security Modules: an enforcement-hook framework (§21.14). Further reading: [116].