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