Public bug reported:

[Impact]

polkitd in noble (124-2ubuntu1.24.04.4) leaks memory every time it looks up
the credentials of a D-Bus caller. This happens at least once for every
CheckAuthorization call, so the daemon grows for as long as the system is up
and the memory is never returned. How fast it grows depends on how often
clients talk to polkit:

 * With plain `pkcheck` calls it is about 0.4 KiB per call.
 * On a Kubuntu 24.04 desktop that is also reached over SSH, every new logind
   session makes desktop components (powerdevil, kded) re-check several
   actions, and polkitd grows by about 43-52 KiB per session. On my machine a
   frequently polling SSH client created ~88,000 sessions, and polkitd
   reached about 4.5 GB (RSS + swap) before I found it.
 * Bug #2131002 ("Polkit drains memory", 24.04.3 server, ~2 GB) looks like
   the same problem.

The only workaround is to restart polkit.service from time to time.

The cause is in polkit_system_bus_name_get_creds_sync()
(src/polkit/polkitsystembusname.c): the GVariantIter returned by
g_variant_get (result, "(a{sv})", &iter) is never freed, and it keeps the
D-Bus reply referenced. The bug was introduced upstream in 8cabb11 (first
released in 124) and fixed upstream by 5cce296 "Free the credentials iterator
as well" (released in 126). Only noble ships an affected version: jammy has
0.105, and resolute has 127.

The fix is a one-line backport of that upstream commit.

[Test Plan]

1. On an up-to-date noble system, note polkitd's memory:

     grep VmRSS /proc/$(pgrep -x polkitd)/status

2. Make 5000 authorization checks, then measure again. Repeat this a few
   times:

     for i in $(seq 5000); do
       pkcheck --action-id org.freedesktop.policykit.exec --process $$ 
>/dev/null 2>&1
     done
     grep VmRSS /proc/$(pgrep -x polkitd)/status

   With 124-2ubuntu1.24.04.4, VmRSS goes up by about 2 MB for every 5000
   calls and never comes back down. (Measured: 15436 -> 17448 -> 19524 kB.)

3. Install policykit-1/polkitd and libpolkit-gobject-1-0 from
   noble-proposed, restart polkit.service (or reboot), and repeat steps 1-2.
   After a short warm-up, VmRSS should stay flat.

I also ran this test under heaptrack, creating 1000 logind sessions over
OpenSSH on a Kubuntu 24.04 machine:

                          | RSS before -> after | heaptrack "leaked"
  124-2ubuntu1.24.04.4    | 16028 -> 59496 kB   | 33.87 MB
  with this patch         | 15908 -> 17716 kB   | 0.17 MB

With the patch, peak heap use dropped from 36.83 MB to 3.17 MB. Before the
patch, 99% of the leaked memory was allocated under
polkit_system_bus_name_get_creds_sync(), called from
polkit_system_bus_name_get_user_sync() and
polkit_system_bus_name_get_process_sync().

[Where problems could occur]

The patch adds a single g_variant_iter_free() for an iterator that the
function owns and does not use afterwards. If the iterator were still in use
or freed twice, polkitd could crash. polkitd is D-Bus activated, so it would
be started again, but authorization requests made while it was down (pkexec,
desktop privilege prompts, systemctl/udisks actions that ask polkit) would
fail.

This change has been in upstream since 126 (November 2024) and is shipped in
127 in resolute. I found no follow-up fixes to it upstream. I ran polkitd
with the patched library for the 1000-session test above, and it served
authorization requests normally the whole time.

[Other Info]

I'm not a polkit developer; I ran into this on my home server and tracked it
down. I used an AI assistant for the analysis and drafting, and verified the
results myself on my machine.

 * Upstream fix: https://github.com/polkit-org/polkit/commit/5cce296
 * Upstream report: https://github.com/polkit-org/polkit/issues/150
 * Possibly the same problem: LP: #2131002
 * A debdiff for noble is attached.

** Affects: policykit-1 (Ubuntu)
     Importance: Undecided
         Status: New

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

Title:
  [SRU] polkitd leaks memory on every D-Bus caller credential lookup
  (fixed upstream in 126)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/policykit-1/+bug/2177258/+subscriptions


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

Reply via email to