Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ci/pi-agent-qualification-v1-linux-amd64.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"agent": "pi",
"platform": "linux/amd64",
"image": "ghcr.io/nvidia/nemoclaw/pi-sandbox",
"digest": "sha256:847d94d8ce4e17abbb66df6f689f1180bbef5a8fe6355da2d6243c31835eba34",
"reference": "ghcr.io/nvidia/nemoclaw/pi-sandbox@sha256:847d94d8ce4e17abbb66df6f689f1180bbef5a8fe6355da2d6243c31835eba34",
"digest": "sha256:f08fb0bc8158f194c464966436e54d4be817af2584f61d7e90e20d740dca20bf",
"reference": "ghcr.io/nvidia/nemoclaw/pi-sandbox@sha256:f08fb0bc8158f194c464966436e54d4be817af2584f61d7e90e20d740dca20bf",
"source": {
"repository": "NVIDIA/NemoClaw",
"revision": "a303db9ea635ef6b057c0170112758dcd458b848",
"revision": "661933f525fe32a20688242de773612b7580aaa5",
"release": "v0.1.0",
"cohort": "ghrun-40584663299-1"
"cohort": "ghrun-40564501527-1"
},
"startupProfileContractVersion": 1,
"capabilityContractVersion": 1
Expand Down
8 changes: 4 additions & 4 deletions ci/pi-agent-qualification-v1-linux-arm64.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"agent": "pi",
"platform": "linux/arm64",
"image": "ghcr.io/nvidia/nemoclaw/pi-sandbox",
"digest": "sha256:85ed5b06b9e32af36de308055f79e6fcfab44d490d77ccc36a3adf593af04836",
"reference": "ghcr.io/nvidia/nemoclaw/pi-sandbox@sha256:85ed5b06b9e32af36de308055f79e6fcfab44d490d77ccc36a3adf593af04836",
"digest": "sha256:1821d757717264fd577f2d55c54bd361ffc27fce915f0b002ce44e800e2932e3",
"reference": "ghcr.io/nvidia/nemoclaw/pi-sandbox@sha256:1821d757717264fd577f2d55c54bd361ffc27fce915f0b002ce44e800e2932e3",
"source": {
"repository": "NVIDIA/NemoClaw",
"revision": "a303db9ea635ef6b057c0170112758dcd458b848",
"revision": "661933f525fe32a20688242de773612b7580aaa5",
"release": "v0.1.0",
"cohort": "ghrun-40584663299-1"
"cohort": "ghrun-40564501527-1"
},
"startupProfileContractVersion": 1,
"capabilityContractVersion": 1
Expand Down
4 changes: 2 additions & 2 deletions src/lib/agent/candidate-authority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const CANDIDATE_QUALIFICATION_RECEIPT_DIGESTS: Readonly<
Record<CandidateManagedImageAgent, readonly string[]>
> = Object.freeze({
pi: Object.freeze([
"49662a6eb37e1e93206c5f6b1539dd6133c85a5249ac4e4970902ad5b728f03d",
"9acabcf8bb74eab2c85315a42047a6f44864e871b453e05e9d31afc1dabbbc3f",
"728f5392f3d72326a025a618780e5c6083ccb4e3937d15dc98b960c4cb643cfe",
"89f2df3fc20e3a0744012d5d5498c0e8f27b288d3a804e0aee0fb536daf0b056",
]),
});

Expand Down
176 changes: 172 additions & 4 deletions src/lib/onboard/managed-startup-image-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ describe("managed startup image runtime", () => {
shareDirectory: string,
target: string,
beforeSourceCleanup?: () => void,
sourceOwner: { readonly uid: bigint; readonly gid: bigint } = { uid: 998n, gid: 999n },
): void {
const realFstatSync = fs.fstatSync.bind(fs);
const realLstatSync = fs.lstatSync.bind(fs);
const rootOwned = (stat: fs.Stats): fs.Stats =>
new Proxy(stat, {
Expand All @@ -566,20 +568,71 @@ describe("managed startup image runtime", () => {
return typeof value === "function" ? value.bind(inner) : value;
},
});
const sandboxOwnedBigInt = (stat: fs.BigIntStats): fs.BigIntStats =>
new Proxy(stat, {
get(inner, property) {
const value =
property === "uid"
? sourceOwner.uid
: property === "gid"
? sourceOwner.gid
: Reflect.get(inner, property);
return typeof value === "function" ? value.bind(inner) : value;
},
});
const setprivStat = {
gid: 0,
isFile: () => true,
isSymbolicLink: () => false,
mode: 0o755,
uid: 0,
} as fs.Stats;
vi.spyOn(fs, "fstatSync").mockImplementation(((
descriptor: number,
options?: { bigint?: boolean },
) => {
const stat = options?.bigint
? realFstatSync(descriptor, { bigint: true })
: realFstatSync(descriptor);
return options?.bigint ? sandboxOwnedBigInt(stat as fs.BigIntStats) : stat;
}) as typeof fs.fstatSync);
vi.spyOn(fs, "lstatSync").mockImplementation(((
file: fs.PathLike,
options?: { bigint?: boolean },
) => {
const filePath = file.toString();
const sourceCleanupHook =
file.toString() === source && options?.bigint === true ? beforeSourceCleanup : undefined;
filePath === source && options?.bigint === true ? beforeSourceCleanup : undefined;
sourceCleanupHook?.();
const stat = options?.bigint ? realLstatSync(file, { bigint: true }) : realLstatSync(file);
const rootPath = file.toString() === shareDirectory || file.toString() === target;
return rootPath && options?.bigint !== true ? rootOwned(stat as fs.Stats) : stat;
const stat =
filePath === "/usr/bin/setpriv"
? setprivStat
: options?.bigint
? realLstatSync(file, { bigint: true })
: realLstatSync(file);
const rootPath = filePath === shareDirectory || filePath === target;
return filePath === "/usr/bin/setpriv"
? setprivStat
: filePath === source && options?.bigint === true
? sandboxOwnedBigInt(stat as fs.BigIntStats)
: rootPath && options?.bigint !== true
? rootOwned(stat as fs.Stats)
: stat;
}) as typeof fs.lstatSync);
vi.spyOn(fs, "fchownSync").mockImplementation(() => undefined);
}

function mockRootUnlinkFailure(source: string, code: "EACCES" | "EPERM"): () => void {
const realUnlinkSync = fs.unlinkSync.bind(fs);
const denySource = (): never => {
throw Object.assign(new Error(`${code}: permission denied, unlink '${source}'`), { code });
};
vi.spyOn(fs, "unlinkSync").mockImplementation((targetPath) =>
targetPath.toString() === source ? denySource() : realUnlinkSync(targetPath),
);
return () => realUnlinkSync(source);
}

it("promotes generated Hermes policy to one root-owned, read-only runtime artifact", () => {
const directory = temporaryPolicyDirectory();
const shareDirectory = path.join(directory, "share");
Expand Down Expand Up @@ -616,6 +669,121 @@ describe("managed startup image runtime", () => {
expect(fs.readFileSync(target, "utf8")).toBe(policy);
});

it.each(["EACCES", "EPERM"] as const)(
"removes the Hermes managed policy source as the sandbox user when root unlink fails with %s",
(code) => {
const directory = temporaryPolicyDirectory();
const shareDirectory = path.join(directory, "share");
const source = path.join(directory, "managed-policy.json");
const target = path.join(shareDirectory, "hermes-managed-policy.json");
const policy = '{"schema_version":3}\n';
fs.mkdirSync(shareDirectory);
fs.writeFileSync(source, policy, { mode: 0o600 });
mockRootOwnedPolicyInstallPaths(source, shareDirectory, target);
const removeSource = mockRootUnlinkFailure(source, code);
childProcessMock.spawnSync.mockImplementation((command: string, args: readonly string[]) =>
command === "/usr/bin/id"
? { status: 0, stdout: args[0] === "-u" ? "998\n" : "999\n" }
: (removeSource(), { status: 0, stdout: "" }),
);

installHermesManagedPolicy(source, target);

expect(fs.existsSync(source)).toBe(false);
expect(fs.readFileSync(target, "utf8")).toBe(policy);
expect(fs.statSync(target).mode & 0o777).toBe(0o444);
expect(childProcessMock.spawnSync).toHaveBeenCalledWith(
"/usr/bin/setpriv",
["--reuid=998", "--regid=999", "--init-groups", "--", "/bin/rm", "-f", "--", source],
expect.objectContaining({
encoding: "utf8",
stdio: "ignore",
}),
);
},
);

it("refuses sandbox-user removal when the Hermes managed policy source is not owned by the sandbox account", () => {
const directory = temporaryPolicyDirectory();
const shareDirectory = path.join(directory, "share");
const source = path.join(directory, "managed-policy.json");
const target = path.join(shareDirectory, "hermes-managed-policy.json");
const policy = '{"schema_version":3}\n';
fs.mkdirSync(shareDirectory);
fs.writeFileSync(source, policy, { mode: 0o600 });
mockRootOwnedPolicyInstallPaths(source, shareDirectory, target, undefined, {
uid: 1000n,
gid: 1000n,
});
mockRootUnlinkFailure(source, "EACCES");
childProcessMock.spawnSync.mockImplementation((_command: string, args: readonly string[]) => ({
status: 0,
stdout: args[0] === "-u" ? "998\n" : "999\n",
}));

expect(() => installHermesManagedPolicy(source, target)).toThrow(/unexpected owner/u);
expect(fs.existsSync(source)).toBe(true);
expect(childProcessMock.spawnSync).not.toHaveBeenCalledWith(
"/usr/bin/setpriv",
expect.anything(),
expect.anything(),
);
});

it.each([
[
"setpriv exits non-zero",
1,
true,
false,
/could not remove Hermes managed policy source as the sandbox user/u,
],
[
"source file remains present after setpriv",
0,
false,
false,
/could not remove Hermes managed policy source as the sandbox user/u,
],
[
"source removal cannot be verified after setpriv",
0,
true,
true,
/could not verify Hermes managed policy source cleanup/u,
],
] as const)(
"fails closed when %s during Hermes managed policy source cleanup",
(_label, status, removeFile, failPostCleanupLstat, expectedError) => {
const directory = temporaryPolicyDirectory();
const shareDirectory = path.join(directory, "share");
const source = path.join(directory, "managed-policy.json");
const target = path.join(shareDirectory, "hermes-managed-policy.json");
const policy = '{"schema_version":3}\n';
let postCleanupHook: () => void = () => undefined;
const failCleanupLstat = (): never => {
throw Object.assign(new Error(`EACCES: permission denied, lstat '${source}'`), {
code: "EACCES",
});
};
const nextCleanupHook = failPostCleanupLstat ? failCleanupLstat : () => undefined;
fs.mkdirSync(shareDirectory);
fs.writeFileSync(source, policy, { mode: 0o600 });
mockRootOwnedPolicyInstallPaths(source, shareDirectory, target, () => {
postCleanupHook();
});
const removeSource = mockRootUnlinkFailure(source, "EACCES");
const maybeRemoveSource = removeFile ? removeSource : () => undefined;
childProcessMock.spawnSync.mockImplementation((command: string, args: readonly string[]) =>
command === "/usr/bin/id"
? { status: 0, stdout: args[0] === "-u" ? "998\n" : "999\n" }
: ((postCleanupHook = nextCleanupHook), maybeRemoveSource(), { status, stdout: "" }),
);

expect(() => installHermesManagedPolicy(source, target)).toThrow(expectedError);
},
);

it("verifies a host-copied transaction only through the explicit read-only receipt mode", async () => {
const profile = managedStartupE2eProfile("openclaw");
const profileFingerprint = fingerprintManagedStartupProfile(profile);
Expand Down
54 changes: 53 additions & 1 deletion src/lib/onboard/managed-startup/image-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,50 @@ export function readStableRegularFile(target: string, maxBytes: number): Buffer
return readStableRegularFileSnapshot(target, maxBytes).bytes;
}

function removeHermesManagedPolicySourceAsSandbox(
source: string,
expectedStat: fs.BigIntStats,
): void {
if (!trustedExecutable("/usr/bin/setpriv")) {
fail("a trusted setpriv executable is required");
}
const identity = readSandboxIdentity();
if (expectedStat.uid !== BigInt(identity.uid) || expectedStat.gid !== BigInt(identity.gid)) {
fail(`refusing to remove Hermes managed policy source with unexpected owner: ${source}`);
}
const removed = spawnSync(
"/usr/bin/setpriv",
[
`--reuid=${identity.uid}`,
`--regid=${identity.gid}`,
"--init-groups",
"--",
"/bin/rm",
"-f",
"--",
source,
],
{
encoding: "utf8",
env: { PATH: FIXED_PATH },
stdio: "ignore",
},
);
let sourceRemoved = false;
try {
fs.lstatSync(source, { bigint: true });
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
sourceRemoved = true;
} else {
fail(`could not verify Hermes managed policy source cleanup: ${source}`);
}
}
if (removed.error || removed.status !== 0 || !sourceRemoved) {
fail(`could not remove Hermes managed policy source as the sandbox user: ${source}`);
}
}

/** Promote the generator output to the one root-owned policy artifact used at runtime. */
export function installHermesManagedPolicy(
source = HERMES_GENERATED_MANAGED_POLICY_FILE,
Expand All @@ -896,7 +940,15 @@ export function installHermesManagedPolicy(
if (!sameStableFileMetadata(generated.stat, current)) {
fail(`Hermes managed policy changed before source cleanup: ${source}`);
}
fs.unlinkSync(source);
try {
fs.unlinkSync(source);
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code !== "EACCES" && code !== "EPERM") {
throw error;
}
removeHermesManagedPolicySourceAsSandbox(source, generated.stat);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/mcp/mcp-tool-discovery-image-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe("MCP tool discovery image contract", () => {
// source-shape-contract: security -- Exact reviewed runtime digests reject substituted executable and license artifacts before managed image construction.
it.each([
{
expectedHash: "18cda1ab5f57d3c4cdab0b6de5abad09f73bea11151bc149d141de26a593b398",
expectedHash: "d64d14a1febddf9e3fcde222dc644b8512c2907a92cbb8dc2554aedb133c2194",
relativePath: "managed-startup-direct-image-runtime.bundle",
},
{
Expand Down

Large diffs are not rendered by default.

Loading