Public bug reported:

[ Impact ]

In noble's GLib, g_dbus_message_set_body() replaces the cached first
argument of a message's body without releasing the old one, so every
body a message has had stays in memory for good. GNOME fixed this
upstream in commit fe89e9f3cb, "gdbusmessage: Clean the cached arg0 when
setting the message body", backported to the glib-2-80 branch in merge
request 4060 and first released in GLib 2.80.3 on 2024-06-10. Noble has
2.80.0-6ubuntu3.10, which does not carry it. Every later supported
release has GLib 2.84 or newer and has the fix.

The program this hurts most on a desktop is xdg-dbus-proxy, which
Flatpak starts beside every sandboxed app. The proxy filters each reply
to ListNames by setting a new body on the parsed reply, so it keeps one
copy of the bus's full name list for every ListNames call the app makes.
On stock Ubuntu 24.04.5 with xdg-dbus-proxy 0.1.5-1ubuntu0.2, the proxy
keeps 4 481 bytes for each call on a bus with 153 names. On a Linux Mint
22 desktop, which uses noble's glib2.0 package unchanged, a Stream Deck
app whose media plugin called ListNames five times a second grew its
proxy to 4.4 GB of RAM and swap in five days.

Bug 2083258 fixed leaks in xdg-dbus-proxy's own code in 2024. This leak
is in GLib, and it remains with that fix in place.

[ Test Plan ]

1. On noble, install python3-gi and save this script as repro.py:

    import gi
    gi.require_version("Gio", "2.0")
    from gi.repository import Gio, GLib

    def rss_kb():
        for line in open("/proc/self/status"):
            if line.startswith("VmRSS:"):
                return int(line.split()[1])

    names = ["org.example.Name%d" % i for i in range(200)]
    one = GLib.Variant("(as)", (["org.example.Kept"],))
    before = rss_kb()
    for _ in range(20000):
        message = Gio.DBusMessage.new()
        message.set_body(GLib.Variant("(as)", (names,)))
        message.set_body(one)
    print("grew by %d kB over 20000 messages" % (rss_kb() - before))

2. Run python3 repro.py. With 2.80.0-6ubuntu3.10 it prints "grew by
658980 kB over 20000 messages".

3. Install libglib2.0-0t64 and gir1.2-glib-2.0 from noble-proposed and
run it again. With the fix it prints a few hundred kB. Built from the
attached debdiff, it printed "grew by 228 kB over 20000 messages".

To see the effect on xdg-dbus-proxy as well, install python3-dbus, dbus-
daemon and xdg-dbus-proxy, save this script as proxy-test.py, and run
dbus-run-session -- python3 proxy-test.py before and after the upgrade:

    """ListNames through xdg-dbus-proxy on a private bus, and what the proxy 
keeps for each call."""
    import os, subprocess, sys, tempfile, time
    import dbus, dbus.bus

    addr = os.environ["DBUS_SESSION_BUS_ADDRESS"]
    owner = dbus.bus.BusConnection(addr)
    for i in range(150):
        owner.request_name("org.example.Name%d" % i)
    sock = os.path.join(tempfile.mkdtemp(), "proxy")
    proxy = subprocess.Popen(["xdg-dbus-proxy", addr, sock, "--filter", 
"--talk=org.example.Name10"])
    while not os.path.exists(sock):
        if proxy.poll() is not None: sys.exit("xdg-dbus-proxy exited")
        time.sleep(0.1)
    bus = dbus.bus.BusConnection("unix:path=" + sock)

    def kept():
        return sum(int(l.split()[1]) for l in open("/proc/%d/smaps_rollup" % 
proxy.pid) if l.startswith(("Pss_Anon:", "SwapPss:")))

    seen = len(bus.list_names())
    time.sleep(0.5)
    before = kept()
    for _ in range(5000):
        bus.list_names()
    time.sleep(0.5)
    after = kept()
    print("names through the proxy %d of %d; the proxy kept %d bytes for each 
ListNames" % (seen, len(owner.list_names()), (after - before) * 1024 // 5000))
    proxy.terminate()

It printed "the proxy kept 4481 bytes for each ListNames" with
2.80.0-6ubuntu3.10, and 4 bytes with the attached debdiff.

[ Where problems could occur ]

The change is three lines in g_dbus_message_set_body(). It releases the
old cached value before the cache is set again, and removes the two
assignments of NULL that the release makes redundant. A fault would show
first in code that reads a message's arg0 after its body has been
replaced: arg0 matching on signal subscriptions in GDBusConnection, and
D-Bus filters such as xdg-dbus-proxy's. It would show as a crash or a
wrong match. The change has been in every GLib release since 2.80.3.

With the attached debdiff built in a noble container, all 30 gdbus tests
in libglib2.0-tests pass under gnome-desktop-testing-runner.

[ Other Info ]

Upstream commit: 
https://gitlab.gnome.org/GNOME/glib/-/commit/fe89e9f3cb6e0fd0dc2bd8a2d413799e1443cef1
Backport to glib-2-80: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4060

The attached debdiff for noble adds the upstream commit as a quilt patch
with a DEP-3 header, and a changelog entry, and nothing else.

** Affects: glib2.0 (Ubuntu)
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Ubuntu
Desktop Bugs, which is subscribed to glib2.0 in Ubuntu.
https://bugs.launchpad.net/bugs/2178431

Title:
  g_dbus_message_set_body() leaks the cached arg0, so xdg-dbus-proxy
  grows with every ListNames call

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/glib2.0/+bug/2178431/+subscriptions


-- 
desktop-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/desktop-bugs

Reply via email to