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