Fresh pull from upstream
[librecmc/librecmc.git] / toolchain / musl / patches / 020-verify-that-ttyname-refers-to-the-same-file-as-the-fd.patch
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
5
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.
9 ---
10  src/unistd/ttyname_r.c | 15 +++++++++++----
11  1 file changed, 11 insertions(+), 4 deletions(-)
12
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
17 @@ -1,10 +1,12 @@
18  #include <unistd.h>
19  #include <errno.h>
20 +#include <sys/stat.h>
21  
22  void __procfdname(char *, unsigned);
23  
24  int ttyname_r(int fd, char *name, size_t size)
25  {
26 +       struct stat st1, st2;
27         char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2];
28         ssize_t l;
29  
30 @@ -15,8 +17,13 @@ int ttyname_r(int fd, char *name, size_t size)
31  
32         if (l < 0) return errno;
33         else if (l == size) return ERANGE;
34 -       else {
35 -               name[l] = 0;
36 -               return 0;
37 -       }
38 +
39 +       name[l] = 0;
40 +
41 +       if (stat(name, &st1) || fstat(fd, &st2))
42 +               return errno;
43 +       if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
44 +               return ENOENT;
45 +
46 +       return 0;
47  }
48 -- 
49 cgit v0.11.2