diff --git a/src/process/ExecutableLookup.ts b/src/process/ExecutableLookup.ts index d79c3b27c9..a978739b92 100644 --- a/src/process/ExecutableLookup.ts +++ b/src/process/ExecutableLookup.ts @@ -40,8 +40,10 @@ export interface ExecutableLookupBounds { /** * Resolve a command the way process spawning searches for it. A command that * contains a path separator is the selected path; PATH search continues past - * denied directories, and an existing file without execute permission is not - * a match. The working directory is read only when the search reaches a + * 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 * 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 `..` @@ -109,7 +111,11 @@ export const lookupExecutable = async ( cause: metadata.cause, }; checked.push(`${candidate}${systemErrorSuffix(code)}`); - continue; + // A spawned search moves past these and stops at any other error, so a + // later candidate would not run. + if (code === "ENOENT" || code === "ENOTDIR" || code === "EACCES") + continue; + return { status: "missing", checked }; } if (!metadata.value.isFile()) { checked.push(`${candidate} (not a regular file)`); diff --git a/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts b/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts index d032f27f9a..cfa5e9883a 100644 --- a/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts +++ b/tests/boundary/filesystem/reverseEngineeringToolIdentity.test.ts @@ -1,6 +1,6 @@ import { createHash } from "node:crypto"; -import { stat, writeFile } from "node:fs/promises"; -import { delimiter, dirname, relative } from "node:path"; +import { chmod, rm, stat, symlink, writeFile } from "node:fs/promises"; +import { delimiter, dirname, join, relative } from "node:path"; import { expect, expectTypeOf, it, vi } from "vitest"; @@ -398,6 +398,33 @@ posix( }, ); +posix( + "ends the PATH search where a spawned search ends, at a symlink loop, but not at a non-executable file", + async () => { + const { executable } = await rizinExecutableFixture(); + const directory = dirname(executable); + const looped = dirname((await rizinExecutableFixture()).executable); + await rm(join(looped, "rizin")); + await symlink("rizin", join(looped, "rizin")); + const unexecutable = dirname((await rizinExecutableFixture()).executable); + await chmod(join(unexecutable, "rizin"), 0o644); + const cwd = () => process.cwd(); + // A launch by name fails with ELOOP here, so the later match must not run. + const stopped = await lookupExecutable( + "rizin", + cwd, + `${looped}${delimiter}${directory}`, + ); + expect(stopped).toEqual({ + status: "missing", + checked: [`${join(looped, "rizin")} (ELOOP)`], + }); + await expect( + lookupExecutable("rizin", cwd, `${unexecutable}${delimiter}${directory}`), + ).resolves.toEqual({ status: "found", path: executable }); + }, +); + posix.each(["cancellation", "deadline"] as const)( "stops a stalled executable lookup on its %s without running anything", async (interruption) => {