public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Serge E. Hallyn" <serge@hallyn.com>
To: "Serge E. Hallyn" <serge@hallyn.com>,
	"Florian Weimer" <fweimer@redhat.com>,
	libc-alpha@sourceware.org,
	"Stéphane Graber" <stgraber@ubuntu.com>
Subject: Re: [PATCH] linux ttyname and ttyname_r: return link if appropriate
Date: Mon, 03 Oct 2016 06:16:00 -0000	[thread overview]
Message-ID: <20161003061602.GA5257@mail.hallyn.com> (raw)
In-Reply-To: <20160810231818.GA20183@altlinux.org>

On Thu, Aug 11, 2016 at 02:18:18AM +0300, Dmitry V. Levin wrote:
> On Wed, Aug 10, 2016 at 06:03:51PM -0500, Serge E. Hallyn wrote:
> [...]
> > But, even if we decide that part is dangerous, the part where we do not
> > return /dev/pts/N when /proc/self/fd/M is from a different devpts mount
> > than the /dev/pts/N in caller's namespace is I think very important, and
> > should at least be separately applied.
> 
> I agree.
> In that case, what should ttyname/ttyname_r set errno to? ENOTTY?

I chose ENODEV below.  I like that as it is more meaningful to uptodate
userspace.  Is it ok with you?

thanks,
-serge

From 72de5a6616dde09c2851554b917a07dd7ebc1449 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn@ubuntu.com>
Date: Fri, 15 Apr 2016 10:21:07 -0500
Subject: [PATCH 1/1] linux ttyname and ttyname_r: do not return wrong results

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   | 25 +++++++++++++++++++++++--
 sysdeps/unix/sysv/linux/ttyname_r.c | 26 ++++++++++++++++++++++++--
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c
index 7a001b4..a9e7e20 100644
--- a/sysdeps/unix/sysv/linux/ttyname.c
+++ b/sysdeps/unix/sysv/linux/ttyname.c
@@ -25,6 +25,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
+#include <sys/sysmacros.h>
 
 #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,19 @@ ttyname (int fd)
 #ifdef _STATBUF_ST_RDEV
 	  && S_ISCHR (st1.st_mode)
 	  && st1.st_rdev == st.st_rdev
-#else
+#endif
 	  && 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 (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..e4a2ac6 100644
--- a/sysdeps/unix/sysv/linux/ttyname_r.c
+++ b/sysdeps/unix/sysv/linux/ttyname_r.c
@@ -25,6 +25,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
+#include <sys/sysmacros.h>
 
 #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,20 @@ __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
+#endif
 	  && 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))
+	{
+	  __set_errno (ENODEV);
+	  return ENODEV;
+	}
     }
 
   /* Prepare the result buffer.  */
-- 
2.7.4

  parent reply	other threads:[~2016-10-03  6:16 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-15 15:29 [PATCH 1/1] linux ttyname: " Serge Hallyn
2016-04-15 16:27 ` Florian Weimer
2016-04-15 16:47   ` Serge Hallyn
2016-04-15 17:06     ` Florian Weimer
2016-04-15 17:43       ` Serge Hallyn
2016-04-15 18:48         ` Serge Hallyn
2016-04-15 19:59           ` Mike Frysinger
2016-04-18 19:53             ` [PATCH 1/2] " Serge Hallyn
2016-04-18 20:02               ` Mike Frysinger
2016-04-18 20:23                 ` Serge Hallyn
2016-04-20  2:10                   ` [PATCH] linux ttyname and ttyname_r: " Serge Hallyn
2016-04-20  2:28                     ` Mike Frysinger
2016-04-20 18:51                       ` Serge Hallyn
2016-07-27 13:43                         ` Serge E. Hallyn
2016-07-27 17:28                         ` Dmitry V. Levin
2016-08-06  2:09                           ` Serge E. Hallyn
2016-08-06  8:46                             ` Mike Frysinger
2016-08-06 15:00                               ` Serge E. Hallyn
2016-08-09 20:41                                 ` Serge E. Hallyn
2016-08-09 21:18                                 ` Dmitry V. Levin
2016-08-09 21:39                                   ` Serge E. Hallyn
2016-08-09 23:26                                     ` Dmitry V. Levin
2016-08-10 23:24                                       ` Serge E. Hallyn
2016-08-10 23:48                                         ` Dmitry V. Levin
2016-08-10  6:38                                     ` Florian Weimer
2016-08-10 23:03                                       ` Serge E. Hallyn
2016-08-10 23:18                                         ` Dmitry V. Levin
2016-08-10 23:26                                           ` Serge E. Hallyn
2016-10-03  6:16                                           ` Serge E. Hallyn [this message]
2016-10-03  7:29                                             ` Andreas Schwab
2016-10-03 14:05                                               ` [PATCH 1/1] linux ttyname and ttyname_r: do not return wrong results Serge E. Hallyn
2016-10-04  9:53                                             ` [PATCH] linux ttyname and ttyname_r: return link if appropriate Florian Weimer
2016-10-04 12:47                                               ` Serge E. Hallyn
2016-04-18 19:53             ` [PATCH 1/1] linux ttyname: " Serge Hallyn
2016-04-18 19:54             ` [PATCH 2/2] linux ttyname_r: " Serge Hallyn
2016-04-18 20:03               ` Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161003061602.GA5257@mail.hallyn.com \
    --to=serge@hallyn.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=stgraber@ubuntu.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).