teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0320-sd-bus-use-queue-message-references-for-managing-r-w.patch

5d2ee9
From 781a055c17400e953bb7929434fe7a2e6517d5e8 Mon Sep 17 00:00:00 2001
5d2ee9
From: Lennart Poettering <lennart@poettering.net>
5d2ee9
Date: Thu, 17 Jan 2019 18:31:59 +0100
5d2ee9
Subject: [PATCH] sd-bus: use "queue" message references for managing r/w
5d2ee9
 message queues in connection objects
5d2ee9
5d2ee9
Let's make use of the new concept the previous commit added.
5d2ee9
5d2ee9
See: #4846
5d2ee9
(cherry picked from commit c1757a70eac0382c4837a3833d683919f6a48ed7)
5d2ee9
Related: CVE-2020-1712
5d2ee9
---
5d2ee9
 src/libsystemd/sd-bus/bus-socket.c |  6 ++-
5d2ee9
 src/libsystemd/sd-bus/sd-bus.c     | 60 ++++++++++++++----------------
5d2ee9
 2 files changed, 32 insertions(+), 34 deletions(-)
5d2ee9
5d2ee9
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
5d2ee9
index 17cfa8e1fd..4a72795d2b 100644
5d2ee9
--- a/src/libsystemd/sd-bus/bus-socket.c
5d2ee9
+++ b/src/libsystemd/sd-bus/bus-socket.c
5d2ee9
@@ -1116,8 +1116,10 @@ static int bus_socket_make_message(sd_bus *bus, size_t size) {
5d2ee9
         bus->fds = NULL;
5d2ee9
         bus->n_fds = 0;
5d2ee9
 
5d2ee9
-        if (t)
5d2ee9
-                bus->rqueue[bus->rqueue_size++] = t;
5d2ee9
+        if (t) {
5d2ee9
+                bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(t, bus);
5d2ee9
+                sd_bus_message_unref(t);
5d2ee9
+        }
5d2ee9
 
5d2ee9
         return 1;
5d2ee9
 }
5d2ee9
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
5d2ee9
index 68ad6cbe89..a3509f7e89 100644
5d2ee9
--- a/src/libsystemd/sd-bus/sd-bus.c
5d2ee9
+++ b/src/libsystemd/sd-bus/sd-bus.c
5d2ee9
@@ -148,13 +148,13 @@ static void bus_reset_queues(sd_bus *b) {
5d2ee9
         assert(b);
5d2ee9
 
5d2ee9
         while (b->rqueue_size > 0)
5d2ee9
-                sd_bus_message_unref(b->rqueue[--b->rqueue_size]);
5d2ee9
+                bus_message_unref_queued(b->rqueue[--b->rqueue_size], b);
5d2ee9
 
5d2ee9
         b->rqueue = mfree(b->rqueue);
5d2ee9
         b->rqueue_allocated = 0;
5d2ee9
 
5d2ee9
         while (b->wqueue_size > 0)
5d2ee9
-                sd_bus_message_unref(b->wqueue[--b->wqueue_size]);
5d2ee9
+                bus_message_unref_queued(b->wqueue[--b->wqueue_size], b);
5d2ee9
 
5d2ee9
         b->wqueue = mfree(b->wqueue);
5d2ee9
         b->wqueue_allocated = 0;
5d2ee9
@@ -493,7 +493,7 @@ static int synthesize_connected_signal(sd_bus *bus) {
5d2ee9
 
5d2ee9
         /* Insert at the very front */
5d2ee9
         memmove(bus->rqueue + 1, bus->rqueue, sizeof(sd_bus_message*) * bus->rqueue_size);
5d2ee9
-        bus->rqueue[0] = TAKE_PTR(m);
5d2ee9
+        bus->rqueue[0] = bus_message_ref_queued(m, bus);
5d2ee9
         bus->rqueue_size++;
5d2ee9
 
5d2ee9
         return 0;
5d2ee9
@@ -1760,7 +1760,7 @@ static int dispatch_wqueue(sd_bus *bus) {
5d2ee9
                          * anyway. */
5d2ee9
 
5d2ee9
                         bus->wqueue_size--;
5d2ee9
-                        sd_bus_message_unref(bus->wqueue[0]);
5d2ee9
+                        bus_message_unref_queued(bus->wqueue[0], bus);
5d2ee9
                         memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
5d2ee9
                         bus->windex = 0;
5d2ee9
 
5d2ee9
@@ -1789,6 +1789,15 @@ int bus_rqueue_make_room(sd_bus *bus) {
5d2ee9
         return 0;
5d2ee9
 }
5d2ee9
 
5d2ee9
+static void rqueue_drop_one(sd_bus *bus, size_t i) {
5d2ee9
+        assert(bus);
5d2ee9
+        assert(i < bus->rqueue_size);
5d2ee9
+
5d2ee9
+        bus_message_unref_queued(bus->rqueue[i], bus);
5d2ee9
+        memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
5d2ee9
+        bus->rqueue_size--;
5d2ee9
+}
5d2ee9
+
5d2ee9
 static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **m) {
5d2ee9
         int r, ret = 0;
5d2ee9
 
5d2ee9
@@ -1803,10 +1812,8 @@ static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd
5d2ee9
         for (;;) {
5d2ee9
                 if (bus->rqueue_size > 0) {
5d2ee9
                         /* Dispatch a queued message */
5d2ee9
-
5d2ee9
-                        *m = bus->rqueue[0];
5d2ee9
-                        bus->rqueue_size--;
5d2ee9
-                        memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
5d2ee9
+                        *m = sd_bus_message_ref(bus->rqueue[0]);
5d2ee9
+                        rqueue_drop_one(bus, 0);
5d2ee9
                         return 1;
5d2ee9
                 }
5d2ee9
 
5d2ee9
@@ -1884,7 +1891,7 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie) {
5d2ee9
                          * of the wqueue array is always allocated so
5d2ee9
                          * that we always can remember how much was
5d2ee9
                          * written. */
5d2ee9
-                        bus->wqueue[0] = sd_bus_message_ref(m);
5d2ee9
+                        bus->wqueue[0] = bus_message_ref_queued(m, bus);
5d2ee9
                         bus->wqueue_size = 1;
5d2ee9
                         bus->windex = idx;
5d2ee9
                 }
5d2ee9
@@ -1898,7 +1905,7 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie) {
5d2ee9
                 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
5d2ee9
                         return -ENOMEM;
5d2ee9
 
5d2ee9
-                bus->wqueue[bus->wqueue_size++] = sd_bus_message_ref(m);
5d2ee9
+                bus->wqueue[bus->wqueue_size++] = bus_message_ref_queued(m, bus);
5d2ee9
         }
5d2ee9
 
5d2ee9
 finish:
5d2ee9
@@ -2124,37 +2131,30 @@ _public_ int sd_bus_call(
5d2ee9
                 usec_t left;
5d2ee9
 
5d2ee9
                 while (i < bus->rqueue_size) {
5d2ee9
-                        sd_bus_message *incoming = NULL;
5d2ee9
+                        _cleanup_(sd_bus_message_unrefp) sd_bus_message *incoming = NULL;
5d2ee9
 
5d2ee9
-                        incoming = bus->rqueue[i];
5d2ee9
+                        incoming = sd_bus_message_ref(bus->rqueue[i]);
5d2ee9
 
5d2ee9
                         if (incoming->reply_cookie == cookie) {
5d2ee9
                                 /* Found a match! */
5d2ee9
 
5d2ee9
-                                memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
5d2ee9
-                                bus->rqueue_size--;
5d2ee9
+                                rqueue_drop_one(bus, i);
5d2ee9
                                 log_debug_bus_message(incoming);
5d2ee9
 
5d2ee9
                                 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
5d2ee9
 
5d2ee9
                                         if (incoming->n_fds <= 0 || bus->accept_fd) {
5d2ee9
                                                 if (reply)
5d2ee9
-                                                        *reply = incoming;
5d2ee9
-                                                else
5d2ee9
-                                                        sd_bus_message_unref(incoming);
5d2ee9
+                                                        *reply = TAKE_PTR(incoming);
5d2ee9
 
5d2ee9
                                                 return 1;
5d2ee9
                                         }
5d2ee9
 
5d2ee9
-                                        r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
5d2ee9
-                                        sd_bus_message_unref(incoming);
5d2ee9
-                                        return r;
5d2ee9
+                                        return sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
5d2ee9
 
5d2ee9
-                                } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
5d2ee9
-                                        r = sd_bus_error_copy(error, &incoming->error);
5d2ee9
-                                        sd_bus_message_unref(incoming);
5d2ee9
-                                        return r;
5d2ee9
-                                } else {
5d2ee9
+                                } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
5d2ee9
+                                        return sd_bus_error_copy(error, &incoming->error);
5d2ee9
+                                else {
5d2ee9
                                         r = -EIO;
5d2ee9
                                         goto fail;
5d2ee9
                                 }
5d2ee9
@@ -2164,15 +2164,11 @@ _public_ int sd_bus_call(
5d2ee9
                                    incoming->sender &&
5d2ee9
                                    streq(bus->unique_name, incoming->sender)) {
5d2ee9
 
5d2ee9
-                                memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
5d2ee9
-                                bus->rqueue_size--;
5d2ee9
+                                rqueue_drop_one(bus, i);
5d2ee9
 
5d2ee9
-                                /* Our own message? Somebody is trying
5d2ee9
-                                 * to send its own client a message,
5d2ee9
-                                 * let's not dead-lock, let's fail
5d2ee9
-                                 * immediately. */
5d2ee9
+                                /* Our own message? Somebody is trying to send its own client a message,
5d2ee9
+                                 * let's not dead-lock, let's fail immediately. */
5d2ee9
 
5d2ee9
-                                sd_bus_message_unref(incoming);
5d2ee9
                                 r = -ELOOP;
5d2ee9
                                 goto fail;
5d2ee9
                         }