about summary refs log tree commit diff
path: root/sysdeps/unix/sysv/linux/ttyname.c
diff options
context:
space:
mode:
authorChristian Brauner <christian.brauner@canonical.com>2017-01-27 15:59:59 +0100
committerDmitry V. Levin <ldv@altlinux.org>2017-03-19 17:36:13 +0000
commit15e9a4f378c8607c2ae1aa465436af4321db0e23 (patch)
tree021ac3c62fe588a8745fcdf09d29392a837759e0 /sysdeps/unix/sysv/linux/ttyname.c
parent345118d7f5bc42c8d4c3a8ef70b9b709fa57e93d (diff)
downloadglibc-15e9a4f378c8607c2ae1aa465436af4321db0e23.tar.gz
glibc-15e9a4f378c8607c2ae1aa465436af4321db0e23.tar.xz
glibc-15e9a4f378c8607c2ae1aa465436af4321db0e23.zip
linux ttyname and ttyname_r: do not return wrong results
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 <serge@hallyn.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Diffstat (limited to 'sysdeps/unix/sysv/linux/ttyname.c')
-rw-r--r--sysdeps/unix/sysv/linux/ttyname.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c
index e2686b5a23..5909cb765f 100644
--- a/sysdeps/unix/sysv/linux/ttyname.c
+++ b/sysdeps/unix/sysv/linux/ttyname.c
@@ -28,6 +28,8 @@
 
 #include <_itoa.h>
 
+#include "ttyname.h"
+
 #if 0
 /* Is this used anywhere?  It is not exported.  */
 char *__ttyname;
@@ -170,12 +172,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))