dryang / rpms / systemd

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