Re: [PATCH 3/3] tools/nolibc: validate directory with O_DIRECTORY in opendir()

From: David Laight

Date: Tue Sep 01 2026 - 04:57:16 EST


On Mon, 31 Aug 2026 18:05:01 +0200
Thomas Weißschuh <linux@xxxxxxxxxxxxxx> wrote:

> fdopendir() requires a call to fstat() to determine if the opened file
> is a directory. Currently opendir() inherits this extra syscall.
>
> Switch to O_DIRECTORY and remove the call to fdopendir() in opendir()
> to make the directory type check cheaper.

This should probably be the first patch.
With the changed fdopendir() it leaks an fd on error.
It also fixes the errno return.

David

>
> Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
> ---
> tools/include/nolibc/dirent.h | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h
> index 25fbff208998..2dbf4052b85a 100644
> --- a/tools/include/nolibc/dirent.h
> +++ b/tools/include/nolibc/dirent.h
> @@ -55,10 +55,11 @@ DIR *opendir(const char *name)
> {
> int fd;
>
> - fd = open(name, O_RDONLY);
> + fd = open(name, O_RDONLY | O_DIRECTORY);
> if (fd == -1)
> return NULL;
> - return fdopendir(fd);
> +
> + return (DIR *)(intptr_t)~fd;
> }
>
> static __attribute__((unused))
>