[{"id":1771733,"web_url":"http://patchwork.ozlabs.org/comment/1771733/","msgid":"<20170920105254.ibq3ynrygwnoss2x@gmail.com>","list_archive_url":null,"date":"2017-09-20T10:52:56","subject":"Re: [PATCH 1/2 v5] openpty: close slave pty fd on error","submitter":{"id":70356,"url":"http://patchwork.ozlabs.org/api/people/70356/","name":"Christian Brauner","email":"christian.brauner@canonical.com"},"content":"On Sun, Sep 10, 2017 at 07:45:27PM +0200, Christian Brauner wrote:\n> Hi guys,\n> \n> Any update on whether this is acceptable for inclusion or not now. Linux 4.13\n> has been released which measn TIOCGPTPEER is now stable API.\n> \n> Christian\n\nHi everyone,\n\nFriendly ping about this patch again. I'm not sure where we left of. Last time\nit seemed you were fine with the patch. Any change that you'll merge this soon?\n\nThanks!\nChristian\n\n> \n> On Tue, Aug 29, 2017 at 04:30:36PM +0200, Christian Brauner wrote:\n> > When openpty() failed only the master fd was closed so far. Let's close the\n> > slave fd as well. Also, let's unify the error handling.\n> > \n> > Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>\n> > ---\n> > Changelog 2017-08-29:\n> > * Unify error handling: use a common function exit that frees everything that\n> >   needs freeing. (@Florian)\n> > Changelog 2017-08-29:\n> > * Do not be stupid and only close the file descriptors on error! Duh. (Thanks,\n> >   @Andreas)\n> > ---\n> >  ChangeLog       |  4 ++++\n> >  login/openpty.c | 30 ++++++++++++++++--------------\n> >  2 files changed, 20 insertions(+), 14 deletions(-)\n> > \n> > diff --git a/ChangeLog b/ChangeLog\n> > index bc1cf94dc3..bc5fb8e27f 100644\n> > --- a/ChangeLog\n> > +++ b/ChangeLog\n> > @@ -1,3 +1,7 @@\n> > +2017-08-26  Christian Brauner  <christian.brauner@ubuntu.com>\n> > +\n> > +\t* login/openpty.c (openpty): Close slave pty file descriptor on error.\n> > +\n> >  2017-08-25  H.J. Lu  <hongjiu.lu@intel.com>\n> >  \n> >  \t* sysdeps/x86/cpu-features.h [__ASSEMBLER__]\n> > diff --git a/login/openpty.c b/login/openpty.c\n> > index 41ab0483e2..9e556c27a5 100644\n> > --- a/login/openpty.c\n> > +++ b/login/openpty.c\n> > @@ -92,29 +92,24 @@ openpty (int *amaster, int *aslave, char *name,\n> >    char _buf[512];\n> >  #endif\n> >    char *buf = _buf;\n> > -  int master, slave;\n> > +  int master, ret = -1, slave = -1;\n> >  \n> >    master = getpt ();\n> >    if (master == -1)\n> >      return -1;\n> >  \n> >    if (grantpt (master))\n> > -    goto fail;\n> > +    goto on_error;\n> >  \n> >    if (unlockpt (master))\n> > -    goto fail;\n> > +    goto on_error;\n> >  \n> >    if (pts_name (master, &buf, sizeof (_buf)))\n> > -    goto fail;\n> > +    goto on_error;\n> >  \n> >    slave = open (buf, O_RDWR | O_NOCTTY);\n> >    if (slave == -1)\n> > -    {\n> > -      if (buf != _buf)\n> > -\tfree (buf);\n> > -\n> > -      goto fail;\n> > -    }\n> > +    goto on_error;\n> >  \n> >    /* XXX Should we ignore errors here?  */\n> >    if (termp)\n> > @@ -129,12 +124,19 @@ openpty (int *amaster, int *aslave, char *name,\n> >    if (name != NULL)\n> >      strcpy (name, buf);\n> >  \n> > +  ret = 0;\n> > +\n> > + on_error:\n> > +  if (ret == -1) {\n> > +    close (master);\n> > +\n> > +    if (slave != -1)\n> > +      close (slave);\n> > +  }\n> > +\n> >    if (buf != _buf)\n> >      free (buf);\n> > -  return 0;\n> >  \n> > - fail:\n> > -  close (master);\n> > -  return -1;\n> > +  return ret;\n> >  }\n> >  libutil_hidden_def (openpty)\n> > -- \n> > 2.14.1\n> > \n> \n> On Tue, Aug 29, 2017 at 04:30:37PM +0200, Christian Brauner wrote:\n> > Newer kernels expose the ioctl TIOCGPTPEER [1] call to userspace which allows to\n> > safely allocate a file descriptor for a pty slave based solely on the master\n> > file descriptor. This allows us to avoid path-based operations and makes this\n> > function a lot safer in the face of devpts mounts in different mount namespaces.\n> > \n> > [1]: https://patchwork.kernel.org/patch/9760743/\n> > \n> > Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>\n> > ---\n> > Changelog 2017-08-28:\n> > * Instead of #ifdefing the TIOCGPTPEER ioctl flag we now try the ioctl() first\n> >   and if it fails we fallback to path-based allocation of the slave fd. This\n> >   allows us retain backward compatibility with kernels that do not support this\n> >   ioctl call.\n> > * A note on the following codepath\n> > \n> >    if (name != NULL)\n> >      {\n> >        if (*buf == '\\0')\n> >          if (pts_name (master, &buf, sizeof (_buf)))\n> >            goto fail;\n> > \n> >        strcpy (name, buf);\n> >      }\n> > \n> >   \"buf\" is guaranteed to be allocated in this case. If the pts_name() call above\n> >   failed we would have never reached this code path. If it has been called\n> >   succesfully it will either have handed us a valid buffer or \"buf\" will still\n> >   point to the static char array \"_buf\" which is initialized to 0.\n> > Changelog 2017-08-28:\n> > * Preserve #ifdef for TIOCGPTPEER since it needs to work on non-Linux distros\n> >   too.\n> > * Only intialize first byte of \"_buf\".\n> > Changelog 2017-08-29:\n> > * Adapt to unified error handling as suggested by Florian.\n> > ---\n> >  ChangeLog       |  5 +++++\n> >  login/openpty.c | 30 ++++++++++++++++++++++++------\n> >  2 files changed, 29 insertions(+), 6 deletions(-)\n> > \n> > diff --git a/ChangeLog b/ChangeLog\n> > index bc5fb8e27f..30829e4c16 100644\n> > --- a/ChangeLog\n> > +++ b/ChangeLog\n> > @@ -1,3 +1,8 @@\n> > +2017-08-26  Christian Brauner  <christian.brauner@ubuntu.com>\n> > +\n> > +\t* login/openpty.c (openpty): If defined, use the TIOCGPTPEER ioctl call\n> > +\tto allocate the slave pty file descriptor.\n> > +\n> >  2017-08-26  Christian Brauner  <christian.brauner@ubuntu.com>\n> >  \n> >  \t* login/openpty.c (openpty): Close slave pty file descriptor on error.\n> > diff --git a/login/openpty.c b/login/openpty.c\n> > index 9e556c27a5..6703128ea8 100644\n> > --- a/login/openpty.c\n> > +++ b/login/openpty.c\n> > @@ -94,6 +94,8 @@ openpty (int *amaster, int *aslave, char *name,\n> >    char *buf = _buf;\n> >    int master, ret = -1, slave = -1;\n> >  \n> > +  *buf = '\\0';\n> > +\n> >    master = getpt ();\n> >    if (master == -1)\n> >      return -1;\n> > @@ -104,12 +106,22 @@ openpty (int *amaster, int *aslave, char *name,\n> >    if (unlockpt (master))\n> >      goto on_error;\n> >  \n> > -  if (pts_name (master, &buf, sizeof (_buf)))\n> > -    goto on_error;\n> > -\n> > -  slave = open (buf, O_RDWR | O_NOCTTY);\n> > +#ifdef TIOCGPTPEER\n> > +  /* Try to allocate slave fd solely based on master fd first. */\n> > +  slave = ioctl (master, TIOCGPTPEER, O_RDWR | O_NOCTTY);\n> > +#endif\n> >    if (slave == -1)\n> > -    goto on_error;\n> > +    {\n> > +      /* Fallback to path-based slave fd allocation in case kernel doesn't\n> > +       * support TIOCGPTPEER.\n> > +       */\n> > +      if (pts_name (master, &buf, sizeof (_buf)))\n> > +        goto on_error;\n> > +\n> > +      slave = open (buf, O_RDWR | O_NOCTTY);\n> > +      if (slave == -1)\n> > +        goto on_error;\n> > +    }\n> >  \n> >    /* XXX Should we ignore errors here?  */\n> >    if (termp)\n> > @@ -122,7 +134,13 @@ openpty (int *amaster, int *aslave, char *name,\n> >    *amaster = master;\n> >    *aslave = slave;\n> >    if (name != NULL)\n> > -    strcpy (name, buf);\n> > +    {\n> > +      if (*buf == '\\0')\n> > +        if (pts_name (master, &buf, sizeof (_buf)))\n> > +          goto on_error;\n> > +\n> > +      strcpy (name, buf);\n> > +    }\n> >  \n> >    ret = 0;\n> >  \n> > -- \n> > 2.14.1\n> > \n>","headers":{"Return-Path":"<libc-alpha-return-84774-incoming=patchwork.ozlabs.org@sourceware.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":["patchwork-incoming@bilbo.ozlabs.org","mailing list libc-alpha@sourceware.org"],"Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=sourceware.org\n\t(client-ip=209.132.180.131; helo=sourceware.org;\n\tenvelope-from=libc-alpha-return-84774-incoming=patchwork.ozlabs.org@sourceware.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (1024-bit key;\n\tsecure) header.d=sourceware.org header.i=@sourceware.org\n\theader.b=\"QRY6VRPL\"; dkim-atps=neutral","sourceware.org; auth=none"],"Received":["from sourceware.org (server1.sourceware.org [209.132.180.131])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits)) (No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xxxRV0L7zz9s7c\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed, 20 Sep 2017 20:53:09 +1000 (AEST)","(qmail 72853 invoked by alias); 20 Sep 2017 10:53:02 -0000","(qmail 72837 invoked by uid 89); 20 Sep 2017 10:53:02 -0000"],"DomainKey-Signature":"a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id\n\t:list-unsubscribe:list-subscribe:list-archive:list-post\n\t:list-help:sender:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-type:in-reply-to; q=dns; s=default; b=AmSX\n\taV7KlT2LcAmHry4jS/KTdy9tdQ+QED9S0F8IDktN7qT3gGkGtuEye08GjTdRfbdK\n\tDYhB8X4MLYyyONJ/75v+iJ/iqvw7yOzfDrpLdxDUaYCesR6AUrw6PQvzTzzGD5Ib\n\t4d6CIIkRp+8mJOBP93buhQOHwzOXboky0rEWDwA=","DKIM-Signature":"v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id\n\t:list-unsubscribe:list-subscribe:list-archive:list-post\n\t:list-help:sender:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-type:in-reply-to; s=default; bh=U6oYWY78x3\n\tvTb8K8APNkBVQa+HQ=; b=QRY6VRPLUz6xWr3NBdypxX1xZORz/stvssZHodx0Bs\n\tlzQchbFwE0ORbd8OcP3LO0eyin91OdHlOUux11eplsTj6tlaECA4lQm1AfqqBXqh\n\tBNFHGubSPxoQoHfgyb8rOyKsESedKc5e4MXAx2Rtd5xGo3/rVX0+1WKQX5Q58YV6\n\t4=","Mailing-List":"contact libc-alpha-help@sourceware.org; run by ezmlm","Precedence":"bulk","List-Id":"<libc-alpha.sourceware.org>","List-Unsubscribe":"<mailto:libc-alpha-unsubscribe-incoming=patchwork.ozlabs.org@sourceware.org>","List-Subscribe":"<mailto:libc-alpha-subscribe@sourceware.org>","List-Archive":"<http://sourceware.org/ml/libc-alpha/>","List-Post":"<mailto:libc-alpha@sourceware.org>","List-Help":"<mailto:libc-alpha-help@sourceware.org>,\n\t<http://sourceware.org/ml/#faqs>","Sender":"libc-alpha-owner@sourceware.org","X-Virus-Found":"No","X-Spam-SWARE-Status":"No, score=-23.0 required=5.0 tests=AWL, BAYES_00,\n\tGIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3,\n\tKAM_LAZY_DOMAIN_SECURITY, RCVD_IN_SORBS_SPAM,\n\tRP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=distros,\n\tpty, freeing","X-HELO":"youngberry.canonical.com","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:in-reply-to:user-agent;\n\tbh=1XRFrVb4pimSPI0Vf1q+MxN+imh2MMkFra4GFiunvgY=;\n\tb=cud1vrWb4aFAslGg8GI2nYUI0o3w4cFUURnUBlAMHJIXC50fDW4CYsXIMDjGBM0owT\n\twO/UU1cVc3zjAAnqrMQ57xGQGuFL1AOVU2PazAdc/Zvkwrk2gmrrM4SvbFUN3ujWuS3Z\n\tTHVpxohkScf1mTSvJjfAVVXClNdBucaE9B08syvE2iu2YUd5g4qKPJlSk/PuNi/zrA01\n\teBWptFOLM1OCc9fpeKLFT9/JPwvYBn/9EmIFn2wHdw2jQIldXNNtbgoHJYoGAkgNrKS7\n\tMDw+PWwZyg+MAO8R+oo+paqxvZW3vlldBma+5PzA9dXI6C8CnL8EUOkGcYZE7bHCoN1F\n\tKJqg==","X-Gm-Message-State":"AHPjjUiMgylAnWEXogK6rsFoKhwxnXF3nmTgUKwDzmItsC6+MtakteS9\n\tavDC0o73fPmH+Ql0K7gKl4F2cAU8woz+M1HeyeCBN+7k8urFLCacroCPza81wm308OxpaZssjmM\n\t9tgn/CjbMow8QM4RbgDJVao+juGXRDVQPq8y0IQ==","X-Received":["by 10.28.226.84 with SMTP id z81mr3508408wmg.108.1505904777429; \n\tWed, 20 Sep 2017 03:52:57 -0700 (PDT)","by 10.28.226.84 with SMTP id z81mr3508392wmg.108.1505904777105; \n\tWed, 20 Sep 2017 03:52:57 -0700 (PDT)"],"X-Google-Smtp-Source":"AOwi7QDtZFghjk33iBPLmH31l6FH7j3XtEIxsyYztRiCgut+Cni2divat9vnK74xOR6lSTEh33jWOw==","Date":"Wed, 20 Sep 2017 12:52:56 +0200","From":"Christian Brauner <christian.brauner@canonical.com>","To":"Christian Brauner <christian.brauner@ubuntu.com>","Cc":"libc-alpha@sourceware.org, fweimer@redhat.com, joseph@codesourcery.com, \n\tschwab@suse.de","Subject":"Re: [PATCH 1/2 v5] openpty: close slave pty fd on error","Message-ID":"<20170920105254.ibq3ynrygwnoss2x@gmail.com>","References":"<20170829143037.24231-2-christian.brauner@ubuntu.com>\n\t<20170829143037.24231-1-christian.brauner@ubuntu.com>\n\t<20170910174527.duvyhfq4i2ejkqoa@gmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20170910174527.duvyhfq4i2ejkqoa@gmail.com>","User-Agent":"NeoMutt/20170609 (1.8.3)"}}]