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