teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0026-Introduce-free_and_strndup-and-use-it-in-bus-message.patch

ff6046
From 26de3af817b0c5746cb61b798ae8e138e01ea17c Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Mon, 9 Jul 2018 07:03:01 +0200
ff6046
Subject: [PATCH] Introduce free_and_strndup and use it in bus-message.c
ff6046
ff6046
v2: fix error in free_and_strndup()
ff6046
ff6046
When the orignal and copied message were the same, but shorter than specified
ff6046
length l, memory read past the end of the buffer would be performed. A test
ff6046
case is included: a string that had an embedded NUL ("q\0") is used to replace
ff6046
"q".
ff6046
ff6046
v3: Fix one more bug in free_and_strndup and add tests.
ff6046
ff6046
v4: Some style fixed based on review, one more use of free_and_replace, and
ff6046
make the tests more comprehensive.
ff6046
ff6046
(cherry picked from commit 7f546026abbdc56c453a577e52d57159458c3e9c)
ff6046
ff6046
Resolves: #1635428
ff6046
---
ff6046
 src/basic/string-util.c                       |  28 +++++++-
ff6046
 src/basic/string-util.h                       |   1 +
ff6046
 src/libsystemd/sd-bus/bus-message.c           |  34 ++++------
ff6046
 src/test/test-string-util.c                   |  62 ++++++++++++++++++
ff6046
 ...h-b88ad9ecf4aacf4a0caca5b5543953265367f084 | Bin 0 -> 32 bytes
ff6046
 5 files changed, 103 insertions(+), 22 deletions(-)
ff6046
 create mode 100644 test/fuzz/fuzz-bus-message/crash-b88ad9ecf4aacf4a0caca5b5543953265367f084
ff6046
ff6046
diff --git a/src/basic/string-util.c b/src/basic/string-util.c
ff6046
index 0a40683493..dfa739996f 100644
ff6046
--- a/src/basic/string-util.c
ff6046
+++ b/src/basic/string-util.c
ff6046
@@ -1004,7 +1004,7 @@ int free_and_strdup(char **p, const char *s) {
ff6046
 
ff6046
         assert(p);
ff6046
 
ff6046
-        /* Replaces a string pointer with an strdup()ed new string,
ff6046
+        /* Replaces a string pointer with a strdup()ed new string,
ff6046
          * possibly freeing the old one. */
ff6046
 
ff6046
         if (streq_ptr(*p, s))
ff6046
@@ -1023,6 +1023,32 @@ int free_and_strdup(char **p, const char *s) {
ff6046
         return 1;
ff6046
 }
ff6046
 
ff6046
+int free_and_strndup(char **p, const char *s, size_t l) {
ff6046
+        char *t;
ff6046
+
ff6046
+        assert(p);
ff6046
+        assert(s || l == 0);
ff6046
+
ff6046
+        /* Replaces a string pointer with a strndup()ed new string,
ff6046
+         * freeing the old one. */
ff6046
+
ff6046
+        if (!*p && !s)
ff6046
+                return 0;
ff6046
+
ff6046
+        if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
ff6046
+                return 0;
ff6046
+
ff6046
+        if (s) {
ff6046
+                t = strndup(s, l);
ff6046
+                if (!t)
ff6046
+                        return -ENOMEM;
ff6046
+        } else
ff6046
+                t = NULL;
ff6046
+
ff6046
+        free_and_replace(*p, t);
ff6046
+        return 1;
ff6046
+}
ff6046
+
ff6046
 #if !HAVE_EXPLICIT_BZERO
ff6046
 /*
ff6046
  * Pointer to memset is volatile so that compiler must de-reference
ff6046
diff --git a/src/basic/string-util.h b/src/basic/string-util.h
ff6046
index c0cc4e78d7..96a9260f93 100644
ff6046
--- a/src/basic/string-util.h
ff6046
+++ b/src/basic/string-util.h
ff6046
@@ -176,6 +176,7 @@ char *strrep(const char *s, unsigned n);
ff6046
 int split_pair(const char *s, const char *sep, char **l, char **r);
ff6046
 
ff6046
 int free_and_strdup(char **p, const char *s);
ff6046
+int free_and_strndup(char **p, const char *s, size_t l);
ff6046
 
ff6046
 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
ff6046
 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
ff6046
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
ff6046
index 381034f5f8..7c8bad2bdd 100644
ff6046
--- a/src/libsystemd/sd-bus/bus-message.c
ff6046
+++ b/src/libsystemd/sd-bus/bus-message.c
ff6046
@@ -4175,20 +4175,19 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char
ff6046
 
ff6046
                 if (contents) {
ff6046
                         size_t l;
ff6046
-                        char *sig;
ff6046
 
ff6046
                         r = signature_element_length(c->signature+c->index+1, &l);
ff6046
                         if (r < 0)
ff6046
                                 return r;
ff6046
 
ff6046
-                        assert(l >= 1);
ff6046
+                        /* signature_element_length does verification internally */
ff6046
 
ff6046
-                        sig = strndup(c->signature + c->index + 1, l);
ff6046
-                        if (!sig)
ff6046
+                        assert(l >= 1);
ff6046
+                        if (free_and_strndup(&c->peeked_signature,
ff6046
+                                             c->signature + c->index + 1, l) < 0)
ff6046
                                 return -ENOMEM;
ff6046
 
ff6046
-                        free(c->peeked_signature);
ff6046
-                        *contents = c->peeked_signature = sig;
ff6046
+                        *contents = c->peeked_signature;
ff6046
                 }
ff6046
 
ff6046
                 if (type)
ff6046
@@ -4201,19 +4200,17 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char
ff6046
 
ff6046
                 if (contents) {
ff6046
                         size_t l;
ff6046
-                        char *sig;
ff6046
 
ff6046
                         r = signature_element_length(c->signature+c->index, &l);
ff6046
                         if (r < 0)
ff6046
                                 return r;
ff6046
 
ff6046
                         assert(l >= 2);
ff6046
-                        sig = strndup(c->signature + c->index + 1, l - 2);
ff6046
-                        if (!sig)
ff6046
+                        if (free_and_strndup(&c->peeked_signature,
ff6046
+                                             c->signature + c->index + 1, l - 2) < 0)
ff6046
                                 return -ENOMEM;
ff6046
 
ff6046
-                        free(c->peeked_signature);
ff6046
-                        *contents = c->peeked_signature = sig;
ff6046
+                        *contents = c->peeked_signature;
ff6046
                 }
ff6046
 
ff6046
                 if (type)
ff6046
@@ -4253,9 +4250,8 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char
ff6046
                                 if (k > c->item_size)
ff6046
                                         return -EBADMSG;
ff6046
 
ff6046
-                                free(c->peeked_signature);
ff6046
-                                c->peeked_signature = strndup((char*) q + 1, k - 1);
ff6046
-                                if (!c->peeked_signature)
ff6046
+                                if (free_and_strndup(&c->peeked_signature,
ff6046
+                                                     (char*) q + 1, k - 1) < 0)
ff6046
                                         return -ENOMEM;
ff6046
 
ff6046
                                 if (!signature_is_valid(c->peeked_signature, true))
ff6046
@@ -5085,25 +5081,21 @@ int bus_message_parse_fields(sd_bus_message *m) {
ff6046
 
ff6046
                         if (*p == 0) {
ff6046
                                 size_t l;
ff6046
-                                char *c;
ff6046
 
ff6046
                                 /* We found the beginning of the signature
ff6046
                                  * string, yay! We require the body to be a
ff6046
                                  * structure, so verify it and then strip the
ff6046
                                  * opening/closing brackets. */
ff6046
 
ff6046
-                                l = ((char*) m->footer + m->footer_accessible) - p - (1 + sz);
ff6046
+                                l = (char*) m->footer + m->footer_accessible - p - (1 + sz);
ff6046
                                 if (l < 2 ||
ff6046
                                     p[1] != SD_BUS_TYPE_STRUCT_BEGIN ||
ff6046
                                     p[1 + l - 1] != SD_BUS_TYPE_STRUCT_END)
ff6046
                                         return -EBADMSG;
ff6046
 
ff6046
-                                c = strndup(p + 1 + 1, l - 2);
ff6046
-                                if (!c)
ff6046
+                                if (free_and_strndup(&m->root_container.signature,
ff6046
+                                                     p + 1 + 1, l - 2) < 0)
ff6046
                                         return -ENOMEM;
ff6046
-
ff6046
-                                free(m->root_container.signature);
ff6046
-                                m->root_container.signature = c;
ff6046
                                 break;
ff6046
                         }
ff6046
 
ff6046
diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c
ff6046
index 3e72ce2c0a..43a6b14c34 100644
ff6046
--- a/src/test/test-string-util.c
ff6046
+++ b/src/test/test-string-util.c
ff6046
@@ -5,6 +5,7 @@
ff6046
 #include "macro.h"
ff6046
 #include "string-util.h"
ff6046
 #include "strv.h"
ff6046
+#include "tests.h"
ff6046
 #include "utf8.h"
ff6046
 
ff6046
 static void test_string_erase(void) {
ff6046
@@ -30,6 +31,64 @@ static void test_string_erase(void) {
ff6046
         assert_se(x[9] == '\0');
ff6046
 }
ff6046
 
ff6046
+static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) {
ff6046
+        int r;
ff6046
+
ff6046
+        log_debug("%s: \"%s\", \"%s\", %zd (expect \"%s\", %s)",
ff6046
+                  __func__, strnull(*t), strnull(src), l, strnull(expected), yes_no(change));
ff6046
+
ff6046
+        r = free_and_strndup(t, src, l);
ff6046
+        assert_se(streq_ptr(*t, expected));
ff6046
+        assert_se(r == change); /* check that change occurs only when necessary */
ff6046
+}
ff6046
+
ff6046
+static void test_free_and_strndup(void) {
ff6046
+        static const struct test_case {
ff6046
+                const char *src;
ff6046
+                size_t len;
ff6046
+                const char *expected;
ff6046
+        } cases[] = {
ff6046
+                     {"abc", 0, ""},
ff6046
+                     {"abc", 0, ""},
ff6046
+                     {"abc", 1, "a"},
ff6046
+                     {"abc", 2, "ab"},
ff6046
+                     {"abc", 3, "abc"},
ff6046
+                     {"abc", 4, "abc"},
ff6046
+                     {"abc", 5, "abc"},
ff6046
+                     {"abc", 5, "abc"},
ff6046
+                     {"abc", 4, "abc"},
ff6046
+                     {"abc", 3, "abc"},
ff6046
+                     {"abc", 2, "ab"},
ff6046
+                     {"abc", 1, "a"},
ff6046
+                     {"abc", 0, ""},
ff6046
+
ff6046
+                     {"", 0, ""},
ff6046
+                     {"", 1, ""},
ff6046
+                     {"", 2, ""},
ff6046
+                     {"", 0, ""},
ff6046
+                     {"", 1, ""},
ff6046
+                     {"", 2, ""},
ff6046
+                     {"", 2, ""},
ff6046
+                     {"", 1, ""},
ff6046
+                     {"", 0, ""},
ff6046
+
ff6046
+                     {NULL, 0, NULL},
ff6046
+
ff6046
+                     {"foo", 3, "foo"},
ff6046
+                     {"foobar", 6, "foobar"},
ff6046
+        };
ff6046
+
ff6046
+        _cleanup_free_ char *t = NULL;
ff6046
+        const char *prev_expected = t;
ff6046
+
ff6046
+        for (unsigned i = 0; i < ELEMENTSOF(cases); i++) {
ff6046
+                test_free_and_strndup_one(&t,
ff6046
+                                          cases[i].src, cases[i].len, cases[i].expected,
ff6046
+                                          !streq_ptr(cases[i].expected, prev_expected));
ff6046
+                prev_expected = t;
ff6046
+        }
ff6046
+}
ff6046
+
ff6046
 static void test_ascii_strcasecmp_n(void) {
ff6046
 
ff6046
         assert_se(ascii_strcasecmp_n("", "", 0) == 0);
ff6046
@@ -497,7 +556,10 @@ static void test_memory_startswith(void) {
ff6046
 }
ff6046
 
ff6046
 int main(int argc, char *argv[]) {
ff6046
+        test_setup_logging(LOG_DEBUG);
ff6046
+
ff6046
         test_string_erase();
ff6046
+        test_free_and_strndup();
ff6046
         test_ascii_strcasecmp_n();
ff6046
         test_ascii_strcasecmp_nn();
ff6046
         test_cellescape();
ff6046
diff --git a/test/fuzz/fuzz-bus-message/crash-b88ad9ecf4aacf4a0caca5b5543953265367f084 b/test/fuzz/fuzz-bus-message/crash-b88ad9ecf4aacf4a0caca5b5543953265367f084
ff6046
new file mode 100644
ff6046
index 0000000000000000000000000000000000000000..52469650b5498a45d5d95bd9d933c989cfb47ca7
ff6046
GIT binary patch
ff6046
literal 32
ff6046
ccmd1#|DTBg0(2Mzp)7_%AVVXuuuM|`09r!?!~g&Q
ff6046
ff6046
literal 0
ff6046
HcmV?d00001
ff6046