From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 112226 invoked by alias); 22 Nov 2016 00:14:35 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 112149 invoked by uid 89); 22 Nov 2016 00:14:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.1 required=5.0 tests=AWL,BAYES_00,FAKE_REPLY_C,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM autolearn=no version=3.3.2 spammy=HX-Envelope-From:sk:christi, H*RU:gmail.com, pty, Hx-spam-relays-external:gmail.com X-HELO: mail-qk0-f169.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:in-reply-to:user-agent; bh=lxR0M16aaKELATwnXGBDg0iAHLhAsb6vGjcJVxRcFnU=; b=hJROtOKPYUgMH4pd7H/wRCv1jCYAgchDCo5Q9/2To6riTNDYzR0ISWDuHcjp6ps3On n5/osvYMoZIo1J9VzW2xdOytyuRW5t4+UKavIx6x+F1JQB6NWkxNQTuIL4nzvKqPKSog PuO1BcumjGM/4SkVT2i0WK4+uPndlHCjNh6r1fgMV/9y9BfH4lja0UcyjcgjNFrbOv8P sieJao2u2D09CO2uQ18hNH+lf9eMnGLaUQnwK6arDLLIuUdf0TYJkB72ImzsO0jLv5rf C6cb8WwUiJI0uNTzTP6d3UbMe53n4SlW0lii+IqHADjNRuuVqk/Bi4k0MDkXpJmNbn8S g4HQ== X-Gm-Message-State: AKaTC03SKgdYmqFhhnp25DeiOSi0/G+aX2/JGbWPPgbBsGPi9QB3Xs39EoAt0Ty5JweDaPg8 X-Received: by 10.55.114.70 with SMTP id n67mr20708632qkc.185.1479773662639; Mon, 21 Nov 2016 16:14:22 -0800 (PST) Date: Tue, 22 Nov 2016 00:14:00 -0000 From: Christian Brauner To: libc-alpha@sourceware.org, serge@hallyn.com Cc: stgraber@ubuntu.com Subject: Re: [PATCH] linux ttyname and ttyname_r: do not return wrong results Message-ID: <20161122001421.xvh2znbmw6tyls3w@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20161108165638.21674-2-christian.brauner@canonical.com> <20161108165638.21674-1-christian.brauner@canonical.com> User-Agent: Mutt/1.6.2-neo (2016-08-21) X-SW-Source: 2016-11/txt/msg00757.txt.bz2 Hi, just a friendly check whether you'd be fine with including the patch in its current form. Christian On Tue, Nov 08, 2016 at 05:56:37PM +0100, Christian Brauner wrote: > Hi, > > I've taken over this patch from Serge who has been informed and is CCed on this > thread. There are no significant functional changes. As requested the following > this have been changed: > > - remove obsolete comment in ttyname_r.c > - move is_pty() to common header file and mark as static inline > > I hope this covers everything. :) > > Christian > > Christian Brauner (1): > linux ttyname and ttyname_r: do not return wrong results > > sysdeps/unix/sysv/linux/ttyname.c | 17 +++++++++++++---- > sysdeps/unix/sysv/linux/ttyname.h | 35 +++++++++++++++++++++++++++++++++++ > sysdeps/unix/sysv/linux/ttyname_r.c | 18 ++++++++++++++---- > 3 files changed, 62 insertions(+), 8 deletions(-) > create mode 100644 sysdeps/unix/sysv/linux/ttyname.h > > -- > 2.9.3 > On Tue, Nov 08, 2016 at 05:56:38PM +0100, Christian Brauner wrote: > If a link (say /proc/self/fd/0) pointing 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. > > Signed-off-by: Serge Hallyn > Signed-off-by: Christian Brauner > --- > Changelog: 2016-11-08 > - remove obsolete comment in ttyname_r.c > - move is_pty() to common header file and mark as static inline > --- > sysdeps/unix/sysv/linux/ttyname.c | 17 +++++++++++++---- > sysdeps/unix/sysv/linux/ttyname.h | 35 +++++++++++++++++++++++++++++++++++ > sysdeps/unix/sysv/linux/ttyname_r.c | 18 ++++++++++++++---- > 3 files changed, 62 insertions(+), 8 deletions(-) > create mode 100644 sysdeps/unix/sysv/linux/ttyname.h > > diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c > index 7a001b4..dabed22 100644 > --- a/sysdeps/unix/sysv/linux/ttyname.c > +++ b/sysdeps/unix/sysv/linux/ttyname.c > @@ -25,9 +25,12 @@ > #include > #include > #include > +#include > > #include <_itoa.h> > > +#include "ttyname.h" > + > #if 0 > /* Is this used anywhere? It is not exported. */ > char *__ttyname; > @@ -170,12 +173,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.h b/sysdeps/unix/sysv/linux/ttyname.h > new file mode 100644 > index 0000000..19af6e6 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/ttyname.h > @@ -0,0 +1,35 @@ > +/* Copyright (C) 2004-2016 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public License as > + published by the Free Software Foundation; either version 2.1 of the > + License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; see the file COPYING.LIB. If > + not, see . */ > + > +#include > +#include > +#include > +#include > + > +/* Return true if this is a UNIX98 pty device, as defined in > + linux/Documentation/devices.txt. */ > +static inline 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 > +} > + > diff --git a/sysdeps/unix/sysv/linux/ttyname_r.c b/sysdeps/unix/sysv/linux/ttyname_r.c > index d15bc74..542114b 100644 > --- a/sysdeps/unix/sysv/linux/ttyname_r.c > +++ b/sysdeps/unix/sysv/linux/ttyname_r.c > @@ -25,9 +25,12 @@ > #include > #include > #include > +#include > > #include <_itoa.h> > > +#include "ttyname.h" > + > static int getttyname_r (char *buf, size_t buflen, > dev_t mydev, ino64_t myino, int save, > int *dostat) internal_function; > @@ -152,12 +155,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 (is_pty (&st)) > + { > + __set_errno (ENODEV); > + return ENODEV; > + } > } > > /* Prepare the result buffer. */ > -- > 2.9.3 >