stdbuf: keep the preload path out of reach of other users #16817
Can we simplify this like libstdbuf path contains insecure letter: *?
this should be geteuid(), not the owner of dir.
if the owner of $TMPDIR swaps dir for one of their own before this runs, both uids match and the check passes
please use translate!()
what happens on Android? /data/local/tmp is 0771 and there is no /tmp, so i think stdbuf always fails there now
could you please make this comment shorter? 2 lines should be enough
Android / Test builds (API 28, x86_64) (pull_request) failed:
thread 'test_stdbuf::test_stdbuf_unbuffered_stdout' (6329) panicked at tests/by-util/test_stdbuf.rs:221:10:
Command was expected to succeed. code: 125
Thanks for the review. Addressed in cd34d4fb9:
geteuid(), so a directory swapped in by the owner of $TMPDIR fails the check./tmp does not exist and every writable directory sits below /data, which belongs to system, so the check could never pass there. On Android stdbuf now uses $TMPDIR as before this PR (canonicalized, still with the separator check). That is what fixes test_stdbuf_unbuffered_stdout on API 28. I could not run the Android job locally; I type-checked that branch for x86_64-linux-android.translate!() (en-US and fr-FR).libstdbuf path contains an insecure character: <path>, with the TMPDIR hint kept on the second line.Tested on Linux (glibc 2.41, non-root uid): all 22 test_stdbuf tests pass, and the 11 that run on macOS pass too.
Alphabetize please
Done in 65455102c, thanks.
please squash the fixup
<!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->
Merging this PR will improve performance by ×3.5
<details>
<summary><b>3 warnings, 1 note</b></summary>
:warning: 122 benchmarks are dominated by system calls
> System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.
> Learn more about system calls
> See affected benchmarks
:warning: 2 benchmarks with a significant change ran on different hardware
> Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.
> Learn more about Macro Runners
> See affected benchmarks
:warning: 2 benchmarks with a significant change ran in a different software environment
> Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.
> See affected benchmarks
:information_source: 1 benchmark has no flamegraph
> The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.
> See affected benchmarks
</details>
⚡ 2 improved benchmarks ✅ 421 untouched benchmarks ⏩ 78 skipped benchmarks[^skipped]
Performance Changes
| | Mode | Benchmark | BASE | HEAD | Efficiency |
| --- | ---- | --------- | ------ | ------ | ---------- |
| ⚡ | Memory | [` socket_addrs_direct[100000] ](https://app.codspeed.io/uutils/coreutils/branches/Socialpranker%3Astdbuf-private-preload-dir?uri=src%2Fuu%2Fhostname%2Fbenches%2Fhostname_bench.rs%3A%3Asocket_addrs_direct%255B100000%255D&runnerMode=Memory&utm_source=github&utm_medium=comment-v2&utm_content=benchmark) | 68 KB | 8.6 KB | ×7.9 |
| ⚡ | Memory | [ hostname_ip_lookup[100000] ](https://app.codspeed.io/uutils/coreutils/branches/Socialpranker%3Astdbuf-private-preload-dir?uri=src%2Fuu%2Fhostname%2Fbenches%2Fhostname_bench.rs%3A%3Ahostname_ip_lookup%255B100000%255D&runnerMode=Memory&utm_source=github&utm_medium=comment-v2&utm_content=benchmark) | 160.3 KB | 100.9 KB | +58.88% |
[!TIP]
Curious why performance improved? Comment @codspeedbot explain why performance improved` on this PR, or directly use the CodSpeed MCP with your agent.
---
<sub>Comparing <code>Socialpranker:stdbuf-private-preload-dir</code> (6545510) with <code>main</code> (acd07b1)[^unexpected-base]</sub>
<a href="https://app.codspeed.io/uutils/coreutils/branches/Socialpranker%3Astdbuf-private-preload-dir?utm_source=github&utm_medium=comment-v2&utm_content=button">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://codspeed.io/pr-report/open-in-codspeed-dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://codspeed.io/pr-report/open-in-codspeed-light.svg">
<img alt="Open in CodSpeed" src="https://codspeed.io/pr-report/open-in-codspeed-light.svg" width="169" height="32">
</picture>
</a>
[^skipped]: 78 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.
[^unexpected-base]: No successful run was found on <code>main</code> (1129d00) during the generation of this report, so acd07b1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.
oech3
Socialpranker
The embedded libstdbuf is extracted below
$TMPDIRand its path is put intoLD_PRELOAD. #13622 refuses a:in that path, but two ways around it are left (both came up in #15268):LD_PRELOADon spaces, soTMPDIR='/x/evil.so /../../tmp'makes the command preload/x/evil.so;$TMPDIRcan swap the freshly created directory for their own before the command is executed, with no special character in the path.As in #13622, this matters only when
TMPDIRcrosses a privilege boundary, and only for builds that embed libstdbuf.What changes: the directory is created in
$TMPDIRonly if nobody but root and the current user can rename it or one of its ancestors, otherwise in/tmp. If/tmpdoes not qualify either, stdbuf exits with an error. The path is canonicalized first, and whitespace is refused next to:.I kept a real path instead of memfd because of the problems with child processes found in #15268.
One behavior change: in a user namespace where
/belongs to an unmapped uid, the embedded build now fails instead of running.Tested on Linux with glibc. With a process racing to swap the directory, the foreign library was loaded in 25 of 30 runs before the change and in 0 of 30 after.
The patch was written with AI assistance and tested as described above.