You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
12 KiB

  1. From 10e8001ad876d8cb3b5a17c7492e713bbc047975 Mon Sep 17 00:00:00 2001
  2. From: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
  3. Date: Thu, 28 Mar 2019 18:31:29 -0400
  4. Subject: [PATCH] Fix: getgrnam is not MT-Safe, use getgrnam_r
  5. Running the test suite under a Yocto musl build resulted in musl
  6. coredump due to double freeing.
  7. We get the following backtraces:
  8. 0 a_crash () at ./arch/x86_64/atomic_arch.h:108
  9. 1 unmap_chunk (self=<optimized out>) at src/malloc/malloc.c:515
  10. 2 free (p=<optimized out>) at src/malloc/malloc.c:526
  11. 3 0x00007f46d9dc3849 in __getgrent_a (f=f@entry=0x7f46d9d1f7e0, gr=gr@entry=0x7f46d9e24460 <gr>, line=line@entry=0x7f46d9e26058 <line>, size=size@entry=0x7f46d92db550, mem=mem@entry=0x7f46d9e26050 <mem>, nmem=nmem@entry=0x7f46d92db558, res=0x7f46d92db548) at src/passwd/getgrent_a.c:45
  12. 4 0x00007f46d9dc2e6b in __getgr_a (name=0x487242 "tracing", gid=gid@entry=0, gr=gr@entry=0x7f46d9e24460 <gr>, buf=buf@entry=0x7f46d9e26058 <line>, size=size@entry=0x7f46d92db550, mem=mem@entry=0x7f46d9e26050 <mem>, nmem=0x7f46d92db558, res=0x7f46d92db548) at src/passwd/getgr_a.c:30
  13. 5 0x00007f46d9dc3733 in getgrnam (name=<optimized out>) at src/passwd/getgrent.c:37
  14. 6 0x0000000000460b29 in utils_get_group_id (name=<optimized out>) at ../../../lttng-tools-2.10.6/src/common/utils.c:1241
  15. 7 0x000000000044ee69 in thread_manage_health (data=<optimized out>) at ../../../../lttng-tools-2.10.6/src/bin/lttng-sessiond/main.c:4115
  16. 8 0x00007f46d9de1541 in start (p=<optimized out>) at src/thread/pthread_create.c:195
  17. 9 0x00007f46d9dee661 in __clone () at src/thread/x86_64/clone.s:22
  18. From another run:
  19. 0 a_crash () at ./arch/x86_64/atomic_arch.h:108
  20. 1 unmap_chunk (self=<optimized out>) at src/malloc/malloc.c:515
  21. 2 free (p=<optimized out>) at src/malloc/malloc.c:526
  22. 3 0x00007f5abc210849 in __getgrent_a (f=f@entry=0x7f5abc2733e0, gr=gr@entry=0x7f5abc271460 <gr>, line=line@entry=0x7f5abc273058 <line>, size=size@entry=0x7f5abaef5510, mem=mem@entry=0x7f5abc273050 <mem>, nmem=nmem@entry=0x7f5abaef5518, res=0x7f5abaef5508) at src/passwd/getgrent_a.c:45
  23. 4 0x00007f5abc20fe6b in __getgr_a (name=0x487242 "tracing", gid=gid@entry=0, gr=gr@entry=0x7f5abc271460 <gr>, buf=buf@entry=0x7f5abc273058 <line>, size=size@entry=0x7f5abaef5510, mem=mem@entry=0x7f5abc273050 <mem>, nmem=0x7f5abaef5518, res=0x7f5abaef5508) at src/passwd/getgr_a.c:30
  24. 5 0x00007f5abc210733 in getgrnam (name=<optimized out>) at src/passwd/getgrent.c:37
  25. 6 0x0000000000460b29 in utils_get_group_id (name=<optimized out>) at ../../../lttng-tools-2.10.6/src/common/utils.c:1241
  26. 7 0x000000000042dee4 in notification_channel_socket_create () at ../../../../lttng-tools-2.10.6/src/bin/lttng-sessiond/notification-thread.c:238
  27. 8 init_thread_state (state=0x7f5abaef5560, handle=0x7f5abbf9be40) at ../../../../lttng-tools-2.10.6/src/bin/lttng-sessiond/notification-thread.c:375
  28. 9 thread_notification (data=0x7f5abbf9be40) at ../../../../lttng-tools-2.10.6/src/bin/lttng-sessiond/notification-thread.c:495
  29. 10 0x00007f5abc22e541 in start (p=<optimized out>) at src/thread/pthread_create.c:195
  30. 11 0x00007f5abc23b661 in __clone () at src/thread/x86_64/clone.s:22
  31. The problem was easily reproducible (~6 crash on ~300 runs). A prototype fix
  32. using mutex around the getgrnam yielded no crash in over 1000 runs. This
  33. patch yielded the same results as the prototype fix.
  34. Unfortunately we cannot rely on a mutex in liblttng-ctl since we cannot
  35. enforce the locking for the application using the lib.
  36. Use getgrnam_r instead.
  37. The previous implementation of utils_get_group_id returned the gid of
  38. the root group (0) on error/not found. lttng_check_tracing_group needs
  39. to know if an error/not found occured, returning the root group is not
  40. enough. We now return the gid via the passed parameter. The caller is
  41. responsible for either defaulting to the root group or propagating the
  42. error.
  43. We also do not want to warn when used in liblttng-ctl context. We might
  44. want to move the warning elsewhere in the future. For now, pass a bool
  45. if we need to warn or not.
  46. Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
  47. ---
  48. src/bin/lttng-consumerd/health-consumerd.c | 10 ++-
  49. src/bin/lttng-relayd/health-relayd.c | 20 ++++--
  50. src/bin/lttng-sessiond/main.c | 24 +++++--
  51. src/bin/lttng-sessiond/notification-thread.c | 10 ++-
  52. src/common/utils.c | 75 +++++++++++++++++---
  53. src/common/utils.h | 4 +-
  54. src/lib/lttng-ctl/lttng-ctl.c | 8 +--
  55. 7 files changed, 122 insertions(+), 29 deletions(-)
  56. diff --git a/src/bin/lttng-consumerd/health-consumerd.c b/src/bin/lttng-consumerd/health-consumerd.c
  57. index 1e2f31e4..6045401a 100644
  58. --- a/src/bin/lttng-consumerd/health-consumerd.c
  59. +++ b/src/bin/lttng-consumerd/health-consumerd.c
  60. @@ -184,8 +184,14 @@ void *thread_manage_health(void *data)
  61. is_root = !getuid();
  62. if (is_root) {
  63. /* lttng health client socket path permissions */
  64. - ret = chown(health_unix_sock_path, 0,
  65. - utils_get_group_id(tracing_group_name));
  66. + gid_t gid;
  67. +
  68. + ret = utils_get_group_id(tracing_group_name, true, &gid);
  69. + if (ret) {
  70. + gid = 0; /* Default to root group. */
  71. + }
  72. +
  73. + ret = chown(health_unix_sock_path, 0, gid);
  74. if (ret < 0) {
  75. ERR("Unable to set group on %s", health_unix_sock_path);
  76. PERROR("chown");
  77. diff --git a/src/bin/lttng-relayd/health-relayd.c b/src/bin/lttng-relayd/health-relayd.c
  78. index ba996621..962e88c4 100644
  79. --- a/src/bin/lttng-relayd/health-relayd.c
  80. +++ b/src/bin/lttng-relayd/health-relayd.c
  81. @@ -105,8 +105,14 @@ static int create_lttng_rundir_with_perm(const char *rundir)
  82. int is_root = !getuid();
  83. if (is_root) {
  84. - ret = chown(rundir, 0,
  85. - utils_get_group_id(tracing_group_name));
  86. + gid_t gid;
  87. +
  88. + ret = utils_get_group_id(tracing_group_name, true, &gid);
  89. + if (ret) {
  90. + gid = 0; /* Default to root group.*/
  91. + }
  92. +
  93. + ret = chown(rundir, 0, gid);
  94. if (ret < 0) {
  95. ERR("Unable to set group on %s", rundir);
  96. PERROR("chown");
  97. @@ -256,8 +262,14 @@ void *thread_manage_health(void *data)
  98. is_root = !getuid();
  99. if (is_root) {
  100. /* lttng health client socket path permissions */
  101. - ret = chown(health_unix_sock_path, 0,
  102. - utils_get_group_id(tracing_group_name));
  103. + gid_t gid;
  104. +
  105. + ret = utils_get_group_id(tracing_group_name, true, &gid);
  106. + if (ret) {
  107. + gid = 0; /* Default to root group */
  108. + }
  109. +
  110. + ret = chown(health_unix_sock_path, 0, gid);
  111. if (ret < 0) {
  112. ERR("Unable to set group on %s", health_unix_sock_path);
  113. PERROR("chown");
  114. diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c
  115. index fa6fa483..49307064 100644
  116. --- a/src/bin/lttng-sessiond/main.c
  117. +++ b/src/bin/lttng-sessiond/main.c
  118. @@ -4112,8 +4112,14 @@ static void *thread_manage_health(void *data)
  119. if (is_root) {
  120. /* lttng health client socket path permissions */
  121. - ret = chown(config.health_unix_sock_path.value, 0,
  122. - utils_get_group_id(config.tracing_group_name.value));
  123. + gid_t gid;
  124. +
  125. + ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
  126. + if (ret) {
  127. + gid = 0; /* Default to root group */
  128. + }
  129. +
  130. + ret = chown(config.health_unix_sock_path.value, 0, &gid);
  131. if (ret < 0) {
  132. ERR("Unable to set group on %s", config.health_unix_sock_path.value);
  133. PERROR("chown");
  134. @@ -5238,7 +5244,10 @@ static int set_permissions(char *rundir)
  135. int ret;
  136. gid_t gid;
  137. - gid = utils_get_group_id(config.tracing_group_name.value);
  138. + ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
  139. + if (ret) {
  140. + gid = 0; /* Default to root group */
  141. + }
  142. /* Set lttng run dir */
  143. ret = chown(rundir, 0, gid);
  144. @@ -5349,7 +5358,14 @@ static int set_consumer_sockets(struct consumer_data *consumer_data)
  145. goto error;
  146. }
  147. if (is_root) {
  148. - ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value));
  149. + gid_t gid;
  150. +
  151. + ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
  152. + if (ret) {
  153. + gid = 0; /* Default to root group */
  154. + }
  155. +
  156. + ret = chown(path, 0, gid);
  157. if (ret < 0) {
  158. ERR("Unable to set group on %s", path);
  159. PERROR("chown");
  160. diff --git a/src/bin/lttng-sessiond/notification-thread.c b/src/bin/lttng-sessiond/notification-thread.c
  161. index 92ac597f..18a264d9 100644
  162. --- a/src/bin/lttng-sessiond/notification-thread.c
  163. +++ b/src/bin/lttng-sessiond/notification-thread.c
  164. @@ -235,8 +235,14 @@ int notification_channel_socket_create(void)
  165. }
  166. if (getuid() == 0) {
  167. - ret = chown(sock_path, 0,
  168. - utils_get_group_id(config.tracing_group_name.value));
  169. + gid_t gid;
  170. +
  171. + ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
  172. + if (ret) {
  173. + gid = 0; /* Default to root group. */
  174. + }
  175. +
  176. + ret = chown(sock_path, 0, gid);
  177. if (ret) {
  178. ERR("Failed to set the notification channel socket's group");
  179. ret = -1;
  180. diff --git a/src/common/utils.c b/src/common/utils.c
  181. index c0bb031e..778bc00f 100644
  182. --- a/src/common/utils.c
  183. +++ b/src/common/utils.c
  184. @@ -1231,24 +1231,77 @@ size_t utils_get_current_time_str(const char *format, char *dst, size_t len)
  185. }
  186. /*
  187. - * Return the group ID matching name, else 0 if it cannot be found.
  188. + * Return 0 on success and set *gid to the group_ID matching the passed name.
  189. + * Else -1 if it cannot be found or an error occurred.
  190. */
  191. LTTNG_HIDDEN
  192. -gid_t utils_get_group_id(const char *name)
  193. +int utils_get_group_id(const char *name, bool warn, gid_t *gid)
  194. {
  195. - struct group *grp;
  196. + static volatile int warn_once;
  197. - grp = getgrnam(name);
  198. - if (!grp) {
  199. - static volatile int warn_once;
  200. + int ret;
  201. + long sys_len;
  202. + size_t len;
  203. + struct group grp;
  204. + struct group *result;
  205. + char *buffer = NULL;
  206. - if (!warn_once) {
  207. - WARN("No tracing group detected");
  208. - warn_once = 1;
  209. + /* Get the system limit if it exists */
  210. + sys_len = sysconf(_SC_GETGR_R_SIZE_MAX);
  211. + if (sys_len == -1) {
  212. + len = 1024;
  213. + } else {
  214. + len = (size_t) sys_len;
  215. + }
  216. +
  217. + buffer = malloc(len);
  218. + if (!buffer) {
  219. + PERROR("getgrnam_r malloc");
  220. + ret = -1;
  221. + goto error;
  222. + }
  223. +
  224. + while ((ret = getgrnam_r(name, &grp, buffer, len, &result)) == ERANGE)
  225. + {
  226. + /* Buffer is not big enough, increase its size. */
  227. + size_t new_len = 2 * len;
  228. + char *new_buffer = NULL;
  229. + if (new_len < len) {
  230. + ERR("getgrnam_r buffer size overflow");
  231. + ret = -1;
  232. + goto error;
  233. + }
  234. + len = new_len;
  235. + new_buffer = realloc(buffer, len);
  236. + if (!new_buffer) {
  237. + PERROR("getgrnam_r realloc");
  238. + ret = -1;
  239. + goto error;
  240. }
  241. - return 0;
  242. + buffer = new_buffer;
  243. + }
  244. + if (ret != 0) {
  245. + PERROR("getgrnam_r");
  246. + ret = -1;
  247. + goto error;
  248. + }
  249. +
  250. + /* Group not found. */
  251. + if (!result) {
  252. + ret = -1;
  253. + goto error;
  254. + }
  255. +
  256. + *gid = result->gr_gid;
  257. + ret = 0;
  258. +
  259. +error:
  260. + free(buffer);
  261. + if (ret && warn && !warn_once) {
  262. + WARN("No tracing group detected");
  263. + warn_once = 1;
  264. }
  265. - return grp->gr_gid;
  266. + return ret;
  267. }
  268. /*
  269. diff --git a/src/common/utils.h b/src/common/utils.h
  270. index 18f19ef1..9c72431d 100644
  271. --- a/src/common/utils.h
  272. +++ b/src/common/utils.h
  273. @@ -22,6 +22,8 @@
  274. #include <unistd.h>
  275. #include <stdint.h>
  276. #include <getopt.h>
  277. +#include <stdbool.h>
  278. +#include <sys/types.h>
  279. #define KIBI_LOG2 10
  280. #define MEBI_LOG2 20
  281. @@ -52,7 +54,7 @@ int utils_get_count_order_u64(uint64_t x);
  282. char *utils_get_home_dir(void);
  283. char *utils_get_user_home_dir(uid_t uid);
  284. size_t utils_get_current_time_str(const char *format, char *dst, size_t len);
  285. -gid_t utils_get_group_id(const char *name);
  286. +int utils_get_group_id(const char *name, bool warn, gid_t *gid);
  287. char *utils_generate_optstring(const struct option *long_options,
  288. size_t opt_count);
  289. int utils_create_lock_file(const char *filepath);
  290. diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c
  291. index 2d84aad9..561b0bcf 100644
  292. --- a/src/lib/lttng-ctl/lttng-ctl.c
  293. +++ b/src/lib/lttng-ctl/lttng-ctl.c
  294. @@ -208,15 +208,13 @@ end:
  295. LTTNG_HIDDEN
  296. int lttng_check_tracing_group(void)
  297. {
  298. - struct group *grp_tracing; /* no free(). See getgrnam(3) */
  299. - gid_t *grp_list;
  300. + gid_t *grp_list, tracing_gid;
  301. int grp_list_size, grp_id, i;
  302. int ret = -1;
  303. const char *grp_name = tracing_group;
  304. /* Get GID of group 'tracing' */
  305. - grp_tracing = getgrnam(grp_name);
  306. - if (!grp_tracing) {
  307. + if (utils_get_group_id(grp_name, false, &tracing_gid)) {
  308. /* If grp_tracing is NULL, the group does not exist. */
  309. goto end;
  310. }
  311. @@ -241,7 +239,7 @@ int lttng_check_tracing_group(void)
  312. }
  313. for (i = 0; i < grp_list_size; i++) {
  314. - if (grp_list[i] == grp_tracing->gr_gid) {
  315. + if (grp_list[i] == tracing_gid) {
  316. ret = 1;
  317. break;
  318. }
  319. --
  320. 2.17.1