From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7B9063858D33; Tue, 8 Aug 2023 20:48:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B9063858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1691527728; bh=J4GtG0QISfyRaQvNRja5+TelwdnF+AtSR4Pb9JB+4fk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=kr+/nFoqTVp8uihxvOPhCPehWKrzir1lxgyesB8BY680PXIIRTEYftgx1WMS0ch0n tw6ikhnPH3zJEaKgK6XXVFhtP4kliV0scJBigHhiEpQijDystFV2YUewsYft+OwTuQ UbpwXwTeq43Gye4GFXikC/QLeK9CbL+S81eiNUzI= From: "adhemerval.zanella at linaro dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: glibc X-Bugzilla-Component: libc X-Bugzilla-Version: 2.35 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: adhemerval.zanella at linaro dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://sourceware.org/bugzilla/show_bug.cgi?id=3D30737 Adhemerval Zanella changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |adhemerval.zanella at lina= ro dot o | |rg --- Comment #3 from Adhemerval Zanella --- (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 acce= pt > it for linking to the sample program, or to upgrade the installed glibc 2= .35 > to 2.38.=20 >=20 > 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 w= ith > glibc 2.38. > --------------------------- >=20 > Here is the SSCCE=20 > 'foobar.c' >=20 > (which I obtained by necessary modifications to the sample code in man7 > entry for fdopendir() ) : >=20 >=20 > #define _GNU_SOURCE >=20 > #include > #include > #include > #include > #include >=20 > int > main(int argc, char* argv[]) > { > DIR* d; > struct dirent* dp; > int dfd; >=20 > if ((d =3D fdopendir(open("./dir", O_RDONLY | O_PATH | O_DIRECTORY)))= =3D=3D > NULL) { > fprintf(stderr, "Cannot open directory\n"); > exit(1); > } >=20 > while ((dp =3D readdir(d)) !=3D NULL) { > if (!strcmp(dp->d_name, ".")) > continue; > if (!strcmp(dp->d_name, "..")) > continue; > printf("d_name %s\n", dp->d_name); > } > return 0; > } >=20 > ------------------------- > Here is a script to show the problem: >=20 > $ mkdir dir > $ touch dir/file > $ gcc -lc foobar.c > $ ./a.out > $ >=20 > 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). >=20 > So the DIR* returned from fdopendir() is useless, but fdopendir() returns > it, instead of NULL and setting errno to EBADF , as it should INHO >=20 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 descript= or is opened with O_PATH (it fails early and Linux fdopendir already call fcnt= l, so it is a free check). >=20 > If I remove | O_PATH from the above sample code, I get the expected resul= t: >=20 > $gcc -lc foobar.c > $ ./a.out > d_name file --=20 You are receiving this mail because: You are on the CC list for the bug.=