fix(process): skip PATH entries a spawned search cannot hold

c1d66e52f ended the search at any error a spawned search does not move
past, including ENAMETOOLONG. But glibc's execvp on Linux and libuv's
own search on macOS skip a PATH entry of at least PATH_MAX bytes (4,096
and 1,024) before trying it, so with a 5,000-byte entry first, a launch
by name ran /usr/bin/true while the lookup reported it missing, and
capture_process_scenario rejected a command that worked. Such entries
are now skipped before they are checked; a shorter entry with an
over-long component still stops the search with ENAMETOOLONG, as a
launch by name does. On Linux the search also moves past ESTALE, ENODEV
and ETIMEDOUT, as glibc's execvp does.
This commit is contained in:
Entrpi committed 2026-11-01 17:35:27 +11:00
1 parent 85eb3b6c08
commit 3e32a44220
2 files changed
+49 -3

No files matched your search

+23 -3
View File
@@ -43,8 +43,10 @@ export interface ExecutableLookupBounds {
* contains a path separator is the selected path; PATH search continues past
* a missing file, a non-directory and a denied directory, and an existing file
* without execute permission is not a match. Any other error, such as a
* symlink loop, ends the search, as it ends a spawned search. The working
* directory is read only when the search reaches a
* symlink loop, ends the search, as it ends a spawned search, while an entry
* too long for the native path buffer is skipped before it is checked, as a
* spawned search skips it. The working directory is read only when the search
* reaches a
* relative command or PATH entry, since it may have been removed. When that
* read fails, a relative PATH entry is checked as written, against the
* process's working directory, which the kernel still resolves (even `..`
@@ -84,6 +86,11 @@ export const lookupExecutable = async (
try {
const checked: string[] = [];
for (const entry of entries) {
const entryBytes = Buffer.byteLength(entry);
if (!explicit && entryBytes >= PATH_MAX) {
checked.push(`a PATH entry of ${entryBytes} bytes (skipped)`);
continue;
}
// Resolved only when reached: a match in an earlier entry never needs
// the working directory that a later relative entry would read.
let candidate: string;
@@ -188,7 +195,20 @@ const lookupInterruption = (
* a symlink loop or an I/O error, so a later candidate would not run.
*/
const searchMovesPast = (code: string | undefined): boolean =>
code === "ENOENT" || code === "ENOTDIR" || code === "EACCES";
code === "ENOENT" ||
code === "ENOTDIR" ||
code === "EACCES" ||
// glibc's execvp, which a spawned search uses on Linux, also moves past
// these; libuv's own search on macOS does not.
(process.platform === "linux" &&
(code === "ESTALE" || code === "ENODEV" || code === "ETIMEDOUT"));
/**
* A spawned search skips a PATH entry of at least PATH_MAX bytes, which its
* path buffer cannot hold (glibc's execvp on Linux, libuv on macOS); a
* shorter entry with an over-long component still fails with ENAMETOOLONG.
*/
const PATH_MAX = process.platform === "linux" ? 4096 : 1024;
const systemErrorCode = (cause: unknown): string | undefined =>
typeof cause === "object" &&
@@ -446,6 +446,32 @@ posix(
},
);
posix(
"skips a PATH entry too long for a spawned search's path buffer, but stops at an over-long component",
async () => {
const { executable } = await rizinExecutableFixture();
const directory = dirname(executable);
const cwd = () => process.cwd();
// Longer than PATH_MAX on Linux (4,096 bytes) and macOS (1,024 bytes).
const longEntry = `/${"a".repeat(4_999)}`;
await expect(
lookupExecutable("rizin", cwd, `${longEntry}${delimiter}${directory}`),
).resolves.toEqual({ status: "found", path: executable });
// Within PATH_MAX, a 300-byte component fails as a launch by name does.
const longComponent = `/${"a".repeat(300)}`;
await expect(
lookupExecutable(
"rizin",
cwd,
`${longComponent}${delimiter}${directory}`,
),
).resolves.toEqual({
status: "missing",
checked: [`${longComponent}/rizin (ENAMETOOLONG)`],
});
},
);
posix.each(["cancellation", "deadline"] as const)(
"stops a stalled executable lookup on its %s without running anything",
async (interruption) => {