anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0741-sd-bus-unify-three-code-paths-which-free-struct-bus_.patch

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