c2dfb7
From 8efdae75ddf035c8c04983820f8d8cf767cc17b1 Mon Sep 17 00:00:00 2001
c2dfb7
From: Jan Synacek <jsynacek@redhat.com>
c2dfb7
Date: Fri, 31 Jan 2020 11:34:45 +0100
c2dfb7
Subject: [PATCH] sd-bus: introduce API for re-enqueuing incoming messages
c2dfb7
c2dfb7
When authorizing via PolicyKit we want to process incoming method calls
c2dfb7
twice: once to process and figure out that we need PK authentication,
c2dfb7
and a second time after we aquired PK authentication to actually execute
c2dfb7
the operation. With this new call sd_bus_enqueue_for_read() we have a
c2dfb7
way to put an incoming message back into the read queue for this
c2dfb7
purpose.
c2dfb7
c2dfb7
This might have other uses too, for example debugging.
c2dfb7
Related: CVE-2020-1712
c2dfb7
---
c2dfb7
 src/libsystemd/libsystemd.sym  |  1 +
c2dfb7
 src/libsystemd/sd-bus/sd-bus.c | 24 ++++++++++++++++++++++++
c2dfb7
 src/systemd/sd-bus.h           |  1 +
c2dfb7
 3 files changed, 26 insertions(+)
c2dfb7
c2dfb7
diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym
c2dfb7
index 1eec17db50..e9972593a6 100644
c2dfb7
--- a/src/libsystemd/libsystemd.sym
c2dfb7
+++ b/src/libsystemd/libsystemd.sym
c2dfb7
@@ -569,4 +569,5 @@ global:
c2dfb7
         sd_event_source_get_inotify_mask;
c2dfb7
         sd_event_source_set_destroy_callback;
c2dfb7
         sd_event_source_get_destroy_callback;
c2dfb7
+        sd_bus_enqueue_for_read;
c2dfb7
 } LIBSYSTEMD_238;
c2dfb7
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
c2dfb7
index e49d58137d..68ad6cbe89 100644
c2dfb7
--- a/src/libsystemd/sd-bus/sd-bus.c
c2dfb7
+++ b/src/libsystemd/sd-bus/sd-bus.c
c2dfb7
@@ -4120,3 +4120,27 @@ _public_ int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret) {
c2dfb7
         *ret = bus->wqueue_size;
c2dfb7
         return 0;
c2dfb7
 }
c2dfb7
+
c2dfb7
+_public_ int sd_bus_enqueue_for_read(sd_bus *bus, sd_bus_message *m) {
c2dfb7
+        int r;
c2dfb7
+
c2dfb7
+        assert_return(bus, -EINVAL);
c2dfb7
+        assert_return(bus = bus_resolve(bus), -ENOPKG);
c2dfb7
+        assert_return(m, -EINVAL);
c2dfb7
+        assert_return(m->sealed, -EINVAL);
c2dfb7
+        assert_return(!bus_pid_changed(bus), -ECHILD);
c2dfb7
+
c2dfb7
+        if (!BUS_IS_OPEN(bus->state))
c2dfb7
+                return -ENOTCONN;
c2dfb7
+
c2dfb7
+        /* Re-enqeue a message for reading. This is primarily useful for PolicyKit-style authentication,
c2dfb7
+         * where we want accept a message, then determine we need to interactively authenticate the user, and
c2dfb7
+         * when we have that process the message again. */
c2dfb7
+
c2dfb7
+        r = bus_rqueue_make_room(bus);
c2dfb7
+        if (r < 0)
c2dfb7
+                return r;
c2dfb7
+
c2dfb7
+        bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
c2dfb7
+        return 0;
c2dfb7
+}
c2dfb7
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
c2dfb7
index 54c4b1ca83..9ba757b13d 100644
c2dfb7
--- a/src/systemd/sd-bus.h
c2dfb7
+++ b/src/systemd/sd-bus.h
c2dfb7
@@ -193,6 +193,7 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **r);
c2dfb7
 int sd_bus_process_priority(sd_bus *bus, int64_t max_priority, sd_bus_message **r);
c2dfb7
 int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec);
c2dfb7
 int sd_bus_flush(sd_bus *bus);
c2dfb7
+int sd_bus_enqueue_for_read(sd_bus *bus, sd_bus_message *m);
c2dfb7
 
c2dfb7
 sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus);
c2dfb7
 sd_bus_message* sd_bus_get_current_message(sd_bus *bus);