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