chiark / gitweb /
sd-bus: change serialization of kdbus messages to qualify in their entirety as gvaria...
[elogind.git] / src / libsystemd / sd-bus / bus-kernel.c
index bd829459de225c82fb585cb92e4d91c2b5c71160..be64a12ea247097a42312d4689282d9bf29d2110 100644 (file)
@@ -309,7 +309,7 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
                 m->kdbus->dst_id = destination ? unique : KDBUS_DST_ID_BROADCAST;
 
         m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS;
-        m->kdbus->cookie = (uint64_t) m->header->serial;
+        m->kdbus->cookie = m->header->dbus2.cookie;
         m->kdbus->priority = m->priority;
 
         if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
@@ -411,9 +411,12 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
         struct kdbus_item *d;
         unsigned n_fds = 0;
         _cleanup_free_ int *fds = NULL;
-        struct bus_header *h = NULL;
-        size_t total, n_bytes = 0, idx = 0;
+        struct bus_header *header = NULL;
+        void *footer = NULL;
+        size_t header_size = 0, footer_size = 0;
+        size_t n_bytes = 0, idx = 0;
         const char *destination = NULL, *seclabel = NULL;
+        bool last_was_memfd = false;
         int r;
 
         assert(bus);
@@ -428,21 +431,24 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 switch (d->type) {
 
                 case KDBUS_ITEM_PAYLOAD_OFF:
-                        if (!h) {
-                                h = (struct bus_header *)((uint8_t *)bus->kdbus_buffer + d->vec.offset);
-
-                                if (!bus_header_is_complete(h, d->vec.size))
-                                        return -EBADMSG;
+                        if (!header) {
+                                header = (struct bus_header *)((uint8_t *) bus->kdbus_buffer + d->vec.offset);
+                                header_size = d->vec.size;
                         }
 
+                        footer = (uint8_t*) bus->kdbus_buffer + d->vec.offset;
+                        footer_size = d->vec.size;
+
                         n_bytes += d->vec.size;
+                        last_was_memfd = false;
                         break;
 
                 case KDBUS_ITEM_PAYLOAD_MEMFD:
-                        if (!h)
+                        if (!header) /* memfd cannot be first part */
                                 return -EBADMSG;
 
                         n_bytes += d->memfd.size;
+                        last_was_memfd = true;
                         break;
 
                 case KDBUS_ITEM_FDS: {
@@ -466,23 +472,29 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 }
         }
 
-        if (!h)
+        if (last_was_memfd) /* memfd cannot be last part */
                 return -EBADMSG;
 
-        r = bus_header_message_size(h, &total);
-        if (r < 0)
-                return r;
+        if (!header)
+                return -EBADMSG;
 
-        if (n_bytes != total)
+        if (header_size < sizeof(struct bus_header))
                 return -EBADMSG;
 
         /* on kdbus we only speak native endian gvariant, never dbus1
          * marshalling or reverse endian */
-        if (h->version != 2 ||
-            h->endian != BUS_NATIVE_ENDIAN)
+        if (header->version != 2 ||
+            header->endian != BUS_NATIVE_ENDIAN)
                 return -EPROTOTYPE;
 
-        r = bus_message_from_header(bus, h, sizeof(struct bus_header), fds, n_fds, NULL, seclabel, 0, &m);
+        r = bus_message_from_header(
+                        bus,
+                        header, header_size,
+                        footer, footer_size,
+                        n_bytes,
+                        fds, n_fds,
+                        NULL,
+                        seclabel, 0, &m);
         if (r < 0)
                 return r;
 
@@ -562,10 +574,11 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 case KDBUS_ITEM_PIDS:
 
                         /* The PID/TID might be missing, when the data
-                         * is faked by some data bus proxy and it
-                         * lacks that information about the real
-                         * client since SO_PEERCRED is used for
-                         * that. */
+                         * is faked by a bus proxy and it lacks that
+                         * information about the real client (since
+                         * SO_PEERCRED is used for that). Also kernel
+                         * namespacing might make some of this data
+                         * unavailable when untranslatable. */
 
                         if (d->pids.pid > 0) {
                                 m->creds.pid = (pid_t) d->pids.pid;
@@ -581,7 +594,8 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
 
                 case KDBUS_ITEM_CREDS:
 
-                        /* EUID/SUID/FSUID/EGID/SGID/FSGID might be missing too (see above). */
+                        /* EUID/SUID/FSUID/EGID/SGID/FSGID might be
+                         * missing too (see above). */
 
                         if ((uid_t) d->creds.uid != UID_INVALID) {
                                 m->creds.uid = (uid_t) d->creds.uid;
@@ -659,7 +673,6 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                                 goto fail;
 
                         m->creds.cgroup_root = bus->cgroup_root;
-
                         break;
 
                 case KDBUS_ITEM_AUDIT:
@@ -762,7 +775,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                 goto fail;
 
         /* Refuse messages if kdbus and dbus1 cookie doesn't match up */
-        if ((uint64_t) m->header->serial != k->cookie) {
+        if ((uint64_t) m->header->dbus2.cookie != k->cookie) {
                 r = -EBADMSG;
                 goto fail;
         }
@@ -822,10 +835,6 @@ fail:
 }
 
 int bus_kernel_take_fd(sd_bus *b) {
-        struct kdbus_cmd_free cmd_free = {
-                .size = sizeof(cmd_free),
-                .flags = 0,
-        };
         struct kdbus_bloom_parameter *bloom = NULL;
         struct kdbus_cmd_hello *hello;
         struct kdbus_item_list *items;
@@ -989,12 +998,10 @@ int bus_kernel_take_fd(sd_bus *b) {
 
         /* free returned items */
         (void) bus_kernel_cmd_free(b, hello->offset);
-
         return bus_start_running(b);
 
 fail:
-        cmd_free.offset = hello->offset;
-        (void) ioctl(b->input_fd, KDBUS_CMD_FREE, &cmd_free);
+        (void) bus_kernel_cmd_free(b, hello->offset);
         return r;
 }
 
@@ -1019,7 +1026,6 @@ int bus_kernel_connect(sd_bus *b) {
 int bus_kernel_cmd_free(sd_bus *bus, uint64_t offset) {
         struct kdbus_cmd_free cmd = {
                 .size = sizeof(cmd),
-                .flags = 0,
                 .offset = offset,
         };
         int r;
@@ -1741,30 +1747,21 @@ int bus_kernel_fix_attach_mask(void) {
 
         /* By default we don't want any kdbus metadata fields to be
          * suppressed, hence we reset the kernel mask for it to
-         * (uint64_t) -1. This is overridable via a kernel command
-         * line option, however. */
+         * (uint64_t) -1. If the module argument was overwritten by
+         * the kernel cmdline, we leave it as is. */
 
-        r = get_proc_cmdline_key("systemd.kdbus_attach_flags_mask=", &mask);
+        r = get_proc_cmdline_key("kdbus.attach_flags_mask=", &mask);
         if (r < 0)
                 return log_warning_errno(r, "Failed to read kernel command line: %m");
 
-        if (mask) {
-                const char *p = mask;
-
-                if (startswith(p, "0x"))
-                        p += 2;
-
-                if (sscanf(p, "%" PRIx64, &m) != 1)
-                        log_warning("Couldn't parse systemd.kdbus_attach_flags_mask= kernel command line parameter.");
+        if (r == 0) {
+                sprintf(buf, "0x%" PRIx64 "\n", m);
+                r = write_string_file("/sys/module/kdbus/parameters/attach_flags_mask", buf);
+                if (r < 0)
+                        return log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
+                                              "Failed to write kdbus attach mask: %m");
         }
 
-        sprintf(buf, "0x%" PRIx64 "\n", m);
-        r = write_string_file("/sys/module/kdbus/parameters/attach_flags_mask", buf);
-        if (r < 0)
-                return log_full_errno(
-                                IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
-                                "Failed to write kdbus attach mask: %m");
-
         return 0;
 }