From patchwork Mon Oct 3 14:05:32 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: 677739 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 3snkN946Pdz9s3v for ; Tue, 4 Oct 2016 01:05:45 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=HBBeUr67; 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:cc:subject:message-id:references :mime-version:content-type:in-reply-to; q=dns; s=default; b=rZp6 F25Pqg2LCZ0WVsXdFBgGNo0QVP939a7QOzB+7xh++4jFG1+UiRpjtgQwWxiOr9Rt SzE0vrBcRVOY3qlu4+sBNMSHPvY4Yr89pbaRhoauYZMltJXjU8WQUknqLcWB3dPt n/Tb5gTOgRYpcSdB495MQVswGUinMxL75agt94E= 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:cc:subject:message-id:references :mime-version:content-type:in-reply-to; s=default; bh=iMWBqPYb/L mBJgdCU/MKR41YjlY=; b=HBBeUr670AA6xZQX1zZzbbiqYqsuw1ePEoAg0OAEhu P2rRmdq40j6r97vONZ3JNs8R1dARpopB1PcqvqlxGyjT2iwtYz0d5ap8YdXftJAr FoZCqjKvhi3BkDF8x5j4dCAZk924p9cTU/kcVEwq7b029hb18tCzP17rM7nmaK9b w= Received: (qmail 87133 invoked by alias); 3 Oct 2016 14:05:38 -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 87099 invoked by uid 89); 3 Oct 2016 14:05:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.3 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=pts X-HELO: h2.hallyn.com Date: Mon, 3 Oct 2016 09:05:32 -0500 From: "Serge E. Hallyn" To: Andreas Schwab Cc: "Serge E. Hallyn" , Florian Weimer , libc-alpha@sourceware.org, =?iso-8859-1?Q?St=E9phane?= Graber Subject: [PATCH 1/1] linux ttyname and ttyname_r: do not return wrong results Message-ID: <20161003140532.GA10012@mail.hallyn.com> References: <20160806020855.GA19897@mail.hallyn.com> <20160806084559.GS6702@vapier.lan> <20160806150002.GA24315@mail.hallyn.com> <20160809211841.GB2566@altlinux.org> <20160809213937.GA3392@mail.hallyn.com> <20160810230351.GA20138@mail.hallyn.com> <20160810231818.GA20183@altlinux.org> <20161003061602.GA5257@mail.hallyn.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) If a link (say /proc/self/fd/0) pointint to a device, say /dev/pts/2, in a parent mount namespace is passed to ttyname, 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. Detect this case and return ENODEV. Userspace can choose to take this as a hint that the fd points to a tty device but to act on the fd rather than the link. --- sysdeps/unix/sysv/linux/ttyname.c | 28 ++++++++++++++++++++++++---- sysdeps/unix/sysv/linux/ttyname_r.c | 29 +++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c index 7a001b4..798f396 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,18 @@ ttyname (int fd) #ifdef _STATBUF_ST_RDEV && S_ISCHR (st1.st_mode) && st1.st_rdev == st.st_rdev -#else - && st1.st_ino == st.st_ino - && st1.st_dev == st.st_dev #endif - ) + && st1.st_ino == st.st_ino + && st1.st_dev == st.st_dev) return ttyname_buf; + + /* If the link doesn't exist, then it points to a device in another + namespace. */ + if (is_pty (&st)) + { + __set_errno (ENODEV); + return NULL; + } } 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..2573729 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,19 @@ __ttyname_r (int fd, char *buf, size_t buflen) #ifdef _STATBUF_ST_RDEV && S_ISCHR (st1.st_mode) && st1.st_rdev == st.st_rdev -#else - && st1.st_ino == st.st_ino - && st1.st_dev == st.st_dev #endif - ) + && st1.st_ino == st.st_ino + && st1.st_dev == st.st_dev) 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)) + { + __set_errno (ENODEV); + return ENODEV; + } } /* Prepare the result buffer. */