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

a3e2b5
From eb141ba81158feb74118da4e7a3f2266b11ffe10 Mon Sep 17 00:00:00 2001
a3e2b5
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
a3e2b5
Date: Mon, 9 Jul 2018 08:06:28 +0200
a3e2b5
Subject: [PATCH] sd-bus: unify three code-paths which free struct
a3e2b5
 bus_container
a3e2b5
a3e2b5
We didn't free one of the fields in two of the places.
a3e2b5
a3e2b5
$ valgrind --show-leak-kinds=all --leak-check=full \
a3e2b5
  build/fuzz-bus-message \
a3e2b5
  test/fuzz/fuzz-bus-message/leak-c09c0e2256d43bc5e2d02748c8d8760e7bc25d20
a3e2b5
...
a3e2b5
==14457== HEAP SUMMARY:
a3e2b5
==14457==     in use at exit: 3 bytes in 1 blocks
a3e2b5
==14457==   total heap usage: 509 allocs, 508 frees, 51,016 bytes allocated
a3e2b5
==14457==
a3e2b5
==14457== 3 bytes in 1 blocks are definitely lost in loss record 1 of 1
a3e2b5
==14457==    at 0x4C2EBAB: malloc (vg_replace_malloc.c:299)
a3e2b5
==14457==    by 0x53AFE79: strndup (in /usr/lib64/libc-2.27.so)
a3e2b5
==14457==    by 0x4F52EB8: free_and_strndup (string-util.c:1039)
a3e2b5
==14457==    by 0x4F8E1AB: sd_bus_message_peek_type (bus-message.c:4193)
a3e2b5
==14457==    by 0x4F76CB5: bus_message_dump (bus-dump.c:144)
a3e2b5
==14457==    by 0x108F12: LLVMFuzzerTestOneInput (fuzz-bus-message.c:24)
a3e2b5
==14457==    by 0x1090F7: main (fuzz-main.c:34)
a3e2b5
==14457==
a3e2b5
==14457== LEAK SUMMARY:
a3e2b5
==14457==    definitely lost: 3 bytes in 1 blocks
a3e2b5
a3e2b5
(cherry picked from commit 6d1e0f4fcba8d6f425da3dc91805db95399b3c8b)
a3e2b5
Resolves: #1635435
a3e2b5
---
a3e2b5
 src/libsystemd/sd-bus/bus-message.c           |  64 +++++++++---------
a3e2b5
 ...k-c09c0e2256d43bc5e2d02748c8d8760e7bc25d20 | Bin 0 -> 534 bytes
a3e2b5
 2 files changed, 32 insertions(+), 32 deletions(-)
a3e2b5
 create mode 100644 test/fuzz/fuzz-bus-message/leak-c09c0e2256d43bc5e2d02748c8d8760e7bc25d20
a3e2b5
a3e2b5
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
a3e2b5
index 7c8bad2bdd..d55cb14843 100644
a3e2b5
--- a/src/libsystemd/sd-bus/bus-message.c
a3e2b5
+++ b/src/libsystemd/sd-bus/bus-message.c
a3e2b5
@@ -77,19 +77,38 @@ static void message_reset_parts(sd_bus_message *m) {
a3e2b5
         m->cached_rindex_part_begin = 0;
a3e2b5
 }
a3e2b5
 
a3e2b5
-static void message_reset_containers(sd_bus_message *m) {
a3e2b5
-        unsigned i;
a3e2b5
+static struct bus_container *message_get_container(sd_bus_message *m) {
a3e2b5
+        assert(m);
a3e2b5
+
a3e2b5
+        if (m->n_containers == 0)
a3e2b5
+                return &m->root_container;
a3e2b5
+
a3e2b5
+        assert(m->containers);
a3e2b5
+        return m->containers + m->n_containers - 1;
a3e2b5
+}
a3e2b5
+
a3e2b5
+static void message_free_last_container(sd_bus_message *m) {
a3e2b5
+        struct bus_container *c;
a3e2b5
+
a3e2b5
+        c = message_get_container(m);
a3e2b5
+
a3e2b5
+        free(c->signature);
a3e2b5
+        free(c->peeked_signature);
a3e2b5
+        free(c->offsets);
a3e2b5
+
a3e2b5
+        /* Move to previous container, but not if we are on root container */
a3e2b5
+        if (m->n_containers > 0)
a3e2b5
+                m->n_containers--;
a3e2b5
+}
a3e2b5
 
a3e2b5
+static void message_reset_containers(sd_bus_message *m) {
a3e2b5
         assert(m);
a3e2b5
 
a3e2b5
-        for (i = 0; i < m->n_containers; i++) {
a3e2b5
-                free(m->containers[i].signature);
a3e2b5
-                free(m->containers[i].offsets);
a3e2b5
-        }
a3e2b5
+        while (m->n_containers > 0)
a3e2b5
+                message_free_last_container(m);
a3e2b5
 
a3e2b5
         m->containers = mfree(m->containers);
a3e2b5
-
a3e2b5
-        m->n_containers = m->containers_allocated = 0;
a3e2b5
+        m->containers_allocated = 0;
a3e2b5
         m->root_container.index = 0;
a3e2b5
 }
a3e2b5
 
a3e2b5
@@ -112,10 +131,8 @@ static sd_bus_message* message_free(sd_bus_message *m) {
a3e2b5
                 free(m->iovec);
a3e2b5
 
a3e2b5
         message_reset_containers(m);
a3e2b5
-        free(m->root_container.signature);
a3e2b5
-        free(m->root_container.offsets);
a3e2b5
-
a3e2b5
-        free(m->root_container.peeked_signature);
a3e2b5
+        assert(m->n_containers == 0);
a3e2b5
+        message_free_last_container(m);
a3e2b5
 
a3e2b5
         bus_creds_done(&m->creds);
a3e2b5
         return mfree(m);
a3e2b5
@@ -1113,16 +1130,6 @@ _public_ int sd_bus_message_set_allow_interactive_authorization(sd_bus_message *
a3e2b5
         return 0;
a3e2b5
 }
a3e2b5
 
a3e2b5
-static struct bus_container *message_get_container(sd_bus_message *m) {
a3e2b5
-        assert(m);
a3e2b5
-
a3e2b5
-        if (m->n_containers == 0)
a3e2b5
-                return &m->root_container;
a3e2b5
-
a3e2b5
-        assert(m->containers);
a3e2b5
-        return m->containers + m->n_containers - 1;
a3e2b5
-}
a3e2b5
-
a3e2b5
 struct bus_body_part *message_append_part(sd_bus_message *m) {
a3e2b5
         struct bus_body_part *part;
a3e2b5
 
a3e2b5
@@ -4108,13 +4115,9 @@ _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
a3e2b5
                         return -EBUSY;
a3e2b5
         }
a3e2b5
 
a3e2b5
-        free(c->signature);
a3e2b5
-        free(c->peeked_signature);
a3e2b5
-        free(c->offsets);
a3e2b5
-        m->n_containers--;
a3e2b5
+        message_free_last_container(m);
a3e2b5
 
a3e2b5
         c = message_get_container(m);
a3e2b5
-
a3e2b5
         saved = c->index;
a3e2b5
         c->index = c->saved_index;
a3e2b5
         r = container_next_item(m, c, &m->rindex);
a3e2b5
@@ -4132,16 +4135,13 @@ static void message_quit_container(sd_bus_message *m) {
a3e2b5
         assert(m->sealed);
a3e2b5
         assert(m->n_containers > 0);
a3e2b5
 
a3e2b5
-        c = message_get_container(m);
a3e2b5
-
a3e2b5
         /* Undo seeks */
a3e2b5
+        c = message_get_container(m);
a3e2b5
         assert(m->rindex >= c->before);
a3e2b5
         m->rindex = c->before;
a3e2b5
 
a3e2b5
         /* Free container */
a3e2b5
-        free(c->signature);
a3e2b5
-        free(c->offsets);
a3e2b5
-        m->n_containers--;
a3e2b5
+        message_free_last_container(m);
a3e2b5
 
a3e2b5
         /* Correct index of new top-level container */
a3e2b5
         c = message_get_container(m);
a3e2b5
diff --git a/test/fuzz/fuzz-bus-message/leak-c09c0e2256d43bc5e2d02748c8d8760e7bc25d20 b/test/fuzz/fuzz-bus-message/leak-c09c0e2256d43bc5e2d02748c8d8760e7bc25d20
a3e2b5
new file mode 100644
a3e2b5
index 0000000000000000000000000000000000000000..c371824ffb604708619fd0713e8fca609bac18f7
a3e2b5
GIT binary patch
a3e2b5
literal 534
a3e2b5
zcmZ{h!A`?442GSJP20o?A&zJgm*%p
a3e2b5
z_LnqH{-ic!J`GWMLG(>T#&`l!4rxq{&>8YmwQrOs;B(}I_m11m8`nFp
a3e2b5
z!cs!Q@A35`W+B>`#ek1>oQYVSs`!XH?7Y=}3y9Ye+UliL9^x9s66$8wH+TPdOG`n|
a3e2b5
z5Uhx
a3e2b5
zLhpD3X)k6@tX`CzbBVV-7e$fy9()CjJ&n(=^)uJCKFB5Xi}-<1ru7po5XlEJ?uByQ
a3e2b5
MaEPzRhwknF02{PjtN;K2
a3e2b5
a3e2b5
literal 0
a3e2b5
HcmV?d00001
a3e2b5