anitazha / rpms / systemd

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