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) { ...@@ -118,15 +118,16 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
sz = offsetof(struct kdbus_msg, data); sz = offsetof(struct kdbus_msg, data);
/* Add in fixed header, fields header, fields header padding and payload */ /* Add in fixed header, fields header and payload */
sz += 4 * ALIGN8(offsetof(struct kdbus_msg_data, vec) + sizeof(struct kdbus_vec)); 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); sz += ALIGN8(offsetof(struct kdbus_msg_data, data) + b->bloom_size);
/* Add in well-known destination header */ /* Add in well-known destination header */
if (well_known) { if (well_known) {
dl = strlen(m->destination); 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); m->kdbus = aligned_alloc(8, sz);
...@@ -153,15 +154,8 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) { ...@@ -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)); append_payload_vec(&d, m->header, sizeof(*m->header));
if (m->fields) { if (m->fields)
append_payload_vec(&d, m->fields, m->header->fields_size); append_payload_vec(&d, m->fields, ALIGN8(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->body) if (m->body)
append_payload_vec(&d, m->body, m->header->body_size); 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) { ...@@ -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) { 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; int r;
assert(b); assert(b);
...@@ -271,9 +276,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess ...@@ -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; _cleanup_free_ int *fds = NULL;
struct bus_header *h = NULL; struct bus_header *h = NULL;
size_t total, n_bytes = 0, idx = 0; size_t total, n_bytes = 0, idx = 0;
struct kdbus_creds *creds = NULL; const char *destination = NULL, *seclabel = NULL;
uint64_t nsec = 0;
const char *destination = NULL;
int r; int r;
assert(bus); assert(bus);
...@@ -313,12 +316,10 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess ...@@ -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); memcpy(fds + n_fds, d->fds, j);
n_fds += j; n_fds += j;
} else if (d->type == KDBUS_MSG_SRC_CREDS) } else if (d->type == KDBUS_MSG_DST_NAME)
creds = &d->creds;
else if (d->type == KDBUS_MSG_TIMESTAMP)
nsec = d->ts_ns;
else if (d->type == KDBUS_MSG_DST_NAME)
destination = d->str; destination = d->str;
else if (d->type == KDBUS_MSG_SRC_SECLABEL)
seclabel = d->str;
} }
if (!h) if (!h)
...@@ -331,42 +332,46 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess ...@@ -331,42 +332,46 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
if (n_bytes != total) if (n_bytes != total)
return -EBADMSG; 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) if (r < 0)
return r; return r;
KDBUS_MSG_FOREACH_DATA(d, k) { KDBUS_MSG_FOREACH_DATA(d, k) {
size_t l; size_t l;
if (d->type != KDBUS_MSG_PAYLOAD)
continue;
l = d->size - offsetof(struct kdbus_msg_data, data); l = d->size - offsetof(struct kdbus_msg_data, data);
if (idx == sizeof(struct bus_header) &&
l == 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))) {
sd_bus_message_unref(m);
return -EBADMSG;
}
idx += l; if (d->type == KDBUS_MSG_PAYLOAD) {
}
if (creds) { if (idx == sizeof(struct bus_header) &&
m->pid_starttime = creds->starttime / NSEC_PER_USEC; l == ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)))
m->uid = creds->uid; m->fields = d->data;
m->gid = creds->gid; else if (idx == sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)) &&
m->pid = creds->pid; l == BUS_MESSAGE_BODY_SIZE(m))
m->tid = creds->tid; m->body = d->data;
m->uid_valid = m->gid_valid = true; else if (!(idx == 0 && l == sizeof(struct bus_header))) {
} sd_bus_message_unref(m);
return -EBADMSG;
}
m->timestamp = nsec / NSEC_PER_USEC; idx += l;
} 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;
}
r = bus_message_parse_fields(m); r = bus_message_parse_fields(m);
if (r < 0) { if (r < 0) {
......
...@@ -702,24 +702,56 @@ int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) { ...@@ -702,24 +702,56 @@ int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
return 0; 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) if (!m)
return NULL; return NULL;
return m->label; 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) if (!m)
return -EINVAL; return -EINVAL;
if (m->timestamp <= 0) if (m->monotonic <= 0)
return -ENOENT; return -ENOENT;
*usec = m->timestamp; *usec = m->monotonic;
return 0; 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) { int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
if (!m) if (!m)
return -EINVAL; return -EINVAL;
...@@ -2837,6 +2869,7 @@ int bus_message_parse_fields(sd_bus_message *m) { ...@@ -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 bus_message_seal(sd_bus_message *m, uint64_t serial) {
int r; int r;
size_t l, a;
assert(m); assert(m);
...@@ -2859,6 +2892,22 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) { ...@@ -2859,6 +2892,22 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) {
return r; 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->header->serial = serial;
m->sealed = true; m->sealed = true;
...@@ -2933,8 +2982,18 @@ int bus_message_dump(sd_bus_message *m) { ...@@ -2933,8 +2982,18 @@ int bus_message_dump(sd_bus_message *m) {
printf("\tgid=%lu\n", (unsigned long) m->gid); printf("\tgid=%lu\n", (unsigned long) m->gid);
if (m->pid_starttime != 0) if (m->pid_starttime != 0)
printf("\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime); printf("\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
if (m->timestamp) if (m->monotonic != 0)
printf("\ttimestamp=%llu\n", (unsigned long long) m->timestamp); 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); r = sd_bus_message_rewind(m, true);
if (r < 0) { if (r < 0) {
......
...@@ -68,7 +68,8 @@ struct sd_bus_message { ...@@ -68,7 +68,8 @@ struct sd_bus_message {
pid_t pid; pid_t pid;
pid_t tid; pid_t tid;
usec_t pid_starttime; usec_t pid_starttime;
usec_t timestamp; usec_t monotonic;
usec_t realtime;
bool sealed:1; bool sealed:1;
bool dont_send:1; bool dont_send:1;
...@@ -96,7 +97,7 @@ struct sd_bus_message { ...@@ -96,7 +97,7 @@ struct sd_bus_message {
struct bus_container root_container, *containers; struct bus_container root_container, *containers;
unsigned n_containers; unsigned n_containers;
struct iovec iovec[4]; struct iovec iovec[3];
unsigned n_iovec; unsigned n_iovec;
char *peeked_signature; char *peeked_signature;
...@@ -105,6 +106,10 @@ struct sd_bus_message { ...@@ -105,6 +106,10 @@ struct sd_bus_message {
char sender_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1]; char sender_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
char destination_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) #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) { ...@@ -77,15 +77,8 @@ static void bus_message_setup_iovec(sd_bus_message *m) {
append_iovec(m, m->header, sizeof(*m->header)); append_iovec(m, m->header, sizeof(*m->header));
if (m->fields) { if (m->fields)
append_iovec(m, m->fields, m->header->fields_size); append_iovec(m, m->fields, ALIGN8(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->body) if (m->body)
append_iovec(m, m->body, m->header->body_size); append_iovec(m, m->body, m->header->body_size);
......
...@@ -53,6 +53,11 @@ struct kdbus_audit { ...@@ -53,6 +53,11 @@ struct kdbus_audit {
__u64 loginuid; __u64 loginuid;
}; };
struct kdbus_timestamp {
__u64 monotonic_ns;
__u64 realtime_ns;
};
#define KDBUS_SRC_ID_KERNEL (0) #define KDBUS_SRC_ID_KERNEL (0)
#define KDBUS_DST_ID_WELL_KNOWN_NAME (0) #define KDBUS_DST_ID_WELL_KNOWN_NAME (0)
#define KDBUS_MATCH_SRC_ID_ANY (~0ULL) #define KDBUS_MATCH_SRC_ID_ANY (~0ULL)
...@@ -70,7 +75,7 @@ enum { ...@@ -70,7 +75,7 @@ enum {
/* Filled in by kernelspace */ /* Filled in by kernelspace */
KDBUS_MSG_SRC_NAMES = 0x200,/* NUL separated string list with well-known names of source */ 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_CREDS, /* .creds */
KDBUS_MSG_SRC_PID_COMM, /* optional, in .str */ KDBUS_MSG_SRC_PID_COMM, /* optional, in .str */
KDBUS_MSG_SRC_TID_COMM, /* optional, in .str */ KDBUS_MSG_SRC_TID_COMM, /* optional, in .str */
...@@ -110,17 +115,15 @@ struct kdbus_msg_data { ...@@ -110,17 +115,15 @@ struct kdbus_msg_data {
/* inline data */ /* inline data */
__u8 data[0]; __u8 data[0];
char str[0]; char str[0];
__u32 data_u32[0];
__u64 data_u64[0];
/* data vector */ /* data vector */
struct kdbus_vec vec; struct kdbus_vec vec;
/* specific fields */ /* specific fields */
int fds[0]; /* int array of file descriptors */ int fds[0]; /* int array of file descriptors */
__u64 ts_ns; /* timestamp in nanoseconds */
struct kdbus_creds creds; struct kdbus_creds creds;
struct kdbus_audit audit; struct kdbus_audit audit;
struct kdbus_timestamp timestamp;
struct kdbus_manager_msg_name_change name_change; struct kdbus_manager_msg_name_change name_change;
struct kdbus_manager_msg_id_change id_change; struct kdbus_manager_msg_id_change id_change;
}; };
......
...@@ -159,7 +159,7 @@ static int server(sd_bus *bus) { ...@@ -159,7 +159,7 @@ static int server(sd_bus *bus) {
continue; continue;
sd_bus_message_get_pid(m, &pid); 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); */ /* bus_message_dump(m); */
/* sd_bus_message_rewind(m, true); */ /* sd_bus_message_rewind(m, true); */
......
...@@ -33,11 +33,13 @@ extern "C" { ...@@ -33,11 +33,13 @@ extern "C" {
#endif #endif
/* TODO: /* TODO:
* - instead of adding in padding iovec when sending simply extend header buffer
* - add page donation logic * - add page donation logic
* - api for appending/reading fixed arrays * - api for appending/reading fixed arrays
* - merge busctl into systemctl or so? * - merge busctl into systemctl or so?
* - default policy (allow uid == 0 and our own uid) * - 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; typedef struct sd_bus sd_bus;
...@@ -121,13 +123,17 @@ const char *sd_bus_message_get_destination(sd_bus_message *m); ...@@ -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 char *sd_bus_message_get_sender(sd_bus_message *m);
const sd_bus_error *sd_bus_message_get_error(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_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_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_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_tid(sd_bus_message *m, pid_t *tid);
int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec); 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); const char *sd_bus_message_get_selinux_context(sd_bus_message *m);
int sd_bus_message_get_timestamp(sd_bus_message *m, uint64_t *usec); 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_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); 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