Commit 69aec65c authored by Lennart Poettering's avatar Lennart Poettering

kdbus: parse even more kernel meta data fields

parent 5b7d4c1c
......@@ -118,15 +118,16 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
sz = offsetof(struct kdbus_msg, data);
/* Add in fixed header, fields header, fields header padding and payload */
sz += 4 * ALIGN8(offsetof(struct kdbus_msg_data, vec) + sizeof(struct kdbus_vec));
/* Add in fixed header, fields header and payload */
sz += 3 * ALIGN8(offsetof(struct kdbus_msg_data, vec) + sizeof(struct kdbus_vec));
/* Add space for bloom filter */
sz += ALIGN8(offsetof(struct kdbus_msg_data, data) + b->bloom_size);
/* Add in well-known destination header */
if (well_known) {
dl = strlen(m->destination);
sz += ALIGN8(offsetof(struct kdbus_msg, data) + dl + 1);
sz += ALIGN8(offsetof(struct kdbus_msg_data, str) + dl + 1);
}
m->kdbus = aligned_alloc(8, sz);
......@@ -153,15 +154,8 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
append_payload_vec(&d, m->header, sizeof(*m->header));
if (m->fields) {
append_payload_vec(&d, m->fields, m->header->fields_size);
if (m->header->fields_size % 8 != 0) {
static const uint8_t padding[7] = {};
append_payload_vec(&d, padding, 8 - (m->header->fields_size % 8));
}
}
if (m->fields)
append_payload_vec(&d, m->fields, ALIGN8(m->header->fields_size));
if (m->body)
append_payload_vec(&d, m->body, m->header->body_size);
......@@ -184,7 +178,18 @@ 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 = {};
struct kdbus_cmd_hello hello = {
.conn_flags =
KDBUS_CMD_HELLO_ACCEPT_FD|
KDBUS_CMD_HELLO_ACCEPT_MMAP|
KDBUS_CMD_HELLO_ATTACH_COMM|
KDBUS_CMD_HELLO_ATTACH_EXE|
KDBUS_CMD_HELLO_ATTACH_CMDLINE|
KDBUS_CMD_HELLO_ATTACH_CGROUP|
KDBUS_CMD_HELLO_ATTACH_CAPS|
KDBUS_CMD_HELLO_ATTACH_SECLABEL|
KDBUS_CMD_HELLO_ATTACH_AUDIT
};
int r;
assert(b);
......@@ -271,9 +276,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
_cleanup_free_ int *fds = NULL;
struct bus_header *h = NULL;
size_t total, n_bytes = 0, idx = 0;
struct kdbus_creds *creds = NULL;
uint64_t nsec = 0;
const char *destination = NULL;
const char *destination = NULL, *seclabel = NULL;
int r;
assert(bus);
......@@ -313,12 +316,10 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
memcpy(fds + n_fds, d->fds, j);
n_fds += j;
} else if (d->type == KDBUS_MSG_SRC_CREDS)
creds = &d->creds;
else if (d->type == KDBUS_MSG_TIMESTAMP)
nsec = d->ts_ns;
else if (d->type == KDBUS_MSG_DST_NAME)
} else if (d->type == KDBUS_MSG_DST_NAME)
destination = d->str;
else if (d->type == KDBUS_MSG_SRC_SECLABEL)
seclabel = d->str;
}
if (!h)
......@@ -331,43 +332,47 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
if (n_bytes != total)
return -EBADMSG;
r = bus_message_from_header(h, sizeof(struct bus_header), fds, n_fds, NULL, NULL, 0, &m);
r = bus_message_from_header(h, sizeof(struct bus_header), fds, n_fds, NULL, seclabel, 0, &m);
if (r < 0)
return r;
KDBUS_MSG_FOREACH_DATA(d, k) {
size_t l;
if (d->type != KDBUS_MSG_PAYLOAD)
continue;
l = d->size - offsetof(struct kdbus_msg_data, data);
if (d->type == KDBUS_MSG_PAYLOAD) {
if (idx == sizeof(struct bus_header) &&
l == BUS_MESSAGE_FIELDS_SIZE(m))
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)) &&
!(idx == sizeof(struct bus_header) + BUS_MESSAGE_FIELDS_SIZE(m))) {
else if (!(idx == 0 && l == sizeof(struct bus_header))) {
sd_bus_message_unref(m);
return -EBADMSG;
}
idx += l;
}
if (creds) {
m->pid_starttime = creds->starttime / NSEC_PER_USEC;
m->uid = creds->uid;
m->gid = creds->gid;
m->pid = creds->pid;
m->tid = creds->tid;
} else if (d->type == KDBUS_MSG_SRC_CREDS) {
m->pid_starttime = d->creds.starttime / NSEC_PER_USEC;
m->uid = d->creds.uid;
m->gid = d->creds.gid;
m->pid = d->creds.pid;
m->tid = d->creds.tid;
m->uid_valid = m->gid_valid = true;
} else if (d->type == KDBUS_MSG_TIMESTAMP) {
m->realtime = d->timestamp.realtime_ns / NSEC_PER_USEC;
m->monotonic = d->timestamp.monotonic_ns / NSEC_PER_USEC;
} else if (d->type == KDBUS_MSG_SRC_PID_COMM)
m->comm = d->str;
else if (d->type == KDBUS_MSG_SRC_TID_COMM)
m->tid_comm = d->str;
else if (d->type == KDBUS_MSG_SRC_EXE)
m->exe = d->str;
}
m->timestamp = nsec / NSEC_PER_USEC;
r = bus_message_parse_fields(m);
if (r < 0) {
sd_bus_message_unref(m);
......
......@@ -702,24 +702,56 @@ int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
return 0;
}
const char *sd_bus_message_get_label(sd_bus_message *m) {
const char *sd_bus_message_get_selinux_context(sd_bus_message *m) {
if (!m)
return NULL;
return m->label;
}
int sd_bus_message_get_timestamp(sd_bus_message *m, uint64_t *usec) {
int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
if (!m)
return -EINVAL;
if (m->timestamp <= 0)
if (m->monotonic <= 0)
return -ENOENT;
*usec = m->timestamp;
*usec = m->monotonic;
return 0;
}
int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
if (!m)
return -EINVAL;
if (m->realtime <= 0)
return -ENOENT;
*usec = m->realtime;
return 0;
}
const char *sd_bus_message_get_comm(sd_bus_message *m) {
if (!m)
return NULL;
return m->comm;
}
const char *sd_bus_message_get_tid_comm(sd_bus_message *m) {
if (!m)
return NULL;
return m->tid_comm;
}
const char *sd_bus_message_get_exe(sd_bus_message *m) {
if (!m)
return NULL;
return m->exe;
}
int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
if (!m)
return -EINVAL;
......@@ -2837,6 +2869,7 @@ int bus_message_parse_fields(sd_bus_message *m) {
int bus_message_seal(sd_bus_message *m, uint64_t serial) {
int r;
size_t l, a;
assert(m);
......@@ -2859,6 +2892,22 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) {
return r;
}
l = BUS_MESSAGE_FIELDS_SIZE(m);
a = ALIGN8(l) - l;
if (a > 0) {
/* Add padding at the end, since we know the body
* needs to start at an 8 byte alignment. */
void *p;
p = message_extend_fields(m, 1, a);
if (!p)
return -ENOMEM;
memset(p, 0, a);
m->header->fields_size -= a;
}
m->header->serial = serial;
m->sealed = true;
......@@ -2933,8 +2982,18 @@ int bus_message_dump(sd_bus_message *m) {
printf("\tgid=%lu\n", (unsigned long) m->gid);
if (m->pid_starttime != 0)
printf("\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
if (m->timestamp)
printf("\ttimestamp=%llu\n", (unsigned long long) m->timestamp);
if (m->monotonic != 0)
printf("\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
if (m->realtime != 0)
printf("\trealtime=%llu\n", (unsigned long long) m->realtime);
if (m->exe)
printf("\texe=[%s]\n", m->exe);
if (m->comm)
printf("\tcomm=[%s]\n", m->comm);
if (m->tid_comm)
printf("\ttid_comm=[%s]\n", m->tid_comm);
if (m->label)
printf("\tlabel=[%s]\n", m->label);
r = sd_bus_message_rewind(m, true);
if (r < 0) {
......
......@@ -68,7 +68,8 @@ struct sd_bus_message {
pid_t pid;
pid_t tid;
usec_t pid_starttime;
usec_t timestamp;
usec_t monotonic;
usec_t realtime;
bool sealed:1;
bool dont_send:1;
......@@ -96,7 +97,7 @@ struct sd_bus_message {
struct bus_container root_container, *containers;
unsigned n_containers;
struct iovec iovec[4];
struct iovec iovec[3];
unsigned n_iovec;
char *peeked_signature;
......@@ -105,6 +106,10 @@ struct sd_bus_message {
char sender_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
char destination_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
const char *exe;
const char *comm;
const char *tid_comm;
};
#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_NATIVE_ENDIAN)
......
......@@ -77,15 +77,8 @@ static void bus_message_setup_iovec(sd_bus_message *m) {
append_iovec(m, m->header, sizeof(*m->header));
if (m->fields) {
append_iovec(m, m->fields, m->header->fields_size);
if (m->header->fields_size % 8 != 0) {
static const uint8_t padding[7] = {};
append_iovec(m, padding, 8 - (m->header->fields_size % 8));
}
}
if (m->fields)
append_iovec(m, m->fields, ALIGN8(m->header->fields_size));
if (m->body)
append_iovec(m, m->body, m->header->body_size);
......
......@@ -53,6 +53,11 @@ struct kdbus_audit {
__u64 loginuid;
};
struct kdbus_timestamp {
__u64 monotonic_ns;
__u64 realtime_ns;
};
#define KDBUS_SRC_ID_KERNEL (0)
#define KDBUS_DST_ID_WELL_KNOWN_NAME (0)
#define KDBUS_MATCH_SRC_ID_ANY (~0ULL)
......@@ -70,7 +75,7 @@ enum {
/* Filled in by kernelspace */
KDBUS_MSG_SRC_NAMES = 0x200,/* NUL separated string list with well-known names of source */
KDBUS_MSG_TIMESTAMP, /* .ts_ns of CLOCK_MONOTONIC */
KDBUS_MSG_TIMESTAMP, /* .timestamp */
KDBUS_MSG_SRC_CREDS, /* .creds */
KDBUS_MSG_SRC_PID_COMM, /* optional, in .str */
KDBUS_MSG_SRC_TID_COMM, /* optional, in .str */
......@@ -110,17 +115,15 @@ struct kdbus_msg_data {
/* inline data */
__u8 data[0];
char str[0];
__u32 data_u32[0];
__u64 data_u64[0];
/* data vector */
struct kdbus_vec vec;
/* specific fields */
int fds[0]; /* int array of file descriptors */
__u64 ts_ns; /* timestamp in nanoseconds */
struct kdbus_creds creds;
struct kdbus_audit audit;
struct kdbus_timestamp timestamp;
struct kdbus_manager_msg_name_change name_change;
struct kdbus_manager_msg_id_change id_change;
};
......
......@@ -159,7 +159,7 @@ static int server(sd_bus *bus) {
continue;
sd_bus_message_get_pid(m, &pid);
log_info("Got message! member=%s pid=%lu label=%s", strna(sd_bus_message_get_member(m)), (unsigned long) pid, strna(sd_bus_message_get_label(m)));
log_info("Got message! member=%s pid=%lu label=%s", strna(sd_bus_message_get_member(m)), (unsigned long) pid, strna(sd_bus_message_get_selinux_context(m)));
/* bus_message_dump(m); */
/* sd_bus_message_rewind(m, true); */
......
......@@ -33,11 +33,13 @@ extern "C" {
#endif
/* TODO:
* - instead of adding in padding iovec when sending simply extend header buffer
* - add page donation logic
* - api for appending/reading fixed arrays
* - merge busctl into systemctl or so?
* - default policy (allow uid == 0 and our own uid)
*
* - enforce alignment of pointers passed in
* - negotiation for attach attributes
*/
typedef struct sd_bus sd_bus;
......@@ -121,13 +123,17 @@ const char *sd_bus_message_get_destination(sd_bus_message *m);
const char *sd_bus_message_get_sender(sd_bus_message *m);
const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m);
int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec);
int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec);
int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid);
int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid);
int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid);
int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid);
int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec);
const char *sd_bus_message_get_label(sd_bus_message *m);
int sd_bus_message_get_timestamp(sd_bus_message *m, uint64_t *usec);
const char *sd_bus_message_get_selinux_context(sd_bus_message *m);
const char *sd_bus_message_get_comm(sd_bus_message *m);
const char *sd_bus_message_get_tid_comm(sd_bus_message *m);
const char *sd_bus_message_get_exe(sd_bus_message *m);
int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member);
int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment