[PATCH v2 12/26] qga: replace pipe() with g_unix_open_pipe(CLOEXEC)

marcandre.lureau@redhat.com posted 26 patches 3 years, 5 months ago
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Gerd Hoffmann <kraxel@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Jason Wang <jasowang@redhat.com>, Eric Blake <eblake@redhat.com>, Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>, Michael Roth <michael.roth@amd.com>, Alexander Bulekov <alxndr@bu.edu>, Bandan Das <bsd@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Thomas Huth <thuth@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Laurent Vivier <lvivier@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <ani@anisinha.ca>, John Snow <jsnow@redhat.com>, Fam Zheng <fam@euphon.net>, Peter Maydell <peter.maydell@linaro.org>, "Cédric Le Goater" <clg@kaod.org>, Andrew Jeffery <andrew@aj.id.au>, Joel Stanley <joel@jms.id.au>, Dmitry Fleytman <dmitry.fleytman@gmail.com>, Hannes Reinecke <hare@suse.com>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, Bin Meng <bin.meng@windriver.com>, Su Hang <suhang16@mails.ucas.ac.cn>, Daniel Henrique Barboza <danielhb413@gmail.com>, David Gibson <david@gibson.dropbear.id.au>, Greg Kurz <groug@kaod.org>, Coiby Xu <Coiby.Xu@gmail.com>, Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Havard Skinnemoen <hskinnemoen@google.com>, Tyrone Ting <kfting@nuvoton.com>, Keith Busch <kbusch@kernel.org>, Klaus Jensen <its@irrelevant.dk>, Markus Armbruster <armbru@redhat.com>, Zhang Chen <chen.zhang@intel.com>, Li Zhijian <lizhijian@fujitsu.com>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Amit Shah <amit@kernel.org>, Vikram Garhwal <fnu.vikram@xilinx.com>, Francisco Iglesias <francisco.iglesias@xilinx.com>, Stefan Weil <sw@weilnetz.de>
There is a newer version of this series
[PATCH v2 12/26] qga: replace pipe() with g_unix_open_pipe(CLOEXEC)
Posted by marcandre.lureau@redhat.com 3 years, 5 months ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 qga/commands-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 77f4672ca2c9..094487c2c395 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -2529,7 +2529,7 @@ void qmp_guest_set_user_password(const char *username,
         goto out;
     }
 
-    if (pipe(datafd) < 0) {
+    if (!g_unix_open_pipe(datafd, FD_CLOEXEC, NULL)) {
         error_setg(errp, "cannot create pipe FDs");
         goto out;
     }
-- 
2.36.0


Re: [PATCH v2 12/26] qga: replace pipe() with g_unix_open_pipe(CLOEXEC)
Posted by Richard Henderson 3 years, 5 months ago
On 4/26/22 02:27, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>   qga/commands-posix.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index 77f4672ca2c9..094487c2c395 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -2529,7 +2529,7 @@ void qmp_guest_set_user_password(const char *username,
>           goto out;
>       }
>   
> -    if (pipe(datafd) < 0) {
> +    if (!g_unix_open_pipe(datafd, FD_CLOEXEC, NULL)) {
>           error_setg(errp, "cannot create pipe FDs");
>           goto out;
>       }

This looks wrong, since the next thing that happens is fork+execl.


r~

Re: [PATCH v2 12/26] qga: replace pipe() with g_unix_open_pipe(CLOEXEC)
Posted by Marc-André Lureau 3 years, 5 months ago
Hi

On Wed, Apr 27, 2022 at 5:08 AM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 4/26/22 02:27, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >
> > Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> >   qga/commands-posix.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> > index 77f4672ca2c9..094487c2c395 100644
> > --- a/qga/commands-posix.c
> > +++ b/qga/commands-posix.c
> > @@ -2529,7 +2529,7 @@ void qmp_guest_set_user_password(const char
> *username,
> >           goto out;
> >       }
> >
> > -    if (pipe(datafd) < 0) {
> > +    if (!g_unix_open_pipe(datafd, FD_CLOEXEC, NULL)) {
> >           error_setg(errp, "cannot create pipe FDs");
> >           goto out;
> >       }
>
> This looks wrong, since the next thing that happens is fork+execl.
>
>
Before exec(), it does
        close(datafd[1]);
        dup2(datafd[0], 0);

0, the newfd, does not share file descriptor flags (the close-on-exec flag).

I did a quick test, and it seems to be fine.


-- 
Marc-André Lureau
Re: [PATCH v2 12/26] qga: replace pipe() with g_unix_open_pipe(CLOEXEC)
Posted by Daniel P. Berrangé 3 years, 4 months ago
On Wed, Apr 27, 2022 at 12:24:44PM +0400, Marc-André Lureau wrote:
> Hi
> 
> On Wed, Apr 27, 2022 at 5:08 AM Richard Henderson <
> richard.henderson@linaro.org> wrote:
> 
> > On 4/26/22 02:27, marcandre.lureau@redhat.com wrote:
> > > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> > >
> > > Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
> > > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > > ---
> > >   qga/commands-posix.c | 2 +-
> > >   1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> > > index 77f4672ca2c9..094487c2c395 100644
> > > --- a/qga/commands-posix.c
> > > +++ b/qga/commands-posix.c
> > > @@ -2529,7 +2529,7 @@ void qmp_guest_set_user_password(const char
> > *username,
> > >           goto out;
> > >       }
> > >
> > > -    if (pipe(datafd) < 0) {
> > > +    if (!g_unix_open_pipe(datafd, FD_CLOEXEC, NULL)) {
> > >           error_setg(errp, "cannot create pipe FDs");
> > >           goto out;
> > >       }
> >
> > This looks wrong, since the next thing that happens is fork+execl.
> >
> >
> Before exec(), it does
>         close(datafd[1]);
>         dup2(datafd[0], 0);
> 
> 0, the newfd, does not share file descriptor flags (the close-on-exec flag).
> 
> I did a quick test, and it seems to be fine.

The 'dup' man page says

       The  two  file  descriptors do not share file descriptor flags
       (the close-on-exec flag).  The close-on-exec flag (FD_CLOEXEC;
       see fcntl(2)) for the duplicate descriptor is off.

so we're fine in this respect. You could need to use dup3 to explicitly
turn on FD_CLOEXEC on the duplicate, so

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 :|