return 0;
}
-int bus_message_from_malloc(void *buffer, size_t length, sd_bus_message **ret) {
+int bus_message_from_malloc(
+ void *buffer,
+ size_t length,
+ struct ucred *ucred,
+ const char *label,
+ sd_bus_message **ret) {
+
sd_bus_message *m;
struct bus_header *h;
- size_t total, fs, bs;
+ size_t total, fs, bs, label_sz, a;
int r;
assert(buffer || length <= 0);
if (length != total)
return -EBADMSG;
- m = new0(sd_bus_message, 1);
+ if (label) {
+ label_sz = strlen(label);
+ a = ALIGN(sizeof(sd_bus_message)) + label_sz + 1;
+ } else
+ a = sizeof(sd_bus_message);
+
+ m = malloc0(a);
if (!m)
return -ENOMEM;
m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN_TO(fs, 8);
m->sealed = true;
+ if (ucred) {
+ m->uid = ucred->uid;
+ m->pid = ucred->pid;
+ m->gid = ucred->gid;
+ m->uid_valid = m->gid_valid = true;
+ }
+
+ if (label) {
+ m->label = (char*) m + ALIGN(sizeof(sd_bus_message));
+ memcpy(m->label, label, label_sz + 1);
+ }
+
m->n_iovec = 1;
m->iovec[0].iov_base = buffer;
m->iovec[0].iov_len = length;
return 0;
}
+const char *sd_bus_message_get_label(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ return m->label;
+}
+
int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
if (!m)
return -EINVAL;
if (!contents)
return -EINVAL;
+ /*
+ * We enforce a global limit on container depth, that is much
+ * higher than the 32 structs and 32 arrays the specification
+ * mandates. This is simpler to implement for us, and we need
+ * this only to ensure our container array doesn't grow
+ * without bounds. We are happy to return any data from a
+ * message as long as the data itself is valid, even if the
+ * overall message might be not.
+ *
+ * Note that the message signature is validated when
+ * parsing the headers, and that validation does check the
+ * 32/32 limit.
+ *
+ * Note that the specification defines no limits on the depth
+ * of stacked variants, but we do.
+ */
+ if (m->n_containers >= BUS_CONTAINER_DEPTH)
+ return -EBADMSG;
+
w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
if (!w)
return -ENOMEM;
return buffer_peek(m->fields, BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
}
+static int message_peek_field_uint32(
+ sd_bus_message *m,
+ size_t *ri,
+ uint32_t *ret) {
+
+ int r;
+ void *q;
+
+ assert(m);
+ assert(ri);
+
+ r = message_peek_fields(m, ri, 4, 4, &q);
+ if (r < 0)
+ return r;
+
+ if (ret)
+ *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+
+ return 0;
+}
+
static int message_peek_field_string(
sd_bus_message *m,
bool (*validate)(const char *p),
size_t *ri,
const char **ret) {
- size_t l;
+ uint32_t l;
int r;
void *q;
assert(m);
assert(ri);
- r = message_peek_fields(m, ri, 4, 4, &q);
+ r = message_peek_field_uint32(m, ri, &l);
if (r < 0)
return r;
- l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
r = message_peek_fields(m, ri, 1, l+1, &q);
if (r < 0)
return r;
return 0;
}
-static int message_peek_field_uint32(
- sd_bus_message *m,
- size_t *ri,
- uint32_t *ret) {
-
- int r;
- void *q;
-
- assert(m);
- assert(ri);
-
- r = message_peek_fields(m, ri, 4, 4, &q);
- if (r < 0)
- return r;
-
- if (ret)
- *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
-
- return 0;
-}
-
static int message_skip_fields(
sd_bus_message *m,
size_t *ri,
for (;;) {
char t;
- void *q;
size_t l;
if (array_size != (uint32_t) -1 &&
assert(l >= 1);
{
char sig[l-1], *s;
- size_t nas;
+ uint32_t nas;
int alignment;
strncpy(sig, *signature + 1, l-1);
if (alignment < 0)
return alignment;
- r = message_peek_fields(m, ri, 4, 4, &q);
+ r = message_peek_field_uint32(m, ri, &nas);
if (r < 0)
return r;
-
- nas = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
if (nas > BUS_ARRAY_MAX_SIZE)
return -EBADMSG;
free(m->root_container.signature);
m->root_container.signature = c;
-
- r = 0;
break;
}