From fd8d62d94016d1981f65b9414af2218250fba070 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 10 May 2013 03:36:55 +0200 Subject: [PATCH] bus: catch up with latest kdbus --- src/libsystemd-bus/bus-internal.h | 2 + src/libsystemd-bus/bus-kernel.c | 168 ++++++++++++++++----------- src/libsystemd-bus/bus-message.c | 6 + src/libsystemd-bus/bus-message.h | 3 + src/libsystemd-bus/kdbus.h | 35 ++++-- src/libsystemd-bus/test-bus-kernel.c | 3 + 6 files changed, 136 insertions(+), 81 deletions(-) diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index 4babfac86..05184fd56 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -150,6 +150,8 @@ struct sd_bus { uint64_t hello_serial; unsigned iteration_counter; + + void *kdbus_buffer; }; static inline void bus_unrefp(sd_bus **b) { diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c index 0762b7836..aecf40864 100644 --- a/src/libsystemd-bus/bus-kernel.c +++ b/src/libsystemd-bus/bus-kernel.c @@ -25,6 +25,7 @@ #include #include +#include #include "util.h" @@ -44,6 +45,8 @@ #define KDBUS_ITEM_HEADER_SIZE offsetof(struct kdbus_item, data) #define KDBUS_ITEM_SIZE(s) ALIGN8((s) + KDBUS_ITEM_HEADER_SIZE) +#define KDBUS_BUFFER_SIZE (4*1024*1024) + static int parse_unique_name(const char *s, uint64_t *id) { int r; @@ -111,7 +114,7 @@ static void append_fds(struct kdbus_item **d, const int fds[], unsigned n_fds) { *d = ALIGN8_PTR(*d); (*d)->size = offsetof(struct kdbus_item, fds) + sizeof(int) * n_fds; - (*d)->type = KDBUS_MSG_UNIX_FDS; + (*d)->type = KDBUS_MSG_FDS; memcpy((*d)->fds, fds, sizeof(int) * n_fds); *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size); @@ -275,17 +278,12 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) { } int bus_kernel_take_fd(sd_bus *b) { - struct kdbus_cmd_hello hello = { - .conn_flags = - KDBUS_HELLO_ACCEPT_FD| - KDBUS_HELLO_ATTACH_COMM| - KDBUS_HELLO_ATTACH_EXE| - KDBUS_HELLO_ATTACH_CMDLINE| - KDBUS_HELLO_ATTACH_CGROUP| - KDBUS_HELLO_ATTACH_CAPS| - KDBUS_HELLO_ATTACH_SECLABEL| - KDBUS_HELLO_ATTACH_AUDIT - }; + uint8_t h[ALIGN8(sizeof(struct kdbus_cmd_hello)) + + ALIGN8(KDBUS_ITEM_HEADER_SIZE) + + ALIGN8(sizeof(struct kdbus_vec))] = {}; + + struct kdbus_cmd_hello *hello = (struct kdbus_cmd_hello*) h; + int r; assert(b); @@ -293,20 +291,44 @@ int bus_kernel_take_fd(sd_bus *b) { if (b->is_server) return -EINVAL; - r = ioctl(b->input_fd, KDBUS_CMD_HELLO, &hello); + if (!b->kdbus_buffer) { + b->kdbus_buffer = mmap(NULL, KDBUS_BUFFER_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if (b->kdbus_buffer == MAP_FAILED) { + b->kdbus_buffer = NULL; + return -errno; + } + } + + hello->size = sizeof(h); + hello->conn_flags = + KDBUS_HELLO_ACCEPT_FD| + KDBUS_HELLO_ATTACH_COMM| + KDBUS_HELLO_ATTACH_EXE| + KDBUS_HELLO_ATTACH_CMDLINE| + KDBUS_HELLO_ATTACH_CGROUP| + KDBUS_HELLO_ATTACH_CAPS| + KDBUS_HELLO_ATTACH_SECLABEL| + KDBUS_HELLO_ATTACH_AUDIT; + + hello->items[0].type = KDBUS_HELLO_BUFFER; + hello->items[0].size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec); + hello->items[0].vec.address = (uint64_t) b->kdbus_buffer; + hello->items[0].vec.size = KDBUS_BUFFER_SIZE; + + r = ioctl(b->input_fd, KDBUS_CMD_HELLO, hello); if (r < 0) return -errno; /* The higher 32bit of both flags fields are considered * 'incompatible flags'. Refuse them all for now. */ - if (hello.bus_flags > 0xFFFFFFFFULL || - hello.conn_flags > 0xFFFFFFFFULL) + if (hello->bus_flags > 0xFFFFFFFFULL || + hello->conn_flags > 0xFFFFFFFFULL) return -ENOTSUP; - if (hello.bloom_size != BLOOM_SIZE) + if (hello->bloom_size != BLOOM_SIZE) return -ENOTSUP; - if (asprintf(&b->unique_name, ":1.%llu", (unsigned long long) hello.id) < 0) + if (asprintf(&b->unique_name, ":1.%llu", (unsigned long long) hello->id) < 0) return -ENOMEM; b->is_kernel = true; @@ -356,18 +378,36 @@ int bus_kernel_write_message(sd_bus *bus, sd_bus_message *m) { return 1; } -static void close_kdbus_msg(struct kdbus_msg *k) { +static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) { struct kdbus_item *d; + assert(bus); + assert(k); + + ioctl(bus->input_fd, KDBUS_CMD_MSG_RELEASE, k); + KDBUS_ITEM_FOREACH(d, k) { - if (d->type != KDBUS_MSG_UNIX_FDS) + if (d->type != KDBUS_MSG_FDS) continue; close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int)); } } +static bool range_contains(size_t astart, size_t asize, size_t bstart, size_t bsize, void *a, void **b) { + + if (bstart < astart) + return false; + + if (bstart + bsize > astart + asize) + return false; + + *b = (uint8_t*) a + (bstart - astart); + + return true; +} + static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_message **ret) { sd_bus_message *m = NULL; struct kdbus_item *d; @@ -390,19 +430,19 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess l = d->size - offsetof(struct kdbus_item, data); - if (d->type == KDBUS_MSG_PAYLOAD) { + if (d->type == KDBUS_MSG_PAYLOAD_VEC) { if (!h) { - if (l < sizeof(struct bus_header)) + if (d->vec.size < sizeof(struct bus_header)) return -EBADMSG; - h = (struct bus_header*) d->data; + h = (struct bus_header*) d->vec.address; } n_payload++; - n_bytes += l; + n_bytes += d->vec.size; - } else if (d->type == KDBUS_MSG_UNIX_FDS) { + } else if (d->type == KDBUS_MSG_FDS) { int *f; unsigned j; @@ -431,6 +471,9 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess if (n_bytes != total) return -EBADMSG; + if (n_payload > 2) + return -EBADMSG; + r = bus_message_from_header(h, sizeof(struct bus_header), fds, n_fds, NULL, seclabel, 0, &m); if (r < 0) return r; @@ -440,20 +483,13 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess l = d->size - offsetof(struct kdbus_item, data); - if (d->type == KDBUS_MSG_PAYLOAD) { - - if (idx == sizeof(struct bus_header) && - l == ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m))) - m->fields = d->data; - else if (idx == sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)) && - l == BUS_MESSAGE_BODY_SIZE(m)) - m->body = d->data; - else if (!(idx == 0 && l == sizeof(struct bus_header))) { - sd_bus_message_unref(m); - return -EBADMSG; - } + if (d->type == KDBUS_MSG_PAYLOAD_VEC) { + + range_contains(idx, d->vec.size, ALIGN8(sizeof(struct bus_header)), BUS_MESSAGE_FIELDS_SIZE(m), (void*) d->vec.address, &m->fields); + range_contains(idx, d->vec.size, ALIGN8(sizeof(struct bus_header)) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)), BUS_MESSAGE_BODY_SIZE(m), (void*) d->vec.address, &m->body); + + idx += d->vec.size; - idx += l; } else if (d->type == KDBUS_MSG_SRC_CREDS) { m->pid_starttime = d->creds.starttime / NSEC_PER_USEC; m->uid = d->creds.uid; @@ -480,10 +516,18 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess else if (d->type == KDBUS_MSG_SRC_CAPS) { m->capability = d->data; m->capability_size = l; - } else + } else if (d->type != KDBUS_MSG_FDS && + d->type != KDBUS_MSG_DST_NAME && + d->type != KDBUS_MSG_SRC_SECLABEL) log_debug("Got unknown field from kernel %llu", d->type); } + if ((BUS_MESSAGE_FIELDS_SIZE(m) > 0 && !m->fields) || + (BUS_MESSAGE_BODY_SIZE(m) > 0 && !m->body)) { + sd_bus_message_unref(m); + return -EBADMSG; + } + r = bus_message_parse_fields(m); if (r < 0) { sd_bus_message_unref(m); @@ -509,7 +553,8 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess /* We take possession of the kmsg struct now */ m->kdbus = k; - m->free_kdbus = true; + m->bus = sd_bus_ref(bus); + m->release_kdbus = true; m->free_fds = true; fds = NULL; @@ -520,48 +565,31 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess int bus_kernel_read_message(sd_bus *bus, sd_bus_message **m) { struct kdbus_msg *k; - size_t sz = 1024; int r; assert(bus); assert(m); - for (;;) { - void *q; - - q = memalign(8, sz); - if (!q) - return -errno; - - free(bus->rbuffer); - k = bus->rbuffer = q; - k->size = sz; - - /* Let's tell valgrind that there's really no need to - * initialize this fully. This should be removed again - * when valgrind learned the kdbus ioctls natively. */ -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(k, sz); -#endif - - r = ioctl(bus->input_fd, KDBUS_CMD_MSG_RECV, bus->rbuffer); - if (r >= 0) - break; - + r = ioctl(bus->input_fd, KDBUS_CMD_MSG_RECV, &k); + if (r < 0) { if (errno == EAGAIN) return 0; - if (errno != ENOBUFS) - return -errno; - - sz *= 2; + return -errno; } + +/* /\* Let's tell valgrind that there's really no need to */ +/* * initialize this fully. This should be removed again */ +/* * when valgrind learned the kdbus ioctls natively. *\/ */ +/* #ifdef HAVE_VALGRIND_MEMCHECK_H */ +/* VALGRIND_MAKE_MEM_DEFINED(k, sz); */ +/* #endif */ + + r = bus_kernel_make_message(bus, k, m); - if (r > 0) - bus->rbuffer = NULL; - else - close_kdbus_msg(k); + if (r <= 0) + close_kdbus_msg(bus, k); return r < 0 ? r : 1; } diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c index fdc3ac681..cc2b78a8c 100644 --- a/src/libsystemd-bus/bus-message.c +++ b/src/libsystemd-bus/bus-message.c @@ -66,11 +66,17 @@ static void message_free(sd_bus_message *m) { if (m->free_kdbus) free(m->kdbus); + if (m->release_kdbus) + ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, m->kdbus); + if (m->free_fds) { close_many(m->fds, m->n_fds); free(m->fds); } + if (m->bus) + sd_bus_unref(m->bus); + free(m->cmdline_array); reset_containers(m); diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h index eafbb7c6c..86a41a73b 100644 --- a/src/libsystemd-bus/bus-message.h +++ b/src/libsystemd-bus/bus-message.h @@ -53,6 +53,8 @@ struct bus_header { struct sd_bus_message { unsigned n_ref; + sd_bus *bus; + uint32_t reply_serial; const char *path; @@ -81,6 +83,7 @@ struct sd_bus_message { bool free_body:1; bool free_kdbus:1; bool free_fds:1; + bool release_kdbus:1; struct bus_header *header; void *fields; diff --git a/src/libsystemd-bus/kdbus.h b/src/libsystemd-bus/kdbus.h index db5e243c1..a2eba186c 100644 --- a/src/libsystemd-bus/kdbus.h +++ b/src/libsystemd-bus/kdbus.h @@ -70,14 +70,15 @@ enum { KDBUS_MSG_NULL, /* Filled in by userspace */ - KDBUS_MSG_PAYLOAD, /* .data, inline memory */ KDBUS_MSG_PAYLOAD_VEC, /* .data_vec, reference to memory area */ - KDBUS_MSG_UNIX_FDS, /* .data_fds of file descriptors */ + KDBUS_MSG_PAYLOAD_MEMFD, /* file descriptor of a special data file */ + KDBUS_MSG_FDS, /* .data_fds of file descriptors */ KDBUS_MSG_BLOOM, /* for broadcasts, carries bloom filter blob in .data */ KDBUS_MSG_DST_NAME, /* destination's well-known name, in .str */ + KDBUS_MSG_PRIORITY, /* queue priority for message */ /* Filled in by kernelspace */ - KDBUS_MSG_SRC_NAMES = 0x200,/* NUL separated string list with well-known names of source */ + KDBUS_MSG_SRC_NAMES = 0x400,/* NUL separated string list with well-known names of source */ KDBUS_MSG_TIMESTAMP, /* .timestamp */ KDBUS_MSG_SRC_CREDS, /* .creds */ KDBUS_MSG_SRC_PID_COMM, /* optional, in .str */ @@ -90,7 +91,7 @@ enum { KDBUS_MSG_SRC_AUDIT, /* .audit */ /* Special messages from kernel, consisting of one and only one of these data blocks */ - KDBUS_MSG_NAME_ADD = 0x400,/* .name_change */ + KDBUS_MSG_NAME_ADD = 0x800,/* .name_change */ KDBUS_MSG_NAME_REMOVE, /* .name_change */ KDBUS_MSG_NAME_CHANGE, /* .name_change */ KDBUS_MSG_ID_ADD, /* .id_change */ @@ -99,14 +100,14 @@ enum { KDBUS_MSG_REPLY_DEAD, /* dito */ }; -enum { - KDBUS_VEC_ALIGNED = 1 << 0, -}; - struct kdbus_vec { __u64 address; __u64 size; - __u64 flags; +}; + +struct kdbus_memfd { + __u64 size; + int fd; }; /** @@ -137,6 +138,7 @@ struct kdbus_item { struct kdbus_timestamp timestamp; /* specific fields */ + int fd; int fds[0]; struct kdbus_manager_msg_name_change name_change; struct kdbus_manager_msg_id_change id_change; @@ -236,6 +238,8 @@ enum { /* Items to append to struct kdbus_cmd_hello */ enum { KDBUS_HELLO_NULL, + KDBUS_HELLO_BUFFER, /* kdbus_vec, userspace supplied buffer to + * place received messages */ }; struct kdbus_cmd_hello { @@ -276,7 +280,7 @@ enum { * cgroup membership paths * to messages. */ KDBUS_MAKE_CRED, /* allow translator services which connect * to the bus on behalf of somebody else, - * allow specifying the credentials of the + * allow specifiying the credentials of the * client to connect on behalf on. Needs * privileges */ }; @@ -401,7 +405,8 @@ enum kdbus_cmd { /* kdbus ep node commands: require connected state */ KDBUS_CMD_MSG_SEND = _IOWR(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg), - KDBUS_CMD_MSG_RECV = _IOWR(KDBUS_IOC_MAGIC, 0x41, struct kdbus_msg), + KDBUS_CMD_MSG_RECV = _IOWR(KDBUS_IOC_MAGIC, 0x41, struct kdbus_msg *), + KDBUS_CMD_MSG_RELEASE = _IOWR(KDBUS_IOC_MAGIC, 0x42, struct kdbus_msg), KDBUS_CMD_NAME_ACQUIRE = _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name), KDBUS_CMD_NAME_RELEASE = _IOWR(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name), @@ -414,5 +419,13 @@ enum kdbus_cmd { /* kdbus ep node commands: require ep owner state */ KDBUS_CMD_EP_POLICY_SET = _IOWR(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy), + + /* kdbus ep node commands: */ + KDBUS_CMD_MEMFD_NEW = _IOWR(KDBUS_IOC_MAGIC, 0x80, int *), + + /* kdbus memfd commands: */ + KDBUS_CMD_MEMFD_SIZE_GET = _IOWR(KDBUS_IOC_MAGIC, 0x81, __u64 *), + KDBUS_CMD_MEMFD_SEAL_GET = _IOWR(KDBUS_IOC_MAGIC, 0x82, int *), + KDBUS_CMD_MEMFD_SEAL_SET = _IOWR(KDBUS_IOC_MAGIC, 0x83, int), }; #endif diff --git a/src/libsystemd-bus/test-bus-kernel.c b/src/libsystemd-bus/test-bus-kernel.c index 1095e57e4..72514a3de 100644 --- a/src/libsystemd-bus/test-bus-kernel.c +++ b/src/libsystemd-bus/test-bus-kernel.c @@ -22,6 +22,7 @@ #include #include "util.h" +#include "log.h" #include "sd-bus.h" #include "bus-message.h" @@ -36,6 +37,8 @@ int main(int argc, char *argv[]) { sd_bus *a, *b; int r, pipe_fds[2]; + log_set_max_level(LOG_DEBUG); + bus_ref = bus_kernel_create("deine-mutter", &bus_name); if (bus_ref == -ENOENT) return EXIT_TEST_SKIP; -- 2.30.2