0003-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. [Adam: Update for v241]
  12. Signed-off-by: Adam Duskett <aduskett@gmail.com>
  13. ---
  14. src/libsystemd/sd-bus/bus-objects.c | 68 +++++++++++++++++++++++------
  15. 1 file changed, 54 insertions(+), 14 deletions(-)
  16. diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c
  17. index 58329f3fe78..54b977418e0 100644
  18. --- a/src/libsystemd/sd-bus/bus-objects.c
  19. +++ b/src/libsystemd/sd-bus/bus-objects.c
  20. @@ -1133,7 +1133,8 @@ static int object_manager_serialize_path_and_fallbacks(
  21. const char *path,
  22. sd_bus_error *error) {
  23. - char *prefix;
  24. + _cleanup_free_ char *prefix = NULL;
  25. + size_t pl;
  26. int r;
  27. assert(bus);
  28. @@ -1149,7 +1150,12 @@ static int object_manager_serialize_path_and_fallbacks(
  29. return 0;
  30. /* Second, add fallback vtables registered for any of the prefixes */
  31. - prefix = newa(char, strlen(path) + 1);
  32. + pl = strlen(path);
  33. + assert(pl <= BUS_PATH_SIZE_MAX);
  34. + prefix = new(char, pl + 1);
  35. + if (!prefix)
  36. + return -ENOMEM;
  37. +
  38. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  39. r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
  40. if (r < 0)
  41. @@ -1345,6 +1351,7 @@ static int object_find_and_run(
  42. }
  43. int bus_process_object(sd_bus *bus, sd_bus_message *m) {
  44. + _cleanup_free_ char *prefix = NULL;
  45. int r;
  46. size_t pl;
  47. bool found_object = false;
  48. @@ -1369,9 +1376,12 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) {
  49. assert(m->member);
  50. pl = strlen(m->path);
  51. - do {
  52. - char prefix[pl+1];
  53. + assert(pl <= BUS_PATH_SIZE_MAX);
  54. + prefix = new(char, pl + 1);
  55. + if (!prefix)
  56. + return -ENOMEM;
  57. + do {
  58. bus->nodes_modified = false;
  59. r = object_find_and_run(bus, m, m->path, false, &found_object);
  60. @@ -1498,9 +1508,15 @@ static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const
  61. n = hashmap_get(bus->nodes, path);
  62. if (!n) {
  63. - char *prefix;
  64. + _cleanup_free_ char *prefix = NULL;
  65. + size_t pl;
  66. +
  67. + pl = strlen(path);
  68. + assert(pl <= BUS_PATH_SIZE_MAX);
  69. + prefix = new(char, pl + 1);
  70. + if (!prefix)
  71. + return -ENOMEM;
  72. - prefix = newa(char, strlen(path) + 1);
  73. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  74. n = hashmap_get(bus->nodes, prefix);
  75. if (n)
  76. @@ -2083,8 +2099,9 @@ _public_ int sd_bus_emit_properties_changed_strv(
  77. const char *interface,
  78. char **names) {
  79. + _cleanup_free_ char *prefix = NULL;
  80. bool found_interface = false;
  81. - char *prefix;
  82. + size_t pl;
  83. int r;
  84. assert_return(bus, -EINVAL);
  85. @@ -2105,6 +2122,12 @@ _public_ int sd_bus_emit_properties_changed_strv(
  86. BUS_DONT_DESTROY(bus);
  87. + pl = strlen(path);
  88. + assert(pl <= BUS_PATH_SIZE_MAX);
  89. + prefix = new(char, pl + 1);
  90. + if (!prefix)
  91. + return -ENOMEM;
  92. +
  93. do {
  94. bus->nodes_modified = false;
  95. @@ -2114,7 +2137,6 @@ _public_ int sd_bus_emit_properties_changed_strv(
  96. if (bus->nodes_modified)
  97. continue;
  98. - prefix = newa(char, strlen(path) + 1);
  99. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  100. r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
  101. if (r != 0)
  102. @@ -2246,7 +2268,8 @@ static int object_added_append_all_prefix(
  103. static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
  104. _cleanup_set_free_ Set *s = NULL;
  105. - char *prefix;
  106. + _cleanup_free_ char *prefix = NULL;
  107. + size_t pl;
  108. int r;
  109. assert(bus);
  110. @@ -2291,7 +2314,12 @@ static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *p
  111. if (bus->nodes_modified)
  112. return 0;
  113. - prefix = newa(char, strlen(path) + 1);
  114. + pl = strlen(path);
  115. + assert(pl <= BUS_PATH_SIZE_MAX);
  116. + prefix = new(char, pl + 1);
  117. + if (!prefix)
  118. + return -ENOMEM;
  119. +
  120. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  121. r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
  122. if (r < 0)
  123. @@ -2430,7 +2458,8 @@ static int object_removed_append_all_prefix(
  124. static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
  125. _cleanup_set_free_ Set *s = NULL;
  126. - char *prefix;
  127. + _cleanup_free_ char *prefix = NULL;
  128. + size_t pl;
  129. int r;
  130. assert(bus);
  131. @@ -2462,7 +2491,12 @@ static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char
  132. if (bus->nodes_modified)
  133. return 0;
  134. - prefix = newa(char, strlen(path) + 1);
  135. + pl = strlen(path);
  136. + assert(pl <= BUS_PATH_SIZE_MAX);
  137. + prefix = new(char, pl + 1);
  138. + if (!prefix)
  139. + return -ENOMEM;
  140. +
  141. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  142. r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
  143. if (r < 0)
  144. @@ -2612,7 +2646,8 @@ static int interfaces_added_append_one(
  145. const char *path,
  146. const char *interface) {
  147. - char *prefix;
  148. + _cleanup_free_ char *prefix = NULL;
  149. + size_t pl;
  150. int r;
  151. assert(bus);
  152. @@ -2626,7 +2661,12 @@ static int interfaces_added_append_one(
  153. if (bus->nodes_modified)
  154. return 0;
  155. - prefix = newa(char, strlen(path) + 1);
  156. + pl = strlen(path);
  157. + assert(pl <= BUS_PATH_SIZE_MAX);
  158. + prefix = new(char, pl + 1);
  159. + if (!prefix)
  160. + return -ENOMEM;
  161. +
  162. OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
  163. r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
  164. if (r != 0)
  165. --
  166. 2.20.1