Skip to content

stdbuf: fix tmpdir leak - #14059

Open
Ecordonnier wants to merge 6 commits into
uutils:mainfrom
Ecordonnier:stdbuf-fix-tmpdir-leak
Open

stdbuf: fix tmpdir leak#14059
Ecordonnier wants to merge 6 commits into
uutils:mainfrom
Ecordonnier:stdbuf-fix-tmpdir-leak

Conversation

@Ecordonnier

Copy link
Copy Markdown
Collaborator

Two security/correctness issues reported in #13939:

  1. Leaked temporary directories: using exec() replaced the stdbuf process before TempDir's destructor could run, leaving one .tmp* directory per invocation in $TMPDIR forever.
    Fix: use spawn() + wait() so the parent process survives to drop the TempDir after the child exits.

  2. World-readable temporary directory: the tmpdir was created with default permissions, making libstdbuf.so writable by any user on a multi-user system (privilege-escalation risk).
    Fix: call set_permissions(0o700) immediately after creation, bypassing the umask.

Both fixes apply only when feat_external_libstdbuf is not set (i.e. the embedded .so path).

sylvestre and others added 6 commits August 20, 2026 15:39
Two security/correctness issues reported in uutils#13939:

1. Leaked temporary directories: using exec() replaced the stdbuf
   process before TempDir's destructor could run, leaving one
   .tmp* directory per invocation in $TMPDIR forever.
   Fix: use spawn() + wait() so the parent process survives to drop
   the TempDir after the child exits.

2. World-readable temporary directory: the tmpdir was created with
   default permissions, making libstdbuf.so writable by any user on
   a multi-user system (privilege-escalation risk).
   Fix: call set_permissions(0o700) immediately after creation,
   bypassing the umask.

Both fixes apply only when feat_external_libstdbuf is not set
(i.e. the embedded .so path).
Use tempfile::Builder::permissions so the directory is never world-accessible
between tempdir() and chmod, closing a TOCTOU window on permissive umasks.

Co-authored-by: Cursor <cursoragent@cursor.com>
Open the injected shared library with an explicit mode so umask 0 cannot
leave a world-writable .so in the private temp directory.

Co-authored-by: Cursor <cursoragent@cursor.com>
Point at rust-lang/cargo#8317 as the still-open request for cargo install
to support installing shared libraries, since that's the root cause of
the /tmp fallback described in the comment above.

Co-authored-by: Cursor <cursoragent@cursor.com>
PathBuf lost its import when the exec() use was removed, and the 0700
tempdir helpers were gated on the feature alone rather than on Unix.
The external-libstdbuf path no longer creates a temp directory it
immediately throws away, and waiting on the child now maps a signal
death to 128+signal so the caller still sees what exec() would have
shown it.
The Windows build never creates a temporary directory: build.rs enables
feat_external_libstdbuf off Unix and the DLL comes from the Cygwin
package. Saying spawn() is there for the TempDir destructor only
described the Unix half of a shared path.
Copilot AI lite review requested due to automatic review settings August 21, 2026 08:45
@Ecordonnier Ecordonnier changed the title Stdbuf fix tmpdir leak stdbuf: fix tmpdir leak Aug 21, 2026

Copilot AI 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@github-actions

github-actions Bot commented Aug 21, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/misc/io-errors (fails in this run but passes in the 'main' branch)
Note: The gnu test tests/basenc/bounded-memory is now being skipped but was previously passing.
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.
Congrats! The gnu test tests/cut/cut-huge-range is now passing!
Congrats! The gnu test tests/tail/tail-n0f is now passing!
Skip an intermittent issue tests/pr/bounded-memory (was skipped on 'main', now failing)

@codspeed-hq

codspeed-hq Bot commented Aug 21, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.22%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 356 untouched benchmarks
⏩ 50 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation du_summarize_balanced_tree[(5, 4, 10)] 16.2 ms 16.9 ms -4.22%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Ecordonnier:stdbuf-fix-tmpdir-leak (e00ac36) with main (df30282)

Open in CodSpeed

Footnotes

  1. 50 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@oech3

oech3 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Can we reuse a directory named libstdbuf-XXXXXX and avoid spawn instead?

Copilot AI review requested due to automatic review settings August 21, 2026 17:41

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/uu/stdbuf/src/stdbuf.rs Outdated
#[cfg(unix)]
let e = command.exec();
#[cfg(windows)]
// Spawn a child so the TempDir destructor fires in the parent, cleaning up

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.

I think this is mistake because WIndows native stdbuf depends on Cygwin's libstdbuf.dll binary given by different package.

Copilot AI review requested due to automatic review settings August 21, 2026 19:27

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants