Fix: fd tracker: do not allow signal handlers to close lttng-ust FDs
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-fd-tracker.c
CommitLineData
6548fca4
MD
1/*
2 * Copyright (C) 2016 - Aravind HT <aravind.ht@gmail.com>
3 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#define _GNU_SOURCE
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <assert.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <sys/select.h>
31#include <sys/resource.h>
32#include <sys/time.h>
33#include <fcntl.h>
34#include <pthread.h>
96a6162e
MD
35#include <signal.h>
36#include <stdbool.h>
6548fca4
MD
37#include <urcu/compiler.h>
38#include <urcu/tls-compat.h>
7d34f27d 39#include <urcu/system.h>
6548fca4
MD
40
41#include <ust-fd.h>
42#include <helper.h>
43#include <lttng/ust-error.h>
44#include <usterr-signal-safe.h>
45
46#include "../liblttng-ust/compat.h"
47
48/* Operations on the fd set. */
49#define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd)
50#define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE]))
51#define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE)
f5c453e9 52#define IS_FD_STD(fd) (IS_FD_VALID(fd) && (fd) <= STDERR_FILENO)
6548fca4
MD
53
54/* Check fd validity before calling these. */
55#define ADD_FD_TO_SET(fd, fd_sets) \
56 FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
57#define IS_FD_SET(fd, fd_sets) \
58 FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
59#define DEL_FD_FROM_SET(fd, fd_sets) \
60 FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets))
61
62/*
63 * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
64 * within the libc dl lock. Therefore, we need to fixup the TLS before
65 * nesting into this lock.
c1be081a
MD
66 *
67 * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
68 * is also held across fork.
6548fca4
MD
69 */
70static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
283f4bec
MD
71
72/*
73 * Cancel state when grabbing the ust_safe_guard_fd_mutex. Saved when
74 * locking, restored on unlock. Protected by ust_safe_guard_fd_mutex.
75 */
76static int ust_safe_guard_saved_cancelstate;
77
6548fca4
MD
78/*
79 * Track whether we are within lttng-ust or application, for close
48a143c0
MD
80 * system call override by LD_PRELOAD library. Threads registered
81 * as being lttng-ust listener threads need to perform fd tracker
82 * locking explicitly around their use of file descriptor manipulation
83 * functions.
84 */
85static DEFINE_URCU_TLS(int, thread_fd_tracking);
86
87/*
88 * Track whether we are invoking close() from a signal handler nested on
89 * an application thread.
6548fca4 90 */
96a6162e 91static DEFINE_URCU_TLS(int, ust_fd_mutex_nest);
6548fca4 92
48a143c0 93
6548fca4
MD
94/* fd_set used to book keep fd being used by lttng-ust. */
95static fd_set *lttng_fd_set;
96static int lttng_ust_max_fd;
97static int num_fd_sets;
7d34f27d 98static int init_done;
6548fca4
MD
99
100/*
101 * Force a read (imply TLS fixup for dlopen) of TLS variables.
102 */
103void lttng_ust_fixup_fd_tracker_tls(void)
104{
48a143c0 105 asm volatile ("" : : "m" (URCU_TLS(thread_fd_tracking)));
96a6162e 106 asm volatile ("" : : "m" (URCU_TLS(ust_fd_mutex_nest)));
6548fca4
MD
107}
108
109/*
110 * Allocate the fd set array based on the hard limit set for this
111 * process. This will be called during the constructor execution
112 * and will also be called in the child after fork via lttng_ust_init.
113 */
114void lttng_ust_init_fd_tracker(void)
115{
116 struct rlimit rlim;
117 int i;
118
7d34f27d
MD
119 if (CMM_LOAD_SHARED(init_done))
120 return;
121
6548fca4
MD
122 memset(&rlim, 0, sizeof(rlim));
123 /* Get the current possible max number of fd for this process. */
124 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
125 abort();
126 /*
127 * FD set array size determined using the hard limit. Even if
128 * the process wishes to increase its limit using setrlimit, it
129 * can only do so with the softlimit which will be less than the
130 * hard limit.
131 */
132 lttng_ust_max_fd = rlim.rlim_max;
133 num_fd_sets = lttng_ust_max_fd / FD_SETSIZE;
134 if (lttng_ust_max_fd % FD_SETSIZE)
135 ++num_fd_sets;
136 if (lttng_fd_set != NULL) {
137 free(lttng_fd_set);
138 lttng_fd_set = NULL;
139 }
140 lttng_fd_set = malloc(num_fd_sets * (sizeof(fd_set)));
141 if (!lttng_fd_set)
142 abort();
143 for (i = 0; i < num_fd_sets; i++)
144 FD_ZERO((&lttng_fd_set[i]));
7d34f27d 145 CMM_STORE_SHARED(init_done, 1);
6548fca4
MD
146}
147
148void lttng_ust_lock_fd_tracker(void)
149{
96a6162e 150 sigset_t sig_all_blocked, orig_mask;
283f4bec
MD
151 int ret, oldstate;
152
153 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
154 if (ret) {
155 ERR("pthread_setcancelstate: %s", strerror(ret));
156 }
96a6162e
MD
157 sigfillset(&sig_all_blocked);
158 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
159 if (ret) {
160 ERR("pthread_sigmask: %s", strerror(ret));
161 }
162 if (!URCU_TLS(ust_fd_mutex_nest)++) {
163 /*
164 * Ensure the compiler don't move the store after the close()
165 * call in case close() would be marked as leaf.
166 */
167 cmm_barrier();
168 pthread_mutex_lock(&ust_safe_guard_fd_mutex);
169 ust_safe_guard_saved_cancelstate = oldstate;
170 }
171 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
172 if (ret) {
173 ERR("pthread_sigmask: %s", strerror(ret));
174 }
6548fca4
MD
175}
176
177void lttng_ust_unlock_fd_tracker(void)
178{
96a6162e 179 sigset_t sig_all_blocked, orig_mask;
283f4bec 180 int ret, newstate, oldstate;
96a6162e 181 bool restore_cancel = false;
283f4bec 182
96a6162e
MD
183 sigfillset(&sig_all_blocked);
184 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
185 if (ret) {
186 ERR("pthread_sigmask: %s", strerror(ret));
187 }
6548fca4
MD
188 /*
189 * Ensure the compiler don't move the store before the close()
190 * call, in case close() would be marked as leaf.
191 */
192 cmm_barrier();
96a6162e
MD
193 if (!--URCU_TLS(ust_fd_mutex_nest)) {
194 newstate = ust_safe_guard_saved_cancelstate;
195 restore_cancel = true;
196 pthread_mutex_unlock(&ust_safe_guard_fd_mutex);
197 }
198 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
283f4bec 199 if (ret) {
96a6162e
MD
200 ERR("pthread_sigmask: %s", strerror(ret));
201 }
202 if (restore_cancel) {
203 ret = pthread_setcancelstate(newstate, &oldstate);
204 if (ret) {
205 ERR("pthread_setcancelstate: %s", strerror(ret));
206 }
283f4bec 207 }
6548fca4
MD
208}
209
f5c453e9
JR
210static int dup_std_fd(int fd)
211{
5a4d96d1 212 int ret, i;
f5c453e9
JR
213 int fd_to_close[STDERR_FILENO + 1];
214 int fd_to_close_count = 0;
215 int dup_cmd = F_DUPFD; /* Default command */
216 int fd_valid = -1;
217
218 if (!(IS_FD_STD(fd))) {
219 /* Should not be here */
220 ret = -1;
221 goto error;
222 }
223
224 /* Check for FD_CLOEXEC flag */
225 ret = fcntl(fd, F_GETFD);
226 if (ret < 0) {
227 PERROR("fcntl on f_getfd");
228 ret = -1;
229 goto error;
230 }
231
232 if (ret & FD_CLOEXEC) {
233 dup_cmd = F_DUPFD_CLOEXEC;
234 }
235
236 /* Perform dup */
5a4d96d1 237 for (i = 0; i < STDERR_FILENO + 1; i++) {
f5c453e9
JR
238 ret = fcntl(fd, dup_cmd, 0);
239 if (ret < 0) {
240 PERROR("fcntl dup fd");
241 goto error;
242 }
243
244 if (!(IS_FD_STD(ret))) {
245 /* fd is outside of STD range, use it. */
246 fd_valid = ret;
247 /* Close fd received as argument. */
248 fd_to_close[i] = fd;
249 fd_to_close_count++;
250 break;
251 }
252
253 fd_to_close[i] = ret;
254 fd_to_close_count++;
255 }
256
257 /* Close intermediary fds */
5a4d96d1 258 for (i = 0; i < fd_to_close_count; i++) {
f5c453e9
JR
259 ret = close(fd_to_close[i]);
260 if (ret) {
261 PERROR("close on temporary fd: %d.", fd_to_close[i]);
262 /*
263 * Not using an abort here would yield a complicated
264 * error handling for the caller. If a failure occurs
265 * here, the system is already in a bad state.
266 */
267 abort();
268 }
269 }
270
271 ret = fd_valid;
272error:
273 return ret;
274}
275
6548fca4
MD
276/*
277 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
278 * Has strict checking of fd validity.
f5c453e9
JR
279 *
280 * If fd <= 2, dup the fd until fd > 2. This enables us to bypass
281 * problems that can be encountered if UST uses stdin, stdout, stderr
282 * fds for internal use (daemon etc.). This can happen if the
283 * application closes either of those file descriptors. Intermediary fds
284 * are closed as needed.
285 *
286 * Return -1 on error.
287 *
6548fca4 288 */
f5c453e9 289int lttng_ust_add_fd_to_tracker(int fd)
6548fca4 290{
f5c453e9 291 int ret;
7d34f27d
MD
292 /*
293 * Ensure the tracker is initialized when called from
294 * constructors.
295 */
296 lttng_ust_init_fd_tracker();
96a6162e 297 assert(URCU_TLS(ust_fd_mutex_nest));
f5c453e9
JR
298
299 if (IS_FD_STD(fd)) {
300 ret = dup_std_fd(fd);
301 if (ret < 0) {
302 goto error;
303 }
304 fd = ret;
305 }
306
6548fca4
MD
307 /* Trying to add an fd which we can not accommodate. */
308 assert(IS_FD_VALID(fd));
309 /* Setting an fd thats already set. */
310 assert(!IS_FD_SET(fd, lttng_fd_set));
311
312 ADD_FD_TO_SET(fd, lttng_fd_set);
f5c453e9
JR
313 return fd;
314error:
315 return ret;
6548fca4
MD
316}
317
318/*
319 * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd.
320 * Has strict checking for fd validity.
321 */
322void lttng_ust_delete_fd_from_tracker(int fd)
323{
7d34f27d
MD
324 /*
325 * Ensure the tracker is initialized when called from
326 * constructors.
327 */
328 lttng_ust_init_fd_tracker();
329
96a6162e 330 assert(URCU_TLS(ust_fd_mutex_nest));
6548fca4
MD
331 /* Not a valid fd. */
332 assert(IS_FD_VALID(fd));
333 /* Deleting an fd which was not set. */
334 assert(IS_FD_SET(fd, lttng_fd_set));
335
336 DEL_FD_FROM_SET(fd, lttng_fd_set);
337}
338
339/*
340 * Interface allowing applications to close arbitrary file descriptors.
341 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
342 * instead of closing it if it is the case.
343 */
344int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int fd))
345{
346 int ret = 0;
347
348 lttng_ust_fixup_fd_tracker_tls();
349
7d34f27d
MD
350 /*
351 * Ensure the tracker is initialized when called from
352 * constructors.
353 */
354 lttng_ust_init_fd_tracker();
355
6548fca4
MD
356 /*
357 * If called from lttng-ust, we directly call close without
358 * validating whether the FD is part of the tracked set.
359 */
48a143c0 360 if (URCU_TLS(thread_fd_tracking))
6548fca4
MD
361 return close_cb(fd);
362
363 lttng_ust_lock_fd_tracker();
364 if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) {
365 ret = -1;
366 errno = EBADF;
367 } else {
368 ret = close_cb(fd);
369 }
370 lttng_ust_unlock_fd_tracker();
371
372 return ret;
373}
374
52a20dc7
MD
375/*
376 * Interface allowing applications to close arbitrary streams.
377 * We check if it is owned by lttng-ust, and return -1, errno=EBADF
378 * instead of closing it if it is the case.
379 */
380int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream))
381{
382 int ret = 0, fd;
383
384 lttng_ust_fixup_fd_tracker_tls();
385
7d34f27d
MD
386 /*
387 * Ensure the tracker is initialized when called from
388 * constructors.
389 */
390 lttng_ust_init_fd_tracker();
391
52a20dc7
MD
392 /*
393 * If called from lttng-ust, we directly call fclose without
394 * validating whether the FD is part of the tracked set.
395 */
48a143c0 396 if (URCU_TLS(thread_fd_tracking))
52a20dc7
MD
397 return fclose_cb(stream);
398
399 fd = fileno(stream);
400
401 lttng_ust_lock_fd_tracker();
402 if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) {
403 ret = -1;
404 errno = EBADF;
405 } else {
406 ret = fclose_cb(stream);
407 }
408 lttng_ust_unlock_fd_tracker();
409
410 return ret;
411}
412
6548fca4
MD
413#ifdef __OpenBSD__
414static void set_close_success(int *p)
415{
416 *p = 1;
417}
418static int test_close_success(const int *p)
419{
420 return *p;
421}
422#else
423static void set_close_success(int *p __attribute__((unused)))
424{
425}
426static int test_close_success(const int *p __attribute__((unused)))
427{
428 return 1;
429}
430#endif
431
432/*
433 * Implement helper for closefrom() override.
434 */
435int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int fd))
436{
437 int ret = 0, close_success = 0, i;
438
439 lttng_ust_fixup_fd_tracker_tls();
440
7d34f27d
MD
441 /*
442 * Ensure the tracker is initialized when called from
443 * constructors.
444 */
445 lttng_ust_init_fd_tracker();
446
6548fca4
MD
447 if (lowfd < 0) {
448 /*
449 * NetBSD return EBADF if fd is invalid.
450 */
451 errno = EBADF;
452 ret = -1;
453 goto end;
454 }
455 /*
456 * If called from lttng-ust, we directly call close without
457 * validating whether the FD is part of the tracked set.
458 */
48a143c0 459 if (URCU_TLS(thread_fd_tracking)) {
6548fca4
MD
460 for (i = lowfd; i < lttng_ust_max_fd; i++) {
461 if (close_cb(i) < 0) {
462 switch (errno) {
463 case EBADF:
464 continue;
465 case EINTR:
466 default:
467 ret = -1;
468 goto end;
469 }
470 }
471 set_close_success(&close_success);
472 }
473 } else {
474 lttng_ust_lock_fd_tracker();
475 for (i = lowfd; i < lttng_ust_max_fd; i++) {
476 if (IS_FD_VALID(i) && IS_FD_SET(i, lttng_fd_set))
477 continue;
478 if (close_cb(i) < 0) {
479 switch (errno) {
480 case EBADF:
481 continue;
482 case EINTR:
483 default:
484 ret = -1;
485 lttng_ust_unlock_fd_tracker();
486 goto end;
487 }
488 }
489 set_close_success(&close_success);
490 }
491 lttng_ust_unlock_fd_tracker();
492 }
493 if (!test_close_success(&close_success)) {
494 /*
495 * OpenBSD return EBADF if fd is greater than all open
496 * file descriptors.
497 */
498 ret = -1;
499 errno = EBADF;
500 }
501end:
502 return ret;
503}
48a143c0
MD
504
505void lttng_ust_fd_tracker_register_thread(void)
506{
507 URCU_TLS(thread_fd_tracking) = 1;
508}
This page took 0.042916 seconds and 4 git commands to generate.