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