b9a53a
From bc2d7df4fc21e9e54413169d5aad21616314d65e Mon Sep 17 00:00:00 2001
4b8c80
From: Lennart Poettering <lennart@poettering.net>
4b8c80
Date: Thu, 17 Jan 2019 18:18:54 +0100
4b8c80
Subject: [PATCH] bus-message: introduce two kinds of references to bus
4b8c80
 messages
4b8c80
4b8c80
Before this commit bus messages had a single reference count: when it
4b8c80
reached zero the message would be freed. This simple approach meant a
4b8c80
cyclic dependency was typically seen: a message that was enqueued in a
4b8c80
bus connection object would reference the bus connection object but also
4b8c80
itself be referenced by the bus connection object. So far out strategy
4b8c80
to avoid cases like this was: make sure to process the bus connection
4b8c80
regularly so that messages don#t stay queued, and at exit flush/close
4b8c80
the connection so that the message queued would be emptied, and thus the
4b8c80
cyclic dependencies resolved. Im many cases this isn't done properly
4b8c80
however.
4b8c80
4b8c80
With this change, let's address the issue more systematically: let's
4b8c80
break the reference cycle. Specifically, there are now two types of
4b8c80
references to a bus message:
4b8c80
4b8c80
1. A regular one, which keeps both the message and the bus object it is
4b8c80
   associated with pinned.
4b8c80
4b8c80
2. A "queue" reference, which is weaker: it pins the message, but not
4b8c80
   the bus object it is associated with.
4b8c80
4b8c80
The idea is then that regular user handling uses regular references, but
4b8c80
when a message is enqueued on its connection, then this takes a "queue"
4b8c80
reference instead. This then means that a queued message doesn't imply
4b8c80
the connection itself remains pinned, only regular references to the
4b8c80
connection or a message associated with it do. Thus, if we end up in the
4b8c80
situation where a user allocates a bus and a message and enqueues the
4b8c80
latter in the former and drops all refs to both, then this will detect
4b8c80
this case and free both.
4b8c80
4b8c80
Note that this scheme isn't perfect, it only covers references between
4b8c80
messages and the busses they are associated with. If OTOH a bus message
4b8c80
is enqueued on a different bus than it is associated with cyclic deps
4b8c80
cannot be recognized with this simple algorithm, and thus if you enqueue
4b8c80
a message associated with a bus A on a bus B, and another message
4b8c80
associated with bus B on a bus A, a cyclic ref will be in effect and not
4b8c80
be discovered. However, given that this is an exotic case (though one
4b8c80
that happens, consider systemd-bus-stdio-bridge), it should be OK not to
4b8c80
cover with this, and people have to explicit flush all queues on exit in
4b8c80
that case.
4b8c80
4b8c80
Note that this commit only establishes the separate reference counters
4b8c80
per message. A follow-up commit will start making use of this from the
4b8c80
bus connection object.
4b8c80
4b8c80
(cherry picked from commit 1b3f9dd759ca0ea215e7b89f8ce66d1b724497b9)
4b8c80
Related: CVE-2020-1712
4b8c80
---
4b8c80
 src/libsystemd/sd-bus/bus-message.c | 60 ++++++++++++++++++++++++++---
4b8c80
 src/libsystemd/sd-bus/bus-message.h | 14 ++++++-
4b8c80
 2 files changed, 68 insertions(+), 6 deletions(-)
4b8c80
4b8c80
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
b9a53a
index 306b6d6816..7fe8929f82 100644
4b8c80
--- a/src/libsystemd/sd-bus/bus-message.c
4b8c80
+++ b/src/libsystemd/sd-bus/bus-message.c
4b8c80
@@ -120,7 +120,8 @@ static sd_bus_message* message_free(sd_bus_message *m) {
4b8c80
 
4b8c80
         message_reset_parts(m);
4b8c80
 
4b8c80
-        sd_bus_unref(m->bus);
4b8c80
+        /* Note that we don't unref m->bus here. That's already done by sd_bus_message_unref() as each user
4b8c80
+         * reference to the bus message also is considered a reference to the bus connection itself. */
4b8c80
 
4b8c80
         if (m->free_fds) {
4b8c80
                 close_many(m->fds, m->n_fds);
4b8c80
@@ -893,27 +894,76 @@ int bus_message_new_synthetic_error(
4b8c80
 }
4b8c80
 
4b8c80
 _public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
4b8c80
-
4b8c80
         if (!m)
4b8c80
                 return NULL;
4b8c80
 
4b8c80
-        assert(m->n_ref > 0);
4b8c80
+        /* We are fine if this message so far was either explicitly reffed or not reffed but queued into at
4b8c80
+         * least one bus connection object. */
4b8c80
+        assert(m->n_ref > 0 || m->n_queued > 0);
4b8c80
+
4b8c80
         m->n_ref++;
4b8c80
 
4b8c80
+        /* Each user reference to a bus message shall also be considered a ref on the bus */
4b8c80
+        sd_bus_ref(m->bus);
4b8c80
         return m;
4b8c80
 }
4b8c80
 
4b8c80
 _public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
4b8c80
-
4b8c80
         if (!m)
4b8c80
                 return NULL;
4b8c80
 
4b8c80
         assert(m->n_ref > 0);
4b8c80
+
4b8c80
+        sd_bus_unref(m->bus); /* Each regular ref is also a ref on the bus connection. Let's hence drop it
4b8c80
+                               * here. Note we have to do this before decrementing our own n_ref here, since
4b8c80
+                               * otherwise, if this message is currently queued sd_bus_unref() might call
4b8c80
+                               * bus_message_unref_queued() for this which might then destroy the message
4b8c80
+                               * while we are still processing it. */
4b8c80
         m->n_ref--;
4b8c80
 
4b8c80
-        if (m->n_ref > 0)
4b8c80
+        if (m->n_ref > 0 || m->n_queued > 0)
4b8c80
                 return NULL;
4b8c80
 
4b8c80
+        /* Unset the bus field if neither the user has a reference nor this message is queued. We are careful
4b8c80
+         * to reset the field only after the last reference to the bus is dropped, after all we might keep
4b8c80
+         * multiple references to the bus, once for each reference kept on outselves. */
4b8c80
+        m->bus = NULL;
4b8c80
+
4b8c80
+        return message_free(m);
4b8c80
+}
4b8c80
+
4b8c80
+sd_bus_message* bus_message_ref_queued(sd_bus_message *m, sd_bus *bus) {
4b8c80
+        if (!m)
4b8c80
+                return NULL;
4b8c80
+
4b8c80
+        /* If this is a different bus than the message is associated with, then implicitly turn this into a
4b8c80
+         * regular reference. This means that you can create a memory leak by enqueuing a message generated
4b8c80
+         * on one bus onto another at the same time as enqueueing a message from the second one on the first,
4b8c80
+         * as we'll not detect the cyclic references there. */
4b8c80
+        if (bus != m->bus)
4b8c80
+                return sd_bus_message_ref(m);
4b8c80
+
4b8c80
+        assert(m->n_ref > 0 || m->n_queued > 0);
4b8c80
+        m->n_queued++;
4b8c80
+
4b8c80
+        return m;
4b8c80
+}
4b8c80
+
4b8c80
+sd_bus_message* bus_message_unref_queued(sd_bus_message *m, sd_bus *bus) {
4b8c80
+        if (!m)
4b8c80
+                return NULL;
4b8c80
+
4b8c80
+        if (bus != m->bus)
4b8c80
+                return sd_bus_message_unref(m);
4b8c80
+
4b8c80
+        assert(m->n_queued > 0);
4b8c80
+        m->n_queued--;
4b8c80
+
4b8c80
+        if (m->n_ref > 0 || m->n_queued > 0)
4b8c80
+                return NULL;
4b8c80
+
4b8c80
+        m->bus = NULL;
4b8c80
+
4b8c80
         return message_free(m);
4b8c80
 }
4b8c80
 
4b8c80
diff --git a/src/libsystemd/sd-bus/bus-message.h b/src/libsystemd/sd-bus/bus-message.h
4b8c80
index 97f6060e30..ded88005e2 100644
4b8c80
--- a/src/libsystemd/sd-bus/bus-message.h
4b8c80
+++ b/src/libsystemd/sd-bus/bus-message.h
4b8c80
@@ -51,7 +51,16 @@ struct bus_body_part {
4b8c80
 };
4b8c80
 
4b8c80
 struct sd_bus_message {
4b8c80
-        unsigned n_ref;
4b8c80
+        /* Caveat: a message can be referenced in two different ways: the main (user-facing) way will also
4b8c80
+         * pin the bus connection object the message is associated with. The secondary way ("queued") is used
4b8c80
+         * when a message is in the read or write queues of the bus connection object, which will not pin the
4b8c80
+         * bus connection object. This is necessary so that we don't have to have a pair of cyclic references
4b8c80
+         * between a message that is queued and its connection: as soon as a message is only referenced by
4b8c80
+         * the connection (by means of being queued) and the connection itself has no other references it
4b8c80
+         * will be freed. */
4b8c80
+
4b8c80
+        unsigned n_ref;     /* Counter of references that pin the connection */
4b8c80
+        unsigned n_queued;  /* Counter of references that do not pin the connection */
4b8c80
 
4b8c80
         sd_bus *bus;
4b8c80
 
4b8c80
@@ -216,3 +225,6 @@ int bus_message_append_sender(sd_bus_message *m, const char *sender);
4b8c80
 
4b8c80
 void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m);
4b8c80
 void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);
4b8c80
+
4b8c80
+sd_bus_message* bus_message_ref_queued(sd_bus_message *m, sd_bus *bus);
4b8c80
+sd_bus_message* bus_message_unref_queued(sd_bus_message *m, sd_bus *bus);