Skip to contentSkip to conversation

tools/libxs: Fix fcntl() invocation in set_cloexec()

PATCH In review Andrew Cooper 3 participants 3 messages Jul 8, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libxs: Fix fcntl() invocation in set_cloexec()

set_cloexec() had a bit too much copy&pate from setnonblock(), and insufficient testing on ancient versions of Linux...

As written (emulating ancient linux by undef'ing O_CLOEXEC), strace shows:

open("/dev/xen/xenbus", O_RDWR) = 3
fcntl(3, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE)
fcntl(3, 0x8003 /* F_??? */, 0x7ffe4a771d90) = -1 EINVAL (Invalid argument)
close(3) = 0

which is obviously nonsense.

Switch F_GETFL -> F_GETFD, and fix the second invocation to use F_SETFD. With this, strace is rather happer:

  open("/dev/xen/xenbus", O_RDWR)         = 3
  fcntl(3, F_GETFD)                       = 0
  fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
Fixes bf7c1464706a ("tools/libxs: Fix CLOEXEC handling in get_dev()")

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Jan Beulich <[email protected]>

I'm embarassed to say that this was only spotted by Ross when I was cherrypicking fixes from staging-4.17 into the XenServer patchqueue.

This is urgent to take for 4.19, and to backport into 4.17/18 seeing as the breakage has been backported already.

---
 tools/libs/store/xs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libs/store/xs.c +2 -2
index c8845b69e284..38a6ce3cf2ea 100644
@@ -182,12 +182,12 @@ static bool setnonblock(int fd, int nonblock) {
 
 static bool set_cloexec(int fd)
 {
-	int flags = fcntl(fd, F_GETFL);
+	int flags = fcntl(fd, F_GETFD);
 
 	if (flags < 0)
 		return false;
 
-	return fcntl(fd, flags | FD_CLOEXEC) >= 0;
+	return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
 }
 
 static int pipe_cloexec(int fds[2])
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Andrew Cooper

set_cloexec() had a bit too much copy&pate from setnonblock(), and insufficient testing on ancient versions of Linux...

As written (emulating ancient linux by undef'ing O_CLOEXEC), strace shows:

open("/dev/xen/xenbus", O_RDWR) = 3
fcntl(3, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE)
fcntl(3, 0x8003 /* F_??? */, 0x7ffe4a771d90) = -1 EINVAL (Invalid argument)
close(3) = 0

which is obviously nonsense.

Switch F_GETFL -> F_GETFD, and fix the second invocation to use F_SETFD. With this, strace is rather happer:

Quoted 7 lines
  open("/dev/xen/xenbus", O_RDWR)         = 3
  fcntl(3, F_GETFD)                       = 0
  fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
Fixes bf7c1464706a ("tools/libxs: Fix CLOEXEC handling in get_dev()")

Thanks

Reviewed-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 15 lines · Andrew Cooper

set_cloexec() had a bit too much copy&pate from setnonblock(), and insufficient testing on ancient versions of Linux...

As written (emulating ancient linux by undef'ing O_CLOEXEC), strace shows:

   open("/dev/xen/xenbus", O_RDWR)         = 3
   fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|O_LARGEFILE)
   fcntl(3, 0x8003 /* F_??? */, 0x7ffe4a771d90) = -1 EINVAL (Invalid argument)
   close(3)                                = 0

which is obviously nonsense.

Switch F_GETFL -> F_GETFD, and fix the second invocation to use F_SETFD. With this, strace is rather happer:

Quoted 7 lines
   open("/dev/xen/xenbus", O_RDWR)         = 3
   fcntl(3, F_GETFD)                       = 0
   fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
Fixes bf7c1464706a ("tools/libxs: Fix CLOEXEC handling in get_dev()")

Juergen