From patchwork Sat Aug 6 02:08:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Serge E. Hallyn" X-Patchwork-Id: 656333 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3s5nDJ3VX3z9t15 for ; Sat, 6 Aug 2016 12:09:20 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=gYBzt/Pj; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:subject:message-id:references :mime-version:content-type:in-reply-to; q=dns; s=default; b=iQhY bRoignK8dQFdPh43q/22HwUwSb0kZlneF5zA+8xTKIhXYbZATRIJRvPEqY8w+KTJ QoDBntsTDoiJpfyTe96H4HsERoIZAUAqPUXy7MoOBKZQjF1ILXdwyaNUgY49INFd Kg0Q91v7mmVZUvtrCTuwRhb+Da817Fj8GJzic1s= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:subject:message-id:references :mime-version:content-type:in-reply-to; s=default; bh=zcpCJSmj// lEnpqrIvH+lvQfV/Q=; b=gYBzt/Pj0eBm6PUpi7L5yUTrBISgtmfodL+Re5iVDS dlh6atlN8++8jIhw27Td8MO5Of5IVCPa+EHdpJGSBBZyuiZAa3R1EMyvpDKm26Ed wg0Sk7FplNdwI9A71DUl1SJV82kCI8o5X++VD3ZxDbqqUTkapj/NhXrQgK2ErINP g= Received: (qmail 54828 invoked by alias); 6 Aug 2016 02:09:13 -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 54818 invoked by uid 89); 6 Aug 2016 02:09:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=levin, unix98, UNIX98, sysmacrosh X-HELO: h2.hallyn.com Date: Fri, 5 Aug 2016 21:08:55 -0500 From: "Serge E. Hallyn" To: Serge Hallyn , libc-alpha@sourceware.org Subject: [PATCH] linux ttyname and ttyname_r: return link if appropriate Message-ID: <20160806020855.GA19897@mail.hallyn.com> References: <20160415174245.GA9610@ubuntumail> <20160415184746.GA10830@ubuntumail> <20160415195938.GP6588@vapier.lan> <20160418195318.GB30476@ubuntumail> <20160418200222.GY5369@vapier.lan> <20160418202301.GA30919@ubuntumail> <20160420021012.GA19449@ubuntumail> <20160420022823.GT5369@vapier.lan> <20160420185141.GA31095@ubuntumail> <20160727165711.GA27815@altlinux.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20160727165711.GA27815@altlinux.org> User-Agent: Mutt/1.5.21 (2010-09-15) Quoting Dmitry V. Levin (ldv@altlinux.org): > On Wed, Apr 20, 2016 at 06:51:41PM +0000, Serge Hallyn wrote: > > 1. If the passed-in link (say /proc/self/fd/0) points to a > > device, say /dev/pts/2, in a parent mount namespace, and a > > /dev/pts/2 exists (in a different devpts) in the current > > namespace, then it returns /dev/pts/2. But /dev/pts/2 is > > NOT the current tty, it is a different file and device. > [...] > > --- a/sysdeps/unix/sysv/linux/ttyname.c > > +++ b/sysdeps/unix/sysv/linux/ttyname.c > [...] > > @@ -170,12 +184,19 @@ ttyname (int fd) > > #ifdef _STATBUF_ST_RDEV > > && S_ISCHR (st1.st_mode) > > && st1.st_rdev == st.st_rdev > > + && st1.st_dev == st.st_dev > > #else > > && st1.st_ino == st.st_ino > > && st1.st_dev == st.st_dev > > #endif > > __ttyname_r also needs this st_dev check. > > To be on the safe side, I'd check st_ino in _STATBUF_ST_RDEV case as well. (new patch follows) The current ttyname does the wrong thing in two cases: 1. If the passed-in link (say /proc/self/fd/0) points to a device, say /dev/pts/2, in a parent mount namespace, and a /dev/pts/2 exists (in a different devpts) in the current namespace, then it returns /dev/pts/2. But /dev/pts/2 is NOT the current tty, it is a different file and device. 2. If the passed-in link (say /proc/self/fd/0) points to a device, say /dev/pts/2, in a parent mount namespace, and /dev/pts/2 does not exist in the current namespace, it returns success but an empty name. As far as I can tell, there is no reason for it to not return /proc/self/fd/0. http://pubs.opengroup.org/onlinepubs/009695399/functions/ttyname.html does not say anything about not returning a link. --- sysdeps/unix/sysv/linux/ttyname.c | 22 ++++++++++++++++++++++ sysdeps/unix/sysv/linux/ttyname_r.c | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c index 7a001b4..c45a847 100644 --- a/sysdeps/unix/sysv/linux/ttyname.c +++ b/sysdeps/unix/sysv/linux/ttyname.c @@ -25,6 +25,7 @@ #include #include #include +#include #include <_itoa.h> @@ -33,6 +34,19 @@ char *__ttyname; #endif +/* Return true if this is a UNIX98 pty device, as defined in + linux/Documentation/devices.txt. */ +static int +is_pty (struct stat64 *sb) +{ +#ifdef _STATBUF_ST_RDEV + int m = major (sb->st_rdev); + return (136 <= m && m <= 143); +#else + return false; +#endif +} + static char *getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat) internal_function; @@ -170,12 +184,20 @@ ttyname (int fd) #ifdef _STATBUF_ST_RDEV && S_ISCHR (st1.st_mode) && st1.st_rdev == st.st_rdev + && st1.st_dev == st.st_dev + && st1.st_ino == st.st_ino #else && st1.st_ino == st.st_ino && st1.st_dev == st.st_dev #endif ) return ttyname_buf; + + /* If the link doesn't exist, then it points to a device in another + namespace. If it is a UNIX98 pty, then return the /proc/self + fd, as it points to a name unreachable in our namespace. */ + if (is_pty (&st) && strlen (procname) < buflen - 1) + return strcpy (ttyname_buf, procname); } if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode)) diff --git a/sysdeps/unix/sysv/linux/ttyname_r.c b/sysdeps/unix/sysv/linux/ttyname_r.c index d15bc74..d95d07e 100644 --- a/sysdeps/unix/sysv/linux/ttyname_r.c +++ b/sysdeps/unix/sysv/linux/ttyname_r.c @@ -25,6 +25,7 @@ #include #include #include +#include #include <_itoa.h> @@ -32,6 +33,19 @@ static int getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, int save, int *dostat) internal_function; +/* Return true if this is a UNIX98 pty device, as defined in + linux/Documentation/devices.txt. */ +static int +is_pty (struct stat64 *sb) +{ +#ifdef _STATBUF_ST_RDEV + int m = major (sb->st_rdev); + return (136 <= m && m <= 143); +#else + return false; +#endif +} + static int internal_function attribute_compat_text_section getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, @@ -152,12 +166,23 @@ __ttyname_r (int fd, char *buf, size_t buflen) #ifdef _STATBUF_ST_RDEV && S_ISCHR (st1.st_mode) && st1.st_rdev == st.st_rdev + && st1.st_dev == st.st_dev + && st1.st_ino == st.st_ino #else && st1.st_ino == st.st_ino && st1.st_dev == st.st_dev #endif ) return 0; + + /* If the link doesn't exist, then it points to a device in another + namespace. If it is a UNIX98 pty, then return the /proc/self + fd, as it points to a name unreachable in our namespace. */ + if (is_pty (&st) && strlen (procname) < buflen - 1) + { + strcpy (buf, procname); + return 0; + } } /* Prepare the result buffer. */