diff options
| author | Thomas Weißschuh <linux@weissschuh.net> | 2026-08-31 18:04:59 +0200 |
|---|---|---|
| committer | Thomas Weißschuh <linux@weissschuh.net> | 2026-08-31 22:51:06 +0200 |
| commit | 9e697dcb8cc01f708cacc4ef3d8b6fb8a67dcd54 (patch) | |
| tree | 032b54d69102ce7a2ac64a1103a0b8bb27582499 | |
| parent | 9c47af906bc655c8f45aaf1f156656239c2c5073 (diff) | |
| download | linux-next-9e697dcb8cc01f708cacc4ef3d8b6fb8a67dcd54.tar.gz linux-next-9e697dcb8cc01f708cacc4ef3d8b6fb8a67dcd54.zip | |
tools/nolibc: verify that a directory is opened
If a non-directory is opened, ENODIR should be returned from
opendir()/fdopendir() right away and not only during readdir_r().
Validate the type of opened file during open.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Reviewed-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260831-nolibc-fdopendir-enotdir-v1-1-8cf0e79c4e6f@weissschuh.net
| -rw-r--r-- | tools/include/nolibc/dirent.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h index 4e02ef25e72d..25fbff208998 100644 --- a/tools/include/nolibc/dirent.h +++ b/tools/include/nolibc/dirent.h @@ -30,10 +30,23 @@ typedef struct { static __attribute__((unused)) DIR *fdopendir(int fd) { + struct stat buf; + int ret; + if (fd < 0) { SET_ERRNO(EBADF); return NULL; } + + ret = fstat(fd, &buf); + if (ret < 0) + return NULL; + + if (!S_ISDIR(buf.st_mode)) { + SET_ERRNO(ENOTDIR); + return NULL; + } + return (DIR *)(intptr_t)~fd; } |
