1 From 0a950dcf15bb9f7274c804dca490e9e20e475f3e Mon Sep 17 00:00:00 2001
2 From: Szabolcs Nagy <nsz@port70.net>
3 Date: Sat, 20 Aug 2016 21:04:31 +0200
4 Subject: verify that ttyname refers to the same file as the fd
6 linux containers use separate mount namespace so the /proc
7 symlink might not point to the right device if the fd was
8 opened in the parent namespace, in this case return ENOENT.
10 src/unistd/ttyname_r.c | 15 +++++++++++----
11 1 file changed, 11 insertions(+), 4 deletions(-)
13 diff --git a/src/unistd/ttyname_r.c b/src/unistd/ttyname_r.c
14 index 8bac7b2..a38ba4f 100644
15 --- a/src/unistd/ttyname_r.c
16 +++ b/src/unistd/ttyname_r.c
20 +#include <sys/stat.h>
22 void __procfdname(char *, unsigned);
24 int ttyname_r(int fd, char *name, size_t size)
26 + struct stat st1, st2;
27 char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2];
30 @@ -15,8 +17,13 @@ int ttyname_r(int fd, char *name, size_t size)
32 if (l < 0) return errno;
33 else if (l == size) return ERANGE;
41 + if (stat(name, &st1) || fstat(fd, &st2))
43 + if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)