Skip to content
Merged
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
6 changes: 0 additions & 6 deletions apps/connector/src/machine-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
nvxSupport,
programAt,
type SupportHost,
verifyAttestation,
} from "./nvx";
import type { Output } from "./output";
import { hostSize, type ResourcePool, resourcePool, savedLimits } from "./resources";
Expand Down Expand Up @@ -191,11 +190,6 @@ export async function openMachineSandbox(options: {
process.platform,
download,
unpack,
async (archive, repository) => {
if (await verifyAttestation(archive, repository)) {
output.say(output.style.dim(" Verified its build provenance attestation."));
}
},
bytes => {
let megabytes = Math.floor(bytes / 1024 / 1024 / 50) * 50;
if (megabytes > shown) {
Expand Down
53 changes: 1 addition & 52 deletions apps/connector/src/nvx.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import { spawnSync } from "node:child_process";
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
realpathSync,
rmSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { managedEnvironment, privateAgentState } from "./ai-agent";
Expand All @@ -26,7 +17,6 @@ import {
SandboxRefused,
STARTED_LINE,
type SupportHost,
verifyAttestation,
type VmContext,
type VmLaunch,
vmSpec,
Expand Down Expand Up @@ -664,44 +654,3 @@ describe("paths in a guest on Windows", () => {
expect(linux.environment).toMatchObject({ FOO: "1", SHELL: "/bin/bash" });
});
});

describe.skipIf(process.platform === "win32")("an NVX package's attestation", () => {
let scratch = mkdtempSync(join(tmpdir(), "chopin-attest-"));
afterAll(() => rmSync(scratch, { recursive: true, force: true }));
let asked = join(scratch, "asked");
let bin = join(scratch, "bin");
mkdirSync(bin);
// A gh that says what it was asked, then exits as $FAKE_GH_EXIT says.
writeFileSync(
join(bin, "gh"),
`#!/bin/sh\necho "gh $*" >> "${asked}"\necho "the last of what gh said" >&2\nexit "$FAKE_GH_EXIT"\n`,
{ mode: 0o755 },
);
let attest = (env: Record<string, string>) =>
verifyAttestation("/downloads/nvx-0.1.0-linux-kvm.tar.gz", "lavaman131/nvx", {
PATH: bin,
...env,
});

test("is verified with gh as chopin's install verifies chopin, and refused where gh can't confirm it", async () => {
rmSync(asked, { force: true });
expect(await attest({ FAKE_GH_EXIT: "0" })).toBe(true);
expect(readFileSync(asked, "utf8")).toBe(
"gh attestation verify /downloads/nvx-0.1.0-linux-kvm.tar.gz -R lavaman131/nvx\n",
);
await expect(attest({ FAKE_GH_EXIT: "1" })).rejects.toThrow(
"gh attestation verify could not confirm the NVX package was built by lavaman131/nvx, so "
+ "nothing was installed. (CHOPIN_VERIFY_ATTESTATION=0 skips this check.) gh: the last "
+ "of what gh said",
);
});

test("isn't checked without gh, with a gh that isn't signed in, or with CHOPIN_VERIFY_ATTESTATION=0", async () => {
rmSync(asked, { force: true });
expect(await attest({ PATH: join(scratch, "nothing") })).toBe(false);
expect(await attest({ FAKE_GH_EXIT: "0", CHOPIN_VERIFY_ATTESTATION: "0" })).toBe(false);
expect(existsSync(asked)).toBe(false);
// gh's exit status when it has no GitHub sign-in to ask with.
expect(await attest({ FAKE_GH_EXIT: "4" })).toBe(false);
});
});
107 changes: 20 additions & 87 deletions apps/connector/src/nvx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,36 @@ import { createHash } from "node:crypto";
import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync } from "node:fs";
import { join, posix, win32 } from "node:path";

export type NvxPackage = "linux-kvm" | "linux-mshv" | "windows-whp";

/** A release of NVX a connector can pin. */
export type NvxRelease = {
version: string;
/** The GitHub repository the release is in; NVX's own (`NVX_REPOSITORY`) where it names none. */
repository?: string;
tag: string;
/**
* Whether the release attests its packages' build provenance, which an install then verifies
* (see `verifyAttestation`). NVX's own releases don't.
*/
attested?: boolean;
/**
* Whether its `nvx sandbox run` takes `--processors`, so an agent's microVM may boot with more
* than one vCPU. Without it every guest boots with one.
*/
sandboxProcessors: boolean;
/** The package of each host, with its SHA-256. */
packages: Record<NvxPackage, { file: string; sha256: string }>;
};

/** NVX's own repository, whose releases a pin names unless it names another. */
export const NVX_REPOSITORY = "microsoft/nvx";

/**
* The NVX release this connector runs every agent in. NVX publishes development releases only,
* several a day, so the pin moves only with a `chopin` release that was tested on it.
*
* For now it is a fork's build of NVX's release v0.1.0-dev.4006d33bb288 with microsoft/nvx#2439,
* which gives `nvx sandbox run` its `--processors`: upstream's own packages, checked against its
* release, with their `nvx` rebuilt from that change on the release's commit, as NVX's CI builds
* it, and the records that describe it; every other file is upstream's. The pin moves back to an
* NVX release, and `repository` and `attested` go, once NVX ships #2439.
* The NVX release this connector runs every agent in: its version and, for each host, the package
* downloaded from NVX's own GitHub release, with its SHA-256. NVX publishes development releases
* only, several a day, so the pin moves only with a `chopin` release that was tested on it.
*/
export const NVX_RELEASE: NvxRelease = {
version: "0.1.0-dev.4006d33bb288-processors.1",
repository: "lavaman131/nvx",
tag: "v0.1.0-dev.4006d33bb288-processors.1",
attested: true,
export const NVX_RELEASE = {
version: "0.1.0-dev.31f662c95379",
tag: "v0.1.0-dev.31f662c95379",
/**
* Whether this release's `nvx sandbox run` takes `--processors`, so an agent's microVM may boot
* with more than one vCPU. It does, from microsoft/nvx#2439.
*/
sandboxProcessors: true,
packages: {
"linux-kvm": {
file: "nvx-0.1.0-linux-kvm.tar.gz",
sha256: "abba4c3413079c22b2a05750de9f6793f7b7c9025e0a97c9785c9a9e6ea827a5",
sha256: "86e0785e67dae6fbd3c042415571b9f956001764ee806bb8508b5c458a9d8c44",
},
"linux-mshv": {
file: "nvx-0.1.0-linux-mshv.tar.gz",
sha256: "500be37008799e8fa2eb664a9ae47b2b857d74eb2375fb01f20fdddc2fd53b94",
sha256: "4d20f4181349ea23fb7857997b71575c0ca3acf3c02820f9e2cbabc2667042df",
},
"windows-whp": {
file: "nvx-0.1.0-windows-whp.zip",
sha256: "bf00d4ee6fcdbe6b1ac68b41f32e0de146653b59e7982463a4da2c75d6219195",
sha256: "39cfe91eab8ca705cfb2f51969ff70e521a31f7455d5a4273800d8e271044ce8",
},
},
};
} as const;

export type NvxPackage = keyof typeof NVX_RELEASE.packages;
export type Hypervisor = "kvm" | "mshv" | "whp";

/** What every refusal of a machine that can't run NVX starts with. */
Expand Down Expand Up @@ -228,11 +201,10 @@ export function nvxDirectory(base: string) {
return join(base, "nvx", NVX_RELEASE.version);
}

/** Where the pinned package is downloaded from: its release. */
/** Where the pinned package is downloaded from: NVX's own release. */
export function packageUrls(name: NvxPackage): string[] {
let file = NVX_RELEASE.packages[name].file;
let repository = NVX_RELEASE.repository ?? NVX_REPOSITORY;
return [`https://github.com/${repository}/releases/download/${NVX_RELEASE.tag}/${file}`];
return [`https://github.com/microsoft/nvx/releases/download/${NVX_RELEASE.tag}/${file}`];
}

/** Lines of a package's `SHA256SUMS` that its files don't match, as `<file>: <why>`. */
Expand Down Expand Up @@ -871,54 +843,18 @@ export type Fetcher = (
/** Unpacks a package archive into a folder: `tar` reads `.tar.gz` and, on Windows 10 and later, `.zip`. */
export type Unpacker = (archive: string, into: string) => Promise<void>;

/** Verifies the build provenance attestation of a package `repository` published, or throws. */
export type Attester = (archive: string, repository: string) => Promise<void>;

/**
* Verifies a package's build provenance attestation as chopin's install and `chopin update` verify
* chopin's own binary: `gh attestation verify <package> -R <repository>`, where gh is installed and
* CHOPIN_VERIFY_ATTESTATION isn't 0. Resolves to whether it verified it. A gh that isn't signed in
* (its exit status 4) can't ask GitHub for the attestation, so it checks nothing, as no gh does;
* any other failure throws.
*/
export async function verifyAttestation(
archive: string,
repository: string,
env: Record<string, string | undefined> = process.env,
): Promise<boolean> {
let gh = env.CHOPIN_VERIFY_ATTESTATION === "0" ? null : Bun.which("gh", { PATH: env.PATH ?? "" });
if (!gh) return false;
let child = Bun.spawn([gh, "attestation", "verify", archive, "-R", repository], {
env,
stdout: "ignore",
stderr: "pipe",
});
let said = (await new Response(child.stderr).text()).trim();
let code = await child.exited;
if (code === 0) return true;
if (code === 4) return false;
throw new Error(
`gh attestation verify could not confirm the NVX package was built by ${repository}, so `
+ `nothing was installed. (CHOPIN_VERIFY_ATTESTATION=0 skips this check.)${
said ? ` gh: ${said.split(/\s*\n\s*/).at(-1)}` : ""
}`,
);
}

/**
* Installs the pinned NVX release for this machine into `<base>/nvx/<version>`: downloaded, its
* SHA-256 checked against the pin, its attestation verified where the release attests its packages,
* unpacked, each of its files checked against the package's own `SHA256SUMS`, then made the
* installed release in one rename. A release that fails any check is removed and nothing is
* installed.
* SHA-256 checked against the pin, unpacked, each of its files checked against the package's own
* `SHA256SUMS`, then made the installed release in one rename. A release that fails any check is
* removed and nothing is installed.
*/
export async function installNvx(
base: string,
name: NvxPackage,
platform: NodeJS.Platform,
fetchTo: Fetcher,
unpack: Unpacker,
attest: Attester,
progress?: (bytes: number) => void,
): Promise<NvxArtifacts> {
let { mkdir, rename, rm, readdir, writeFile } = await import("node:fs/promises");
Expand Down Expand Up @@ -946,9 +882,6 @@ export async function installNvx(
`The NVX package downloaded isn't the one this chopin pins (SHA-256 ${digest}), so nothing was installed.`,
);
}
if (NVX_RELEASE.attested) {
await attest(archive, NVX_RELEASE.repository ?? NVX_REPOSITORY);
}
let unpacked = join(work, "unpacked");
await mkdir(unpacked);
await unpack(archive, unpacked);
Expand Down
6 changes: 1 addition & 5 deletions docs/nvx-sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,7 @@ Linux/KVM has been validated end to end. On Windows/WHP, image boot, shares, std

## Shipping NVX

Each `chopin` CLI release pins one tested NVX release (`NVX_RELEASE` in `nvx.ts`) by version, GitHub repository and per-platform SHA-256 (`linux-kvm`, `linux-mshv`, `windows-whp`). On first run, `chopin` downloads the package (~275 MB on Linux, ~150 MB on Windows) from that repository's release, verifies the pin, verifies its build provenance attestation where the release has one, verifies `SHA256SUMS`, extracts to `<base>/nvx/<version>/` (`0700`), and deletes older unpinned versions. Set `CHOPIN_NVX_ROOT` to use an offline pre-extracted package.

The attestation is checked as chopin's own install and `chopin update` check chopin: with `gh attestation verify <package> -R <repository>` where the [GitHub CLI](https://cli.github.com) is installed, refusing a package it can't confirm. `CHOPIN_VERIFY_ATTESTATION=0` skips it, and nothing is checked without `gh` or with a `gh` that isn't signed in (its exit status 4), since `gh` asks GitHub for the attestation; the pinned SHA-256 still holds the package to the one tested.

**The pin is temporary.** NVX's own releases boot a sandbox with one vCPU: `nvx sandbox run` takes `--processors` only from [microsoft/nvx#2439](https://github.com/microsoft/nvx/pull/2439), which isn't released yet. Until it is, the pin is [v0.1.0-dev.4006d33bb288-processors.1](https://github.com/lavaman131/nvx/releases/tag/v0.1.0-dev.4006d33bb288-processors.1) of the fork that change comes from: NVX's release [v0.1.0-dev.4006d33bb288](https://github.com/microsoft/nvx/releases/tag/v0.1.0-dev.4006d33bb288), whose packages its workflow checks against NVX's release, with `nvx` rebuilt from #2439 on that release's commit as NVX's CI builds it, and the three records that describe `nvx` (`provenance/nvx.provenance.json`, the `nvx` object of `SOURCE-MANIFEST.json`, and `SHA256SUMS`). Every other file, OpenVMM, the kernel and the guest layers among them, is NVX's own, byte for byte, and each package has a build provenance attestation. Once #2439 ships in an NVX release, the pin moves back to NVX's own release (dropping `repository` and `attested`).
Each `chopin` CLI release pins one tested NVX release (`NVX_RELEASE` in `nvx.ts`) by version and per-platform SHA-256 (`linux-kvm`, `linux-mshv`, `windows-whp`). On first run, `chopin` downloads the package (~275 MB on Linux, ~150 MB on Windows), verifies both the pin and `SHA256SUMS`, extracts to `<base>/nvx/<version>/` (`0700`), and deletes older unpinned versions. Set `CHOPIN_NVX_ROOT` to use an offline pre-extracted package. NVX's releases carry no build provenance attestation, so the install relies on those checksums.

## Migration

Expand Down
Loading