public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "adhemerval.zanella at linaro dot org" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug libc/30737] fdopendir() is not robust - returns bogus DIR* instead of flagging an error
Date: Tue, 08 Aug 2023 20:48:48 +0000	[thread overview]
Message-ID: <bug-30737-131-DzogGt0hHD@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-30737-131@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=30737

Adhemerval Zanella <adhemerval.zanella at linaro dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |adhemerval.zanella at linaro dot o
                   |                            |rg

--- Comment #3 from Adhemerval Zanella <adhemerval.zanella at linaro dot org> ---
(In reply to Mark Galeck from comment #0)
> First, dislclaimer: I checked this issue is present on the latest Ubuntu
> distribution, which has glibc version 2.35, not the latest stable 2.38.  I
> was able to build 2.38 locally, but I am not able to force Ubuntu to accept
> it for linking to the sample program, or to upgrade the installed glibc 2.35
> to 2.38. 
> 
> If you know how to test the sample program with 2.38, please let me know,
> and I will do so and I will remove this report if it does not reproduce with
> glibc 2.38.
> ---------------------------
> 
> Here is the SSCCE 
> 'foobar.c'
> 
> (which I obtained by necessary modifications to the sample code in man7
> entry for fdopendir() ) :
> 
> 
> #define _GNU_SOURCE
> 
> #include <stdio.h>
> #include <dirent.h>
> #include <fcntl.h>
> #include <stdlib.h>
> #include <string.h>
> 
> int
> main(int argc, char* argv[])
> {
>     DIR* d;
>     struct dirent* dp;
>     int dfd;
> 
>     if ((d = fdopendir(open("./dir", O_RDONLY | O_PATH | O_DIRECTORY))) ==
> NULL) {
>         fprintf(stderr, "Cannot open directory\n");
>         exit(1);
>     }
> 
>     while ((dp = readdir(d)) != NULL) {
>         if (!strcmp(dp->d_name, "."))
>             continue;
>         if (!strcmp(dp->d_name, ".."))
>             continue;
>         printf("d_name %s\n", dp->d_name);
>     }
>     return 0;
> }
> 
> -------------------------
> Here is a script to show the problem:
> 
> $ mkdir dir
> $ touch dir/file
> $ gcc -lc foobar.c
> $ ./a.out
> $
> 
> When called on a descriptor opened with O_PATH , fdopendir() succeeds
> happily, but then, readdir() reports nothing!

But readdir *does* report it, if you set errno as 0 prior calling, you will see
that EBADF is correctly set (from getdents64).

> 
> So the DIR* returned from fdopendir() is useless, but fdopendir() returns
> it, instead of NULL and setting errno to EBADF , as it should INHO
> 

Although POSIX defines fdopendir EBADF for file descriptors not open for
reading, O_PATH is a Linux extension, and standard-wise not failing with it is
still conformant. However, I agree this is QoI to fail if the file descriptor
is opened with O_PATH (it fails early and Linux fdopendir already call fcntl,
so it is a free check).

> 
> If I remove | O_PATH from the above sample code, I get the expected result:
> 
> $gcc -lc foobar.c
> $ ./a.out
> d_name file

-- 
You are receiving this mail because:
You are on the CC list for the bug.

  parent reply	other threads:[~2023-08-08 20:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-08 15:52 [Bug libc/30737] New: " markgaleck at gmail dot com
2023-08-08 16:26 ` [Bug libc/30737] " markgaleck at gmail dot com
2023-08-08 16:27 ` markgaleck at gmail dot com
2023-08-08 20:48 ` adhemerval.zanella at linaro dot org [this message]
2023-08-08 23:35 ` markgaleck at gmail dot com
2023-08-10 11:04 ` adhemerval.zanella at linaro dot org
2023-11-30 16:38 ` adhemerval.zanella at linaro dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-30737-131-DzogGt0hHD@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).