Repository navigation
Thread creation while a directory handle is open does a fchdir, affecting other threads (race condition) #23010
Description
Activity
I had copied an incorrect version of the script. This is now the correct one.
Note that as I said in a comment in the Debian bug, if I change the script to do
opendir DIR, $dir or die "$0: opendir failed ($!)\n"; my @files = readdir DIR; foreach my $file (@files) { $nthreads < $maxthreads or join_threads; $nthreads++ < $maxthreads or die "$0: internal error\n"; threads->create(\&stat_test, $file); } closedir DIR or die "$0: closedir failed ($!)\n";
then the failures still occur. But if I change it to do
opendir DIR, $dir or die "$0: opendir failed ($!)\n"; my @files = readdir DIR; closedir DIR or die "$0: closedir failed ($!)\n"; foreach my $file (@files) { $nthreads < $maxthreads or join_threads; $nthreads++ < $maxthreads or die "$0: internal error\n"; threads->create(\&stat_test, $file); }
(i.e. moving the
closedirbefore the loop), then the failures no longer occur (and thestraceoutput shows that thefchdiris no longer done). So the failures occur only when the directory handle is open, explaining what I wrote above.I think that this bug comes from the fix of #10387 (old rt.perl.org bug 75174), 11a11ec, which does a fchdir in Perl_dirp_dup from sv.c,
Yeah. There's a real bug there that they were fixing, but this feels like the wrong solution. It needs a new
DIR*structure but not a new handle. Using it from two threads would be garbage but so is using it from a forked process so that's nothing new. The important thing is that it doesn't crash.Reacted by Dave VasilevskyAs the current working directory is global to the process, this affects other threads. Even though the current working directory is set back to the old value, this is a race condition, which can affect real scripts (this is how I identified this bug).
Well, as part of this bug, there is a second bug: in
Perl_dirp_dup, after the firstfchdir, if thePerlDir_openfails for any reason (for instance, after it has been opened, the directory has been set tod--x--x--xpermissions (111 in octal), which allows one to chdir to this directory but not to open it again), then the current working directory is not set back to the old value, so that there isn't even a race condition in such a case. This is unusual, but could be used as an attack against some scripts.There are other issues with the current
Perl_dirp_dupcode:PerlDir_open(".")may fail for good reasons (the current directory needs to be executable, but not necessarily readable), and the secondfchdirmay also fail.Crossreference to the oss-security post from @vinc17fr: https://www.openwall.com/lists/oss-security/2025/05/22/2
CVE-2025-40909 was assigned for this issue according to https://www.openwall.com/lists/oss-security/2025/05/23/1
As of 5.41.13, the
fchdir-based code path no longer exists inPerl_dirp_dup. Thanks for bringing this issue to our attention.- added a commit that references this issue
on May 29, 2025 I have pushed a squashed version of the fix to the CVE-2025-40909 branch (currently 918bfff) for anyone
who wants to backport it.- added a commit that references this issue
on May 11, 2026 - added a commit that references this issue
on May 14, 2026
When a thread is created while a directory handle is open, a
fchdiris done (according tostraceoutput under GNU Linux), affecting other threads. The issue can be reproduced on various platforms (GNU Linux, Android, macOS) with the following testcase, wherestatrandomly fails with a "No such file or directory" error (ENOENT):In short, for each file in the directory, a thread is created, which does 100
staton the file. This script uses no more than 2 worker threads at the same time by default: the main loop waits for a worker thread to terminate before a new one is created.To reproduce the issue more easily, create a directory with many files:
Then run this script with the directory name (e.g.
test) in argument. Failures are much more likely to occur withstrace -f(as usual, also use the-ooption to redirect the output to a file).In the
straceoutput under GNU Linux, one can see for instance:This excerpt shows 3
newfstataton the same filetest/275. The first two succeeded, but the third one failed withENOENT(No such file or directory).I think that this bug comes from the fix of #10387 (old rt.perl.org bug 75174), 11a11ec, which does a
fchdirinPerl_dirp_dupfromsv.c, so Perl versions since 2010 should be affected, and this issue is still present in the repository. As the current working directory is global to the process, this affects other threads. Even though the current working directory is set back to the old value, this is a race condition, which can affect real scripts (this is how I identified this bug). Note that this is a vulnerability as the directory may be an untrusted one, so really bad things could happen (even when the directory is trusted, BTW).I could reproduce the issue under GNU Linux with several file systems (ext4, tmpfs, NFS), and also on my Android phone (using Termux) and on a macOS machine (from the cfarm project).
Past bug reports: