Skip to content

daemon: Fix GetCapabilities memory leak - #262

Merged
vkareh merged 1 commit into
mate-desktop:masterfrom
64kramsystem:fix-get-capabilities-leak
Jul 30, 2026
Merged

vkareh merged 1 commit into
mate-desktop:masterfrom
64kramsystem:fix-get-capabilities-leak

Conversation

@64kramsystem

Copy link
Copy Markdown
Contributor

Summary

  • pass the compile-time capability list directly to the generated D-Bus
    completion helper
  • remove the temporary GVariantBuilder, GVariant, and duplicated string
    vector
  • preserve all eight capabilities and their existing order

Real-world report

This leak was found in a real MATE desktop session after rebuilding Ubuntu's
mate-notification-daemon 1.26.1 package with AddressSanitizer and
UndefinedBehaviorSanitizer. A normal client call to
org.freedesktop.Notifications.GetCapabilities produced a 64-byte direct leak
for the duplicated pointer vector and 72 indirect bytes for its seven strings.

I reproduced the same ownership bug on current upstream master using an
ASan/UBSan build, Xvfb, and a private D-Bus session. Five
GetCapabilities calls leaked 156 bytes each: a 72-byte vector and 84 bytes
across eight strings. The isolated run produced only these two leak records:

Direct leak of 360 byte(s) in 5 object(s) allocated from:
    #2 g_variant_dup_strv
    #3 notify_daemon_get_capabilities src/daemon/daemon.c:1963

Indirect leak of 420 byte(s) in 40 object(s) allocated from:
    #3 g_variant_dup_strv
    #4 notify_daemon_get_capabilities src/daemon/daemon.c:1963

SUMMARY: AddressSanitizer: 780 byte(s) leaked in 45 allocation(s).

g_variant_dup_strv() allocates both the NULL-terminated vector and each
string. The generated completion helper marshals its input with
g_variant_new ("(^as)", return_caps), which copies the contents into the
outgoing D-Bus value and does not take ownership of the input. The duplicated
vector therefore had no owner after the helper returned.

The capability names are compile-time constants, so this change passes a
static, NULL-terminated vector directly to the helper instead.

Verification

  • built unmodified master with ASan/UBSan and reproduced 780 leaked bytes
    across five calls
  • rebuilt the patched source with identical sanitizer flags and repeated the
    same five calls; the g_variant_dup_strv() leak was absent
  • captured all five gdbus call replies before and after; their direct diff was
    empty
  • configured with --enable-compile-warnings=maximum and built the patched
    daemon normally

g_variant_dup_strv() allocates a vector and duplicates every string.  The
generated completion helper copies that vector into the outgoing GVariant,
leaving the duplicated input unowned.  AddressSanitizer reports 360 direct
bytes and 420 indirect bytes leaked after five calls.

Pass the constant capability list directly to the helper instead.  This
avoids the temporary allocations while leaving the returned capabilities
unchanged.

Signed-off-by: Saverio Miroddi <saverio.pub2@gmail.com>
@vkareh
vkareh merged commit 3ce6b85 into mate-desktop:master Jul 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants