Commit266870d1Recorded7 Nov 2026Repositorysigil-dbus

Guard multibyte D-Bus string clearing

Changed
 README.md                  |  1 +
 src/sigil/dbus/buffers.sgl | 20 +++++++++++++++++++-
 src/sigil/dbus/wire.sgl    |  2 +-
 test/test-wire.sgl         | 20 +++++++++++++++++++-
 4 files changed, 40 insertions(+), 3 deletions(-)
Diff
README.mdmodified
@@ -222,6 +222,7 @@ success, send failure, invalid output, and suppressed replies. This covers
222
only those owned buffers. It does not erase copies previously made by the
223
caller, operating system, bus daemon, recipient, runtime, or debugger. Avoid
224
converting secrets from strings or logging handler inputs/results.
+225
This does not guarantee that a password never lingers in process memory.
226
227
```sh
228
sigil --no-auto-compile -L src eval -f test/test-wire.sgl
src/sigil/dbus/buffers.sglmodified
@@ -1,7 +1,25 @@
1
;;; Clearing belongs to the owner of a temporary wire buffer.
2
(define-library (sigil dbus buffers)
−3
(export dbus-clear-buffer!)
+3
(export dbus-clear-buffer! dbus-clear-string!)
4
(begin
+5
;; string-set! requires the replacement to keep its UTF-8 width. Clear
+6
;; each character independently so an unwritable value cannot break an
+7
;; agent reply or replace the wire decoder's original validation error.
+8
(define (dbus-clear-string! text)
+9
(guard (e (else #f))
+10
(let ((n (string-length text)) (cleared? #t))
+11
(let loop ((i 0))
+12
(when (< i n)
+13
(guard (e (else (set! cleared? #f)))
+14
(let* ((code (char->integer (string-ref text i)))
+15
(replacement
+16
(cond ((< code 128) 0)
+17
((< code 2048) 128)
+18
((< code 65536) 2048)
+19
(else 65536))))
+20
(string-set! text i (integer->char replacement))))
+21
(loop (+ i 1))))
+22
cleared?)))
23
(define (dbus-clear-buffer! bytes)
24
(let loop ((i 0))
25
(when (< i (bytevector-length bytes))
src/sigil/dbus/wire.sglmodified
@@ -151,7 +151,7 @@
151
(lambda ()
152
(check-utf8 bytes)
153
(let ((text (utf8->string bytes)))
−154
(guard (e (else (string-fill! text (integer->char 0)) (raise e)))
+154
(guard (e (else (dbus-clear-string! text) (raise e)))
155
(vector-set! state 1 (+ start n))
156
(unless (= 0 (read-byte! state)) (error "Missing D-Bus string terminator"))
157
(check-text text type)
test/test-wire.sglmodified
@@ -1,4 +1,5 @@
−1
(import (sigil dbus) (sigil dbus types) (sigil dbus transport)
+1
(import (sigil dbus) (sigil dbus types) (sigil dbus transport) (sigil dbus buffers)
+2
(only (sigil error) exception-message)
3
(sigil test) (sigil process))
4
5
(test-group "wire round trips"
@@ -152,4 +153,21 @@
153
(test "address escape requires two hex digits" (assert-error (dbus-parse-address "unix:path=%+a")))
154
(test "address NUL" (assert-error (dbus-parse-address "unix:path=%00"))))
155
+156
(test "string clearing preserves all UTF-8 widths"
+157
(let* ((text (utf8->string (string->utf8 "aé中🙂")))
+158
(size (bytevector-length (string->utf8 text))))
+159
(assert-true (dbus-clear-string! text))
+160
(assert-equal size (bytevector-length (string->utf8 text)))
+161
(assert-equal '(0 128 2048 65536) (map char->integer (string->list text)))))
+162
+163
(test "string clearing guards values it cannot overwrite"
+164
(assert-false (dbus-clear-string! #f)))
+165
+166
(test "multibyte rejected object path keeps its original error"
+167
(let ((message
+168
(guard (e (else (exception-message e)))
+169
(dbus-unmarshal "o" (dbus-marshal "s" (list "é/bad")))
+170
#f)))
+171
(assert-equal "Invalid D-Bus object path" message)))
+172
173
(unless (run-tests) (exit 1))