c62b8e
From 8f77b455f768d1f95971cedd8d4903f249f683bf Mon Sep 17 00:00:00 2001
5d65fd
From: Riccardo Schirone <rschiron@redhat.com>
5d65fd
Date: Mon, 4 Feb 2019 14:29:28 +0100
5d65fd
Subject: [PATCH] Allocate temporary strings to hold dbus paths on the heap
5d65fd
5d65fd
Paths are limited to BUS_PATH_SIZE_MAX but the maximum size is anyway too big
5d65fd
to be allocated on the stack, so let's switch to the heap where there is a
5d65fd
clear way to understand if the allocation fails.
5d65fd
c62b8e
Resolves: #1667871
5d65fd
---
5d65fd
 src/libsystemd/sd-bus/bus-objects.c | 58 +++++++++++++++++++++++------
5d65fd
 1 file changed, 46 insertions(+), 12 deletions(-)
5d65fd
5d65fd
diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c
c62b8e
index fc6c223283..8df73bdf4c 100644
5d65fd
--- a/src/libsystemd/sd-bus/bus-objects.c
5d65fd
+++ b/src/libsystemd/sd-bus/bus-objects.c
5d65fd
@@ -1104,7 +1104,8 @@ static int object_manager_serialize_path_and_fallbacks(
5d65fd
                 const char *path,
5d65fd
                 sd_bus_error *error) {
5d65fd
 
5d65fd
-        char *prefix;
5d65fd
+        _cleanup_free_ char *prefix = NULL;
5d65fd
+        size_t pl;
5d65fd
         int r;
5d65fd
 
5d65fd
         assert(bus);
5d65fd
@@ -1120,7 +1121,12 @@ static int object_manager_serialize_path_and_fallbacks(
5d65fd
                 return 0;
5d65fd
 
5d65fd
         /* Second, add fallback vtables registered for any of the prefixes */
5d65fd
-        prefix = alloca(strlen(path) + 1);
5d65fd
+        pl = strlen(path);
5d65fd
+        assert(pl <= BUS_PATH_SIZE_MAX);
5d65fd
+        prefix = new(char, pl + 1);
5d65fd
+        if (!prefix)
5d65fd
+                return -ENOMEM;
5d65fd
+
5d65fd
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
5d65fd
                 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
5d65fd
                 if (r < 0)
5d65fd
@@ -1316,6 +1322,7 @@ static int object_find_and_run(
5d65fd
 }
5d65fd
 
5d65fd
 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
5d65fd
+        _cleanup_free_ char *prefix = NULL;
5d65fd
         int r;
5d65fd
         size_t pl;
5d65fd
         bool found_object = false;
5d65fd
@@ -1340,9 +1347,12 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) {
5d65fd
         assert(m->member);
5d65fd
 
5d65fd
         pl = strlen(m->path);
5d65fd
-        do {
5d65fd
-                char prefix[pl+1];
5d65fd
+        assert(pl <= BUS_PATH_SIZE_MAX);
5d65fd
+        prefix = new(char, pl + 1);
5d65fd
+        if (!prefix)
5d65fd
+                return -ENOMEM;
5d65fd
 
5d65fd
+        do {
5d65fd
                 bus->nodes_modified = false;
5d65fd
 
5d65fd
                 r = object_find_and_run(bus, m, m->path, false, &found_object);
5d65fd
@@ -2044,9 +2054,10 @@ _public_ int sd_bus_emit_properties_changed_strv(
5d65fd
                 const char *interface,
5d65fd
                 char **names) {
5d65fd
 
5d65fd
+        _cleanup_free_ char *prefix = NULL;
5d65fd
         BUS_DONT_DESTROY(bus);
5d65fd
         bool found_interface = false;
5d65fd
-        char *prefix;
5d65fd
+        size_t pl;
5d65fd
         int r;
5d65fd
 
5d65fd
         assert_return(bus, -EINVAL);
5d65fd
@@ -2064,6 +2075,12 @@ _public_ int sd_bus_emit_properties_changed_strv(
5d65fd
         if (names && names[0] == NULL)
5d65fd
                 return 0;
5d65fd
 
5d65fd
+        pl = strlen(path);
5d65fd
+        assert(pl <= BUS_PATH_SIZE_MAX);
5d65fd
+        prefix = new(char, pl + 1);
5d65fd
+        if (!prefix)
5d65fd
+                return -ENOMEM;
5d65fd
+
5d65fd
         do {
5d65fd
                 bus->nodes_modified = false;
5d65fd
 
5d65fd
@@ -2073,7 +2090,6 @@ _public_ int sd_bus_emit_properties_changed_strv(
5d65fd
                 if (bus->nodes_modified)
5d65fd
                         continue;
5d65fd
 
5d65fd
-                prefix = alloca(strlen(path) + 1);
5d65fd
                 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
5d65fd
                         r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
5d65fd
                         if (r != 0)
5d65fd
@@ -2204,7 +2220,8 @@ static int object_added_append_all_prefix(
5d65fd
 
5d65fd
 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
5d65fd
         _cleanup_set_free_ Set *s = NULL;
5d65fd
-        char *prefix;
5d65fd
+        _cleanup_free_ char *prefix = NULL;
5d65fd
+        size_t pl;
5d65fd
         int r;
5d65fd
 
5d65fd
         assert(bus);
5d65fd
@@ -2249,7 +2266,12 @@ static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *p
5d65fd
         if (bus->nodes_modified)
5d65fd
                 return 0;
5d65fd
 
5d65fd
-        prefix = alloca(strlen(path) + 1);
5d65fd
+        pl = strlen(path);
5d65fd
+        assert(pl <= BUS_PATH_SIZE_MAX);
5d65fd
+        prefix = new(char, pl + 1);
5d65fd
+        if (!prefix)
5d65fd
+                return -ENOMEM;
5d65fd
+
5d65fd
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
5d65fd
                 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
5d65fd
                 if (r < 0)
5d65fd
@@ -2380,7 +2402,8 @@ static int object_removed_append_all_prefix(
5d65fd
 
5d65fd
 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
5d65fd
         _cleanup_set_free_ Set *s = NULL;
5d65fd
-        char *prefix;
5d65fd
+        _cleanup_free_ char *prefix = NULL;
5d65fd
+        size_t pl;
5d65fd
         int r;
5d65fd
 
5d65fd
         assert(bus);
5d65fd
@@ -2412,7 +2435,12 @@ static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char
5d65fd
         if (bus->nodes_modified)
5d65fd
                 return 0;
5d65fd
 
5d65fd
-        prefix = alloca(strlen(path) + 1);
5d65fd
+        pl = strlen(path);
5d65fd
+        assert(pl <= BUS_PATH_SIZE_MAX);
5d65fd
+        prefix = new(char, pl + 1);
5d65fd
+        if (!prefix)
5d65fd
+                return -ENOMEM;
5d65fd
+
5d65fd
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
5d65fd
                 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
5d65fd
                 if (r < 0)
5d65fd
@@ -2554,7 +2582,8 @@ static int interfaces_added_append_one(
5d65fd
                 const char *path,
5d65fd
                 const char *interface) {
5d65fd
 
5d65fd
-        char *prefix;
5d65fd
+        _cleanup_free_ char *prefix = NULL;
5d65fd
+        size_t pl;
5d65fd
         int r;
5d65fd
 
5d65fd
         assert(bus);
5d65fd
@@ -2568,7 +2597,12 @@ static int interfaces_added_append_one(
5d65fd
         if (bus->nodes_modified)
5d65fd
                 return 0;
5d65fd
 
5d65fd
-        prefix = alloca(strlen(path) + 1);
5d65fd
+        pl = strlen(path);
5d65fd
+        assert(pl <= BUS_PATH_SIZE_MAX);
5d65fd
+        prefix = new(char, pl + 1);
5d65fd
+        if (!prefix)
5d65fd
+                return -ENOMEM;
5d65fd
+
5d65fd
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
5d65fd
                 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
5d65fd
                 if (r != 0)