4bff0a
From 5eb96e51ca2ce0e90e90ecb9f78e8305f51fa5dd Mon Sep 17 00:00:00 2001
563e3e
From: Riccardo Schirone <rschiron@redhat.com>
563e3e
Date: Mon, 4 Feb 2019 14:29:28 +0100
563e3e
Subject: [PATCH] Allocate temporary strings to hold dbus paths on the heap
563e3e
563e3e
Paths are limited to BUS_PATH_SIZE_MAX but the maximum size is anyway too big
563e3e
to be allocated on the stack, so let's switch to the heap where there is a
563e3e
clear way to understand if the allocation fails.
563e3e
563e3e
(cherry-picked from commit f519a19bcd5afe674a9b8fc462cd77d8bad403c1)
563e3e
563e3e
Related: #1678641
563e3e
---
563e3e
 src/libsystemd/sd-bus/bus-objects.c | 68 +++++++++++++++++++++++------
563e3e
 1 file changed, 54 insertions(+), 14 deletions(-)
563e3e
563e3e
diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c
4bff0a
index a18ff88b07..53bf0fd620 100644
563e3e
--- a/src/libsystemd/sd-bus/bus-objects.c
563e3e
+++ b/src/libsystemd/sd-bus/bus-objects.c
563e3e
@@ -1134,7 +1134,8 @@ static int object_manager_serialize_path_and_fallbacks(
563e3e
                 const char *path,
563e3e
                 sd_bus_error *error) {
563e3e
 
563e3e
-        char *prefix;
563e3e
+        _cleanup_free_ char *prefix = NULL;
563e3e
+        size_t pl;
563e3e
         int r;
563e3e
 
563e3e
         assert(bus);
563e3e
@@ -1150,7 +1151,12 @@ static int object_manager_serialize_path_and_fallbacks(
563e3e
                 return 0;
563e3e
 
563e3e
         /* Second, add fallback vtables registered for any of the prefixes */
563e3e
-        prefix = alloca(strlen(path) + 1);
563e3e
+        pl = strlen(path);
563e3e
+        assert(pl <= BUS_PATH_SIZE_MAX);
563e3e
+        prefix = new(char, pl + 1);
563e3e
+        if (!prefix)
563e3e
+                return -ENOMEM;
563e3e
+
563e3e
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
563e3e
                 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
563e3e
                 if (r < 0)
563e3e
@@ -1346,6 +1352,7 @@ static int object_find_and_run(
563e3e
 }
563e3e
 
563e3e
 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
563e3e
+        _cleanup_free_ char *prefix = NULL;
563e3e
         int r;
563e3e
         size_t pl;
563e3e
         bool found_object = false;
563e3e
@@ -1370,9 +1377,12 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) {
563e3e
         assert(m->member);
563e3e
 
563e3e
         pl = strlen(m->path);
563e3e
-        do {
563e3e
-                char prefix[pl+1];
563e3e
+        assert(pl <= BUS_PATH_SIZE_MAX);
563e3e
+        prefix = new(char, pl + 1);
563e3e
+        if (!prefix)
563e3e
+                return -ENOMEM;
563e3e
 
563e3e
+        do {
563e3e
                 bus->nodes_modified = false;
563e3e
 
563e3e
                 r = object_find_and_run(bus, m, m->path, false, &found_object);
563e3e
@@ -1499,9 +1509,15 @@ static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const
563e3e
 
563e3e
         n = hashmap_get(bus->nodes, path);
563e3e
         if (!n) {
563e3e
-                char *prefix;
563e3e
+                _cleanup_free_ char *prefix = NULL;
563e3e
+                size_t pl;
563e3e
+
563e3e
+                pl = strlen(path);
563e3e
+                assert(pl <= BUS_PATH_SIZE_MAX);
563e3e
+                prefix = new(char, pl + 1);
563e3e
+                if (!prefix)
563e3e
+                        return -ENOMEM;
563e3e
 
563e3e
-                prefix = alloca(strlen(path) + 1);
563e3e
                 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
563e3e
                         n = hashmap_get(bus->nodes, prefix);
563e3e
                         if (n)
563e3e
@@ -2090,8 +2106,9 @@ _public_ int sd_bus_emit_properties_changed_strv(
563e3e
                 const char *interface,
563e3e
                 char **names) {
563e3e
 
563e3e
+        _cleanup_free_ char *prefix = NULL;
563e3e
         bool found_interface = false;
563e3e
-        char *prefix;
563e3e
+        size_t pl;
563e3e
         int r;
563e3e
 
563e3e
         assert_return(bus, -EINVAL);
563e3e
@@ -2112,6 +2129,12 @@ _public_ int sd_bus_emit_properties_changed_strv(
563e3e
 
563e3e
         BUS_DONT_DESTROY(bus);
563e3e
 
563e3e
+        pl = strlen(path);
563e3e
+        assert(pl <= BUS_PATH_SIZE_MAX);
563e3e
+        prefix = new(char, pl + 1);
563e3e
+        if (!prefix)
563e3e
+                return -ENOMEM;
563e3e
+
563e3e
         do {
563e3e
                 bus->nodes_modified = false;
563e3e
 
563e3e
@@ -2121,7 +2144,6 @@ _public_ int sd_bus_emit_properties_changed_strv(
563e3e
                 if (bus->nodes_modified)
563e3e
                         continue;
563e3e
 
563e3e
-                prefix = alloca(strlen(path) + 1);
563e3e
                 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
563e3e
                         r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
563e3e
                         if (r != 0)
563e3e
@@ -2253,7 +2275,8 @@ static int object_added_append_all_prefix(
563e3e
 
563e3e
 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
563e3e
         _cleanup_set_free_ Set *s = NULL;
563e3e
-        char *prefix;
563e3e
+        _cleanup_free_ char *prefix = NULL;
563e3e
+        size_t pl;
563e3e
         int r;
563e3e
 
563e3e
         assert(bus);
563e3e
@@ -2298,7 +2321,12 @@ static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *p
563e3e
         if (bus->nodes_modified)
563e3e
                 return 0;
563e3e
 
563e3e
-        prefix = alloca(strlen(path) + 1);
563e3e
+        pl = strlen(path);
563e3e
+        assert(pl <= BUS_PATH_SIZE_MAX);
563e3e
+        prefix = new(char, pl + 1);
563e3e
+        if (!prefix)
563e3e
+                return -ENOMEM;
563e3e
+
563e3e
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
563e3e
                 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
563e3e
                 if (r < 0)
563e3e
@@ -2437,7 +2465,8 @@ static int object_removed_append_all_prefix(
563e3e
 
563e3e
 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
563e3e
         _cleanup_set_free_ Set *s = NULL;
563e3e
-        char *prefix;
563e3e
+        _cleanup_free_ char *prefix = NULL;
563e3e
+        size_t pl;
563e3e
         int r;
563e3e
 
563e3e
         assert(bus);
563e3e
@@ -2469,7 +2498,12 @@ static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char
563e3e
         if (bus->nodes_modified)
563e3e
                 return 0;
563e3e
 
563e3e
-        prefix = alloca(strlen(path) + 1);
563e3e
+        pl = strlen(path);
563e3e
+        assert(pl <= BUS_PATH_SIZE_MAX);
563e3e
+        prefix = new(char, pl + 1);
563e3e
+        if (!prefix)
563e3e
+                return -ENOMEM;
563e3e
+
563e3e
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
563e3e
                 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
563e3e
                 if (r < 0)
563e3e
@@ -2619,7 +2653,8 @@ static int interfaces_added_append_one(
563e3e
                 const char *path,
563e3e
                 const char *interface) {
563e3e
 
563e3e
-        char *prefix;
563e3e
+        _cleanup_free_ char *prefix = NULL;
563e3e
+        size_t pl;
563e3e
         int r;
563e3e
 
563e3e
         assert(bus);
563e3e
@@ -2633,7 +2668,12 @@ static int interfaces_added_append_one(
563e3e
         if (bus->nodes_modified)
563e3e
                 return 0;
563e3e
 
563e3e
-        prefix = alloca(strlen(path) + 1);
563e3e
+        pl = strlen(path);
563e3e
+        assert(pl <= BUS_PATH_SIZE_MAX);
563e3e
+        prefix = new(char, pl + 1);
563e3e
+        if (!prefix)
563e3e
+                return -ENOMEM;
563e3e
+
563e3e
         OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
563e3e
                 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
563e3e
                 if (r != 0)