From a09dfc19edcbac3f96d5410529b724db0a583879 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 15 Nov 2017 20:36:44 +0100 Subject: linux ttyname{_r}: Don't bail prematurely [BZ #22145] Commit 15e9a4f378c8607c2ae1aa465436af4321db0e23 introduced logic for ttyname() sending back ENODEV to signal that we can't get a name for the TTY because we inherited it from a different mount namespace. However, just because we inherited it from a different mount namespace and it isn't available at its original path, doesn't mean that its name is unknowable; we can still try to find it by allowing the normal fall back on iterating through devices. An example scenario where this happens is with "/dev/console" in containers. It's a common practice among container managers to allocate a PTY master/slave pair in the host's mount namespace (the slave having a path like "/dev/pty/$X"), bind mount the slave to "/dev/console" in the container's mount namespace, and send the slave FD to a process in the container. Inside of the container, the slave-end isn't available at its original path ("/dev/pts/$X"), since the container mount namespace has a separate devpts instance from the host (that path may or may not exist in the container; if it does exist, it's not the same PTY slave device). Currently ttyname{_r} sees that the file at the original "/dev/pts/$X" path doesn't match the FD passed to it, and fails early and gives up, even though if it kept searching it would find the TTY at "/dev/console". Fix that; don't have the ENODEV path force an early return inhibiting the fall-back search. This change is based on the previous patch that adds use of is_mytty in getttyname and getttyname_r. Without that change, this effectively reverts 15e9a4f, which made us disregard the false similarity of file pointed to by "/proc/self/fd/$Y", because if it doesn't bail prematurely then that file ("/dev/pts/$X") will just come up again anyway in the fall-back search. Reviewed-by: Christian Brauner --- sysdeps/unix/sysv/linux/ttyname.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'sysdeps/unix/sysv/linux/ttyname.c') diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c index 6e97d2d..f4c955f 100644 --- a/sysdeps/unix/sysv/linux/ttyname.c +++ b/sysdeps/unix/sysv/linux/ttyname.c @@ -115,6 +115,7 @@ ttyname (int fd) char procname[30]; struct stat64 st, st1; int dostat = 0; + int doispty = 0; char *name; int save = errno; struct termios term; @@ -165,13 +166,7 @@ ttyname (int fd) && is_mytty (&st, &st1)) 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; - } + doispty = 1; } if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode)) @@ -195,5 +190,15 @@ ttyname (int fd) name = getttyname ("/dev", &st, save, &dostat); } + if (!name && doispty && is_pty (&st)) + { + /* We failed to figure out the TTY's name, but we can at least + signal that we did verify that it really is a PTY slave. + This happens when we have inherited the file descriptor from + a different mount namespace. */ + __set_errno (ENODEV); + return NULL; + } + return name; } -- cgit v1.1