qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] util: fix fd leak in qemu_write_pidfile()
@ 2021-05-10 14:11 Jie Wang
  2021-05-10 15:07 ` Peter Maydell
  0 siblings, 1 reply; 3+ messages in thread
From: Jie Wang @ 2021-05-10 14:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: wangxinxin.wang, wangjie88

if execute qemu_open success, have no branch to free the fd,
so unlink it inadvance, let it free by process exit.

Signed-off-by: Jie Wang <wangjie88@huawei.com>
---
 util/oslib-posix.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 36820fec16..fa881f2ee8 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -131,6 +131,7 @@ bool qemu_write_pidfile(const char *path, Error **errp)
             error_setg_errno(errp, errno, "Cannot open pid file");
             return false;
         }
+        unlink(path);
 
         if (fstat(fd, &b) < 0) {
             error_setg_errno(errp, errno, "Cannot stat file");
-- 
2.23.0



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

* Re: [PATCH v2] util: fix fd leak in qemu_write_pidfile()
  2021-05-10 14:11 [PATCH v2] util: fix fd leak in qemu_write_pidfile() Jie Wang
@ 2021-05-10 15:07 ` Peter Maydell
  2021-05-10 15:22   ` Daniel P. Berrangé
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Maydell @ 2021-05-10 15:07 UTC (permalink / raw)
  To: Jie Wang; +Cc: wangxinxin.wang, QEMU Developers

On Mon, 10 May 2021 at 15:15, Jie Wang <wangjie88@huawei.com> wrote:
>
> if execute qemu_open success, have no branch to free the fd,
> so unlink it inadvance, let it free by process exit.
>
> Signed-off-by: Jie Wang <wangjie88@huawei.com>
> ---
>  util/oslib-posix.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 36820fec16..fa881f2ee8 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -131,6 +131,7 @@ bool qemu_write_pidfile(const char *path, Error **errp)
>              error_setg_errno(errp, errno, "Cannot open pid file");
>              return false;
>          }
> +        unlink(path);
>
>          if (fstat(fd, &b) < 0) {
>              error_setg_errno(errp, errno, "Cannot stat file");

This code change doesn't match the commit message -- the commit
message says it's trying to free a filedescriptor, but the code
change is unlinking a file.

Unlinking the file is definitely wrong, because the purpose of the
pidfile is to comminucate the QEMU PID to other processes -- if we
delete the file then those other processes can't find it. (The file
gets deleted when QEMU exits -- see qemu_unlink_pidfile() and the code
that calls it.)

thanks
-- PMM


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

* Re: [PATCH v2] util: fix fd leak in qemu_write_pidfile()
  2021-05-10 15:07 ` Peter Maydell
@ 2021-05-10 15:22   ` Daniel P. Berrangé
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel P. Berrangé @ 2021-05-10 15:22 UTC (permalink / raw)
  To: Peter Maydell; +Cc: wangxinxin.wang, QEMU Developers, Jie Wang

On Mon, May 10, 2021 at 04:07:51PM +0100, Peter Maydell wrote:
> On Mon, 10 May 2021 at 15:15, Jie Wang <wangjie88@huawei.com> wrote:
> >
> > if execute qemu_open success, have no branch to free the fd,
> > so unlink it inadvance, let it free by process exit.
> >
> > Signed-off-by: Jie Wang <wangjie88@huawei.com>
> > ---
> >  util/oslib-posix.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> > index 36820fec16..fa881f2ee8 100644
> > --- a/util/oslib-posix.c
> > +++ b/util/oslib-posix.c
> > @@ -131,6 +131,7 @@ bool qemu_write_pidfile(const char *path, Error **errp)
> >              error_setg_errno(errp, errno, "Cannot open pid file");
> >              return false;
> >          }
> > +        unlink(path);
> >
> >          if (fstat(fd, &b) < 0) {
> >              error_setg_errno(errp, errno, "Cannot stat file");
> 
> This code change doesn't match the commit message -- the commit
> message says it's trying to free a filedescriptor, but the code
> change is unlinking a file.
> 
> Unlinking the file is definitely wrong, because the purpose of the
> pidfile is to comminucate the QEMU PID to other processes -- if we
> delete the file then those other processes can't find it. (The file
> gets deleted when QEMU exits -- see qemu_unlink_pidfile() and the code
> that calls it.)


We are also *intentionally* leaking the file descriptor. We have a lock
held on the FD and that lock must not be released until QEMU exits. We
thus leak the FD and rely on the OS to do cleanup when we exit.

We really ought to put a explicit comment in this method and people are
repeatedly trying to fix this non-bug.

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] 3+ messages in thread

end of thread, other threads:[~2021-05-10 15:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-10 14:11 [PATCH v2] util: fix fd leak in qemu_write_pidfile() Jie Wang
2021-05-10 15:07 ` Peter Maydell
2021-05-10 15:22   ` Daniel P. Berrangé

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).