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
Merged 23h 8m ago, live in production
grokbot/forge:fix/landlock-floor-proc-readable → main 0 commits fork 1 thread · 1 unresolved
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: | ||
| 138 | 138 | // `rest` are the hidden paths at or under `dir`. |
| 139 | 139 | for (const name of list(dir)) { |
| 140 | 140 | 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; | |
| 144 | 141 | const below = rest.filter((h) => h === path || h.startsWith(`${path}/`)); |
| 145 | 142 | // A link is never a root: its target is granted by its own place, and a link granted would |
| 146 | 143 | // open whatever it names, hidden or not. |
| 147 | 144 | 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); | |
| 150 | 150 | } else if (!below.includes(path)) descend(path, below, roots, list, link); |
| 151 | 151 | } |
| 152 | 152 | } |
src/sandbox/landlock_helper.ts −2
| @@ -80,8 +80,6 @@ function main(): void { | ||
| 80 | 80 | |
| 81 | 81 | // Hidden paths leave no single root: each place that stays readable is its own rule. |
| 82 | 82 | 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); | |
| 85 | 83 | for (const path of plan.writable) allow(ruleset, path, plan.handledFs); |
| 86 | 84 | // struct landlock_net_port_attr: u64 allowed_access, u64 port. |
| 87 | 85 | for (const port of plan.connect) { |
test/landlock.test.ts +5 −5
| @@ -50,10 +50,10 @@ describe("reads around a hidden path", () => { | ||
| 50 | 50 | }); |
| 51 | 51 | |
| 52 | 52 | 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", () => { | |
| 54 | 54 | const tree: Record<string, string[]> = { "/": ["usr", "proc", "data"], "/proc": ["1", "4242", "sys", "cpuinfo", "self"] }; |
| 55 | 55 | 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"]); | |
| 57 | 57 | }); |
| 58 | 58 | test("a hidden path that is not there fails the plan instead of leaving it readable", () => { |
| 59 | 59 | expect(() => landlockPlan(6, { writable: [], hide: ["/nonexistent-forge-data"] })).toThrow("cannot resolve hidden path"); |
| @@ -100,7 +100,7 @@ test("parseAbi takes a positive integer only", () => { | ||
| 100 | 100 | // The kernel is the judge: skipped (and so named) where this host has no Landlock. |
| 101 | 101 | const probe = process.platform === "linux" ? await probeLandlock({}) : { ok: false as const, reason: "not linux" }; |
| 102 | 102 | 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", () => { | |
| 104 | 104 | if (!probe.ok) return; |
| 105 | 105 | const base = mkdtempSync(join(tmpdir(), "ll-alias-")); |
| 106 | 106 | try { |
| @@ -109,9 +109,9 @@ describe.skipIf(!probe.ok)("on this kernel", () => { | ||
| 109 | 109 | symlinkSync(join(base, "real"), join(base, "data")); |
| 110 | 110 | const plan = landlockPlan(probe.abi, { writable: ["/dev/null"], hide: [join(base, "data")] }); |
| 111 | 111 | 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("; "); | |
| 113 | 113 | 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", ""]); | |
| 115 | 115 | } finally { |
| 116 | 116 | rmSync(base, { recursive: true }); |
| 117 | 117 | } |
new above
offline
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.