{"id":807115,"url":"http://patchwork.ozlabs.org/api/patches/807115/?format=json","web_url":"http://patchwork.ozlabs.org/project/glibc/patch/20170829143037.24231-2-christian.brauner@ubuntu.com/","project":{"id":41,"url":"http://patchwork.ozlabs.org/api/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":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170829143037.24231-2-christian.brauner@ubuntu.com>","list_archive_url":null,"date":"2017-08-29T14:30:37","name":"[2/2,v5] openpty: use TIOCGPTPEER to open slave side fd","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"bd6bf3bf8e35e7479c40ab4c6a1ab776a70aca5b","submitter":{"id":72235,"url":"http://patchwork.ozlabs.org/api/people/72235/?format=json","name":"Christian Brauner","email":"christian.brauner@ubuntu.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/glibc/patch/20170829143037.24231-2-christian.brauner@ubuntu.com/mbox/","series":[{"id":379,"url":"http://patchwork.ozlabs.org/api/series/379/?format=json","web_url":"http://patchwork.ozlabs.org/project/glibc/list/?series=379","date":"2017-08-29T14:30:37","name":"[1/2,v5] openpty: close slave pty fd on error","version":5,"mbox":"http://patchwork.ozlabs.org/series/379/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/807115/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/807115/checks/","tags":{},"related":[],"headers":{"Return-Path":"<libc-alpha-return-83827-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-83827-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=\"WDOtGigA\"; 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 3xhWKJ3CcVz9t16\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed, 30 Aug 2017 00:31:16 +1000 (AEST)","(qmail 115946 invoked by alias); 29 Aug 2017 14:31:06 -0000","(qmail 115860 invoked by uid 89); 29 Aug 2017 14:31:03 -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=p1V83aN5SO08TBBtKTvhQZvjJkjzAgw\n\t6T3FOPH3wDpAhYWbS2d5XPVIhMmNJRzfgwMh94AV5szz2fKEpEXpW//hHCUqhjEo\n\tRDA1E8v54kgrHOL6B0wyOh92Goa9AE9pSg5ificaaYK5abfCfDs55vtMAq6iCDD8\n\tcWtyS4btYAFA=","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=JEwHfonVsSwLF5n1gqGrPCEXP/c=; b=WDOtG\n\tigATqn+wRIJRQA1UESK73UIj5DxZNE2y1Ca2ZF1syUIi8VnXfiWBXBMF/qlmy7gU\n\tJbrylokDb22ejNnOU+3QBXXOTS82Cg7zJ19Rz0nwDrfUbvqukEJfAl9NWa+u7SYj\n\t7+bzwuc9PqI8GTTT74Rz2GJBixjxdAhZ8c6qbY=","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=(unknown)","X-HELO":"mx2.mailbox.org","From":"Christian Brauner <christian.brauner@ubuntu.com>","To":"libc-alpha@sourceware.org, fweimer@redhat.com, joseph@codesourcery.com,\n\tschwab@suse.de","Cc":"Christian Brauner <christian.brauner@ubuntu.com>","Subject":"[PATCH 2/2 v5] openpty: use TIOCGPTPEER to open slave side fd","Date":"Tue, 29 Aug 2017 16:30:37 +0200","Message-Id":"<20170829143037.24231-2-christian.brauner@ubuntu.com>","In-Reply-To":"<20170829143037.24231-1-christian.brauner@ubuntu.com>","References":"<mvma82ijwxx.fsf@suse.de>\n\t<20170829143037.24231-1-christian.brauner@ubuntu.com>"},"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\".\nChangelog 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(-)","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 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","prefixes":["2/2","v5"]}