Skip to contentSkip to conversation

fix fcntl(F_SETFD) usage

PATCH In review Eric Blake 4 participants 8 messages Apr 20, 2020 – Jun 2, 2020

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

cover fix fcntl(F_SETFD) usage

As recently pointed out:
https://lists.gnu.org/archive/html/qemu-devel/2020-04/msg03133.html
code that blindly calls fcntl(fd, F_SETFD, 1) rather than performing a read-modify-write when it intends to add FD_CLOEXEC is broken, in that it can inadvertently clear other bits.

Thankfully, the culprits fixed in this series are unlikely to be clearing either FD_CLOFORK (if Linux ever follows Solaris' lead in adding that), or the new FD_32BIT_MODE being proposed (as the fds in question are unlikely to have that set) - but it is still better to write proper code than to set up a bad example prone to copy-and-paste propagation. And as these usages are not new to 5.0, I don't see any reason against waiting until 5.1 to apply them.

Eric Blake (2):
hax: Fix setting of FD_CLOEXEC
tools: Fix use of fcntl(F_SETFD) during socket activation

 target/i386/hax-posix.c | 6 +++---
 util/systemd.c          | 4 +++-
 2 files changed, 6 insertions(+), 4 deletions(-)
Signature · 1 line
2.26.1
author
#1 lore raw

PATCH 1/2 hax: Fix setting of FD_CLOEXEC

Blindly setting FD_CLOEXEC without a read-modify-write will inadvertently clear any other intentionally-set bits, such as a proposed new bit for designating a fd that must behave in 32-bit mode. Use our wrapper function instead of an incorrect hand-rolled version.

---
 target/i386/hax-posix.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/i386/hax-posix.c +3 -3
index 3bad89f13337..5f9d1b803dec 100644
@@ -23,7 +23,7 @@ hax_fd hax_mod_open(void)
         fprintf(stderr, "Failed to open the hax module\n");
     }
 
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(fd);
 
     return fd;
 }
@@ -147,7 +147,7 @@ hax_fd hax_host_open_vm(struct hax_state *hax, int vm_id)
     fd = open(vm_name, O_RDWR);
     g_free(vm_name);
 
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(fd);
 
     return fd;
 }
@@ -200,7 +200,7 @@ hax_fd hax_host_open_vcpu(int vmid, int vcpuid)
     if (fd < 0) {
         fprintf(stderr, "Failed to open the vcpu devfs\n");
     }
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(fd);
     return fd;
 }
 
Signature · 1 line
2.26.1
#2 lore raw

Re: [PATCH 1/2] hax: Fix setting of FD_CLOEXEC

Quoted 6 lines · Eric Blake

Blindly setting FD_CLOEXEC without a read-modify-write will inadvertently clear any other intentionally-set bits, such as a proposed new bit for designating a fd that must behave in 32-bit mode. Use our wrapper function instead of an incorrect hand-rolled version.

Thanks, queued for 5.1.

Signature · 1 line
Eduardo
author
#3 lore raw

Re: [PATCH 1/2] hax: Fix setting of FD_CLOEXEC

Quoted 9 lines · Eduardo Habkost
Quoted 6 lines · Eric Blake

Blindly setting FD_CLOEXEC without a read-modify-write will inadvertently clear any other intentionally-set bits, such as a proposed new bit for designating a fd that must behave in 32-bit mode. Use our wrapper function instead of an incorrect hand-rolled version.

Thanks, queued for 5.1.

Ping - I still don't see this upstream.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] hax: Fix setting of FD_CLOEXEC

In reply to Eric Blake

Looks good to me.

--
Best Regards,
Colin Xu

Quoted 45 lines · Eric Blake

Blindly setting FD_CLOEXEC without a read-modify-write will inadvertently clear any other intentionally-set bits, such as a proposed new bit for designating a fd that must behave in 32-bit mode. Use our wrapper function instead of an incorrect hand-rolled version.

---
target/i386/hax-posix.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
target/i386/hax-posix.c +3 -3
index 3bad89f13337..5f9d1b803dec 100644
@@ -23,7 +23,7 @@ hax_fd hax_mod_open(void)
        fprintf(stderr, "Failed to open the hax module\n");
    }
 
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(fd);
 
    return fd;
 }
@@ -147,7 +147,7 @@ hax_fd hax_host_open_vm(struct hax_state *hax, int vm_id)
    fd = open(vm_name, O_RDWR);
    g_free(vm_name);
 
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(fd);
 
    return fd;
 }
@@ -200,7 +200,7 @@ hax_fd hax_host_open_vcpu(int vmid, int vcpuid)
    if (fd < 0) {
        fprintf(stderr, "Failed to open the vcpu devfs\n");
    }
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(fd);
    return fd;
 }
 
Signature · 1 line
2.26.1
author
#5 lore raw

PATCH 2/2 tools: Fix use of fcntl(F_SETFD) during socket activation

In reply to Eric Blake

Blindly setting FD_CLOEXEC without a read-modify-write will inadvertently clear any other intentionally-set bits, such as a proposed new bit for designating a fd that must behave in 32-bit mode. However, we cannot use our wrapper qemu_set_cloexec(), because that wrapper intentionally abort()s on failure, whereas the probe here intentionally tolerates failure to deal with incorrect socket activation gracefully. Instead, fix the code to do the proper read-modify-write.

---
 util/systemd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/systemd.c +3 -1
index 1dd0367d9a84..5bcac9b40169 100644
@@ -23,6 +23,7 @@ unsigned int check_socket_activation(void)
     unsigned long nr_fds;
     unsigned int i;
     int fd;
+    int f;
     int err;
 
     s = getenv("LISTEN_PID");
@@ -54,7 +55,8 @@ unsigned int check_socket_activation(void)
     /* So the file descriptors don't leak into child processes. */
     for (i = 0; i < nr_fds; ++i) {
         fd = FIRST_SOCKET_ACTIVATION_FD + i;
-        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
+        f = fcntl(fd, F_GETFD);
+        if (f == -1 || fcntl(fd, F_SETFD, f | FD_CLOEXEC) == -1) {
             /* If we cannot set FD_CLOEXEC then it probably means the file
              * descriptor is invalid, so socket activation has gone wrong
              * and we should exit.
Signature · 1 line
2.26.1
author
#6 lore raw

Re: [PATCH 2/2] tools: Fix use of fcntl(F_SETFD) during socket activation

Quoted 9 lines · Eric Blake

Blindly setting FD_CLOEXEC without a read-modify-write will inadvertently clear any other intentionally-set bits, such as a proposed new bit for designating a fd that must behave in 32-bit mode. However, we cannot use our wrapper qemu_set_cloexec(), because that wrapper intentionally abort()s on failure, whereas the probe here intentionally tolerates failure to deal with incorrect socket activation gracefully. Instead, fix the code to do the proper read-modify-write.

Quoted 4 lines
---
  util/systemd.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

As qemu-nbd is impacted, I'll queue 2/2 through my NBD tree if qemu-trivial doesn't pick it up first.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
Reviewed-by on the v1 series
#7 lore raw
In reply to Eric Blake
Quoted 7 lines · Eric Blake

As recently pointed out:
https://lists.gnu.org/archive/html/qemu-devel/2020-04/msg03133.html
code that blindly calls fcntl(fd, F_SETFD, 1) rather than performing a read-modify-write when it intends to add FD_CLOEXEC is broken, in that it can inadvertently clear other bits.

Quoted 7 lines

Thankfully, the culprits fixed in this series are unlikely to be clearing either FD_CLOFORK (if Linux ever follows Solaris' lead in adding that), or the new FD_32BIT_MODE being proposed (as the fds in question are unlikely to have that set) - but it is still better to write proper code than to set up a bad example prone to copy-and-paste propagation. And as these usages are not new to 5.0, I don't see any reason against waiting until 5.1 to apply them.

thanks
-- PMM