qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC
@ 2025-07-11 14:12 Peter Maydell
  2025-07-11 15:06 ` Daniel P. Berrangé
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Peter Maydell @ 2025-07-11 14:12 UTC (permalink / raw)
  To: qemu-devel; +Cc: Laurent Vivier

In the linux-user do_fork() function we try to set the FD_CLOEXEC
flag on a pidfd like this:

    fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL) | FD_CLOEXEC);

This has two problems:
 (1) it doesn't check errors, which Coverity complains about
 (2) we use F_GETFL when we mean F_GETFD

Deal with both of these problems by using qemu_set_cloexec() instead.
That function will assert() if the fcntls fail, which is fine (we are
inside fork_start()/fork_end() so we know nothing can mess around
with our file descriptors here, and we just got this one from
pidfd_open()).

(As we are touching the if() statement here, we correct the
indentation.)

Coverity: CID 1508111
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 linux-user/syscall.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index c600d5ccc0e..b7ec9a4f363 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -6743,10 +6743,9 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
                 int pid_child = ret;
                 pid_fd = pidfd_open(pid_child, 0);
                 if (pid_fd >= 0) {
-                        fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
-                                               | FD_CLOEXEC);
+                    qemu_set_cloexec(pid_fd);
                 } else {
-                        pid_fd = 0;
+                    pid_fd = 0;
                 }
 #endif
                 put_user_u32(pid_fd, parent_tidptr);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC
  2025-07-11 14:12 [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC Peter Maydell
@ 2025-07-11 15:06 ` Daniel P. Berrangé
  2025-07-11 16:29 ` Richard Henderson
  2025-07-11 16:47 ` Richard Henderson
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel P. Berrangé @ 2025-07-11 15:06 UTC (permalink / raw)
  To: Peter Maydell; +Cc: qemu-devel, Laurent Vivier

On Fri, Jul 11, 2025 at 03:12:17PM +0100, Peter Maydell wrote:
> In the linux-user do_fork() function we try to set the FD_CLOEXEC
> flag on a pidfd like this:
> 
>     fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL) | FD_CLOEXEC);
> 
> This has two problems:
>  (1) it doesn't check errors, which Coverity complains about
>  (2) we use F_GETFL when we mean F_GETFD
> 
> Deal with both of these problems by using qemu_set_cloexec() instead.
> That function will assert() if the fcntls fail, which is fine (we are
> inside fork_start()/fork_end() so we know nothing can mess around
> with our file descriptors here, and we just got this one from
> pidfd_open()).
> 
> (As we are touching the if() statement here, we correct the
> indentation.)
> 
> Coverity: CID 1508111
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  linux-user/syscall.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC
  2025-07-11 14:12 [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC Peter Maydell
  2025-07-11 15:06 ` Daniel P. Berrangé
@ 2025-07-11 16:29 ` Richard Henderson
  2025-07-11 16:47 ` Richard Henderson
  2 siblings, 0 replies; 4+ messages in thread
From: Richard Henderson @ 2025-07-11 16:29 UTC (permalink / raw)
  To: qemu-devel

On 7/11/25 08:12, Peter Maydell wrote:
> In the linux-user do_fork() function we try to set the FD_CLOEXEC
> flag on a pidfd like this:
> 
>      fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL) | FD_CLOEXEC);
> 
> This has two problems:
>   (1) it doesn't check errors, which Coverity complains about
>   (2) we use F_GETFL when we mean F_GETFD
> 
> Deal with both of these problems by using qemu_set_cloexec() instead.
> That function will assert() if the fcntls fail, which is fine (we are
> inside fork_start()/fork_end() so we know nothing can mess around
> with our file descriptors here, and we just got this one from
> pidfd_open()).
> 
> (As we are touching the if() statement here, we correct the
> indentation.)
> 
> Coverity: CID 1508111
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>   linux-user/syscall.c | 5 ++---
>   1 file changed, 2 insertions(+), 3 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC
  2025-07-11 14:12 [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC Peter Maydell
  2025-07-11 15:06 ` Daniel P. Berrangé
  2025-07-11 16:29 ` Richard Henderson
@ 2025-07-11 16:47 ` Richard Henderson
  2 siblings, 0 replies; 4+ messages in thread
From: Richard Henderson @ 2025-07-11 16:47 UTC (permalink / raw)
  To: qemu-devel

On 7/11/25 08:12, Peter Maydell wrote:
> In the linux-user do_fork() function we try to set the FD_CLOEXEC
> flag on a pidfd like this:
> 
>      fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL) | FD_CLOEXEC);
> 
> This has two problems:
>   (1) it doesn't check errors, which Coverity complains about
>   (2) we use F_GETFL when we mean F_GETFD
> 
> Deal with both of these problems by using qemu_set_cloexec() instead.
> That function will assert() if the fcntls fail, which is fine (we are
> inside fork_start()/fork_end() so we know nothing can mess around
> with our file descriptors here, and we just got this one from
> pidfd_open()).
> 
> (As we are touching the if() statement here, we correct the
> indentation.)
> 
> Coverity: CID 1508111
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>   linux-user/syscall.c | 5 ++---
>   1 file changed, 2 insertions(+), 3 deletions(-)

Queued, thanks.

r~


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-07-11 16:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-11 14:12 [PATCH] linux-user: Use qemu_set_cloexec() to mark pidfd as FD_CLOEXEC Peter Maydell
2025-07-11 15:06 ` Daniel P. Berrangé
2025-07-11 16:29 ` Richard Henderson
2025-07-11 16:47 ` Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).