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