fix(process): end the PATH search at errors a spawned search stops at

The lookup moved past every PATH candidate whose stat failed. A spawned
search moves past only a missing file, a non-directory and a denied
directory; at any other error, such as a symlink loop (ELOOP), it stops
and the launch fails. With PATH=<loop>:/usr/bin, the lookup selected
/usr/bin/rizin and the service hashed, probed and ran it, although a
launch by name fails. The search now ends there, reporting the error in
checked, so the launch by name fails as a spawned search would. A
non-executable file earlier on PATH still lets the search continue.
This commit is contained in:
Entrpi committed 2026-11-01 17:16:40 +11:00
1 parent 162fcd0800
commit c1d66e52fe
2 files changed
+38 -5

No files matched your search

+9 -3
View File
@@ -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)`);
@@ -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) => {