chiark / gitweb /
sd-bus: rework credential query logic
[elogind.git] / src / libsystemd / sd-bus / bus-control.c
index 076bbce..71fdbcf 100644 (file)
@@ -58,12 +58,15 @@ static int bus_request_name_kernel(sd_bus *bus, const char *name, uint64_t flags
         assert(bus);
         assert(name);
 
-        l = strlen(name);
-        size = offsetof(struct kdbus_cmd_name, name) + l + 1;
-        n = alloca0(size);
+        l = strlen(name) + 1;
+        size = offsetof(struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(l);
+        n = alloca0_align(size, 8);
         n->size = size;
-        kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
-        memcpy(n->name, name, l+1);
+        n->flags = request_name_flags_to_kdbus(flags);
+
+        n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
+        n->items[0].type = KDBUS_ITEM_NAME;
+        memcpy(n->items[0].str, name, l);
 
 #ifdef HAVE_VALGRIND_MEMCHECK_H
         VALGRIND_MAKE_MEM_DEFINED(n, n->size);
@@ -144,16 +147,20 @@ _public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags)
 
 static int bus_release_name_kernel(sd_bus *bus, const char *name) {
         struct kdbus_cmd_name *n;
-        size_t l;
+        size_t size, l;
         int r;
 
         assert(bus);
         assert(name);
 
-        l = strlen(name);
-        n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
-        n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
-        memcpy(n->name, name, l+1);
+        l = strlen(name) + 1;
+        size = offsetof(struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(l);
+        n = alloca0_align(size, 8);
+        n->size = size;
+
+        n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
+        n->items[0].type = KDBUS_ITEM_NAME;
+        memcpy(n->items[0].str, name, l);
 
 #ifdef HAVE_VALGRIND_MEMCHECK_H
         VALGRIND_MAKE_MEM_DEFINED(n, n->size);
@@ -162,7 +169,7 @@ static int bus_release_name_kernel(sd_bus *bus, const char *name) {
         if (r < 0)
                 return -errno;
 
-        return n->flags;
+        return 0;
 }
 
 static int bus_release_name_dbus1(sd_bus *bus, const char *name) {
@@ -219,7 +226,7 @@ _public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
 static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
         struct kdbus_cmd_name_list cmd = {};
         struct kdbus_name_list *name_list;
-        struct kdbus_cmd_name *name;
+        struct kdbus_name_info *name;
         uint64_t previous_id = 0;
         int r;
 
@@ -235,31 +242,42 @@ static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
 
         KDBUS_ITEM_FOREACH(name, name_list, names) {
 
+                struct kdbus_item *item;
+                const char *entry_name = NULL;
+
                 if ((flags & KDBUS_NAME_LIST_UNIQUE) && name->owner_id != previous_id) {
                         char *n;
 
-                        if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
-                                return -ENOMEM;
+                        if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0) {
+                                r = -ENOMEM;
+                                goto fail;
+                        }
 
                         r = strv_consume(x, n);
                         if (r < 0)
-                                return r;
+                                goto fail;
 
                         previous_id = name->owner_id;
                 }
 
-                if (name->size > sizeof(*name) && service_name_is_valid(name->name)) {
-                        r = strv_extend(x, name->name);
-                        if (r < 0)
-                                return -ENOMEM;
+                KDBUS_ITEM_FOREACH(item, name, items)
+                        if (item->type == KDBUS_ITEM_OWNED_NAME)
+                                entry_name = item->name.name;
+
+                if (entry_name && service_name_is_valid(entry_name)) {
+                        r = strv_extend(x, entry_name);
+                        if (r < 0) {
+                                r = -ENOMEM;
+                                goto fail;
+                        }
                 }
         }
 
-        r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
-        if (r < 0)
-                return -errno;
+        r = 0;
 
-        return 0;
+fail:
+        bus_kernel_cmd_free(bus, cmd.offset);
+        return r;
 }
 
 static int bus_list_names_kernel(sd_bus *bus, char ***acquired, char ***activatable) {
@@ -357,90 +375,92 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatabl
                 return bus_list_names_dbus1(bus, acquired, activatable);
 }
 
-static int bus_get_owner_kdbus(
+static int bus_populate_creds_from_items(
                 sd_bus *bus,
-                const char *name,
+                struct kdbus_info *info,
                 uint64_t mask,
-                sd_bus_creds **creds) {
+                sd_bus_creds *c) {
 
-        _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
-        struct kdbus_cmd_conn_info *cmd;
-        struct kdbus_conn_info *conn_info;
         struct kdbus_item *item;
-        size_t size;
-        uint64_t m, id;
+        uint64_t m;
         int r;
 
-        r = bus_kernel_parse_unique_name(name, &id);
-        if (r < 0)
-                return r;
-        if (r > 0) {
-                size = offsetof(struct kdbus_cmd_conn_info, name);
-                cmd = alloca0(size);
-                cmd->id = id;
-        } else {
-                size = offsetof(struct kdbus_cmd_conn_info, name) + strlen(name) + 1;
-                cmd = alloca0(size);
-                strcpy(cmd->name, name);
-        }
-
-        cmd->size = size;
-        kdbus_translate_attach_flags(mask, (uint64_t*) &cmd->flags);
+        assert(bus);
+        assert(info);
+        assert(c);
 
-        r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd);
-        if (r < 0)
-                return -errno;
+        KDBUS_ITEM_FOREACH(item, info, items) {
 
-        conn_info = (struct kdbus_conn_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset);
+                switch (item->type) {
 
-        /* Non-activated names are considered not available */
-        if (conn_info->flags & KDBUS_HELLO_ACTIVATOR)
-                return name[0] == ':' ? -ENXIO : -ESRCH;
+                case KDBUS_ITEM_PIDS:
 
-        c = bus_creds_new();
-        if (!c)
-                return -ENOMEM;
-
-        if (mask & SD_BUS_CREDS_UNIQUE_NAME) {
-                if (asprintf(&c->unique_name, ":1.%llu", (unsigned long long) conn_info->id) < 0)
-                        return -ENOMEM;
+                        if (mask & SD_BUS_CREDS_PID && item->pids.pid > 0) {
+                                c->pid = (pid_t) item->pids.pid;
+                                c->mask |= SD_BUS_CREDS_PID;
+                        }
 
-                c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
-        }
+                        if (mask & SD_BUS_CREDS_TID && item->pids.tid > 0) {
+                                c->tid = (pid_t) item->pids.tid;
+                                c->mask |= SD_BUS_CREDS_TID;
+                        }
 
-        KDBUS_ITEM_FOREACH(item, conn_info, items) {
+                        if (mask & SD_BUS_CREDS_PID_STARTTIME && item->pids.starttime > 0) {
+                                c->pid_starttime = item->pids.starttime;
+                                c->mask |= SD_BUS_CREDS_PID_STARTTIME;
+                        }
 
-                switch (item->type) {
+                        break;
 
                 case KDBUS_ITEM_CREDS:
-                        m = (SD_BUS_CREDS_UID | SD_BUS_CREDS_GID | SD_BUS_CREDS_PID) & mask;
 
-                        if (m) {
+                        if (mask & SD_BUS_CREDS_UID && (uid_t) item->creds.uid != (uid_t) -1) {
                                 c->uid = (uid_t) item->creds.uid;
-                                c->pid = (pid_t) item->creds.pid;
+                                c->mask |= SD_BUS_CREDS_UID;
+                        }
+
+                        if (mask & SD_BUS_CREDS_EUID && (uid_t) item->creds.euid != (uid_t) -1) {
+                                c->euid = (uid_t) item->creds.euid;
+                                c->mask |= SD_BUS_CREDS_EUID;
+                        }
+
+                        if (mask & SD_BUS_CREDS_SUID && (uid_t) item->creds.suid != (uid_t) -1) {
+                                c->suid = (uid_t) item->creds.suid;
+                                c->mask |= SD_BUS_CREDS_SUID;
+                        }
+
+                        if (mask & SD_BUS_CREDS_FSUID && (uid_t) item->creds.fsuid != (uid_t) -1) {
+                                c->fsuid = (uid_t) item->creds.fsuid;
+                                c->mask |= SD_BUS_CREDS_FSUID;
+                        }
+
+                        if (mask & SD_BUS_CREDS_GID && (gid_t) item->creds.gid != (gid_t) -1) {
                                 c->gid = (gid_t) item->creds.gid;
-                                c->mask |= m;
+                                c->mask |= SD_BUS_CREDS_GID;
                         }
 
-                        if (mask & SD_BUS_CREDS_TID && item->creds.tid > 0) {
-                                c->tid = (pid_t) item->creds.tid;
-                                c->mask |= SD_BUS_CREDS_TID;
+                        if (mask & SD_BUS_CREDS_EGID && (gid_t) item->creds.egid != (gid_t) -1) {
+                                c->egid = (gid_t) item->creds.egid;
+                                c->mask |= SD_BUS_CREDS_EGID;
                         }
 
-                        if (mask & SD_BUS_CREDS_PID_STARTTIME && item->creds.starttime > 0) {
-                                c->pid_starttime = item->creds.starttime;
-                                c->mask |= SD_BUS_CREDS_PID_STARTTIME;
+                        if (mask & SD_BUS_CREDS_SGID && (gid_t) item->creds.sgid != (gid_t) -1) {
+                                c->sgid = (gid_t) item->creds.sgid;
+                                c->mask |= SD_BUS_CREDS_SGID;
+                        }
+
+                        if (mask & SD_BUS_CREDS_FSGID && (gid_t) item->creds.fsgid != (gid_t) -1) {
+                                c->fsgid = (gid_t) item->creds.fsgid;
+                                c->mask |= SD_BUS_CREDS_FSGID;
                         }
 
                         break;
 
                 case KDBUS_ITEM_PID_COMM:
                         if (mask & SD_BUS_CREDS_COMM) {
-                                c->comm = strdup(item->str);
-                                if (!c->comm) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                r = free_and_strdup(&c->comm, item->str);
+                                if (r < 0)
+                                        return r;
 
                                 c->mask |= SD_BUS_CREDS_COMM;
                         }
@@ -448,11 +468,9 @@ static int bus_get_owner_kdbus(
 
                 case KDBUS_ITEM_TID_COMM:
                         if (mask & SD_BUS_CREDS_TID_COMM) {
-                                c->tid_comm = strdup(item->str);
-                                if (!c->tid_comm) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                r = free_and_strdup(&c->tid_comm, item->str);
+                                if (r < 0)
+                                        return r;
 
                                 c->mask |= SD_BUS_CREDS_TID_COMM;
                         }
@@ -460,11 +478,9 @@ static int bus_get_owner_kdbus(
 
                 case KDBUS_ITEM_EXE:
                         if (mask & SD_BUS_CREDS_EXE) {
-                                c->exe = strdup(item->str);
-                                if (!c->exe) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                r = free_and_strdup(&c->exe, item->str);
+                                if (r < 0)
+                                        return r;
 
                                 c->mask |= SD_BUS_CREDS_EXE;
                         }
@@ -472,12 +488,10 @@ static int bus_get_owner_kdbus(
 
                 case KDBUS_ITEM_CMDLINE:
                         if (mask & SD_BUS_CREDS_CMDLINE) {
-                                c->cmdline_size = item->size - KDBUS_ITEM_HEADER_SIZE;
+                                c->cmdline_size = item->size - offsetof(struct kdbus_item, data);
                                 c->cmdline = memdup(item->data, c->cmdline_size);
-                                if (!c->cmdline) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                if (!c->cmdline)
+                                        return -ENOMEM;
 
                                 c->mask |= SD_BUS_CREDS_CMDLINE;
                         }
@@ -489,23 +503,17 @@ static int bus_get_owner_kdbus(
                              SD_BUS_CREDS_SESSION | SD_BUS_CREDS_OWNER_UID) & mask;
 
                         if (m) {
-                                c->cgroup = strdup(item->str);
-                                if (!c->cgroup) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
-
-                                if (!bus->cgroup_root) {
-                                        r = cg_get_root_path(&bus->cgroup_root);
-                                        if (r < 0)
-                                                goto fail;
-                                }
-
-                                c->cgroup_root = strdup(bus->cgroup_root);
-                                if (!c->cgroup_root) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                r = free_and_strdup(&c->cgroup, item->str);
+                                if (r < 0)
+                                        return r;
+
+                                r = bus_get_root_path(bus);
+                                if (r < 0)
+                                        return r;
+
+                                r = free_and_strdup(&c->cgroup_root, bus->cgroup_root);
+                                if (r < 0)
+                                        return r;
 
                                 c->mask |= m;
                         }
@@ -516,12 +524,10 @@ static int bus_get_owner_kdbus(
                              SD_BUS_CREDS_INHERITABLE_CAPS | SD_BUS_CREDS_BOUNDING_CAPS) & mask;
 
                         if (m) {
-                                c->capability_size = item->size - KDBUS_ITEM_HEADER_SIZE;
-                                c->capability = memdup(item->data, c->capability_size);
-                                if (!c->capability) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                c->capability_size = item->size - offsetof(struct kdbus_item, caps.caps);
+                                c->capability = memdup(item->caps.caps, c->capability_size);
+                                if (!c->capability)
+                                        return -ENOMEM;
 
                                 c->mask |= m;
                         }
@@ -529,50 +535,161 @@ static int bus_get_owner_kdbus(
 
                 case KDBUS_ITEM_SECLABEL:
                         if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
-                                c->label = strdup(item->str);
-                                if (!c->label) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                r = free_and_strdup(&c->label, item->str);
+                                if (r < 0)
+                                        return r;
 
                                 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
                         }
                         break;
 
                 case KDBUS_ITEM_AUDIT:
-                        m = (SD_BUS_CREDS_AUDIT_SESSION_ID | SD_BUS_CREDS_AUDIT_LOGIN_UID) & mask;
+                        if (mask & SD_BUS_CREDS_AUDIT_SESSION_ID && (uint32_t) item->audit.sessionid != (uint32_t) -1) {
+                                c->audit_session_id = (uint32_t) item->audit.sessionid;
+                                c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
+                        }
 
-                        if (m) {
-                                c->audit_session_id = item->audit.sessionid;
-                                c->audit_login_uid = item->audit.loginuid;
-                                c->mask |= m;
+                        if (mask & SD_BUS_CREDS_AUDIT_LOGIN_UID && (uid_t) item->audit.loginuid != (uid_t) -1) {
+                                c->audit_login_uid = (uid_t) item->audit.loginuid;
+                                c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
                         }
                         break;
 
-                case KDBUS_ITEM_NAME:
+                case KDBUS_ITEM_OWNED_NAME:
                         if ((mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) && service_name_is_valid(item->name.name)) {
                                 r = strv_extend(&c->well_known_names, item->name.name);
                                 if (r < 0)
-                                        goto fail;
+                                        return r;
 
                                 c->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
                         }
                         break;
 
-                case KDBUS_ITEM_CONN_NAME:
-                        if ((mask & SD_BUS_CREDS_CONNECTION_NAME)) {
-                                c->conn_name = strdup(item->str);
-                                if (!c->conn_name) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                case KDBUS_ITEM_CONN_DESCRIPTION:
+                        if (mask & SD_BUS_CREDS_DESCRIPTION) {
+                                r = free_and_strdup(&c->description, item->str);
+                                if (r < 0)
+                                        return r;
 
-                                c->mask |= SD_BUS_CREDS_CONNECTION_NAME;
+                                c->mask |= SD_BUS_CREDS_DESCRIPTION;
+                        }
+                        break;
+
+                case KDBUS_ITEM_AUXGROUPS:
+                        if (mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
+                                size_t n;
+                                uid_t *g;
+
+                                assert_cc(sizeof(gid_t) == sizeof(uint32_t));
+
+                                n = (item->size - offsetof(struct kdbus_item, data32)) / sizeof(uint32_t);
+                                g = newdup(gid_t, item->data32, n);
+                                if (!g)
+                                        return -ENOMEM;
+
+                                free(c->supplementary_gids);
+                                c->supplementary_gids = g;
+                                c->n_supplementary_gids = n;
+
+                                c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
                         }
                         break;
                 }
         }
 
+        return 0;
+}
+
+static int bus_get_name_creds_kdbus(
+                sd_bus *bus,
+                const char *name,
+                uint64_t mask,
+                sd_bus_creds **creds) {
+
+        _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
+        struct kdbus_cmd_info *cmd;
+        struct kdbus_info *conn_info;
+        size_t size, l;
+        uint64_t id;
+        int r;
+
+        r = bus_kernel_parse_unique_name(name, &id);
+        if (r < 0)
+                return r;
+        if (r > 0) {
+                size = offsetof(struct kdbus_cmd_info, items);
+                cmd = alloca0_align(size, 8);
+                cmd->id = id;
+        } else {
+                l = strlen(name) + 1;
+                size = offsetof(struct kdbus_cmd_info, items) + KDBUS_ITEM_SIZE(l);
+                cmd = alloca0_align(size, 8);
+                cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
+                cmd->items[0].type = KDBUS_ITEM_NAME;
+                memcpy(cmd->items[0].str, name, l);
+        }
+
+        cmd->size = size;
+        cmd->flags = attach_flags_to_kdbus(mask);
+
+        /* If augmentation is on, and the bus doesn't didn't allow us
+         * to get the bits we want, then ask for the PID/TID so that we
+         * can read the rest from /proc. */
+        if ((mask & SD_BUS_CREDS_AUGMENT) &&
+            (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+                     SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
+                     SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
+                     SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
+                     SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
+                     SD_BUS_CREDS_SELINUX_CONTEXT|
+                     SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))
+                cmd->flags |= KDBUS_ATTACH_PIDS;
+
+        r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd);
+        if (r < 0)
+                return -errno;
+
+        conn_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset);
+
+        /* Non-activated names are considered not available */
+        if (conn_info->flags & KDBUS_HELLO_ACTIVATOR) {
+                if (name[0] == ':')
+                        r = -ENXIO;
+                else
+                        r = -ESRCH;
+                goto fail;
+        }
+
+        c = bus_creds_new();
+        if (!c) {
+                r = -ENOMEM;
+                goto fail;
+        }
+
+        if (mask & SD_BUS_CREDS_UNIQUE_NAME) {
+                if (asprintf(&c->unique_name, ":1.%llu", (unsigned long long) conn_info->id) < 0) {
+                        r = -ENOMEM;
+                        goto fail;
+                }
+
+                c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
+        }
+
+        /* If KDBUS_ITEM_OWNED_NAME is requested then we'll get 0 of
+           them in case the service has no names. This does not mean
+           however that the list of owned names could not be
+           acquired. Hence, let's explicitly clarify that the data is
+           complete. */
+        c->mask |= mask & SD_BUS_CREDS_WELL_KNOWN_NAMES;
+
+        r = bus_populate_creds_from_items(bus, conn_info, mask, c);
+        if (r < 0)
+                goto fail;
+
+        r = bus_creds_add_more(c, mask, 0, 0);
+        if (r < 0)
+                goto fail;
+
         if (creds) {
                 *creds = c;
                 c = NULL;
@@ -581,11 +698,11 @@ static int bus_get_owner_kdbus(
         r = 0;
 
 fail:
-        ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd->offset);
+        bus_kernel_cmd_free(bus, cmd->offset);
         return r;
 }
 
-static int bus_get_owner_dbus1(
+static int bus_get_name_creds_dbus1(
                 sd_bus *bus,
                 const char *name,
                 uint64_t mask,
@@ -631,11 +748,17 @@ static int bus_get_owner_dbus1(
                         c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
                 }
 
-                if (mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_GID|
-                            SD_BUS_CREDS_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
-                            SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
-                            SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
-                            SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)) {
+                if ((mask & SD_BUS_CREDS_PID) ||
+                    ((mask & SD_BUS_CREDS_AUGMENT) &&
+                     (mask & (SD_BUS_CREDS_PID_STARTTIME|
+                              SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+                              SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
+                              SD_BUS_CREDS_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
+                              SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
+                              SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
+                              SD_BUS_CREDS_SELINUX_CONTEXT|
+                              SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))) {
+
                         uint32_t u;
 
                         r = sd_bus_call_method(
@@ -691,6 +814,7 @@ static int bus_get_owner_dbus1(
                 }
 
                 if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
+                        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
                         const void *p = NULL;
                         size_t sz = 0;
 
@@ -700,22 +824,24 @@ static int bus_get_owner_dbus1(
                                         "/org/freedesktop/DBus",
                                         "org.freedesktop.DBus",
                                         "GetConnectionSELinuxSecurityContext",
-                                        NULL,
+                                        &error,
                                         &reply,
                                         "s",
                                         unique ? unique : name);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_bus_message_read_array(reply, 'y', &p, &sz);
-                        if (r < 0)
-                                return r;
+                        if (r < 0) {
+                                if (!sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown"))
+                                        return r;
+                        } else {
+                                r = sd_bus_message_read_array(reply, 'y', &p, &sz);
+                                if (r < 0)
+                                        return r;
 
-                        c->label = strndup(p, sz);
-                        if (!c->label)
-                                return -ENOMEM;
+                                c->label = strndup(p, sz);
+                                if (!c->label)
+                                        return -ENOMEM;
 
-                        c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
+                                c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
+                        }
                 }
 
                 r = bus_creds_add_more(c, mask, pid, 0);
@@ -731,7 +857,7 @@ static int bus_get_owner_dbus1(
         return 0;
 }
 
-_public_ int sd_bus_get_owner(
+_public_ int sd_bus_get_name_creds(
                 sd_bus *bus,
                 const char *name,
                 uint64_t mask,
@@ -739,7 +865,7 @@ _public_ int sd_bus_get_owner(
 
         assert_return(bus, -EINVAL);
         assert_return(name, -EINVAL);
-        assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP);
+        assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -ENOTSUP);
         assert_return(mask == 0 || creds, -EINVAL);
         assert_return(!bus_pid_changed(bus), -ECHILD);
         assert_return(service_name_is_valid(name), -EINVAL);
@@ -749,9 +875,118 @@ _public_ int sd_bus_get_owner(
                 return -ENOTCONN;
 
         if (bus->is_kernel)
-                return bus_get_owner_kdbus(bus, name, mask, creds);
+                return bus_get_name_creds_kdbus(bus, name, mask, creds);
+        else
+                return bus_get_name_creds_dbus1(bus, name, mask, creds);
+}
+
+static int bus_get_owner_creds_kdbus(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
+        _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
+        struct kdbus_cmd_info cmd = {
+                .size = sizeof(struct kdbus_cmd_info)
+        };
+        struct kdbus_info *creator_info;
+        pid_t pid = 0;
+        int r;
+
+        c = bus_creds_new();
+        if (!c)
+                return -ENOMEM;
+
+        cmd.flags = attach_flags_to_kdbus(mask);
+
+        /* If augmentation is on, and the bus doesn't didn't allow us
+         * to get the bits we want, then ask for the PID/TID so that we
+         * can read the rest from /proc. */
+        if ((mask & SD_BUS_CREDS_AUGMENT) &&
+            (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+                     SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
+                     SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
+                     SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|
+                     SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
+                     SD_BUS_CREDS_SELINUX_CONTEXT|
+                     SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))
+                cmd.flags |= KDBUS_ATTACH_PIDS;
+
+        r = ioctl(bus->input_fd, KDBUS_CMD_BUS_CREATOR_INFO, &cmd);
+        if (r < 0)
+                return -errno;
+
+        creator_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
+
+        r = bus_populate_creds_from_items(bus, creator_info, mask, c);
+        bus_kernel_cmd_free(bus, cmd.offset);
+        if (r < 0)
+                return r;
+
+        r = bus_creds_add_more(c, mask, pid, 0);
+        if (r < 0)
+                return r;
+
+        *ret = c;
+        c = NULL;
+        return 0;
+}
+
+static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
+        _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
+        pid_t pid = 0;
+        int r;
+
+        if (!bus->ucred_valid && !isempty(bus->label))
+                return -ENODATA;
+
+        c = bus_creds_new();
+        if (!c)
+                return -ENOMEM;
+
+        if (bus->ucred_valid) {
+                if (bus->ucred.pid > 0) {
+                        pid = c->pid = bus->ucred.pid;
+                        c->mask |= SD_BUS_CREDS_PID & mask;
+                }
+
+                if (bus->ucred.uid != (uid_t) -1) {
+                        c->uid = bus->ucred.uid;
+                        c->mask |= SD_BUS_CREDS_UID & mask;
+                }
+
+                if (bus->ucred.gid != (gid_t) -1) {
+                        c->gid = bus->ucred.gid;
+                        c->mask |= SD_BUS_CREDS_GID & mask;
+                }
+        }
+
+        if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
+                c->label = strdup(bus->label);
+                if (!c->label)
+                        return -ENOMEM;
+
+                c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
+        }
+
+        r = bus_creds_add_more(c, mask, pid, 0);
+        if (r < 0)
+                return r;
+
+        *ret = c;
+        c = NULL;
+        return 0;
+}
+
+_public_ int sd_bus_get_owner_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
+        assert_return(bus, -EINVAL);
+        assert_return((mask & ~SD_BUS_CREDS_AUGMENT) <= _SD_BUS_CREDS_ALL, -ENOTSUP);
+        assert_return(ret, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+
+        if (bus->is_kernel)
+                return bus_get_owner_creds_kdbus(bus, mask, ret);
         else
-                return bus_get_owner_dbus1(bus, name, mask, creds);
+                return bus_get_owner_creds_dbus1(bus, mask, ret);
 }
 
 static int add_name_change_match(sd_bus *bus,
@@ -829,7 +1064,7 @@ static int add_name_change_match(sd_bus *bus,
                             offsetof(struct kdbus_notify_name_change, name) +
                             l);
 
-                m = alloca0(sz);
+                m = alloca0_align(sz, 8);
                 m->size = sz;
                 m->cookie = cookie;
 
@@ -839,8 +1074,8 @@ static int add_name_change_match(sd_bus *bus,
                         offsetof(struct kdbus_notify_name_change, name) +
                         l;
 
-                item->name_change.old.id = old_owner_id;
-                item->name_change.new.id = new_owner_id;
+                item->name_change.old_id.id = old_owner_id;
+                item->name_change.new_id.id = new_owner_id;
 
                 if (name)
                         memcpy(item->name_change.name, name, l);
@@ -889,7 +1124,7 @@ static int add_name_change_match(sd_bus *bus,
                             offsetof(struct kdbus_item, id_change) +
                             sizeof(struct kdbus_notify_id_change));
 
-                m = alloca0(sz);
+                m = alloca0_align(sz, 8);
                 m->size = sz;
                 m->cookie = cookie;
 
@@ -1059,7 +1294,7 @@ int bus_add_match_internal_kernel(
         if (using_bloom)
                 sz += ALIGN8(offsetof(struct kdbus_item, data64) + bus->bloom_size);
 
-        m = alloca0(sz);
+        m = alloca0_align(sz, 8);
         m->size = sz;
         m->cookie = cookie;
 
@@ -1203,7 +1438,7 @@ int bus_remove_match_internal(
                 return bus_remove_match_internal_dbus1(bus, match);
 }
 
-_public_ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
+_public_ int sd_bus_get_name_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
         const char *mid;
         int r;