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
11 changes: 8 additions & 3 deletions packages/ui-sdk/tests/fixtures/worker-var-sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ function within(path: string, top: string) {
/**
* Every socket in the visible tree outside `/proc` and `/sys`, entering only
* the directories `descend` admits. Each entry's type comes from `readdir`;
* only entries the filesystem left untyped are stat'ed. Up to 32 directories
* are read at once, so a cold disk is not read one block at a time.
* only entries the filesystem left untyped, and the mount points in the
* walking process's `/proc/self/mountinfo`, are stat'ed. A mount does not
* change the type `readdir` reports for its mount point, so a socket
* bind-mounted onto a file path reads as that regular file (#2745). Up to 32
* directories are read at once, so a cold disk is not read one block at a time.
* Workers receive the source of `within` and the three functions from here
* on, so they refer to nothing else.
*/
async function walkSockets(files: typeof fs, descend: (path: string) => boolean) {
const sockets: string[] = [], queue = ["/"];
let entries = 0, active = 0;
const points = new Set(files.readFileSync("/proc/self/mountinfo", "utf8").trim().split("\n")
.map(line => line.split(" ")[4]!.replace(/\\([0-7]{3})/g, (_, octal: string) => String.fromCharCode(parseInt(octal, 8)))));
const typed = (entry: fs.Dirent) => entry.isFile() || entry.isDirectory() || entry.isSymbolicLink() || entry.isSocket()
|| entry.isFIFO() || entry.isCharacterDevice() || entry.isBlockDevice();
const read = async (path: string) => {
Expand All @@ -40,7 +45,7 @@ async function walkSockets(files: typeof fs, descend: (path: string) => boolean)
const child = path === "/" ? "/" + entry.name : path + "/" + entry.name;
entries++;
let kind: { isDirectory(): boolean; isSocket(): boolean } = entry;
if (!typed(entry)) {
if (!typed(entry) || points.has(child)) {
try { kind = await files.promises.lstat(child); } catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") continue;
throw error;
Expand Down
21 changes: 19 additions & 2 deletions packages/ui-sdk/tests/worker-var-sockets.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { expect, test } from "bun:test";
import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { createServer } from "node:net";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";

const ROOT = resolve(import.meta.dir, "../../..");
const CUPS = "/var/snap/cups/common/run/cups.sock";
const WALK_CONTROL = "/opt/odysseus-var-fixture/inherited/walk-control.sock";
// A socket bind-mounted onto a file path: its directory entry is the empty
// regular file bwrap made as the mount point, so only `lstat` shows the socket.
const FILE_BOUND_CONTROL = "/opt/odysseus-file-bound/walk-control.sock";

test("all worker profiles hide every /var socket while explicit runtime files still execute", async () => {
const root = mkdtempSync(join(tmpdir(), "odysseus-var-sockets-"));
// Outside `root`, under the host /tmp every worker replaces, so only the
// file-bound copy is in any view the walks read.
const source = mkdtempSync(join(tmpdir(), "odysseus-file-bound-")), sourceSocket = join(source, "control.sock");
const sourceServer = createServer(socket => socket.end());
try {
await new Promise<void>((done, reject) => { sourceServer.once("error", reject); sourceServer.listen(sourceSocket, done); });
mkdirSync(join(root, "brain"));
const child = Bun.spawn(["bwrap", "--unshare-net", "--bind", "/", "/", "--proc", "/proc", "--dev", "/dev",
"--tmpfs", "/run", "--tmpfs", "/var", "--tmpfs", "/opt", "--bind", root, "/opt/odysseus-var-fixture",
"--ro-bind", sourceSocket, FILE_BOUND_CONTROL,
...(existsSync(CUPS) ? ["--ro-bind", CUPS, CUPS] : []),
process.execPath, "--preload", join(ROOT, "scripts/test-network-child-preload.ts"),
join(import.meta.dir, "fixtures/worker-var-sockets.ts")],
Expand All @@ -36,10 +46,17 @@ test("all worker profiles hide every /var socket while explicit runtime files st
// The control is inherited from the fixture's view, so finding it proves
// the part of the walk that lstats the baseline instead of reading it.
expect(row.sockets, `${profile}: SOCKET_WALK_SEES_INHERITED_CONTROL`).toContain(WALK_CONTROL);
expect(row.sockets.filter(path => path !== WALK_CONTROL).sort(), `${profile}: SOCKET_WALK_ONLY_APPROVED_RELAY_AND_PROXY`)
// The file-bound control is inherited too, and only a walk that lstats
// every mount point finds it (#2745).
expect(row.sockets, `${profile}: SOCKET_WALK_SEES_FILE_BOUND_CONTROL`).toContain(FILE_BOUND_CONTROL);
expect(row.sockets.filter(path => path !== WALK_CONTROL && path !== FILE_BOUND_CONTROL).sort(), `${profile}: SOCKET_WALK_ONLY_APPROVED_RELAY_AND_PROXY`)
.toEqual(profile === "maintenance" ? [] : ["/run/brain-egress/inference.sock", "/run/brain-egress/proxy.sock"]);
expect(row.hostSockets, `${profile}: VAR_HOST_SOCKET_CONNECTIONS_REFUSED`).toEqual(["ENOENT", "ENOENT", "ENOENT", "ENOENT"]);
if (existsSync(CUPS)) expect(row.cups, `${profile}: VAR_CUPS_SOCKET_UNREACHABLE`).toBe("ENOENT");
}
} finally { rmSync(root, { recursive: true, force: true }); }
} finally {
await new Promise<void>(done => sourceServer.close(() => done()));
rmSync(root, { recursive: true, force: true });
rmSync(source, { recursive: true, force: true });
}
}, 180_000);
Loading