From patchwork Thu Mar 15 12:06:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 26317 Received: (qmail 21572 invoked by alias); 15 Mar 2018 12:07:05 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 21560 invoked by uid 89); 15 Mar 2018 12:07:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=H*MI:ubuntu, H*m:ubuntu, terminals, resides X-HELO: mail-wr0-f193.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=yrNsacWMM12sXDJRel7BHP8iyYctJRIpaQWqjECX3u8=; b=CyCQfbI0JK7OQvMADrSNyD5w62ANsqSU7m29Txp7hOdBM4746agcU93rEAcJ6OjWj2 VqP7DHUOHmKiF1bgI4Y+cjlVvBJRwp92ni14ovgn/qBzTZ1B/SpTiHM3pxCpG+BGBTEY ffoB+YnfrxMIclc7hpyB66VUwdS7nbyu7+AsO6dXLfd4bNRyaHTMTh5tcP2F40ICqw04 WObElVYa8KmwWaGDeWUJZalZWwB0J5cpOBX2Ye1N0Ge1NRcG9P4/sPfsWdeJ/eD4P0Nz r6FyeRN/98y2fO5kxAQFUlDTiL3DZsmGoSXA7cA3WrTtV7FEIaP+C0DljXwaOzQtfSTD J/Mg== X-Gm-Message-State: AElRT7FJOpbsk+rtkOS+uYbC5IDlDIudjrH/9RrOxWSV4lIuY5/jcJPo hAWuJojP7qOwK5nw+PeFsu8= X-Google-Smtp-Source: AG47ELulVyOgJ0OXYmsbrtN7swfI0k9C2cn3klNTYtfv2NHgbWYqE5Kv2haH3+j2TvEOvlhLRpfnCQ== X-Received: by 10.223.156.10 with SMTP id f10mr7395392wrc.174.1521115620825; Thu, 15 Mar 2018 05:07:00 -0700 (PDT) From: Christian Brauner To: ebiederm@xmission.com, libc-alpha@sourceware.org Cc: Christian Brauner Subject: [PATCH] getpt: use /dev/pts/ptmx as default ptmx master Date: Thu, 15 Mar 2018 13:06:51 +0100 Message-Id: <20180315120651.14107-1-christian.brauner@ubuntu.com> For a long time now Linux has placed the ptmx character device directly under the devpts mount at /dev/pts/ptmx. The /dev/ptmx path today is usually either a symlink, an additional character device or a bind-mount. The idea has always been to slowly fade-out /dev/ptmx and switch to using /dev/pts/ptmx exclusively. The kernel currently maintains code to retain backwards compatibility for anyone going through /dev/ptmx. Specifically, if the ptmx device is opened through /dev/ptmx the kernel will look for a "pts" directory in the same directory where the /dev/ptmx device node resides. This implies that the devpts mount at /dev/pts and the /dev/ptmx mount need to have a common ancestor directory. This assumption is usually fulfilled when a symlink or separate device node is used. However, this assumption will be broken when /dev/ptmx is a bind-mount of /dev/pts/ptmx because they are located on different devices. For a detailed analysis of this problem please refer to my upstream patch [1]. It is time to start switching to using /dev/pts/ptmx and use /dev/ptmx as a fallback only. As far as I can tell, we have three cases to reason about: 1. /dev/ptmx is a symlink to /dev/pts/ptmx In this case devpts must have either been mounted with ptmxmode=0666 or chmod 0666 /dev/pts/ptmx must have been called. So any open() on /dev/pts/ptmx will succeed. 2. /dev/ptmx is a bind-mount of /dev/pts/ptmx Analogous to 1. devpts must have either been mounted with ptmxmode=0666 or chmod 0666 /dev/pts/ptmx must have been called. So any open() on /dev/pts/ptmx will succeed. 3. /dev/ptmx is a separate ptmx device node In this case devpts can either be mounted with ptmxmode=0666 or ptmxmode=0000. In the latter case privileged opens of /dev/pts/ptmx will succeed while unprivileged opens will fail. The unprivileged failure case will be unproblematic since we always fallback to opening /dev/ptmx which should have permission 0666. If it doesn't then we would fail the exact same way we always did. [1]: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git/commit/?h=tty-next&id=a319b01d9095da6f6c54bd20c1f1300762506255 Signed-off-by: Christian Brauner --- ChangeLog | 6 ++++++ sysdeps/unix/sysv/linux/getpt.c | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 38154c20ab..01926472cc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2018-03-15 Christian Brauner + + * sysdeps/unix/sysv/linux/getpt.c (__posix_openpt): Try to open + ptmx device node through /dev/pts/ptmx and use /dev/ptmx as a + fallback. + 2018-03-15 Siddhesh Poyarekar * sysdeps/aarch64/strncmp.S (strncmp): Use lsr instead of diff --git a/sysdeps/unix/sysv/linux/getpt.c b/sysdeps/unix/sysv/linux/getpt.c index 77aa468d83..c12a984a36 100644 --- a/sysdeps/unix/sysv/linux/getpt.c +++ b/sysdeps/unix/sysv/linux/getpt.c @@ -25,11 +25,15 @@ #include "linux_fsinfo.h" -/* Path to the master pseudo terminal cloning device. */ -#define _PATH_DEVPTMX _PATH_DEV "ptmx" /* Directory containing the UNIX98 pseudo terminals. */ #define _PATH_DEVPTS _PATH_DEV "pts" +/* Path to the master pseudo terminal cloning device. */ +#define _PATH_DEVPTMX _PATH_DEV "ptmx" + +/* Path to the master pseudo terminal cloning device under devpts mount. */ +#define _PATH_DEVPTS_PTMX _PATH_DEVPTS "/ptmx" + /* Prototype for function that opens BSD-style master pseudo-terminals. */ extern int __bsd_getpt (void) attribute_hidden; @@ -42,7 +46,15 @@ __posix_openpt (int oflag) if (!have_no_dev_ptmx) { - fd = __open (_PATH_DEVPTMX, oflag); + /* Try to open ptmx master pseudo terminal cloning device under the + * devpts mount. + */ + fd = __open (_PATH_DEVPTS_PTMX, oflag); + if (fd == -1) + /* Fallback to opening the legacy ptmx master pseudo terminal + * cloning device. + */ + fd = __open (_PATH_DEVPTMX, oflag); if (fd != -1) { struct statfs fsbuf;