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