naccyde / rpms / systemd

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