0019-Allocate-temporary-strings-to-hold-dbus-paths-on-the.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. From 9e3f5a77226d5320270c92df001f6c79be735af3 Mon Sep 17 00:00:00 2001
  2. From: Riccardo Schirone <rschiron@redhat.com>
  3. Date: Mon, 4 Feb 2019 14:29:28 +0100
  4. Subject: [PATCH] Allocate temporary strings to hold dbus paths on the heap
  5. Paths are limited to BUS_PATH_SIZE_MAX but the maximum size is anyway too big
  6. to be allocated on the stack, so let's switch to the heap where there is a
  7. clear way to understand if the allocation fails.
  8. (cherry picked from commit f519a19bcd5afe674a9b8fc462cd77d8bad403c1)
  9. [baruch: backport to v240]
  10. Signed-off-by: Baruch Siach <baruch@tkos.co.il>
  11. ---
  12. Upstream status: commit f519a19bcd5
  13. src/libsystemd/sd-bus/bus-objects.c | 68 +++++++++++++++++++++++------
  14. 1 file changed, 54 insertions(+), 14 deletions(-)
  15. diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c
  16. index d0538104ae25..54b977418e03 100644
  17. --- a/src/libsystemd/sd-bus/bus-objects.c
  18. +++ b/src/libsystemd/sd-bus/bus-objects.c
  19. @@ -1133,7 +1133,8 @@ static int object_manager_serialize_path_and_fallbacks(
  20. const char *path,
  21. sd_bus_error *error) {
  22. - char *prefix;
  23. + _cleanup_free_ char *prefix = NULL;
  24. + size_t pl;
  25. int r;
  26. assert(bus);
  27. @@ -1149,7 +1150,12 @@ static int object_manager_serialize_path_and_fallbacks(
  28. return 0;
  29. /* Second, add fallback vtables registered for any of the prefixes */
  30. - prefix = alloca(strlen(path) + 1);
  31. + pl = strlen(path);
  32. + assert(pl <= BUS_PATH_SIZE_MAX);
  33. + prefix = new(char, pl + 1);
  34. + if (!prefix)
  35. + return -ENOMEM;
  36. +
  37. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  38. r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
  39. if (r < 0)
  40. @@ -1345,6 +1351,7 @@ static int object_find_and_run(
  41. }
  42. int bus_process_object(sd_bus *bus, sd_bus_message *m) {
  43. + _cleanup_free_ char *prefix = NULL;
  44. int r;
  45. size_t pl;
  46. bool found_object = false;
  47. @@ -1369,9 +1376,12 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) {
  48. assert(m->member);
  49. pl = strlen(m->path);
  50. - do {
  51. - char prefix[pl+1];
  52. + assert(pl <= BUS_PATH_SIZE_MAX);
  53. + prefix = new(char, pl + 1);
  54. + if (!prefix)
  55. + return -ENOMEM;
  56. + do {
  57. bus->nodes_modified = false;
  58. r = object_find_and_run(bus, m, m->path, false, &found_object);
  59. @@ -1498,9 +1508,15 @@ static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const
  60. n = hashmap_get(bus->nodes, path);
  61. if (!n) {
  62. - char *prefix;
  63. + _cleanup_free_ char *prefix = NULL;
  64. + size_t pl;
  65. +
  66. + pl = strlen(path);
  67. + assert(pl <= BUS_PATH_SIZE_MAX);
  68. + prefix = new(char, pl + 1);
  69. + if (!prefix)
  70. + return -ENOMEM;
  71. - prefix = alloca(strlen(path) + 1);
  72. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  73. n = hashmap_get(bus->nodes, prefix);
  74. if (n)
  75. @@ -2083,8 +2099,9 @@ _public_ int sd_bus_emit_properties_changed_strv(
  76. const char *interface,
  77. char **names) {
  78. + _cleanup_free_ char *prefix = NULL;
  79. bool found_interface = false;
  80. - char *prefix;
  81. + size_t pl;
  82. int r;
  83. assert_return(bus, -EINVAL);
  84. @@ -2105,6 +2122,12 @@ _public_ int sd_bus_emit_properties_changed_strv(
  85. BUS_DONT_DESTROY(bus);
  86. + pl = strlen(path);
  87. + assert(pl <= BUS_PATH_SIZE_MAX);
  88. + prefix = new(char, pl + 1);
  89. + if (!prefix)
  90. + return -ENOMEM;
  91. +
  92. do {
  93. bus->nodes_modified = false;
  94. @@ -2114,7 +2137,6 @@ _public_ int sd_bus_emit_properties_changed_strv(
  95. if (bus->nodes_modified)
  96. continue;
  97. - prefix = alloca(strlen(path) + 1);
  98. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  99. r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
  100. if (r != 0)
  101. @@ -2246,7 +2268,8 @@ static int object_added_append_all_prefix(
  102. static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
  103. _cleanup_set_free_ Set *s = NULL;
  104. - char *prefix;
  105. + _cleanup_free_ char *prefix = NULL;
  106. + size_t pl;
  107. int r;
  108. assert(bus);
  109. @@ -2291,7 +2314,12 @@ static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *p
  110. if (bus->nodes_modified)
  111. return 0;
  112. - prefix = alloca(strlen(path) + 1);
  113. + pl = strlen(path);
  114. + assert(pl <= BUS_PATH_SIZE_MAX);
  115. + prefix = new(char, pl + 1);
  116. + if (!prefix)
  117. + return -ENOMEM;
  118. +
  119. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  120. r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
  121. if (r < 0)
  122. @@ -2430,7 +2458,8 @@ static int object_removed_append_all_prefix(
  123. static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
  124. _cleanup_set_free_ Set *s = NULL;
  125. - char *prefix;
  126. + _cleanup_free_ char *prefix = NULL;
  127. + size_t pl;
  128. int r;
  129. assert(bus);
  130. @@ -2462,7 +2491,12 @@ static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char
  131. if (bus->nodes_modified)
  132. return 0;
  133. - prefix = alloca(strlen(path) + 1);
  134. + pl = strlen(path);
  135. + assert(pl <= BUS_PATH_SIZE_MAX);
  136. + prefix = new(char, pl + 1);
  137. + if (!prefix)
  138. + return -ENOMEM;
  139. +
  140. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  141. r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
  142. if (r < 0)
  143. @@ -2612,7 +2646,8 @@ static int interfaces_added_append_one(
  144. const char *path,
  145. const char *interface) {
  146. - char *prefix;
  147. + _cleanup_free_ char *prefix = NULL;
  148. + size_t pl;
  149. int r;
  150. assert(bus);
  151. @@ -2626,7 +2661,12 @@ static int interfaces_added_append_one(
  152. if (bus->nodes_modified)
  153. return 0;
  154. - prefix = alloca(strlen(path) + 1);
  155. + pl = strlen(path);
  156. + assert(pl <= BUS_PATH_SIZE_MAX);
  157. + prefix = new(char, pl + 1);
  158. + if (!prefix)
  159. + return -ENOMEM;
  160. +
  161. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  162. r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
  163. if (r != 0)
  164. --
  165. 2.20.1