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

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

            Bug ID: 30737
           Summary: fdopendir() is not robust - returns bogus DIR* instead
                    of flagging an error
           Product: glibc
           Version: 2.35
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: markgaleck at gmail dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

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!

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


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.

             reply	other threads:[~2023-08-08 15:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-08 15:52 markgaleck at gmail dot com [this message]
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
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@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).