Add close_range wrapper to liblttng-ust-fd.so master
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 18 Apr 2024 15:25:55 +0000 (11:25 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 19 Apr 2024 15:19:18 +0000 (11:19 -0400)
glibc 2.34 implements close_range(2), which is used by the ssh client
(amongst others). This needs to be overridden to make sure ssh does not
close lttng-ust file descriptors.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: Ic4e0046499e1f010395aec71a48316b9d1e9bf3f

src/common/ust-fd.h
src/lib/lttng-ust-common/fd-tracker.c
src/lib/lttng-ust-fd/lttng-ust-fd.c

index 4e6c4a5d2bd6e729bf1209d49f29a05a79ba99f5..85038231588d4a3207d265635f9dafa8256649b7 100644 (file)
@@ -25,5 +25,7 @@ void lttng_ust_unlock_fd_tracker(void);
 int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int));
 int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream));
 int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int));
+int lttng_ust_safe_close_range_fd(unsigned int first, unsigned int last, int flags,
+               int (*close_range_cb)(unsigned int, unsigned int, int));
 
 #endif /* _LTTNG_UST_FD_H */
index f5e6ed21448db3b0bf4003505664b07b6b333085..a39869ef90f39c460ddcfd5532ade69a3a19f994 100644 (file)
@@ -466,3 +466,62 @@ int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int fd))
 end:
        return ret;
 }
+
+/*
+ * Implement helper for close_range() override.
+ */
+int lttng_ust_safe_close_range_fd(unsigned int first, unsigned int last, int flags,
+               int (*close_range_cb)(unsigned int first, unsigned int last, int flags))
+{
+       int ret = 0, i;
+
+       lttng_ust_fd_tracker_alloc_tls();
+
+       /*
+        * Ensure the tracker is initialized when called from
+        * constructors.
+        */
+       lttng_ust_fd_tracker_init();
+
+       if (first > last || last > INT_MAX) {
+               ret = -1;
+               errno = EINVAL;
+               goto end;
+       }
+       /*
+        * If called from lttng-ust, we directly call close_range
+        * without validating whether the FD is part of the tracked set.
+        */
+       if (URCU_TLS(ust_fd_mutex_nest)) {
+               if (close_range_cb(first, last, flags) < 0) {
+                       ret = -1;
+                       goto end;
+               }
+       } else {
+               int last_check = last;
+
+               if (last > lttng_ust_max_fd)
+                       last_check = lttng_ust_max_fd;
+               lttng_ust_lock_fd_tracker();
+               for (i = first; i <= last_check; i++) {
+                       if (IS_FD_VALID(i) && IS_FD_SET(i, lttng_fd_set))
+                               continue;
+                       if (close_range_cb(i, i, flags) < 0) {
+                               ret = -1;
+                               /* propagate errno from close_range_cb. */
+                               lttng_ust_unlock_fd_tracker();
+                               goto end;
+                       }
+               }
+               if (last > lttng_ust_max_fd) {
+                       if (close_range_cb(lttng_ust_max_fd + 1, last, flags) < 0) {
+                               ret = -1;
+                               lttng_ust_unlock_fd_tracker();
+                               goto end;
+                       }
+               }
+               lttng_ust_unlock_fd_tracker();
+       }
+end:
+       return ret;
+}
index d2ebcfbc38be862b6effffdc490f7a7d0ce968cb..0360b6f269a85eca5db049bacf258492b7c97249 100644 (file)
@@ -21,6 +21,8 @@
 
 static int (*__lttng_ust_fd_plibc_close)(int fd) = NULL;
 static int (*__lttng_ust_fd_plibc_fclose)(FILE *stream) = NULL;
+static int (*__lttng_ust_fd_plibc_close_range)(unsigned int first,
+               unsigned int last, int flags) = NULL;
 
 /*
  * Use dlsym to find the original libc close() symbol and store it in
@@ -60,6 +62,24 @@ void *_lttng_ust_fd_init_plibc_fclose(void)
        return __lttng_ust_fd_plibc_fclose;
 }
 
+/*
+ * Use dlsym to find the original libc close_range() symbol and store it
+ * in __lttng_ust_fd_plibc_close_range. The close_range symbol only
+ * appears in glibc 2.34, so it is considered optional.
+ */
+static
+void *_lttng_ust_fd_init_plibc_close_range(void)
+{
+       if (__lttng_ust_fd_plibc_close_range == NULL) {
+               __lttng_ust_fd_plibc_close_range = dlsym(RTLD_NEXT, "close_range");
+
+               if (__lttng_ust_fd_plibc_close_range == NULL)
+                       __lttng_ust_fd_plibc_close_range = (void *) LTTNG_UST_DLSYM_FAILED_PTR;
+       }
+
+       return __lttng_ust_fd_plibc_close_range;
+}
+
 static
 void _lttng_ust_fd_ctor(void)
        __attribute__((constructor));
@@ -75,6 +95,7 @@ void _lttng_ust_fd_ctor(void)
         */
        (void) _lttng_ust_fd_init_plibc_close();
        (void) _lttng_ust_fd_init_plibc_fclose();
+       (void) _lttng_ust_fd_init_plibc_close_range();
 }
 
 /*
@@ -127,6 +148,30 @@ int fclose(FILE *stream)
                        __lttng_ust_fd_plibc_fclose);
 }
 
+/*
+ * Override the libc close_range() symbol with our own, allowing
+ * applications to close arbitrary file descriptors. If the fd is owned
+ * by lttng-ust, return -1, errno=EBADF instead of closing it.
+ *
+ * If dlsym failed to find the original libc close_range() symbol,
+ * return -1, errno=ENOSYS.
+ *
+ * There is a short window before the library constructor has executed where
+ * this wrapper could call dlsym() and thus not be async-signal-safe.
+ */
+int close_range(unsigned int first, unsigned int last, int flags)
+{
+       /*
+        * We can't retry dlsym here since close is async-signal-safe.
+        */
+       if (_lttng_ust_fd_init_plibc_close_range() == (void *) LTTNG_UST_DLSYM_FAILED_PTR) {
+               errno = ENOSYS;
+               return -1;
+       }
+
+       return lttng_ust_safe_close_range_fd(first, last, flags, __lttng_ust_fd_plibc_close_range);
+}
+
 #if defined(__sun__) || defined(__FreeBSD__)
 /* Solaris and FreeBSD. */
 void closefrom(int lowfd)
This page took 0.027223 seconds and 4 git commands to generate.