From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Frediano Ziglio" <frediano.ziglio@cloud.com>,
"Anthony PERARD" <anthony@xenproject.org>,
"Juergen Gross" <jgross@suse.com>,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Oleksii Kurochko" <oleksii.kurochko@gmail.com>
Subject: [PATCH for-4.19 2/3] tools/libxs: Fix CLOEXEC handling in get_socket()
Date: Fri, 28 Jun 2024 15:31:15 +0100 [thread overview]
Message-ID: <20240628143116.1044976-3-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <20240628143116.1044976-1-andrew.cooper3@citrix.com>
get_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.
Reported-by: Frediano Ziglio <frediano.ziglio@cloud.com>
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Anthony PERARD <anthony@xenproject.org>
CC: Juergen Gross <jgross@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
CC: Frediano Ziglio <frediano.ziglio@cloud.com>
CC: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
tools/libs/store/xs.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c
index 037e79d98b58..11a766c50887 100644
--- a/tools/libs/store/xs.c
+++ b/tools/libs/store/xs.c
@@ -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;
--
2.39.2
next prev parent reply other threads:[~2024-06-28 14:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-28 14:31 [PATCH for-4.19(?) 0/3] tools/libxs: More CLOEXEC fixes Andrew Cooper
2024-06-28 14:31 ` [PATCH for-4.19 1/3] tools/libxs: Fix CLOEXEC handling in get_dev() Andrew Cooper
2024-06-28 14:31 ` Andrew Cooper [this message]
2024-06-28 14:31 ` [PATCH for-4.19 3/3] tools/libxs: Fix CLOEXEC handling in xs_fileno() Andrew Cooper
2024-07-01 9:03 ` [PATCH for-4.19(?) 0/3] tools/libxs: More CLOEXEC fixes Jürgen Groß
2024-07-01 9:28 ` Anthony PERARD
2024-07-01 15:06 ` Oleksii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240628143116.1044976-3-andrew.cooper3@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=anthony@xenproject.org \
--cc=frediano.ziglio@cloud.com \
--cc=jgross@suse.com \
--cc=oleksii.kurochko@gmail.com \
--cc=roger.pau@citrix.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.