[systemd-commits] 3 commits - src/libsystemd-bus

Lennart Poettering lennart at kemper.freedesktop.org
Thu Mar 21 17:28:41 PDT 2013


 src/libsystemd-bus/bus-internal.c     |  148 ++++++++++++++++++++++++++++++++++
 src/libsystemd-bus/bus-internal.h     |   11 ++
 src/libsystemd-bus/bus-message.c      |  101 ++++++++++++++++++-----
 src/libsystemd-bus/sd-bus.h           |    1 
 src/libsystemd-bus/test-bus-marshal.c |    3 
 5 files changed, 241 insertions(+), 23 deletions(-)

New commits:
commit 6693860fab7e34ffc59a748d3064d553fba25f2c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Mar 22 01:15:20 2013 +0100

    bus: validate the entire header more closely

diff --git a/src/libsystemd-bus/bus-internal.c b/src/libsystemd-bus/bus-internal.c
index e58a8b1..d27b3f4 100644
--- a/src/libsystemd-bus/bus-internal.c
+++ b/src/libsystemd-bus/bus-internal.c
@@ -60,3 +60,111 @@ bool object_path_is_valid(const char *p) {
 
         return true;
 }
+
+bool interface_name_is_valid(const char *p) {
+        const char *q;
+        bool dot, found_dot;
+
+        if (isempty(p))
+                return false;
+
+        for (dot = true, q = p; *q; q++)
+                if (*q == '.') {
+                        if (dot)
+                                return false;
+
+                        found_dot = dot = true;
+                } else {
+                        bool good;
+
+                        good =
+                                (*q >= 'a' && *q <= 'z') ||
+                                (*q >= 'A' && *q <= 'Z') ||
+                                (!dot && *q >= '0' && *q <= '9') ||
+                                *q == '_';
+
+                        if (!good)
+                                return false;
+
+                        dot = false;
+                }
+
+        if (q - p > 255)
+                return false;
+
+        if (dot)
+                return false;
+
+        if (!found_dot)
+                return false;
+
+        return true;
+}
+
+bool service_name_is_valid(const char *p) {
+        const char *q;
+        bool dot, found_dot, unique;
+
+        if (isempty(p))
+                return false;
+
+        unique = p[0] == ':';
+
+        for (dot = true, q = unique ? p+1 : p; *q; q++)
+                if (*q == '.') {
+                        if (dot)
+                                return false;
+
+                        found_dot = dot = true;
+                } else {
+                        bool good;
+
+                        good =
+                                (*q >= 'a' && *q <= 'z') ||
+                                (*q >= 'A' && *q <= 'Z') ||
+                                ((!dot || unique) && *q >= '0' && *q <= '9') ||
+                                *q == '_' || *q == '-';
+
+                        if (!good)
+                                return false;
+
+                        dot = false;
+                }
+
+        if (q - p > 255)
+                return false;
+
+        if (dot)
+                return false;
+
+        if (!found_dot)
+                return false;
+
+        return true;
+
+}
+
+bool member_name_is_valid(const char *p) {
+        const char *q;
+
+        if (isempty(p))
+                return false;
+
+        for (q = p; *q; q++) {
+                bool good;
+
+                good =
+                        (*q >= 'a' && *q <= 'z') ||
+                        (*q >= 'A' && *q <= 'Z') ||
+                        (*q >= '0' && *q <= '9') ||
+                        *q == '_';
+
+                if (!good)
+                        return false;
+        }
+
+        if (q - p > 255)
+                return false;
+
+        return true;
+}
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 809ad82..3c2478e 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -123,3 +123,8 @@ static inline void bus_unrefp(sd_bus **b) {
 #define BUS_ARRAY_MAX_SIZE 67108864
 
 bool object_path_is_valid(const char *p);
+bool interface_name_is_valid(const char *p);
+bool service_name_is_valid(const char *p);
+bool member_name_is_valid(const char *p);
+
+#define error_name_is_valid interface_name_is_valid
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index f5b60f2..a229625 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -2123,7 +2123,7 @@ static int message_peek_fields(
 
 static int message_peek_field_string(
                 sd_bus_message *m,
-                char type,
+                bool (*validate)(const char *p),
                 size_t *ri,
                 const char **ret) {
 
@@ -2143,8 +2143,11 @@ static int message_peek_field_string(
         if (r < 0)
                 return r;
 
-        if (type == SD_BUS_TYPE_OBJECT_PATH) {
-                if (!validate_object_path(q, l))
+        if (validate) {
+                if (!validate_nul(q, l))
+                        return -EBADMSG;
+
+                if (!validate(q))
                         return -EBADMSG;
         } else {
                 if (!validate_string(q, l))
@@ -2236,10 +2239,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, NULL, ri, NULL);
+                        if (r < 0)
+                                return r;
+
+                        (*signature)++;
+
+                } else if (t == SD_BUS_TYPE_OBJECT_PATH) {
 
-                        r = message_peek_field_string(m, t, ri, NULL);
+                        r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
                         if (r < 0)
                                 return r;
 
@@ -2366,42 +2376,42 @@ static int message_parse_fields(sd_bus_message *m) {
                         if (!streq(signature, "o"))
                                 return -EBADMSG;
 
-                        r = message_peek_field_string(m, 'o', &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, 's', &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, 's', &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, 's', &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, 's', &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, 's', &ri, &m->sender);
+                        r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender);
                         break;
 
 
@@ -2432,6 +2442,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:

commit ac89bf1d53268d39025a2a00c2effdb3fa447ead
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Mar 22 00:42:53 2013 +0100

    bus: properly validate object path values

diff --git a/src/libsystemd-bus/bus-internal.c b/src/libsystemd-bus/bus-internal.c
index 19398af..e58a8b1 100644
--- a/src/libsystemd-bus/bus-internal.c
+++ b/src/libsystemd-bus/bus-internal.c
@@ -20,3 +20,43 @@
 ***/
 
 #include "bus-internal.h"
+
+bool object_path_is_valid(const char *p) {
+        const char *q;
+        bool slash;
+
+        if (!p)
+                return false;
+
+        if (p[0] != '/')
+                return false;
+
+        if (p[1] == 0)
+                return true;
+
+        for (slash = true, q = p+1; *q; q++)
+                if (*q == '/') {
+                        if (slash)
+                                return false;
+
+                        slash = true;
+                } else {
+                        bool good;
+
+                        good =
+                                (*q >= 'a' && *q <= 'z') ||
+                                (*q >= 'A' && *q <= 'Z') ||
+                                (*q >= '0' && *q <= '9') ||
+                                *q == '_';
+
+                        if (!good)
+                                return false;
+
+                        slash = false;
+                }
+
+        if (slash)
+                return false;
+
+        return true;
+}
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 636e998..809ad82 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -117,3 +117,9 @@ static inline void bus_unrefp(sd_bus **b) {
 
 #define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
 #define BUS_AUTH_SIZE_MAX (64*1024)
+
+/* Defined by the specification as maximum size of an array in
+ * bytes */
+#define BUS_ARRAY_MAX_SIZE 67108864
+
+bool object_path_is_valid(const char *p);
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index 524f17e..f5b60f2 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -1374,8 +1374,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))
@@ -1385,6 +1384,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;
@@ -1393,12 +1400,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 */
@@ -1408,6 +1411,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;
@@ -1447,8 +1461,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;
@@ -1565,7 +1584,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);
@@ -2104,6 +2123,7 @@ static int message_peek_fields(
 
 static int message_peek_field_string(
                 sd_bus_message *m,
+                char type,
                 size_t *ri,
                 const char **ret) {
 
@@ -2123,8 +2143,13 @@ static int message_peek_field_string(
         if (r < 0)
                 return r;
 
-        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;
+        }
 
         if (ret)
                 *ret = q;
@@ -2214,7 +2239,7 @@ static int message_skip_fields(
                 if (t == SD_BUS_TYPE_STRING ||
                     t == SD_BUS_TYPE_OBJECT_PATH) {
 
-                        r = message_peek_field_string(m, ri, NULL);
+                        r = message_peek_field_string(m, t, ri, NULL);
                         if (r < 0)
                                 return r;
 
@@ -2264,7 +2289,7 @@ static int message_skip_fields(
                                         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);
@@ -2341,42 +2366,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, 'o', &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, 's', &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, 's', &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, 's', &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, 's', &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, 's', &ri, &m->sender);
                         break;
 
 

commit b8beb2781682738f3a59aab993bf2869447a77c9
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Mar 22 00:24:21 2013 +0100

    bus: generate a nice error when attempting to add a NULL string

diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index c0a0242..524f17e 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -768,11 +768,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;
diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h
index d5101c2..3ea4acc 100644
--- a/src/libsystemd-bus/sd-bus.h
+++ b/src/libsystemd-bus/sd-bus.h
@@ -33,7 +33,6 @@
  * - add page donation logic
  * - api for appending/reading fixed arrays
  * - always verify container depth
- * - handle NULL strings nicer when appending
  * - merge busctl into systemctl or so?
  * - add object handlers
  * - verify object paths
diff --git a/src/libsystemd-bus/test-bus-marshal.c b/src/libsystemd-bus/test-bus-marshal.c
index 15c472c..d12c847 100644
--- a/src/libsystemd-bus/test-bus-marshal.c
+++ b/src/libsystemd-bus/test-bus-marshal.c
@@ -50,6 +50,9 @@ int main(int argc, char *argv[]) {
         r = sd_bus_message_append(m, "s", "a string");
         assert_se(r >= 0);
 
+        r = sd_bus_message_append(m, "s", NULL);
+        assert_se(r < 0);
+
         r = sd_bus_message_append(m, "as", 2, "string #1", "string #2");
         assert_se(r >= 0);
 



More information about the systemd-commits mailing list