ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
c62b8e
From 0c4a78cc276249e51bcaa50b29df3efcf6d22ec3 Mon Sep 17 00:00:00 2001
c62b8e
From: Jan Synacek <jsynacek@redhat.com>
c62b8e
Date: Tue, 12 Feb 2019 11:10:59 +0100
c62b8e
Subject: [PATCH] sd-bus: unify three code-paths which free struct
c62b8e
 bus_container
c62b8e
c62b8e
We didn't free one of the fields in two of the places.
c62b8e
c62b8e
$ valgrind --show-leak-kinds=all --leak-check=full \
c62b8e
  build/fuzz-bus-message \
c62b8e
  test/fuzz/fuzz-bus-message/leak-c09c0e2256d43bc5e2d02748c8d8760e7bc25d20
c62b8e
...
c62b8e
==14457== HEAP SUMMARY:
c62b8e
==14457==     in use at exit: 3 bytes in 1 blocks
c62b8e
==14457==   total heap usage: 509 allocs, 508 frees, 51,016 bytes allocated
c62b8e
==14457==
c62b8e
==14457== 3 bytes in 1 blocks are definitely lost in loss record 1 of 1
c62b8e
==14457==    at 0x4C2EBAB: malloc (vg_replace_malloc.c:299)
c62b8e
==14457==    by 0x53AFE79: strndup (in /usr/lib64/libc-2.27.so)
c62b8e
==14457==    by 0x4F52EB8: free_and_strndup (string-util.c:1039)
c62b8e
==14457==    by 0x4F8E1AB: sd_bus_message_peek_type (bus-message.c:4193)
c62b8e
==14457==    by 0x4F76CB5: bus_message_dump (bus-dump.c:144)
c62b8e
==14457==    by 0x108F12: LLVMFuzzerTestOneInput (fuzz-bus-message.c:24)
c62b8e
==14457==    by 0x1090F7: main (fuzz-main.c:34)
c62b8e
==14457==
c62b8e
==14457== LEAK SUMMARY:
c62b8e
==14457==    definitely lost: 3 bytes in 1 blocks
c62b8e
c62b8e
(cherry picked from commit 6d1e0f4fcba8d6f425da3dc91805db95399b3c8b)
c62b8e
Resolves: #1643394
c62b8e
---
c62b8e
 src/libsystemd/sd-bus/bus-message.c | 66 +++++++++++++++--------------
c62b8e
 1 file changed, 35 insertions(+), 31 deletions(-)
c62b8e
c62b8e
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
c62b8e
index 121e65674d..b786f943e4 100644
c62b8e
--- a/src/libsystemd/sd-bus/bus-message.c
c62b8e
+++ b/src/libsystemd/sd-bus/bus-message.c
c62b8e
@@ -104,20 +104,42 @@ static void message_reset_parts(sd_bus_message *m) {
c62b8e
         m->cached_rindex_part_begin = 0;
c62b8e
 }
c62b8e
 
c62b8e
-static void message_reset_containers(sd_bus_message *m) {
c62b8e
-        unsigned i;
c62b8e
+static struct bus_container *message_get_container(sd_bus_message *m) {
c62b8e
+        assert(m);
c62b8e
+
c62b8e
+        if (m->n_containers == 0)
c62b8e
+                return &m->root_container;
c62b8e
+
c62b8e
+        assert(m->containers);
c62b8e
+        return m->containers + m->n_containers - 1;
c62b8e
+}
c62b8e
+
c62b8e
+static void message_free_last_container(sd_bus_message *m) {
c62b8e
+        struct bus_container *c;
c62b8e
 
c62b8e
+        c = message_get_container(m);
c62b8e
+
c62b8e
+        free(c->signature);
c62b8e
+        free(c->peeked_signature);
c62b8e
+        free(c->offsets);
c62b8e
+
c62b8e
+        /* Move to previous container, but not if we are on root container */
c62b8e
+        if (m->n_containers > 0)
c62b8e
+                m->n_containers--;
c62b8e
+}
c62b8e
+
c62b8e
+
c62b8e
+
c62b8e
+static void message_reset_containers(sd_bus_message *m) {
c62b8e
         assert(m);
c62b8e
 
c62b8e
-        for (i = 0; i < m->n_containers; i++) {
c62b8e
-                free(m->containers[i].signature);
c62b8e
-                free(m->containers[i].offsets);
c62b8e
-        }
c62b8e
+        while (m->n_containers > 0)
c62b8e
+                message_free_last_container(m);
c62b8e
 
c62b8e
         free(m->containers);
c62b8e
         m->containers = NULL;
c62b8e
 
c62b8e
-        m->n_containers = m->containers_allocated = 0;
c62b8e
+        m->containers_allocated = 0;
c62b8e
         m->root_container.index = 0;
c62b8e
 }
c62b8e
 
c62b8e
@@ -151,10 +173,8 @@ static void message_free(sd_bus_message *m) {
c62b8e
         }
c62b8e
 
c62b8e
         message_reset_containers(m);
c62b8e
-        free(m->root_container.signature);
c62b8e
-        free(m->root_container.offsets);
c62b8e
-
c62b8e
-        free(m->root_container.peeked_signature);
c62b8e
+        assert(m->n_containers == 0);
c62b8e
+        message_free_last_container(m);
c62b8e
 
c62b8e
         bus_creds_done(&m->creds);
c62b8e
         free(m);
c62b8e
@@ -1179,16 +1199,6 @@ _public_ int sd_bus_message_set_allow_interactive_authorization(sd_bus_message *
c62b8e
         return 0;
c62b8e
 }
c62b8e
 
c62b8e
-static struct bus_container *message_get_container(sd_bus_message *m) {
c62b8e
-        assert(m);
c62b8e
-
c62b8e
-        if (m->n_containers == 0)
c62b8e
-                return &m->root_container;
c62b8e
-
c62b8e
-        assert(m->containers);
c62b8e
-        return m->containers + m->n_containers - 1;
c62b8e
-}
c62b8e
-
c62b8e
 struct bus_body_part *message_append_part(sd_bus_message *m) {
c62b8e
         struct bus_body_part *part;
c62b8e
 
c62b8e
@@ -2107,6 +2117,7 @@ _public_ int sd_bus_message_open_container(
c62b8e
         w = m->containers + m->n_containers++;
c62b8e
         w->enclosing = type;
c62b8e
         w->signature = signature;
c62b8e
+        w->peeked_signature = NULL;
c62b8e
         w->index = 0;
c62b8e
         w->array_size = array_size;
c62b8e
         w->before = before;
c62b8e
@@ -4195,13 +4206,9 @@ _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
c62b8e
                         return -EBUSY;
c62b8e
         }
c62b8e
 
c62b8e
-        free(c->signature);
c62b8e
-        free(c->peeked_signature);
c62b8e
-        free(c->offsets);
c62b8e
-        m->n_containers--;
c62b8e
+        message_free_last_container(m);
c62b8e
 
c62b8e
         c = message_get_container(m);
c62b8e
-
c62b8e
         saved = c->index;
c62b8e
         c->index = c->saved_index;
c62b8e
         r = container_next_item(m, c, &m->rindex);
c62b8e
@@ -4219,16 +4226,13 @@ static void message_quit_container(sd_bus_message *m) {
c62b8e
         assert(m->sealed);
c62b8e
         assert(m->n_containers > 0);
c62b8e
 
c62b8e
-        c = message_get_container(m);
c62b8e
-
c62b8e
         /* Undo seeks */
c62b8e
+        c = message_get_container(m);
c62b8e
         assert(m->rindex >= c->before);
c62b8e
         m->rindex = c->before;
c62b8e
 
c62b8e
         /* Free container */
c62b8e
-        free(c->signature);
c62b8e
-        free(c->offsets);
c62b8e
-        m->n_containers--;
c62b8e
+        message_free_last_container(m);
c62b8e
 
c62b8e
         /* Correct index of new top-level container */
c62b8e
         c = message_get_container(m);