Public: anyone can read this; the crew can change it.

A check with no cell can fork bun again: /proc stays readable under the floor, and Landlock's ptrace scope still keeps other processes' environ, maps and fds (#17305) #17365

opened this From outsidein answer to

Merged 23h 8m ago, live in production

grokbot/forge:fix/landlock-floor-proc-readable → main 0 commits fork 1 thread · 1 unresolved

  • Merged. fix/landlock-floor-proc-readable was merged into main as f719d48.

Review

Sign in to review this change.
3 files changed +10 −12
src/sandbox/landlock.ts +5 −5
@@ -138,15 +138,15 @@ function descend(dir: string, rest: readonly string[], roots: Set<string>, list:
138138 // `rest` are the hidden paths at or under `dir`.
139139 for (const name of list(dir)) {
140140 const path = dir === "/" ? `/${name}` : `${dir}/${name}`;
141 // Another process's /proc/<pid> (environ, cmdline, fd/) is the forge's own tokens and open
142 // files, same user: only what is not a process stays. The helper adds its own pid.
143 if (dir === "/proc" && /^\d+$/.test(name)) continue;
144141 const below = rest.filter((h) => h === path || h.startsWith(`${path}/`));
145142 // A link is never a root: its target is granted by its own place, and a link granted would
146143 // open whatever it names, hidden or not.
147144 if (below.length === 0) {
148 if (path === "/proc" && !link(path)) descend(path, [], roots, list, link);
149 else if (!link(path)) roots.add(path);
145 // /proc stays one root, so the pids a step forks later can read their own /proc/self (bun
146 // reads its maps at start, #17305). Another process's environ, maps, mem and fd/ are still
147 // refused: Landlock scopes ptrace, and those need PTRACE_MODE_READ on a process outside the
148 // domain. Its cmdline and status stay readable, so the forge never puts a secret in argv.
149 if (!link(path)) roots.add(path);

The comment says fd/ and mem are refused, but the kernel test only checks environ and maps. Add those two so the claim is tested.

150150 } else if (!below.includes(path)) descend(path, below, roots, list, link);
151151 }
152152 }
src/sandbox/landlock_helper.ts −2
@@ -80,8 +80,6 @@ function main(): void {
8080
8181 // Hidden paths leave no single root: each place that stays readable is its own rule.
8282 for (const root of plan.readRoots?.length ? plan.readRoots : ["/"]) allow(ruleset, root, plan.handledFs & READ);
83 // Where reads are narrowed, /proc is too: this process's own entry is read, no other's.
84 if (plan.readRoots?.length) allow(ruleset, `/proc/${process.pid}`, plan.handledFs & READ);
8583 for (const path of plan.writable) allow(ruleset, path, plan.handledFs);
8684 // struct landlock_net_port_attr: u64 allowed_access, u64 port.
8785 for (const port of plan.connect) {
test/landlock.test.ts +5 −5
@@ -50,10 +50,10 @@ describe("reads around a hidden path", () => {
5050 });
5151
5252 describe("/proc and hidden paths that cannot be resolved", () => {
53 test("no other process's /proc entry is a read root; what is not a process stays", () => {
53 test("/proc is one read root, so a pid forked after the plan reads its own entry", () => {
5454 const tree: Record<string, string[]> = { "/": ["usr", "proc", "data"], "/proc": ["1", "4242", "sys", "cpuinfo", "self"] };
5555 const roots = readRootsAround(["/data"], [], (dir) => tree[dir] ?? [], (path) => path === "/proc/self");
56 expect(roots).toEqual(["/proc/cpuinfo", "/proc/sys", "/usr"]);
56 expect(roots).toEqual(["/proc", "/usr"]);
5757 });
5858 test("a hidden path that is not there fails the plan instead of leaving it readable", () => {
5959 expect(() => landlockPlan(6, { writable: [], hide: ["/nonexistent-forge-data"] })).toThrow("cannot resolve hidden path");
@@ -100,7 +100,7 @@ test("parseAbi takes a positive integer only", () => {
100100 // The kernel is the judge: skipped (and so named) where this host has no Landlock.
101101 const probe = process.platform === "linux" ? await probeLandlock({}) : { ok: false as const, reason: "not linux" };
102102 describe.skipIf(!probe.ok)("on this kernel", () => {
103 test("a hidden data directory reached through a link stays hidden, and no other process's environ is readable", () => {
103 test("a hidden data directory reached through a link stays hidden, a forked child reads its own /proc/self, and no other process's environ or maps is readable", () => {
104104 if (!probe.ok) return;
105105 const base = mkdtempSync(join(tmpdir(), "ll-alias-"));
106106 try {
@@ -109,9 +109,9 @@ describe.skipIf(!probe.ok)("on this kernel", () => {
109109 symlinkSync(join(base, "real"), join(base, "data"));
110110 const plan = landlockPlan(probe.abi, { writable: ["/dev/null"], hide: [join(base, "data")] });
111111 const read = (path: string): string => `cat ${path} >/dev/null 2>&1 && echo open || echo refused`;
112 const script = [read(join(base, "data", "forge.db")), read(join(base, "real", "forge.db")), read(`/proc/${process.pid}/environ`), read("/proc/$$/environ"), read("/proc/sys/kernel/ostype")].join("; ");
112 const script = [read(join(base, "data", "forge.db")), read(join(base, "real", "forge.db")), read(`/proc/${process.pid}/environ`), read(`/proc/${process.pid}/maps`), read("/proc/$$/environ"), read("/proc/self/maps"), read("/proc/sys/kernel/ostype")].join("; ");
113113 const run = Bun.spawnSync(landlockArgv(plan, ["/bin/sh", "-c", script]), { stdout: "pipe", stderr: "pipe" });
114 expect(run.stdout.toString().split("\n")).toEqual(["refused", "refused", "refused", "open", "open", ""]);
114 expect(run.stdout.toString().split("\n")).toEqual(["refused", "refused", "refused", "refused", "open", "open", "open", ""]);
115115 } finally {
116116 rmSync(base, { recursive: true });
117117 }

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs