bus-kernel.c revision 606303a93ea52a70ebba55bb3152820e630f2164
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2013 Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (at your option) any later version.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is distributed in the hope that it will be useful, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Lesser General Public License for more details.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering/* When we include libgen.h because we need dirname() we immediately
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * undefine basename() since libgen.h defines it as a macro to the XDG
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * version which is really broken. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint bus_kernel_parse_unique_name(const char *s, uint64_t *id) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void append_payload_vec(struct kdbus_item **d, const void *p, size_t sz) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Note that p can be NULL, which encodes a region full of
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * zeroes, which is useful to optimize certain padding
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * conditions */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering (*d)->size = offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void append_payload_memfd(struct kdbus_item **d, int memfd, size_t start, size_t sz) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (*d)->size = offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void append_destination(struct kdbus_item **d, const char *s, size_t length) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering (*d)->size = offsetof(struct kdbus_item, str) + length + 1;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic struct kdbus_bloom_filter *append_bloom(struct kdbus_item **d, size_t length) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering i->size = offsetof(struct kdbus_item, bloom_filter) +
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering offsetof(struct kdbus_bloom_filter, data) +
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering *d = (struct kdbus_item *) ((uint8_t*) i + i->size);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void append_fds(struct kdbus_item **d, const int fds[], unsigned n_fds) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (*d)->size = offsetof(struct kdbus_item, fds) + sizeof(int) * n_fds;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering memcpy((*d)->fds, fds, sizeof(int) * n_fds);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void add_bloom_arg(void *data, size_t size, unsigned n_hash, unsigned i, const char *t) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *(e++) = '0' + (char) i;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_pair(data, size, n_hash, buf, t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_prefixes(data, size, n_hash, buf, t, '.');
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_prefixes(data, size, n_hash, buf, t, '/');
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mackstatic int bus_message_setup_bloom(sd_bus_message *m, struct kdbus_bloom_filter *bloom) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "message-type", bus_message_type_to_string(m->header->type));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "interface", m->interface);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "member", m->member);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "path", m->path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "path-slash-prefix", m->path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom_add_prefixes(data, m->bus->bloom_size, m->bus->bloom_n_hash, "path-slash-prefix", m->path, '/');
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for (i = 0; i < 64; i++) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering const char *t, *contents;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_peek_type(m, &type, &contents);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* The bloom filter includes simple strings of any kind */
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering r = sd_bus_message_read_basic(m, type, &t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering add_bloom_arg(data, m->bus->bloom_size, m->bus->bloom_n_hash, i, t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } if (type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g")) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* As well as array of simple strings of any kinds */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_enter_container(m, type, contents);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering while ((r = sd_bus_message_read_basic(m, contents[0], &t)) > 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering add_bloom_arg(data, m->bus->bloom_size, m->bus->bloom_n_hash, i, t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Stop adding to bloom filter as soon as we
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * run into the first argument we cannot add
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* We put this together only once, if this message is reused
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * we reuse the earlier-built version */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering destination = m->destination ?: m->destination_ptr;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = bus_kernel_parse_unique_name(destination, &unique);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Add in fixed header, fields header and payload */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sz += (1 + m->n_body_parts) * ALIGN8(offsetof(struct kdbus_item, vec) +
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sizeof(struct kdbus_memfd)));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Add space for bloom filter */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, bloom_filter) +
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering offsetof(struct kdbus_bloom_filter, data) +
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Add in well-known destination header */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, str) + dl + 1);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Add space for unix fds */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, fds) + sizeof(int)*m->n_fds);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering ((m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) ? 0 : KDBUS_MSG_EXPECT_REPLY) |
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering ((m->header->flags & BUS_MESSAGE_NO_AUTO_START) ? KDBUS_MSG_NO_AUTO_START : 0) |
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek ((m->header->type == SD_BUS_MESSAGE_SIGNAL) ? KDBUS_MSG_SIGNAL : 0);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering /* verify_destination_id will usually be 0, which makes the kernel driver only look
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * at the provided well-known name. Otherwise, the kernel will make sure the provided
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * destination id matches the owner of the provided weel-known-name, and fail if they
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * differ. Currently, this is only needed for bus-proxyd. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->kdbus->dst_id = m->verify_destination_id;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->kdbus->dst_id = destination ? unique : KDBUS_DST_ID_BROADCAST;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->kdbus->cookie = m->header->dbus2.cookie;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->kdbus->cookie_reply = m->reply_cookie;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_se(clock_gettime(CLOCK_MONOTONIC_COARSE, &now) == 0);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->kdbus->timeout_ns = now.tv_sec * NSEC_PER_SEC + now.tv_nsec +
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_payload_vec(&d, m->header, BUS_MESSAGE_BODY_BEGIN(m));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* If this is padding then simply send a
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * vector with a NULL data pointer which the
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * kernel will just pass through. This is the
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * most efficient way to encode zeroes */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_payload_vec(&d, NULL, part->size);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (part->memfd >= 0 && part->sealed && destination) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Try to send a memfd, if the part is
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * sealed and this is not a broadcast. Since we can only */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering append_payload_memfd(&d, part->memfd, part->memfd_offset, part->size);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Otherwise, let's send a vector to the actual data.
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * For that, we need to map it first. */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering append_payload_vec(&d, part->data, part->size);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (m->header->type == SD_BUS_MESSAGE_SIGNAL) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bloom = append_bloom(&d, m->bus->bloom_size);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->kdbus->size = (uint8_t*) d - (uint8_t*) m->kdbus;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void unset_memfds(struct sd_bus_message *m) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Make sure the memfds are not freed twice */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void message_set_timestamp(sd_bus *bus, sd_bus_message *m, const struct kdbus_timestamp *ts) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!(bus->attach_flags & KDBUS_ATTACH_TIMESTAMP))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->realtime = ts->realtime_ns / NSEC_PER_USEC;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->monotonic = ts->monotonic_ns / NSEC_PER_USEC;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering const char *destination = NULL, *seclabel = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(k->payload_type == KDBUS_PAYLOAD_DBUS);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering l = d->size - offsetof(struct kdbus_item, data);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering header = (struct bus_header*)((uint8_t*) k + d->vec.offset);
db2cb23b5b179707000d28a11efb3d888d06ee80Umut Tezduyar Lindskog if (!header) /* memfd cannot be first part */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering j = l / sizeof(int);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering f = realloc(fds, sizeof(int) * (n_fds + j));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering memcpy(fds + n_fds, d->fds, sizeof(int) * j);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (last_was_memfd) /* memfd cannot be last part */
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack /* on kdbus we only speak native endian gvariant, never dbus1
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack * marshalling or reverse endian */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* The well-known names list is different from the other
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering credentials. If we asked for it, but nothing is there, this
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering means that the list of well-known names is simply empty, not
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering that we lack any data */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering l = d->size - offsetof(struct kdbus_item, data);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Contains body material */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* A -1 offset is NUL padding. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering part->data = (uint8_t* )k + d->vec.offset;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering part->data = (uint8_t*) k + d->vec.offset + (begin_body - idx);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering part->size = d->vec.size - (begin_body - idx);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* The PID/TID might be missing, when the data
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * is faked by a bus proxy and it lacks that
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * information about the real client (since
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * SO_PEERCRED is used for that). Also kernel
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * namespacing might make some of this data
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering * unavailable when untranslatable. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_PID & bus->creds_mask;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->creds.mask |= SD_BUS_CREDS_TID & bus->creds_mask;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* EUID/SUID/FSUID/EGID/SGID/FSGID might be
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * missing too (see above). */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((uid_t) d->creds.uid != UID_INVALID) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_UID & bus->creds_mask;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if ((uid_t) d->creds.euid != UID_INVALID) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_EUID & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((uid_t) d->creds.suid != UID_INVALID) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_SUID & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((uid_t) d->creds.fsuid != UID_INVALID) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_FSUID & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((gid_t) d->creds.gid != GID_INVALID) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->creds.mask |= SD_BUS_CREDS_GID & bus->creds_mask;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if ((gid_t) d->creds.egid != GID_INVALID) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->creds.mask |= SD_BUS_CREDS_EGID & bus->creds_mask;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if ((gid_t) d->creds.sgid != GID_INVALID) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->creds.mask |= SD_BUS_CREDS_SGID & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_FSGID & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering message_set_timestamp(bus, m, &d->timestamp);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_COMM & bus->creds_mask;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->creds.mask |= SD_BUS_CREDS_TID_COMM & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_EXE & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_CMDLINE & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= (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) & bus->creds_mask;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if ((uint32_t) d->audit.sessionid != (uint32_t) -1) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.audit_session_id = (uint32_t) d->audit.sessionid;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_AUDIT_SESSION_ID & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((uid_t) d->audit.loginuid != UID_INVALID) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.audit_login_uid = (uid_t) d->audit.loginuid;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering m->creds.mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (d->caps.last_cap != cap_last_cap() ||
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering d->size - offsetof(struct kdbus_item, caps.caps) < DIV_ROUND_UP(d->caps.last_cap, 32U) * 4 * 4) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering m->creds.mask |= (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS) & bus->creds_mask;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!service_name_is_valid(d->name.name)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (bus->creds_mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* We just extend the array here, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * do not allocate the strings inside
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * of it, instead we just point to our
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * buffer directly. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n = strv_length(m->creds.well_known_names);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering wkn = realloc(m->creds.well_known_names, (n + 2) * sizeof(char*));
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering m->creds.mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering m->creds.mask |= SD_BUS_CREDS_DESCRIPTION & bus->creds_mask;
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering if (bus->creds_mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering n = (d->size - offsetof(struct kdbus_item, data64)) / sizeof(uint64_t);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering for (i = 0; i < n; i++)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering m->creds.mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering log_debug("Got unknown field from kernel %llu", d->type);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering /* If we requested the list of well-known names to be appended
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * and the sender had none no item for it will be
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * attached. However, this does *not* mean that the kernel
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * didn't want to provide this information to us. Hence, let's
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * explicitly mark this information as available if it was
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * requested. */
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering m->creds.mask |= bus->creds_mask & SD_BUS_CREDS_WELL_KNOWN_NAMES;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering /* Refuse messages if kdbus and dbus1 cookie doesn't match up */
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if ((uint64_t) m->header->dbus2.cookie != k->cookie) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering /* Refuse messages where the reply flag doesn't match up */
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) != !!(k->flags & KDBUS_MSG_EXPECT_REPLY)) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering /* Refuse reply messages where the reply cookie doesn't match up */
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering if ((m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) && m->reply_cookie != k->cookie_reply) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering /* Refuse messages where the autostart flag doesn't match up */
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (!(m->header->flags & BUS_MESSAGE_NO_AUTO_START) != !(k->flags & KDBUS_MSG_NO_AUTO_START)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Override information from the user header with data from the kernel */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering snprintf(m->sender_buffer, sizeof(m->sender_buffer), ":1.%llu", (unsigned long long) k->src_id);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering m->sender = m->creds.unique_name = m->sender_buffer;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else if (k->dst_id == KDBUS_DST_ID_BROADCAST)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering m->destination = bus->unique_name; /* fill in unique name if the well-known name is missing */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering snprintf(m->destination_buffer, sizeof(m->destination_buffer), ":1.%llu", (unsigned long long) k->dst_id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* We take possession of the kmsg struct now */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering struct kdbus_bloom_parameter *bloom = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* If no name is explicitly set, we'll include a hint
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * indicating the library implementation, a hint which
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * kind of bus this is and the thread name */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering g = strappend(b->is_system ? "sd-system-" :
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering b->description = bus_label_unescape(name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sz = ALIGN8(offsetof(struct kdbus_cmd_hello, items)) +
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering ALIGN8(offsetof(struct kdbus_item, str) + m + 1);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, creds) + sizeof(struct kdbus_creds));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, pids) + sizeof(struct kdbus_pids));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sz += ALIGN8(offsetof(struct kdbus_item, str) + l + 1);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering hello->attach_flags_send = _KDBUS_ATTACH_ANY;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering hello->attach_flags_recv = b->attach_flags;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering item->size = offsetof(struct kdbus_item, str) + m + 1;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering item->type = KDBUS_ITEM_CONN_DESCRIPTION;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering item->size = offsetof(struct kdbus_item, creds) + sizeof(struct kdbus_creds);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering item->size = offsetof(struct kdbus_item, pids) + sizeof(struct kdbus_pids);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering item->size = offsetof(struct kdbus_item, str) + l + 1;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = ioctl(b->input_fd, KDBUS_CMD_HELLO, hello);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering b->kdbus_buffer = mmap(NULL, KDBUS_POOL_SIZE, PROT_READ, MAP_SHARED, b->input_fd, 0);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* The higher 32bit of the bus_flags fields are considered
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * 'incompatible flags'. Refuse them all for now. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* extract bloom parameters from items */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering items = (void*)((uint8_t*)b->kdbus_buffer + hello->offset);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering KDBUS_FOREACH(item, items, hello->items_size) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!bloom || !bloom_validate_parameters((size_t) bloom->size, (unsigned) bloom->n_hash)) {
700ff4d97311902a440109a2c081731ab6ae8a20Lennart Poettering b->bloom_n_hash = (unsigned) bloom->n_hash;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (asprintf(&b->unique_name, ":1.%llu", (unsigned long long) hello->id) < 0) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering b->can_fds = !!(hello->flags & KDBUS_HELLO_ACCEPT_FD);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* the kernel told us the UUID of the underlying bus */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering memcpy(b->server_id.bytes, hello->id128, sizeof(b->server_id.bytes));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* free returned items */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (void) bus_kernel_cmd_free(b, hello->offset);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (void) bus_kernel_cmd_free(b, hello->offset);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering b->input_fd = open(b->kernel, O_RDWR|O_NOCTTY|O_CLOEXEC);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint bus_kernel_cmd_free(sd_bus *bus, uint64_t offset) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd);
struct kdbus_item *d;
assert(k);
assert(m);
* kernel, so that it can pass CPU time/scheduling to the
if (hint_sync_call) {
sd_bus_error_setf(&error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Destination %s not known", m->destination);
log_debug("Could not deliver message to %s as destination is not known. Ignoring.", m->destination);
sd_bus_error_setf(&error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Activation of %s not requested", m->destination);
log_debug("Could not deliver message to %s as destination is not activated. Ignoring.", m->destination);
return -errno;
bus,
&error,
&reply);
} else if (hint_sync_call) {
struct kdbus_msg *k;
assert(k);
log_debug("Ignoring message with unknown payload type %llu.", (unsigned long long) k->payload_type);
static int push_name_owner_changed(
const char *name,
const char *old_owner,
const char *new_owner,
bus,
"/org/freedesktop/DBus",
m = NULL;
static int translate_name_change(
const struct kdbus_msg *k,
const struct kdbus_item *d,
assert(k);
assert(d);
if (d->type == KDBUS_ITEM_NAME_ADD || (d->name_change.old_id.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR)))
old_owner[0] = 0;
if (d->type == KDBUS_ITEM_NAME_REMOVE || (d->name_change.new_id.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR))) {
new_owner[0] = 0;
static int translate_id_change(
const struct kdbus_msg *k,
const struct kdbus_item *d,
assert(k);
assert(d);
return push_name_owner_changed(
ts);
static int translate_reply(
const struct kdbus_msg *k,
const struct kdbus_item *d,
assert(k);
assert(d);
bus,
k->cookie_reply,
m = NULL;
static int (* const translate[])(sd_bus *bus, const struct kdbus_msg *k, const struct kdbus_item *d, const struct kdbus_timestamp *ts) = {
assert(k);
if (d->type >= _KDBUS_ITEM_KERNEL_BASE && d->type < _KDBUS_ITEM_KERNEL_BASE + ELEMENTSOF(translate)) {
if (found)
return -EBADMSG;
found = d;
if (!found) {
struct kdbus_msg *k;
if (hint_priority) {
log_debug("%s: kdbus reports %" PRIu64 " dropped broadcast messages, ignoring.", strna(bus->description), (uint64_t) recv.dropped_msgs);
return -errno;
log_debug("Ignoring message with unknown payload type %llu.", (unsigned long long) k->payload_type);
struct memfd_cache *c;
int fd;
return -ENOTSUP;
*mapped = 0;
*allocated = 0;
return fd;
if (size > 0)
struct memfd_cache *c;
assert(b);
for (i = 0; i < b->n_memfd_cache; i++)
uint64_t f = 0;
f |= KDBUS_NAME_QUEUE;
uint64_t m = 0;
m |= KDBUS_ATTACH_CREDS;
m |= KDBUS_ATTACH_PIDS;
m |= KDBUS_ATTACH_PID_COMM;
m |= KDBUS_ATTACH_TID_COMM;
m |= KDBUS_ATTACH_EXE;
m |= KDBUS_ATTACH_CMDLINE;
if (mask & (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))
m |= KDBUS_ATTACH_CGROUP;
if (mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS))
m |= KDBUS_ATTACH_CAPS;
m |= KDBUS_ATTACH_SECLABEL;
m |= KDBUS_ATTACH_AUDIT;
m |= KDBUS_ATTACH_NAMES;
m |= KDBUS_ATTACH_AUXGROUPS;
struct kdbus_item *n;
size_t l;
int fd;
assert(s);
if (fd < 0)
return -errno;
sizeof(struct kdbus_bloom_parameter);
n = KDBUS_ITEM_NEXT(n);
n = KDBUS_ITEM_NEXT(n);
n = KDBUS_ITEM_NEXT(n);
return -errno;
return -ENOMEM;
return fd;
int fd;
if (path) {
return -ENOMEM;
if (fd < 0) {
if (path)
free(p);
return -errno;
if (path)
*path = p;
return fd;
struct kdbus_item *n;
const char *name;
int fd;
if (fd < 0)
return fd;
return -errno;
if (ep_path) {
return -ENOMEM;
*ep_path = p;
return fd;
return -errno;
return -errno;
struct kdbus_item *n;
return -errno;
int bus_kernel_fix_attach_mask(void) {
char *n = NULL;
return -errno;
return -EIO;
*name = n;