teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

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

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