From 4f506bb95ad4bc55e6174cef98803802a35b050e Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 21 Jun 2019 12:09:36 +0200 Subject: fix opendir, fpathconf and ttyname_r to use fstat64(), not fstat() There is no opendir64(), thus even programs built for 64-bit off_t use opendir(). Before this change, internally opendir() uses fstat(), with the following breakage if some of struct stat fields are too narrow: $ strace ls -l execve("/busybox/ls", ["ls", "-l"], 0x7ffcdc43ede8 /* 16 vars */) = 0 ioctl(0, TCGETS, {B38400 opost isig icanon echo ...}) = 0 ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0 getuid32() = 0 time([1551486393 /* 2019-03-02T00:26:33+0000 */]) = 1551486393 (2019-03-02T00:26:33+0000) ioctl(0, TIOCGWINSZ, {ws_row=38, ws_col=120, ws_xpixel=0, ws_ypixel=0}) = 0 ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0 brk(NULL) = 0x9768000 brk(0x9769000) = 0x9769000 lstat64(".", 0xffa6e374) = 0 open(".", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3 fstat(3, 0xffa6e378) = -1 EOVERFLOW (Value too large for defined data type) See https://bugs.busybox.net/show_bug.cgi?id=11651 Signed-off-by: Denys Vlasenko --- libc/termios/ttyname.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'libc/termios/ttyname.c') diff --git a/libc/termios/ttyname.c b/libc/termios/ttyname.c index 5fcf23b64..9cd281be8 100644 --- a/libc/termios/ttyname.c +++ b/libc/termios/ttyname.c @@ -31,6 +31,9 @@ #include #include +#define STAT stat64 +#define FSTAT fstat64 +#define LSTAT lstat64 #define TTYNAME_BUFLEN 32 @@ -45,8 +48,8 @@ static const char dirlist[] = int ttyname_r(int fd, char *ubuf, size_t ubuflen) { struct dirent *d; - struct stat st; - struct stat dst; + struct STAT st; + struct STAT dst; const char *p; char *s; DIR *fp; @@ -54,7 +57,7 @@ int ttyname_r(int fd, char *ubuf, size_t ubuflen) size_t len; char buf[TTYNAME_BUFLEN]; - if (fstat(fd, &st) < 0) { + if (FSTAT(fd, &st) < 0) { return errno; } @@ -86,7 +89,7 @@ int ttyname_r(int fd, char *ubuf, size_t ubuflen) strcpy(s, d->d_name); - if ((lstat(buf, &dst) == 0) + if ((LSTAT(buf, &dst) == 0) #if 0 /* Stupid filesystems like cramfs fail to guarantee that * st_ino and st_dev uniquely identify a file, contrary to -- cgit v1.2.3