dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
9d460a
From 28f73a10df367721b30a95df5d729f6c56d318e5 Mon Sep 17 00:00:00 2001
9d460a
From: Jan Synacek <jsynacek@redhat.com>
9d460a
Date: Tue, 2 Apr 2019 10:23:30 +0200
9d460a
Subject: [PATCH] sd-bus: deal with cookie overruns
9d460a
9d460a
Apparently this happens IRL. Let's carefully deal with issues like this:
9d460a
when we overrun, let's not go back to zero but instead leave the highest
9d460a
cookie bit set. We use that as indication that we are in "overrun
9d460a
territory", and then are particularly careful with checking cookies,
9d460a
i.e. that they haven't been used for still outstanding replies yet. This
9d460a
should retain the quick cookie generation behaviour we used to have, but
9d460a
permits dealing with overruns.
9d460a
9d460a
Replaces: #11804
9d460a
Fixes: #11809
9d460a
9d460a
(cherry picked from commit 1f82f5bb4237ed5f015daf93f818e9db95e764b8)
9d460a
(cherry picked from commit ac46d01c5f6a211bbbbb43e20f63ecae2549da20)
9d460a
9d460a
Resolves: #1720699
9d460a
---
9d460a
 src/libsystemd/sd-bus/sd-bus.c | 49 +++++++++++++++++++++++++++++++++-
9d460a
 src/shared/macro.h             |  2 ++
9d460a
 2 files changed, 50 insertions(+), 1 deletion(-)
9d460a
9d460a
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
9d460a
index b0a3237..44ed2c7 100644
9d460a
--- a/src/libsystemd/sd-bus/sd-bus.c
9d460a
+++ b/src/libsystemd/sd-bus/sd-bus.c
9d460a
@@ -1495,7 +1495,50 @@ _public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
9d460a
         return 0;
9d460a
 }
9d460a
 
9d460a
+#define COOKIE_CYCLED (UINT32_C(1) << 31)
9d460a
+
9d460a
+static uint64_t cookie_inc(uint64_t cookie) {
9d460a
+
9d460a
+        /* Stay within the 32bit range, since classic D-Bus can't deal with more */
9d460a
+        if (cookie >= UINT32_MAX)
9d460a
+                return COOKIE_CYCLED; /* Don't go back to zero, but use the highest bit for checking
9d460a
+                                       * whether we are looping. */
9d460a
+
9d460a
+        return cookie + 1;
9d460a
+}
9d460a
+
9d460a
+static int next_cookie(sd_bus *b) {
9d460a
+        uint64_t new_cookie;
9d460a
+
9d460a
+        assert(b);
9d460a
+
9d460a
+        new_cookie = cookie_inc(b->cookie);
9d460a
+
9d460a
+        /* Small optimization: don't bother with checking for cookie reuse until we overran cookiespace at
9d460a
+         * least once, but then do it thorougly. */
9d460a
+        if (FLAGS_SET(new_cookie, COOKIE_CYCLED)) {
9d460a
+                uint32_t i;
9d460a
+
9d460a
+                /* Check if the cookie is currently in use. If so, pick the next one */
9d460a
+                for (i = 0; i < COOKIE_CYCLED; i++) {
9d460a
+                        if (!ordered_hashmap_contains(b->reply_callbacks, &new_cookie))
9d460a
+                                goto good;
9d460a
+
9d460a
+                        new_cookie = cookie_inc(new_cookie);
9d460a
+                }
9d460a
+
9d460a
+                /* Can't fulfill request */
9d460a
+                return -EBUSY;
9d460a
+        }
9d460a
+
9d460a
+good:
9d460a
+        b->cookie = new_cookie;
9d460a
+        return 0;
9d460a
+}
9d460a
+
9d460a
 static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
9d460a
+        int r;
9d460a
+
9d460a
         assert(b);
9d460a
         assert(m);
9d460a
 
9d460a
@@ -1510,7 +1553,11 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
9d460a
         if (timeout == 0)
9d460a
                 timeout = BUS_DEFAULT_TIMEOUT;
9d460a
 
9d460a
-        return bus_message_seal(m, ++b->cookie, timeout);
9d460a
+        r = next_cookie(b);
9d460a
+        if (r < 0)
9d460a
+                return r;
9d460a
+
9d460a
+        return bus_message_seal(m, b->cookie, timeout);
9d460a
 }
9d460a
 
9d460a
 static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
9d460a
diff --git a/src/shared/macro.h b/src/shared/macro.h
9d460a
index 7a57f4e..08567d2 100644
9d460a
--- a/src/shared/macro.h
9d460a
+++ b/src/shared/macro.h
9d460a
@@ -394,6 +394,8 @@ do {                                                                    \
9d460a
 
9d460a
 #define SET_FLAG(v, flag, b) \
9d460a
         (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
9d460a
+#define FLAGS_SET(v, flags) \
9d460a
+        ((~(v) & (flags)) == 0)
9d460a
 
9d460a
 #define IN_SET(x, y, ...)                                               \
9d460a
         ({                                                              \
9d460a
-- 
9d460a
2.21.0
9d460a