| 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> |
| 35 | #include <urcu/compiler.h> |
| 36 | #include <urcu/tls-compat.h> |
| 37 | |
| 38 | #include <ust-fd.h> |
| 39 | #include <helper.h> |
| 40 | #include <lttng/ust-error.h> |
| 41 | #include <usterr-signal-safe.h> |
| 42 | |
| 43 | #include "../liblttng-ust/compat.h" |
| 44 | |
| 45 | /* Operations on the fd set. */ |
| 46 | #define IS_FD_VALID(fd) ((fd) >= 0 && (fd) < lttng_ust_max_fd) |
| 47 | #define GET_FD_SET_FOR_FD(fd, fd_sets) (&((fd_sets)[(fd) / FD_SETSIZE])) |
| 48 | #define CALC_INDEX_TO_SET(fd) ((fd) % FD_SETSIZE) |
| 49 | |
| 50 | /* Check fd validity before calling these. */ |
| 51 | #define ADD_FD_TO_SET(fd, fd_sets) \ |
| 52 | FD_SET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets)) |
| 53 | #define IS_FD_SET(fd, fd_sets) \ |
| 54 | FD_ISSET(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets)) |
| 55 | #define DEL_FD_FROM_SET(fd, fd_sets) \ |
| 56 | FD_CLR(CALC_INDEX_TO_SET(fd), GET_FD_SET_FOR_FD(fd, fd_sets)) |
| 57 | |
| 58 | /* |
| 59 | * Protect the lttng_fd_set. Nests within the ust_lock, and therefore |
| 60 | * within the libc dl lock. Therefore, we need to fixup the TLS before |
| 61 | * nesting into this lock. |
| 62 | */ |
| 63 | static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 64 | /* |
| 65 | * Track whether we are within lttng-ust or application, for close |
| 66 | * system call override by LD_PRELOAD library. |
| 67 | */ |
| 68 | static DEFINE_URCU_TLS_IE(int, thread_fd_tracking); |
| 69 | |
| 70 | /* fd_set used to book keep fd being used by lttng-ust. */ |
| 71 | static fd_set *lttng_fd_set; |
| 72 | static int lttng_ust_max_fd; |
| 73 | static int num_fd_sets; |
| 74 | |
| 75 | /* |
| 76 | * Force a read (imply TLS fixup for dlopen) of TLS variables. |
| 77 | */ |
| 78 | void lttng_ust_fixup_fd_tracker_tls(void) |
| 79 | { |
| 80 | asm volatile ("" : : "m" (URCU_TLS(thread_fd_tracking))); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Allocate the fd set array based on the hard limit set for this |
| 85 | * process. This will be called during the constructor execution |
| 86 | * and will also be called in the child after fork via lttng_ust_init. |
| 87 | */ |
| 88 | void lttng_ust_init_fd_tracker(void) |
| 89 | { |
| 90 | struct rlimit rlim; |
| 91 | int i; |
| 92 | |
| 93 | memset(&rlim, 0, sizeof(rlim)); |
| 94 | /* Get the current possible max number of fd for this process. */ |
| 95 | if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) |
| 96 | abort(); |
| 97 | /* |
| 98 | * FD set array size determined using the hard limit. Even if |
| 99 | * the process wishes to increase its limit using setrlimit, it |
| 100 | * can only do so with the softlimit which will be less than the |
| 101 | * hard limit. |
| 102 | */ |
| 103 | lttng_ust_max_fd = rlim.rlim_max; |
| 104 | num_fd_sets = lttng_ust_max_fd / FD_SETSIZE; |
| 105 | if (lttng_ust_max_fd % FD_SETSIZE) |
| 106 | ++num_fd_sets; |
| 107 | if (lttng_fd_set != NULL) { |
| 108 | free(lttng_fd_set); |
| 109 | lttng_fd_set = NULL; |
| 110 | } |
| 111 | lttng_fd_set = malloc(num_fd_sets * (sizeof(fd_set))); |
| 112 | if (!lttng_fd_set) |
| 113 | abort(); |
| 114 | for (i = 0; i < num_fd_sets; i++) |
| 115 | FD_ZERO((<tng_fd_set[i])); |
| 116 | } |
| 117 | |
| 118 | void lttng_ust_lock_fd_tracker(void) |
| 119 | { |
| 120 | URCU_TLS(thread_fd_tracking) = 1; |
| 121 | /* |
| 122 | * Ensure the compiler don't move the store after the close() |
| 123 | * call in case close() would be marked as leaf. |
| 124 | */ |
| 125 | cmm_barrier(); |
| 126 | pthread_mutex_lock(&ust_safe_guard_fd_mutex); |
| 127 | } |
| 128 | |
| 129 | void lttng_ust_unlock_fd_tracker(void) |
| 130 | { |
| 131 | pthread_mutex_unlock(&ust_safe_guard_fd_mutex); |
| 132 | /* |
| 133 | * Ensure the compiler don't move the store before the close() |
| 134 | * call, in case close() would be marked as leaf. |
| 135 | */ |
| 136 | cmm_barrier(); |
| 137 | URCU_TLS(thread_fd_tracking) = 0; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd. |
| 142 | * Has strict checking of fd validity. |
| 143 | */ |
| 144 | void lttng_ust_add_fd_to_tracker(int fd) |
| 145 | { |
| 146 | assert(URCU_TLS(thread_fd_tracking)); |
| 147 | /* Trying to add an fd which we can not accommodate. */ |
| 148 | assert(IS_FD_VALID(fd)); |
| 149 | /* Setting an fd thats already set. */ |
| 150 | assert(!IS_FD_SET(fd, lttng_fd_set)); |
| 151 | |
| 152 | ADD_FD_TO_SET(fd, lttng_fd_set); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Needs to be called with ust_safe_guard_fd_mutex held when opening the fd. |
| 157 | * Has strict checking for fd validity. |
| 158 | */ |
| 159 | void lttng_ust_delete_fd_from_tracker(int fd) |
| 160 | { |
| 161 | assert(URCU_TLS(thread_fd_tracking)); |
| 162 | /* Not a valid fd. */ |
| 163 | assert(IS_FD_VALID(fd)); |
| 164 | /* Deleting an fd which was not set. */ |
| 165 | assert(IS_FD_SET(fd, lttng_fd_set)); |
| 166 | |
| 167 | DEL_FD_FROM_SET(fd, lttng_fd_set); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Interface allowing applications to close arbitrary file descriptors. |
| 172 | * We check if it is owned by lttng-ust, and return -1, errno=EBADF |
| 173 | * instead of closing it if it is the case. |
| 174 | */ |
| 175 | int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int fd)) |
| 176 | { |
| 177 | int ret = 0; |
| 178 | |
| 179 | lttng_ust_fixup_fd_tracker_tls(); |
| 180 | |
| 181 | /* |
| 182 | * If called from lttng-ust, we directly call close without |
| 183 | * validating whether the FD is part of the tracked set. |
| 184 | */ |
| 185 | if (URCU_TLS(thread_fd_tracking)) |
| 186 | return close_cb(fd); |
| 187 | |
| 188 | lttng_ust_lock_fd_tracker(); |
| 189 | if (IS_FD_VALID(fd) && IS_FD_SET(fd, lttng_fd_set)) { |
| 190 | ret = -1; |
| 191 | errno = EBADF; |
| 192 | } else { |
| 193 | ret = close_cb(fd); |
| 194 | } |
| 195 | lttng_ust_unlock_fd_tracker(); |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | #ifdef __OpenBSD__ |
| 201 | static void set_close_success(int *p) |
| 202 | { |
| 203 | *p = 1; |
| 204 | } |
| 205 | static int test_close_success(const int *p) |
| 206 | { |
| 207 | return *p; |
| 208 | } |
| 209 | #else |
| 210 | static void set_close_success(int *p __attribute__((unused))) |
| 211 | { |
| 212 | } |
| 213 | static int test_close_success(const int *p __attribute__((unused))) |
| 214 | { |
| 215 | return 1; |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | /* |
| 220 | * Implement helper for closefrom() override. |
| 221 | */ |
| 222 | int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int fd)) |
| 223 | { |
| 224 | int ret = 0, close_success = 0, i; |
| 225 | |
| 226 | lttng_ust_fixup_fd_tracker_tls(); |
| 227 | |
| 228 | if (lowfd < 0) { |
| 229 | /* |
| 230 | * NetBSD return EBADF if fd is invalid. |
| 231 | */ |
| 232 | errno = EBADF; |
| 233 | ret = -1; |
| 234 | goto end; |
| 235 | } |
| 236 | /* |
| 237 | * If called from lttng-ust, we directly call close without |
| 238 | * validating whether the FD is part of the tracked set. |
| 239 | */ |
| 240 | if (URCU_TLS(thread_fd_tracking)) { |
| 241 | for (i = lowfd; i < lttng_ust_max_fd; i++) { |
| 242 | if (close_cb(i) < 0) { |
| 243 | switch (errno) { |
| 244 | case EBADF: |
| 245 | continue; |
| 246 | case EINTR: |
| 247 | default: |
| 248 | ret = -1; |
| 249 | goto end; |
| 250 | } |
| 251 | } |
| 252 | set_close_success(&close_success); |
| 253 | } |
| 254 | } else { |
| 255 | lttng_ust_lock_fd_tracker(); |
| 256 | for (i = lowfd; i < lttng_ust_max_fd; i++) { |
| 257 | if (IS_FD_VALID(i) && IS_FD_SET(i, lttng_fd_set)) |
| 258 | continue; |
| 259 | if (close_cb(i) < 0) { |
| 260 | switch (errno) { |
| 261 | case EBADF: |
| 262 | continue; |
| 263 | case EINTR: |
| 264 | default: |
| 265 | ret = -1; |
| 266 | lttng_ust_unlock_fd_tracker(); |
| 267 | goto end; |
| 268 | } |
| 269 | } |
| 270 | set_close_success(&close_success); |
| 271 | } |
| 272 | lttng_ust_unlock_fd_tracker(); |
| 273 | } |
| 274 | if (!test_close_success(&close_success)) { |
| 275 | /* |
| 276 | * OpenBSD return EBADF if fd is greater than all open |
| 277 | * file descriptors. |
| 278 | */ |
| 279 | ret = -1; |
| 280 | errno = EBADF; |
| 281 | } |
| 282 | end: |
| 283 | return ret; |
| 284 | } |