Skip to content

util: Make files opened with fdbridge::fopen noninheritable - #36677

Open
SirECMG wants to merge 2 commits into
bitcoin:masterfrom
SirECMG:u/sirecmg/36668-fopen-inheritable-fix
Open

SirECMG wants to merge 2 commits into
bitcoin:masterfrom
SirECMG:u/sirecmg/36668-fopen-inheritable-fix

Conversation

@SirECMG

@SirECMG SirECMG commented Oct 27, 2026 •

Copy link
Copy Markdown

Fixes #36668

Use the 'e' mode on POSIX to set close-on-exec and the 'N' mode on windows to prevent file handle inheritance by child processes.

This PR is still in WIP... until I add some test cases and verify that it works on Windows and Mac. I've only tested it on Linux (OpenSUSE Tumbleweed)

@DrahtBot

DrahtBot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

No conflicts as of last run.

Comment thread src/util/fs.cpp Outdated
{
#ifndef WIN32
return ::fopen(p.c_str(), mode);
const std::string safe_mode(std::string{mode} + "e");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "e" available on all non-Windows systems?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep the original e approach on Linux and handle macOS separately with open using O_CLOEXEC, followed by fdopen?

@SirECMG SirECMG Oct 28, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task macOS native: https://github.com/bitcoin/bitcoin/actions/runs/40583887664/job/122476509348
LLM reason (✨ experimental): CI failed because fs_tests/fsbridge_fopen_noninheritable failed its check that the file descriptor has the close-on-exec flag set.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

Comment thread src/util/fs.cpp Outdated
#ifndef WIN32
return ::fopen(p.c_str(), mode);
FILE *fp = ::fopen(p.c_str(), mode);
if(fp && fcntl(::fileno(fp), F_SETFD, FD_CLOEXEC) == -1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the consecutive fopen and fcntl(..., FD_CLOEXEC) calls are not atomic, which is a race in a multithreaded process.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I will try to find a solution on how I can make it safe under multi threaded processes.

@ValeraFinebits

Copy link
Copy Markdown

I tested fsbridge::fopen at ca864d2fc1 with a standalone MSVC test using fs.cpp. The file was opened in append mode ("a"), and handle inheritance was enabled in CreateProcessW. After the parent closed the file, DeleteFileW failed with ERROR_SHARING_VIOLATION on the pre-PR code. With this change, deletion succeeded while the child was still alive.

The compilation errors in fs_tests.cpp prevent test_bitcoin from building on Windows.

@SirECMG
SirECMG force-pushed the u/sirecmg/36668-fopen-inheritable-fix branch from bb15d68 to 9ebc30b Compare October 28, 2026 18:35

@ValeraFinebits ValeraFinebits left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typos

Comment thread src/util/fs.cpp Outdated
flags |= read_write ? O_RDWR : O_RDONLY;
break;
case 'w':
flags |= (read_write ? ORDWR : O_WRONLY) | O_CREAT | O_TRUNC;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flags |= (read_write ? ORDWR : O_WRONLY) | O_CREAT | O_TRUNC;
flags |= (read_write ? O_RDWR : O_WRONLY) | O_CREAT | O_TRUNC;

Comment thread src/util/fs.cpp Outdated

// Match fopen's initial position for append.
if(mode[0] == 'a') {
(void)::lseek(fd, 0, SEED_END);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(void)::lseek(fd, 0, SEED_END);
(void)::lseek(fd, 0, SEEK_END);

@SirECMG
SirECMG force-pushed the u/sirecmg/36668-fopen-inheritable-fix branch 2 times, most recently from f2eac03 to 30c974f Compare October 28, 2026 18:42
@SirECMG

SirECMG commented Oct 28, 2026

Copy link
Copy Markdown
Author

Thanks @ValeraFinebits. I fixed the typos. Let me know what you think.

@ValeraFinebits

ValeraFinebits commented Oct 28, 2026 •

Copy link
Copy Markdown

Thanks @ValeraFinebits. I fixed the typos. Let me know what you think.

Could we also add <cstring> for std::strchr?

@SirECMG
SirECMG force-pushed the u/sirecmg/36668-fopen-inheritable-fix branch 3 times, most recently from e423eb6 to 55f35a2 Compare October 28, 2026 19:05
@SirECMG

SirECMG commented Oct 28, 2026 •

Copy link
Copy Markdown
Author

Added the missing include!

@winterrdog winterrdog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please squash your commits into two: one for the unit test and one for the fix ?

see: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits

@ValeraFinebits

Copy link
Copy Markdown

Tested on Windows at 0c9ab56: the new test passes with the fix and fails on the HANDLE_FLAG_INHERIT check when I revert the Windows change in fs.cpp. Restoring the fix makes it pass again.

@SirECMG
SirECMG force-pushed the u/sirecmg/36668-fopen-inheritable-fix branch from 0c9ab56 to 6a1ff77 Compare October 28, 2026 21:25
@SirECMG

SirECMG commented Oct 28, 2026

Copy link
Copy Markdown
Author

could you please squash your commits into two: one for the unit test and one for the fix ?

see: https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits

I performed a squash to clean up the git history, as asked.

@SirECMG
SirECMG force-pushed the u/sirecmg/36668-fopen-inheritable-fix branch from 6a1ff77 to 17ea8dd Compare October 29, 2026 17:42
@winterrdog

Copy link
Copy Markdown
Contributor

seems like this fixes #36668. you can add Fixes #36668 to the PR description to have it auto-close on merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util: Make files opened via fsbridge::fopen() not inheritable by child processes

5 participants