{"id":806567,"url":"http://patchwork.ozlabs.org/api/1.0/patches/806567/?format=json","project":{"id":41,"url":"http://patchwork.ozlabs.org/api/1.0/projects/41/?format=json","name":"GNU C Library","link_name":"glibc","list_id":"libc-alpha.sourceware.org","list_email":"libc-alpha@sourceware.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20170828125025.21923-1-christian.brauner@ubuntu.com>","date":"2017-08-28T12:50:25","name":"[2/2,v3] openpty: use TIOCGPTPEER to open slave side fd","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"8d3cb9930c60204d0d75120bc2cafab2040f593b","submitter":{"id":72235,"url":"http://patchwork.ozlabs.org/api/1.0/people/72235/?format=json","name":"Christian Brauner","email":"christian.brauner@ubuntu.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/glibc/patch/20170828125025.21923-1-christian.brauner@ubuntu.com/mbox/","series":[{"id":162,"url":"http://patchwork.ozlabs.org/api/1.0/series/162/?format=json","date":"2017-08-28T12:50:25","name":null,"version":3,"mbox":"http://patchwork.ozlabs.org/series/162/mbox/"}],"check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/806567/checks/","tags":{},"headers":{"Return-Path":"<libc-alpha-return-83781-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-83781-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=\"nXELVDNM\"; 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 3xgs8J47JCz9sN5\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon, 28 Aug 2017 22:51:12 +1000 (AEST)","(qmail 10557 invoked by alias); 28 Aug 2017 12:51:01 -0000","(qmail 10524 invoked by uid 89); 28 Aug 2017 12:51:00 -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:from:to:cc:subject:date:message-id:in-reply-to\n\t:references; q=dns; s=default; b=C30P/ZTHhsHI/3UGZDAmBLhWpM0bplg\n\t97xN2ooB56SlO8QzDmxfX9mjnvB9/vOrTJQsNSOXY9BIJwA6lxVmtCVFvePFit53\n\tMCNuC0haqNaSY/ZyLX8axnPAWs9c/wnSyl0v4CGvgxMBoY79GGMDrEB6ob19qywD\n\tK03nhbAYj0Xs=","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:from:to:cc:subject:date:message-id:in-reply-to\n\t:references; s=default; bh=Qzeuk4peErvbL6brOoKgpzlLW28=; b=nXELV\n\tDNMvgXJoHe4sIZtea80hMoLKZAWXkmo/n5RBYQrUEfiyxYdE8MwuzIEw52W83PjG\n\t03OT5I0o9Fm3MxOHddBy0n6dyYPOOS6MLZ9ZbqLYEoAi/FE0hIAHxIZ+dOrW2jry\n\tArh2CIoOqIqskV7UFVw/nZe2daAcgGNiHt+4TI=","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=-26.6 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,\n\tRCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy=","X-HELO":"mx2.mailbox.org","From":"Christian Brauner <christian.brauner@ubuntu.com>","To":"libc-alpha@sourceware.org, stgraber@stgraber.org, serge@hallyn.com,\n\tfweimer@redhat.com, joseph@codesourcery.com, schwab@suse.de","Cc":"Christian Brauner <christian.brauner@ubuntu.com>","Subject":"[PATCH 2/2 v3] openpty: use TIOCGPTPEER to open slave side fd","Date":"Mon, 28 Aug 2017 14:50:25 +0200","Message-Id":"<20170828125025.21923-1-christian.brauner@ubuntu.com>","In-Reply-To":"<alpine.DEB.2.20.1708281221480.17216@digraph.polyomino.org.uk>,\n\t<mvm60d7lvkh.fsf@suse.de>","References":"<alpine.DEB.2.20.1708281221480.17216@digraph.polyomino.org.uk>,\n\t<mvm60d7lvkh.fsf@suse.de>"},"content":"Newer kernels expose the ioctl TIOCGPTPEER [1] call to userspace which allows to\nsafely allocate a file descriptor for a pty slave based solely on the master\nfile descriptor. This allows us to avoid path-based operations and makes this\nfunction a lot safer in the face of devpts mounts in different mount namespaces.\n\n[1]: https://patchwork.kernel.org/patch/9760743/\n\nSigned-off-by: Christian Brauner <christian.brauner@ubuntu.com>\n---\nChangelog 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.\nChangelog 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---\n ChangeLog       |  5 +++++\n login/openpty.c | 36 +++++++++++++++++++++++++++---------\n 2 files changed, 32 insertions(+), 9 deletions(-)","diff":"diff --git a/ChangeLog b/ChangeLog\nindex 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.\ndiff --git a/login/openpty.c b/login/openpty.c\nindex 8fbc66a3ef..33754e18a7 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, slave = -1;\n \n+  *buf = '\\0';\n+\n   master = getpt ();\n   if (master == -1)\n     return -1;\n@@ -104,16 +106,26 @@ openpty (int *amaster, int *aslave, char *name,\n   if (unlockpt (master))\n     goto fail;\n \n-  if (pts_name (master, &buf, sizeof (_buf)))\n-    goto fail;\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     {\n-      if (buf != _buf)\n-\tfree (buf);\n-\n-      goto fail;\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 fail;\n+\n+      slave = open (buf, O_RDWR | O_NOCTTY);\n+      if (slave == -1)\n+        {\n+          if (buf != _buf)\n+\t    free (buf);\n+\n+          goto fail;\n+        }\n     }\n \n   /* XXX Should we ignore errors here?  */\n@@ -127,7 +139,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 fail;\n+\n+      strcpy (name, buf);\n+    }\n \n   if (buf != _buf)\n     free (buf);\n","prefixes":["2/2","v3"]}