From 4e16d89866e660426438238a47c2345bdc47dd97 Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Thu, 10 Aug 2023 08:56:00 -0300 Subject: [PATCH] linux: Make fdopendir fail with O_PATH (BZ 30373) It is not strictly required by the POSIX, since O_PATH is a Linux extension, but it is QoI to fail early instead of at readdir. Also the check is free, since fdopendir already checks if the file descriptor is opened for read. Checked on x86_64-linux-gnu. --- sysdeps/unix/sysv/linux/Makefile | 1 + sysdeps/unix/sysv/linux/fdopendir.c | 8 ++- .../linux/{fdopendir.c => tst-fdopendir-o_path.c} | 58 ++++++++++------------ 3 files changed, 35 insertions(+), 32 deletions(-) copy sysdeps/unix/sysv/linux/{fdopendir.c => tst-fdopendir-o_path.c} (52%) diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 250df6f455..415aa1f14d 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -201,6 +201,7 @@ tests += \ tst-clone3 \ tst-epoll \ tst-fanotify \ + tst-fdopendir-o_path \ tst-getauxval \ tst-gettid \ tst-gettid-kill \ diff --git a/sysdeps/unix/sysv/linux/fdopendir.c b/sysdeps/unix/sysv/linux/fdopendir.c index 861345396c..707fb5960e 100644 --- a/sysdeps/unix/sysv/linux/fdopendir.c +++ b/sysdeps/unix/sysv/linux/fdopendir.c @@ -37,10 +37,16 @@ __fdopendir (int fd) return NULL; } - /* Make sure the descriptor allows for reading. */ int flags = __fcntl64_nocancel (fd, F_GETFL); if (__glibc_unlikely (flags == -1)) return NULL; + /* Fail early for descriptors opened with O_PATH. */ + if (__glibc_unlikely (flags & O_PATH)) + { + __set_errno (EBADF); + return NULL; + } + /* Make sure the descriptor allows for reading. */ if (__glibc_unlikely ((flags & O_ACCMODE) == O_WRONLY)) { __set_errno (EINVAL); diff --git a/sysdeps/unix/sysv/linux/fdopendir.c b/sysdeps/unix/sysv/linux/tst-fdopendir-o_path.c similarity index 52% copy from sysdeps/unix/sysv/linux/fdopendir.c copy to sysdeps/unix/sysv/linux/tst-fdopendir-o_path.c index 861345396c..2531cf8ddb 100644 --- a/sysdeps/unix/sysv/linux/fdopendir.c +++ b/sysdeps/unix/sysv/linux/tst-fdopendir-o_path.c @@ -1,4 +1,5 @@ -/* Copyright (C) 2005-2023 Free Software Foundation, Inc. +/* Check if fdopendir fails with file descriptor opened with O_PATH (BZ 30737) + Copyright (C) 2023 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -15,38 +16,33 @@ License along with the GNU C Library; if not, see . */ -#include #include #include -#include -#include - -#include - +#include +#include +#include +#include -DIR * -__fdopendir (int fd) +static int +do_test (void) { - struct __stat64_t64 statbuf; - - if (__glibc_unlikely (__fstat64_time64 (fd, &statbuf) < 0)) - return NULL; - if (__glibc_unlikely (! S_ISDIR (statbuf.st_mode))) - { - __set_errno (ENOTDIR); - return NULL; - } - - /* Make sure the descriptor allows for reading. */ - int flags = __fcntl64_nocancel (fd, F_GETFL); - if (__glibc_unlikely (flags == -1)) - return NULL; - if (__glibc_unlikely ((flags & O_ACCMODE) == O_WRONLY)) - { - __set_errno (EINVAL); - return NULL; - } - - return __alloc_dir (fd, false, flags, &statbuf); + char *dirname = support_create_temp_directory ("tst-fdopendir-o_path"); + + { + int fd = xopen (dirname, O_RDONLY | O_DIRECTORY, 0600); + DIR *dir = fdopendir (fd); + TEST_VERIFY_EXIT (dir != NULL); + closedir (dir); + } + + { + int fd = xopen (dirname, O_RDONLY | O_PATH | O_DIRECTORY, 0600); + TEST_VERIFY (fdopendir (fd) == NULL); + TEST_COMPARE (errno, EBADF); + xclose (fd); + } + + return 0; } -weak_alias (__fdopendir, fdopendir) + +#include -- 2.11.4.GIT