teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0759-sd-bus-switch-to-a-manual-overflow-check-in-sd_bus_t.patch

b7dd4d
From 598eecf5c1c948535ca626833bc5cea59060913f Mon Sep 17 00:00:00 2001
ff479f
From: Lennart Poettering <lennart@poettering.net>
ff479f
Date: Wed, 20 Apr 2022 22:30:22 +0200
ff479f
Subject: [PATCH] sd-bus: switch to a manual overflow check in
ff479f
 sd_bus_track_add_name()
ff479f
ff479f
This is generally used in a directly client controllable way, hence we
ff479f
should handle ref count overflow gracefully, instead of hitting an
ff479f
assert().
ff479f
ff479f
As discussed:
ff479f
ff479f
https://github.com/systemd/systemd/pull/23099#discussion_r854341850
ff479f
(cherry picked from commit 7f40cb7c86b0fff3a82096a9499570bad9c19fd2)
ff479f
ff479f
[msekleta: We've never switched to using track_item_ref/unref introduced
ff479f
in c2d7dd35d2 hence we still had potential undefined behavior related to
ff479f
overflow check and this commit fixes that.]
ff479f
b7dd4d
Related: #2047373
ff479f
---
ff479f
 src/libsystemd/sd-bus/bus-track.c | 10 +++++++---
ff479f
 1 file changed, 7 insertions(+), 3 deletions(-)
ff479f
ff479f
diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c
ff479f
index 8893f190a1..b818e93bec 100644
ff479f
--- a/src/libsystemd/sd-bus/bus-track.c
ff479f
+++ b/src/libsystemd/sd-bus/bus-track.c
ff479f
@@ -208,12 +208,16 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
ff479f
         i = hashmap_get(track->names, name);
ff479f
         if (i) {
ff479f
                 if (track->recursive) {
ff479f
-                        unsigned k = i->n_ref + 1;
ff479f
+                        assert(i->n_ref > 0);
ff479f
 
ff479f
-                        if (k < i->n_ref) /* Check for overflow */
ff479f
+                        /* Manual oveflow check (instead of a DEFINE_TRIVIAL_REF_FUNC() helper or so), so
ff479f
+                         * that we can return a proper error, given this is almost always called in a
ff479f
+                         * directly client controllable way, and thus better should never hit an assertion
ff479f
+                         * here. */
ff479f
+                        if (i->n_ref >= UINT_MAX)
ff479f
                                 return -EOVERFLOW;
ff479f
 
ff479f
-                        i->n_ref = k;
ff479f
+                        i->n_ref++;
ff479f
                 }
ff479f
 
ff479f
                 bus_track_remove_from_queue(track);