From eea1f8fe82274f7a3238af5d29f202d820d87300 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 2 Nov 2026 12:34:48 +0000 Subject: [PATCH] fix(ui-sdk): stat mount points in the worker socket walk A bind mount does not change the type readdir reports for its mount point, so a socket bind-mounted onto a file path read as the empty regular file bwrap made for it and the walk never reported it. The walk now lstats every mount point listed in the walking process's /proc/self/mountinfo, in the fixture's baseline and in each worker. A socket file-bound into a path every profile inherits is the positive control. Closes #2745 Co-Authored-By: Claude Opus 5.5 --- .../tests/fixtures/worker-var-sockets.ts | 11 +++++++--- .../ui-sdk/tests/worker-var-sockets.test.ts | 21 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/ui-sdk/tests/fixtures/worker-var-sockets.ts b/packages/ui-sdk/tests/fixtures/worker-var-sockets.ts index 7fd5c5edb5..779f46a41a 100644 --- a/packages/ui-sdk/tests/fixtures/worker-var-sockets.ts +++ b/packages/ui-sdk/tests/fixtures/worker-var-sockets.ts @@ -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) => { @@ -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; diff --git a/packages/ui-sdk/tests/worker-var-sockets.test.ts b/packages/ui-sdk/tests/worker-var-sockets.test.ts index 80e6a0af95..1f1402d23d 100644 --- a/packages/ui-sdk/tests/worker-var-sockets.test.ts +++ b/packages/ui-sdk/tests/worker-var-sockets.test.ts @@ -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((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")], @@ -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(done => sourceServer.close(() => done())); + rmSync(root, { recursive: true, force: true }); + rmSync(source, { recursive: true, force: true }); + } }, 180_000);