ff6046
From a82cf4abc81722706b4466e65c1a05f997cf9fdc 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:38:10 +0200
ff6046
Subject: [PATCH] bus-message: use structured initialization to avoid use of
ff6046
 unitialized memory
ff6046
ff6046
As far as I can see, we would either reuse some values from a previously exited
ff6046
container or just random bytes from the heap.
ff6046
ff6046
Should fix #10127.
ff6046
ff6046
(cherry picked from commit cf81c68e96aa29d0c28b5d3a26d1de9aa1b53b85)
ff6046
ff6046
Resolves: #1696224
ff6046
---
ff6046
 src/libsystemd/sd-bus/bus-message.c | 59 +++++++++++++----------------
ff6046
 1 file changed, 27 insertions(+), 32 deletions(-)
ff6046
ff6046
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
ff6046
index 780c8c6185..7f87d018fb 100644
ff6046
--- a/src/libsystemd/sd-bus/bus-message.c
ff6046
+++ b/src/libsystemd/sd-bus/bus-message.c
ff6046
@@ -1956,7 +1956,7 @@ _public_ int sd_bus_message_open_container(
ff6046
                 char type,
ff6046
                 const char *contents) {
ff6046
 
ff6046
-        struct bus_container *c, *w;
ff6046
+        struct bus_container *c;
ff6046
         uint32_t *array_size = NULL;
ff6046
         _cleanup_free_ char *signature = NULL;
ff6046
         size_t before, begin = 0;
ff6046
@@ -2001,17 +2001,14 @@ _public_ int sd_bus_message_open_container(
ff6046
                 return r;
ff6046
 
ff6046
         /* OK, let's fill it in */
ff6046
-        w = m->containers + m->n_containers++;
ff6046
-        w->enclosing = type;
ff6046
-        w->signature = TAKE_PTR(signature);
ff6046
-        w->peeked_signature = NULL;
ff6046
-        w->index = 0;
ff6046
-        w->array_size = array_size;
ff6046
-        w->before = before;
ff6046
-        w->begin = begin;
ff6046
-        w->n_offsets = w->offsets_allocated = 0;
ff6046
-        w->offsets = NULL;
ff6046
-        w->need_offsets = need_offsets;
ff6046
+        m->containers[m->n_containers++] = (struct bus_container) {
ff6046
+                .enclosing = type,
ff6046
+                .signature = TAKE_PTR(signature),
ff6046
+                .array_size = array_size,
ff6046
+                .before = before,
ff6046
+                .begin = begin,
ff6046
+                .need_offsets = need_offsets,
ff6046
+        };
ff6046
 
ff6046
         return 0;
ff6046
 }
ff6046
@@ -3980,10 +3977,10 @@ static int bus_message_enter_dict_entry(
ff6046
 _public_ int sd_bus_message_enter_container(sd_bus_message *m,
ff6046
                                             char type,
ff6046
                                             const char *contents) {
ff6046
-        struct bus_container *c, *w;
ff6046
+        struct bus_container *c;
ff6046
         uint32_t *array_size = NULL;
ff6046
         _cleanup_free_ char *signature = NULL;
ff6046
-        size_t before;
ff6046
+        size_t before, end;
ff6046
         _cleanup_free_ size_t *offsets = NULL;
ff6046
         size_t n_offsets = 0, item_size = 0;
ff6046
         int r;
ff6046
@@ -4062,28 +4059,26 @@ _public_ int sd_bus_message_enter_container(sd_bus_message *m,
ff6046
                 return r;
ff6046
 
ff6046
         /* OK, let's fill it in */
ff6046
-        w = m->containers + m->n_containers++;
ff6046
-        w->enclosing = type;
ff6046
-        w->signature = TAKE_PTR(signature);
ff6046
-        w->peeked_signature = NULL;
ff6046
-        w->index = 0;
ff6046
-
ff6046
-        w->before = before;
ff6046
-        w->begin = m->rindex;
ff6046
-
ff6046
-        /* Unary type has fixed size of 1, but virtual size of 0 */
ff6046
         if (BUS_MESSAGE_IS_GVARIANT(m) &&
ff6046
             type == SD_BUS_TYPE_STRUCT &&
ff6046
             isempty(signature))
ff6046
-                w->end = m->rindex + 0;
ff6046
+                end = m->rindex + 0;
ff6046
         else
ff6046
-                w->end = m->rindex + c->item_size;
ff6046
-
ff6046
-        w->array_size = array_size;
ff6046
-        w->item_size = item_size;
ff6046
-        w->offsets = TAKE_PTR(offsets);
ff6046
-        w->n_offsets = n_offsets;
ff6046
-        w->offset_index = 0;
ff6046
+                end = m->rindex + c->item_size;
ff6046
+
ff6046
+        m->containers[m->n_containers++] = (struct bus_container) {
ff6046
+                 .enclosing = type,
ff6046
+                 .signature = TAKE_PTR(signature),
ff6046
+
ff6046
+                 .before = before,
ff6046
+                 .begin = m->rindex,
ff6046
+                 /* Unary type has fixed size of 1, but virtual size of 0 */
ff6046
+                 .end = end,
ff6046
+                 .array_size = array_size,
ff6046
+                 .item_size = item_size,
ff6046
+                 .offsets = TAKE_PTR(offsets),
ff6046
+                 .n_offsets = n_offsets,
ff6046
+        };
ff6046
 
ff6046
         return 1;
ff6046
 }