From 94caafa040e4b4289c968cd70d53041b1463ac4d Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Thu, 11 Mar 2021 08:21:06 -0300 Subject: io: Return EBAFD for negative file descriptor on fstat (BZ #27559) Now that fstat is implemented on top fstatat we need to handle negative inputs. The implementation now rejects AT_FDCWD, which would otherwise be accepted by the kernel. Checked on x86_64-linux-gnu and on i686-linux-gnu. --- sysdeps/unix/sysv/linux/fstat.c | 6 ++++++ sysdeps/unix/sysv/linux/fstat64.c | 12 ++++++++++++ 2 files changed, 18 insertions(+) (limited to 'sysdeps') diff --git a/sysdeps/unix/sysv/linux/fstat.c b/sysdeps/unix/sysv/linux/fstat.c index fd64362205..31a172dcc8 100644 --- a/sysdeps/unix/sysv/linux/fstat.c +++ b/sysdeps/unix/sysv/linux/fstat.c @@ -19,11 +19,17 @@ #include #include #include +#include #if !XSTAT_IS_XSTAT64 int __fstat (int fd, struct stat *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } return __fstatat (fd, "", buf, AT_EMPTY_PATH); } diff --git a/sysdeps/unix/sysv/linux/fstat64.c b/sysdeps/unix/sysv/linux/fstat64.c index 993abcb445..46de80b663 100644 --- a/sysdeps/unix/sysv/linux/fstat64.c +++ b/sysdeps/unix/sysv/linux/fstat64.c @@ -22,10 +22,16 @@ #include #include #include +#include int __fstat64_time64 (int fd, struct __stat64_t64 *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } return __fstatat64_time64 (fd, "", buf, AT_EMPTY_PATH); } #if __TIMESIZE != 64 @@ -34,6 +40,12 @@ hidden_def (__fstat64_time64) int __fstat64 (int fd, struct stat64 *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } + struct __stat64_t64 st_t64; return __fstat64_time64 (fd, &st_t64) ?: __cp_stat64_t64_stat64 (&st_t64, buf); -- cgit v1.2.3-70-g09d2