Commit06672f99Recorded8 Nov 2026Repositorysigil-dbus

Validate pinned D-Bus reply senders and wire signatures

Changed
 README.md                     | 10 ++++++++++
 src/sigil/dbus/connection.sgl | 33 ++++++++++++++++++++++++---------
 test/test-pinned-replies.sgl  | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+), 9 deletions(-)
Diff
README.mdmodified
@@ -53,6 +53,16 @@ local validation failures raise exceptions. Check `dbus-error?`, then
53
`dbus-error-name` and `dbus-error-message`. A timeout has the name
54
`org.freedesktop.DBus.Error.NoReply`.
55
+56
For a security-sensitive call, resolve and authenticate the unique destination
+57
first, then pass `reply-signature:` to `dbus-call` or `dbus-call-async`. This
+58
requires a unique destination (or the bus daemon) and validates the exact
+59
successful-reply wire signature; a mismatch returns InvalidSignature. Calls to
+60
unique destinations and the bus daemon also pin the reply sender, even without
+61
this option. A foreign sender cannot complete them by matching a reply serial.
+62
Authenticated bus-daemon error replies still terminate the call. Calls to other
+63
well-known names retain their existing body-only behavior; they do not establish
+64
a persistent owner identity.
+65
66
Inside `(with-async ...)`, calls park only their calling fiber. For multiple
67
outstanding calls, `dbus-call-async` takes the same arguments and returns a
68
pending value; `dbus-await` waits for it. Replies are matched by serial, so
src/sigil/dbus/connection.sglmodified
@@ -81,9 +81,10 @@
81
(uid #f) (timeout-ms 25000) (max-bytes 16777216)))
82
(dbus-connect address uid: uid timeout-ms: timeout-ms max-bytes: max-bytes))
83
−84
;; Pending slots: connection, serial, deadline, ready?, result.
+84
;; Pending slots: connection, serial, deadline, ready?, result,
+85
;; authenticated reply sender and optional successful-reply signature.
86
(define (dbus-pending? p)
−86
(and (vector? p) (= 5 (vector-length p)) (vector? (vector-ref p 0))))
+87
(and (vector? p) (= 7 (vector-length p)) (vector? (vector-ref p 0))))
88
(define (dbus-pending-ready? p) (vector-ref p 3))
89
90
(define (expire! c)
@@ -98,17 +99,23 @@
99
(vector-set! c 2 (remove-if dbus-pending-ready? (vector-ref c 2))))
100
101
(define (dbus-call-async c destination path interface member signature values
−101
(keys: (timeout-ms 25000) (flags 0)))
+102
(keys: (timeout-ms 25000) (flags 0) (reply-signature #f)))
103
(when (dbus-closed? c) (error "D-Bus connection closed"))
104
(expire! c)
105
(when (>= (length (vector-ref c 2)) 1024) (error "Too many outstanding D-Bus calls"))
106
(when (odd? flags) (error "Use a reply-expecting call with this API"))
−106
(let* ((serial (next-serial! c))
−107
(pending (vector c serial (dbus-deadline timeout-ms) #f #f))
+107
(let* ((sender (and destination
+108
(or (equal? destination bus-name) (string-starts-with? destination ":"))
+109
destination))
+110
(serial (next-serial! c))
+111
(pending (vector c serial (dbus-deadline timeout-ms) #f #f sender reply-signature))
112
(fields (append
113
(list (cons 1 (dbus-variant "o" path)) (cons 3 (dbus-variant "s" member)))
114
(if interface (list (cons 2 (dbus-variant "s" interface))) '())
115
(if destination (list (cons 6 (dbus-variant "s" destination))) '()))))
+116
(when reply-signature
+117
(unless sender (error "Typed D-Bus replies require a pinned unique destination or bus daemon"))
+118
(dbus-parse-signature reply-signature))
119
(vector-set! c 2 (cons pending (vector-ref c 2)))
120
(guard (e (else (dbus-close! c) (raise e)))
121
(send-message! c (dbus-message 1 serial fields signature values flags: flags) timeout-ms))
@@ -125,9 +132,9 @@
132
(loop))))))
133
134
(define (dbus-call c destination path interface member signature values
−128
(keys: (timeout-ms 25000) (flags 0)))
+135
(keys: (timeout-ms 25000) (flags 0) (reply-signature #f)))
136
(dbus-await (dbus-call-async c destination path interface member signature values
−130
timeout-ms: timeout-ms flags: flags)))
+137
timeout-ms: timeout-ms flags: flags reply-signature: reply-signature)))
138
139
(define (dbus-request-name! c name (keys: (flags dbus-do-not-queue)))
140
(unless (and (integer? flags) (<= 0 flags 7)) (error "Invalid D-Bus name flags"))
@@ -294,14 +301,22 @@
301
(let ((serial (dbus-message-field message 5)))
302
(for-each
303
(lambda (p)
−297
(when (= serial (vector-ref p 1))
+304
(when (and (= serial (vector-ref p 1))
+305
(or (not (vector-ref p 5))
+306
(equal? (vector-ref p 5) (dbus-message-field message 7))
+307
(and (= 3 (dbus-message-type message))
+308
(equal? bus-name (dbus-message-field message 7)))))
309
(vector-set! p 4
310
(if (= 3 (dbus-message-type message))
311
(dbus-error (dbus-message-field message 4)
312
(if (and (pair? (dbus-message-body message))
313
(string? (car (dbus-message-body message))))
314
(car (dbus-message-body message)) "Remote method error"))
−304
(dbus-message-body message)))
+315
(if (and (vector-ref p 6)
+316
(not (equal? (vector-ref p 6) (dbus-message-signature message))))
+317
(dbus-error "org.freedesktop.DBus.Error.InvalidSignature"
+318
"Method reply has an unexpected signature")
+319
(dbus-message-body message))))
320
(vector-set! p 3 #t)))
321
(vector-ref c 2))
322
(expire! c)))
test/test-pinned-replies.sgladded
@@ -0,0 +1,87 @@
+1
#!/usr/bin/env sigil
+2
;;; Reply identity and exact wire types on a private real bus.
+3
(import (sigil dbus) (only (sigil dbus connection) dbus-send-reply!) (sigil async))
+4
(requires (tools dbus-daemon))
+5
(define checked 0)
+6
(define (check value name)
+7
(unless value (error name))
+8
(set! checked (+ checked 1)))
+9
(define root (make-temp-directory))
+10
(println "PINNED_REPLY_RECEIPT=~a" root)
+11
(begin
+12
(define address (str "unix:path=" (path root "bus")))
+13
(define config (path root "bus.conf"))
+14
(write-text config (str "<busconfig><type>session</type><listen>" address "</listen><auth>EXTERNAL</auth>"
+15
"<policy context=\"default\"><allow user=\"*\"/><allow own=\"*\"/>"
+16
"<allow send_destination=\"*\"/><allow receive_sender=\"*\"/>"
+17
"<allow send_type=\"method_return\" send_requested_reply=\"false\"/>"
+18
"<allow receive_type=\"method_return\" receive_requested_reply=\"false\"/></policy></busconfig>"))
+19
(define bus #f)
+20
(define server #f)
+21
(define client #f)
+22
(define foreign #f)
+23
(dynamic-wind
+24
(lambda () #t)
+25
(lambda ()
+26
(set! bus (spawn dbus-daemon --nofork --config-file ,config out: ,(path root "bus.log") err: 'merge))
+27
(wait-until (file-exists? (path root "bus")) timeout: 5)
+28
(set! server (dbus-connect address))
+29
(set! client (dbus-connect address))
+30
(set! foreign (dbus-connect address))
+31
(with-async
+32
(dbus-export! server "/org/example/Pinned"
+33
(list (dbus-interface "org.example.Pinned"
+34
methods: (list
+35
(dbus-method "Unsigned" "" "u" (lambda () '(7)))
+36
(dbus-method "Signed" "" "i" (lambda () '(7)))
+37
(dbus-method "Fail" "" "u" (lambda () (dbus-error "org.example.Refused" "refused")))
+38
(dbus-method "Forged" "" "u"
+39
(lambda (message)
+40
(dbus-send-reply! foreign message "u" '(99))
+41
(sleep 0.04)
+42
'(7)) with-message: #t)))))
+43
(go (dbus-run! server))
+44
(define owner (dbus-unique-name server))
+45
(define (call member signature)
+46
(dbus-call client owner "/org/example/Pinned" "org.example.Pinned" member "" '()
+47
timeout-ms: 500 reply-signature: signature))
+48
(check (equal? '(7) (call "Unsigned" "u")) "PINNED_TYPED_REPLY_MUST_SUCCEED")
+49
(define wrong (call "Signed" "u"))
+50
(check (and (dbus-error? wrong)
+51
(equal? (dbus-error-name wrong) "org.freedesktop.DBus.Error.InvalidSignature"))
+52
"SIGNED_VALUE_MUST_NOT_SATISFY_UNSIGNED_REPLY")
+53
(define pending
+54
(dbus-call-async client owner "/org/example/Pinned" "org.example.Pinned" "Forged" "" '()
+55
timeout-ms: 500 reply-signature: "u"))
+56
(define seen #f)
+57
(define deadline (+ (current-milliseconds) 1000))
+58
(let loop ()
+59
(unless seen
+60
(define message (dbus-pump! client timeout-ms: 10))
+61
(when (and message (equal? (dbus-message-field message 7) (dbus-unique-name foreign)))
+62
(set! seen #t))
+63
(when (< (current-milliseconds) deadline) (unless seen (loop)))))
+64
(check seen "FOREIGN_REPLY_FIXTURE_MUST_DELIVER")
+65
(check (not (dbus-pending-ready? pending)) "FOREIGN_SERIAL_MATCH_MUST_NOT_COMPLETE_PINNED_CALL")
+66
(check (equal? '(7) (dbus-await pending)) "PINNED_CALL_MUST_AWAIT_AUTHENTIC_REPLY")
+67
(define failed (call "Fail" "u"))
+68
(check (and (dbus-error? failed) (equal? (dbus-error-name failed) "org.example.Refused"))
+69
"AUTHENTICATED_REMOTE_ERROR_MUST_SURVIVE")
+70
(define daemon-error
+71
(dbus-call client ":987654.987654" "/org/example/Pinned" "org.example.Pinned" "Unsigned" "" '()
+72
timeout-ms: 500 reply-signature: "u"))
+73
(check (and (dbus-error? daemon-error)
+74
(equal? (dbus-error-name daemon-error) "org.freedesktop.DBus.Error.ServiceUnknown"))
+75
"AUTHENTICATED_DAEMON_ERROR_MUST_SURVIVE")
+76
(check (guard (e (#t #t))
+77
(dbus-call-async client "org.example.Pinned" "/org/example/Pinned" "org.example.Pinned" "Unsigned" "" '()
+78
reply-signature: "u")
+79
#f)
+80
"TYPED_CALL_MUST_REQUIRE_PINNED_DESTINATION")
+81
(dbus-close! server)))
+82
(lambda ()
+83
(when server (dbus-close! server))
+84
(when client (dbus-close! client))
+85
(when foreign (dbus-close! foreign))
+86
(when bus (when (process-alive? bus) (process-kill! bus)) (process-wait bus)))))
+87
(println "PASS ~a pinned reply checks" checked)