valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

Blame SOURCES/0108-Allocate-temporary-strings-to-hold-dbus-paths-on-the.patch

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