chiark / gitweb /
bus: implicitly collect ucred/label information
[elogind.git] / src / libsystemd-bus / bus-message.c
index c0a0242fd612dfd7b2ce2e082f0ebaa069abb20e..01213e31ad9cf4868798a7dca6fbf9c3ae779eda 100644 (file)
@@ -224,10 +224,16 @@ static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x)
         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);
@@ -259,7 +265,13 @@ int bus_message_from_malloc(void *buffer, size_t length, sd_bus_message **ret) {
         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;
 
@@ -270,6 +282,18 @@ int bus_message_from_malloc(void *buffer, size_t length, sd_bus_message **ret) {
         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;
@@ -629,6 +653,13 @@ int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
         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;
@@ -768,11 +799,27 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
 
         case SD_BUS_TYPE_STRING:
         case SD_BUS_TYPE_OBJECT_PATH:
+
+                if (!p) {
+                        if (e)
+                                c->signature[c->index] = 0;
+
+                        return -EINVAL;
+                }
+
                 align = 4;
                 sz = 4 + strlen(p) + 1;
                 break;
 
         case SD_BUS_TYPE_SIGNATURE:
+
+                if (!p) {
+                        if (e)
+                                c->signature[c->index] = 0;
+
+                        return -EINVAL;
+                }
+
                 align = 1;
                 sz = 1 + strlen(p) + 1;
                 break;
@@ -1358,8 +1405,7 @@ static int message_peek_body(sd_bus_message *m, size_t *rindex, size_t align, si
         return buffer_peek(m->body, BUS_MESSAGE_BODY_SIZE(m), rindex, align, nbytes, ret);
 }
 
-static bool validate_string(const char *s, size_t l) {
-        assert(s);
+static bool validate_nul(const char *s, size_t l) {
 
         /* Check for NUL chars in the string */
         if (memchr(s, 0, l))
@@ -1369,6 +1415,14 @@ static bool validate_string(const char *s, size_t l) {
         if (s[l] != 0)
                 return false;
 
+        return true;
+}
+
+static bool validate_string(const char *s, size_t l) {
+
+        if (!validate_nul(s, l))
+                return false;
+
         /* Check if valid UTF8 */
         if (!utf8_is_valid(s))
                 return false;
@@ -1377,12 +1431,8 @@ static bool validate_string(const char *s, size_t l) {
 }
 
 static bool validate_signature(const char *s, size_t l) {
-        /* Check for NUL chars in the signature */
-        if (memchr(s, 0, l))
-                return false;
 
-        /* Check for NUL termination */
-        if (s[l] != 0)
+        if (!validate_nul(s, l))
                 return false;
 
         /* Check if valid signature */
@@ -1392,6 +1442,17 @@ static bool validate_signature(const char *s, size_t l) {
         return true;
 }
 
+static bool validate_object_path(const char *s, size_t l) {
+
+        if (!validate_nul(s, l))
+                return false;
+
+        if (!object_path_is_valid(s))
+                return false;
+
+        return true;
+}
+
 int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
         struct bus_container *c;
         int r;
@@ -1431,8 +1492,13 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
                 if (r == 0)
                         return -EBADMSG;
 
-                if (!validate_string(q, l))
-                        return -EBADMSG;
+                if (type == SD_BUS_TYPE_OBJECT_PATH) {
+                        if (!validate_object_path(q, l))
+                                return -EBADMSG;
+                } else {
+                        if (!validate_string(q, l))
+                                return -EBADMSG;
+                }
 
                 m->rindex = rindex;
                 *(const char**) p = q;
@@ -1549,7 +1615,7 @@ static int bus_message_enter_array(
         if (r <= 0)
                 return r;
 
-        if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > 67108864)
+        if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
                 return -EBADMSG;
 
         r = message_peek_body(m, &rindex, alignment, 0, NULL);
@@ -1706,6 +1772,25 @@ int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *con
         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;
@@ -2086,12 +2171,11 @@ static int message_peek_fields(
         return buffer_peek(m->fields, BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
 }
 
-static int message_peek_field_string(
+static int message_peek_field_uint32(
                 sd_bus_message *m,
                 size_t *ri,
-                const char **ret) {
+                uint32_t *ret) {
 
-        size_t l;
         int r;
         void *q;
 
@@ -2102,43 +2186,43 @@ static int message_peek_field_string(
         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;
-
-        if (!validate_string(q, l))
-                return -EBADMSG;
-
         if (ret)
-                *ret = q;
+                *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
 
         return 0;
 }
 
-static int message_peek_field_signature(
+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, 1, 1, &q);
+        r = message_peek_field_uint32(m, ri, &l);
         if (r < 0)
                 return r;
 
-        l = *(uint8_t*) q;
         r = message_peek_fields(m, ri, 1, l+1, &q);
         if (r < 0)
                 return r;
 
-        if (!validate_signature(q, l))
-                return -EBADMSG;
+        if (validate) {
+                if (!validate_nul(q, l))
+                        return -EBADMSG;
+
+                if (!validate(q))
+                        return -EBADMSG;
+        } else {
+                if (!validate_string(q, l))
+                        return -EBADMSG;
+        }
 
         if (ret)
                 *ret = q;
@@ -2146,23 +2230,32 @@ static int message_peek_field_signature(
         return 0;
 }
 
-static int message_peek_field_uint32(
+static int message_peek_field_signature(
                 sd_bus_message *m,
                 size_t *ri,
-                uint32_t *ret) {
+                const char **ret) {
 
+        size_t l;
         int r;
         void *q;
 
         assert(m);
         assert(ri);
 
-        r = message_peek_fields(m, ri, 4, 4, &q);
+        r = message_peek_fields(m, ri, 1, 1, &q);
         if (r < 0)
                 return r;
 
+        l = *(uint8_t*) q;
+        r = message_peek_fields(m, ri, 1, l+1, &q);
+        if (r < 0)
+                return r;
+
+        if (!validate_signature(q, l))
+                return -EBADMSG;
+
         if (ret)
-                *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+                *ret = q;
 
         return 0;
 }
@@ -2184,7 +2277,6 @@ static int message_skip_fields(
 
         for (;;) {
                 char t;
-                void *q;
                 size_t l;
 
                 if (array_size != (uint32_t) -1 &&
@@ -2195,10 +2287,17 @@ static int message_skip_fields(
                 if (!t)
                         return 0;
 
-                if (t == SD_BUS_TYPE_STRING ||
-                    t == SD_BUS_TYPE_OBJECT_PATH) {
+                if (t == SD_BUS_TYPE_STRING) {
 
-                        r = message_peek_field_string(m, ri, NULL);
+                        r = message_peek_field_string(m, NULL, ri, NULL);
+                        if (r < 0)
+                                return r;
+
+                        (*signature)++;
+
+                } else if (t == SD_BUS_TYPE_OBJECT_PATH) {
+
+                        r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
                         if (r < 0)
                                 return r;
 
@@ -2233,7 +2332,7 @@ static int message_skip_fields(
                         assert(l >= 1);
                         {
                                 char sig[l-1], *s;
-                                size_t nas;
+                                uint32_t nas;
                                 int alignment;
 
                                 strncpy(sig, *signature + 1, l-1);
@@ -2243,12 +2342,10 @@ static int message_skip_fields(
                                 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 > 67108864)
+                                if (nas > BUS_ARRAY_MAX_SIZE)
                                         return -EBADMSG;
 
                                 r = message_peek_fields(m, ri, alignment, 0, NULL);
@@ -2325,42 +2422,42 @@ static int message_parse_fields(sd_bus_message *m) {
                         if (!streq(signature, "o"))
                                 return -EBADMSG;
 
-                        r = message_peek_field_string(m, &ri, &m->path);
+                        r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path);
                         break;
 
                 case SD_BUS_MESSAGE_HEADER_INTERFACE:
                         if (!streq(signature, "s"))
                                 return -EBADMSG;
 
-                        r = message_peek_field_string(m, &ri, &m->interface);
+                        r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface);
                         break;
 
                 case SD_BUS_MESSAGE_HEADER_MEMBER:
                         if (!streq(signature, "s"))
                                 return -EBADMSG;
 
-                        r = message_peek_field_string(m, &ri, &m->member);
+                        r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member);
                         break;
 
                 case SD_BUS_MESSAGE_HEADER_ERROR_NAME:
                         if (!streq(signature, "s"))
                                 return -EBADMSG;
 
-                        r = message_peek_field_string(m, &ri, &m->error.name);
+                        r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name);
                         break;
 
                 case SD_BUS_MESSAGE_HEADER_DESTINATION:
                         if (!streq(signature, "s"))
                                 return -EBADMSG;
 
-                        r = message_peek_field_string(m, &ri, &m->destination);
+                        r = message_peek_field_string(m, service_name_is_valid, &ri, &m->destination);
                         break;
 
                 case SD_BUS_MESSAGE_HEADER_SENDER:
                         if (!streq(signature, "s"))
                                 return -EBADMSG;
 
-                        r = message_peek_field_string(m, &ri, &m->sender);
+                        r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender);
                         break;
 
 
@@ -2381,8 +2478,6 @@ static int message_parse_fields(sd_bus_message *m) {
 
                         free(m->root_container.signature);
                         m->root_container.signature = c;
-
-                        r = 0;
                         break;
                 }
 
@@ -2391,6 +2486,12 @@ static int message_parse_fields(sd_bus_message *m) {
                                 return -EBADMSG;
 
                         r = message_peek_field_uint32(m, &ri, &m->reply_serial);
+                        if (r < 0)
+                                return r;
+
+                        if (m->reply_serial == 0)
+                                return -EBADMSG;
+
                         break;
 
                 default: