tools/libxs: More CLOEXEC fixes
cover tools/libxs: More CLOEXEC fixes
More fixes to CLOEXEC handling in libxenstore. For 4.19, because the first attempt to fix this wasn't complete.
libxl is far worse, but I don't have time to get started on that mess.
Andrew Cooper (3):
tools/libxs: Fix CLOEXEC handling in get_dev() tools/libxs: Fix CLOEXEC handling in get_socket() tools/libxs: Fix CLOEXEC handling in xs_fileno()
tools/config.h.in | 3 ++ tools/configure | 12 ++++++++ tools/configure.ac | 2 ++ tools/libs/store/xs.c | 68 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 72 insertions(+), 13 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/3 tools/libxs: Fix CLOEXEC handling in get_dev()
Move the O_CLOEXEC compatibility outside of an #ifdef USE_PTHREAD block.
Introduce set_cloexec() to wrap fcntl() setting FD_CLOEXEC. It will be reused for other CLOEXEC fixes too.
Use set_cloexec() when O_CLOEXEC isn't available as a best-effort fallback.
---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Frediano Ziglio <[email protected]>
CC: Oleksii Kurochko <[email protected]>
--- tools/libs/store/xs.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-)
tools/libs/store/xs.c +32 -6
@@ -40,6 +40,10 @@#include <xentoolcore_internal.h> #include <xen_list.h> +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif +struct xs_stored_msg {XEN_TAILQ_ENTRY (struct xs_stored_msg ) list;struct xsd_sockmsg hdr;
@@ -54,10 +58,6 @@ struct xs_stored_msg {#include <dlfcn.h> #endif -#ifndef O_CLOEXEC -#define O_CLOEXEC 0 -#endif -struct xs_handle {/* Communications channel to xenstore daemon. */ int fd;
@@ -176,6 +176,16 @@ static bool setnonblock(int fd, int nonblock) {return true; } +static bool set_cloexec (int fd) +{ +int flags =fcntl (fd,F_GETFL ); + +if (flags <0 ) +return false; + +return fcntl (fd, flags |FD_CLOEXEC ) >=0 ; +} +int xs_fileno (struct xs_handle *h) {char c =0 ;
@@ -230,8 +240,24 @@ static int get_socket(const char *connect_to)static int get_dev (const char *connect_to) { -/* We cannot open read-only because requests are writes */ -return open (connect_to,O_RDWR |O_CLOEXEC ); +int fd, saved_errno; + + fd =open (connect_to,O_RDWR |O_CLOEXEC ); +if (fd <0 ) +return -1 ; + +/* Compat for non-O_CLOEXEC environments. Racy. */ +if (!O_CLOEXEC && !set_cloexec (fd)) + goto error; + +return fd; + +error: + saved_errno = errno; +close (fd); + errno = saved_errno; + +return -1 ; }static int all_restrict_cb (Xentoolcore__Active_Handle *ah,domid_t domid) {
Signature · 1 line
2.39.2
PATCH 2/3 tools/libxs: Fix CLOEXEC handling in get_socket()
In reply to Andrew Cooperget_socket() opens a socket, then uses fcntl() to set CLOEXEC. This is racy with exec().
Open the socket with SOCK_CLOEXEC. Use the same compatibility strategy as O_CLOEXEC on ancient versions of Linux.
---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Frediano Ziglio <[email protected]>
CC: Oleksii Kurochko <[email protected]>
--- tools/libs/store/xs.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
tools/libs/store/xs.c +8 -6
@@ -44,6 +44,10 @@#define O_CLOEXEC 0#endif +#ifndef SOCK_CLOEXEC +#define SOCK_CLOEXEC 0 +#endif +struct xs_stored_msg {XEN_TAILQ_ENTRY (struct xs_stored_msg ) list;struct xsd_sockmsg hdr;
@@ -207,16 +211,14 @@ int xs_fileno(struct xs_handle *h)static int get_socket (const char *connect_to) {struct sockaddr_un addr; -int sock, saved_errno, flags; +int sock, saved_errno; - sock =socket (PF_UNIX ,SOCK_STREAM ,0 ); + sock =socket (PF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 );if (sock <0 )return -1 ; -if ((flags =fcntl (sock,F_GETFD )) <0 ) - goto error; - flags |=FD_CLOEXEC ; -if (fcntl (sock,F_SETFD , flags) <0 ) +/* Compat for non-SOCK_CLOEXEC environments. Racy. */ +if (!SOCK_CLOEXEC && !set_cloexec (sock)) goto error; addr.sun_family =AF_UNIX ;
Signature · 1 line
2.39.2
PATCH 3/3 tools/libxs: Fix CLOEXEC handling in xs_fileno()
In reply to Andrew Cooperxs_fileno() opens a pipe on first use to communicate between the watch thread and the main thread. Nothing ever sets CLOEXEC on the file descriptors.
Check for the availability of the pipe2() function with configure. Despite starting life as Linux-only, FreeBSD and NetBSD have gained it.
When pipe2() isn't available, try our best with pipe() and set_cloexec().
---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Frediano Ziglio <[email protected]>
CC: Oleksii Kurochko <[email protected]>
--- tools/config.h.in | 3 +++ tools/configure | 12 ++++++++++++ tools/configure.ac | 2 ++ tools/libs/store/xs.c | 16 +++++++++++++++- 4 files changed, 32 insertions(+), 1 deletion(-)
tools/config.h.in +3 -0
@@ -39,6 +39,9 @@ /* Define to 1 if you have the <memory.h> header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have the `pipe2' function. */ +#undef HAVE_PIPE2 + /* pygrub enabled */ #undef HAVE_PYGRUB
tools/configure +12 -0
@@ -9751,6 +9751,18 @@ if test "$ax_found" = "0"; then : fi +for ac_func in pipe2 +do : + ac_fn_c_check_func "$LINENO" "pipe2" "ac_cv_func_pipe2" +if test "x$ac_cv_func_pipe2" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_PIPE2 1 +_ACEOF + +fi +done + + cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure # tests run on this system so they can be shared between configure
tools/configure.ac +2 -0
@@ -543,4 +543,6 @@ AS_IF([test "x$pvshim" = "xy"], [ AX_FIND_HEADER([INCLUDE_ENDIAN_H], [endian.h sys/endian.h]) +AC_CHECK_FUNCS([pipe2]) + AC_OUTPUT()
tools/libs/store/xs.c +15 -1
@@ -190,13 +190,27 @@ static bool set_cloexec(int fd)return fcntl (fd, flags |FD_CLOEXEC ) >=0 ; } +static int pipe_cloexec (int fds[2 ]) +{ +#if HAVE_PIPE2 +return pipe2 (fds,O_CLOEXEC ); +#else +if (pipe (fds) <0 ) +return -1 ; +/* Best effort to set CLOEXEC. Racy. */ +set_cloexec (fds[0 ]); +set_cloexec (fds[1 ]); +return 0 ; +#endif +} +int xs_fileno (struct xs_handle *h) {char c =0 ;mutex_lock (&h->watch_mutex); -if ((h->watch_pipe[0 ] ==-1 ) && (pipe (h->watch_pipe) !=-1 )) { +if ((h->watch_pipe[0 ] ==-1 ) && (pipe_cloexec (h->watch_pipe) !=-1 )) {/* Kick things off if the watch list is already non-empty. */ if (!XEN_TAILQ_EMPTY (&h->watch_list))while (write (h->watch_pipe[1 ], &c,1 ) !=1 )
Signature · 1 line
2.39.2
Quoted 10 lines · Andrew Cooper
More fixes to CLOEXEC handling in libxenstore. For 4.19, because the first attempt to fix this wasn't complete.
libxl is far worse, but I don't have time to get started on that mess.
Andrew Cooper (3):
tools/libxs: Fix CLOEXEC handling inget_dev()tools/libxs: Fix CLOEXEC handling inget_socket()tools/libxs: Fix CLOEXEC handling inxs_fileno()
Quoted 6 lines
tools/config.h.in | 3 ++ tools/configure | 12 ++++++++ tools/configure.ac | 2 ++ tools/libs/store/xs.c | 68 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 72 insertions(+), 13 deletions(-)
For the series:
Juergen
Anthony PERARD
Acked-byQuoted 18 lines · Jürgen Groß
Quoted 10 lines · Andrew Cooper
More fixes to CLOEXEC handling in libxenstore. For 4.19, because the first attempt to fix this wasn't complete.
libxl is far worse, but I don't have time to get started on that mess.
Andrew Cooper (3):
tools/libxs: Fix CLOEXEC handling inget_dev()tools/libxs: Fix CLOEXEC handling inget_socket()tools/libxs: Fix CLOEXEC handling inxs_fileno()Quoted 6 lines
tools/config.h.in | 3 ++ tools/configure | 12 ++++++++ tools/configure.ac | 2 ++ tools/libs/store/xs.c | 68 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 72 insertions(+), 13 deletions(-)
Quoted 3 lines
For the series:
Reviewed-by Juergen Gross <[email protected]>
Thanks,
--
Anthony Perard | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
Quoted 12 lines · Andrew Cooper
More fixes to CLOEXEC handling in libxenstore. For 4.19, because the first
attempt to fix this wasn't complete.libxl is far worse, but I don't have time to get started on that mess.
Andrew Cooper (3):
tools/libxs: Fix CLOEXEC handling inget_dev()tools/libxs: Fix CLOEXEC handling inget_socket()tools/libxs: Fix CLOEXEC handling inxs_fileno()
Quoted 7 lines
tools/config.h.in | 3 ++
tools/configure | 12 ++++++++
tools/configure.ac | 2 ++
tools/libs/store/xs.c | 68 ++++++++++++++++++++++++++++++++++------- --4 files changed, 72 insertions(+), 13 deletions(-)
For the whole series:
~ Oleksii