diff --git a/src/process/ExecutableLookup.ts b/src/process/ExecutableLookup.ts index 0db75c876e4..ff1e09c87c9 100644 --- a/src/process/ExecutableLookup.ts +++ b/src/process/ExecutableLookup.ts @@ -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" && diff --git a/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts b/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts index 13d4a83b988..f86a42aca2c 100644 --- a/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts +++ b/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts @@ -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) => {