From 999bcdb69a35b828262f41d333a35b1fd16c55a7 Mon Sep 17 00:00:00 2001 From: "congvc-bot[bot]" <3634010+congvc-bot[bot]@users.noreply.github.com> Date: Sun, 22 Nov 2026 18:32:41 +0700 Subject: [PATCH] fix(cso): treat userns-unmapped root ancestors (uid 65534) as root-owned (CHE-8998) --- lib/cso/state.ts | 19 +++++++++++++- test/cso-state-userns.test.ts | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/cso-state-userns.test.ts diff --git a/lib/cso/state.ts b/lib/cso/state.ts index aaaca9f7942..c0f3bc12e3f 100644 --- a/lib/cso/state.ts +++ b/lib/cso/state.ts @@ -686,6 +686,23 @@ export function stateRoot(env: Record = process.env) // The shared chain (lib/state-root.ts), made absolute. Security artifacts are intentionally outside every sync allowlist. return resolve(resolveStateRoot(env)); } +/** + * Inside a user namespace (for example a Codex `bwrap --unshare-user` sandbox) host root-owned + * directories stat as the kernel overflow uid 65534. Treat that as root-owned only when this process + * really runs in a non-initial user namespace, so a genuine uid 65534 (`nobody`) owner on the host is + * still rejected. + */ +export function unmappedRootInUserNamespace( + uid: number, + readUidMap: () => string = () => fs.readFileSync('/proc/self/uid_map', 'utf8'), +): boolean { + if (uid !== 65534 || process.platform !== 'linux') return false; + try { + return readUidMap().trim().split(/\s+/).join(' ') !== '0 0 4294967295'; + } catch { + return false; + } +} function ensureDirectory(path: string, hardenExistingLeaf: boolean): string { const p = resolve(path), root = parse(p).root; @@ -706,7 +723,7 @@ function ensureDirectory(path: string, hardenExistingLeaf: boolean): string { if (s.isSymbolicLink() || !s.isDirectory()) throw new CsoError('UNSAFE_PATH', 'Private state has a symlink or non-directory ancestor'); // Root-owned system ancestors are normal; a writable ancestor owned by anyone else is not. - if (s.uid !== process.getuid?.() && s.uid !== 0) + if (s.uid !== process.getuid?.() && s.uid !== 0 && !unmappedRootInUserNamespace(s.uid)) throw new CsoError('UNSAFE_PATH', 'Private state ancestor has an unexpected owner'); if (process.platform !== 'win32' && s.mode & 0o022 && !(s.mode & 0o1000)) throw new CsoError('UNSAFE_PATH', 'Private state has a group- or world-writable ancestor'); diff --git a/test/cso-state-userns.test.ts b/test/cso-state-userns.test.ts new file mode 100644 index 00000000000..f5d939a7632 --- /dev/null +++ b/test/cso-state-userns.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, test } from 'bun:test'; +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { spawnSync } from 'node:child_process'; +import { unmappedRootInUserNamespace } from '../lib/cso/state'; + +const INITIAL_MAP = ' 0 0 4294967295\n'; +const USERNS_MAP = ' 0 1000 1\n'; +const linuxOnly = process.platform === 'linux'; + +describe('unmappedRootInUserNamespace', () => { + test.skipIf(!linuxOnly)('accepts uid 65534 when uid_map is not the initial identity map', () => { + expect(unmappedRootInUserNamespace(65534, () => USERNS_MAP)).toBe(true); + }); + test.skipIf(!linuxOnly)('rejects uid 65534 when uid_map is the initial map', () => { + expect(unmappedRootInUserNamespace(65534, () => INITIAL_MAP)).toBe(false); + expect(unmappedRootInUserNamespace(65534, () => '0 0 4294967295')).toBe(false); + }); + test('rejects every other uid regardless of the map', () => { + for (const uid of [0, 1, 1000, 65533, 65535]) expect(unmappedRootInUserNamespace(uid, () => USERNS_MAP)).toBe(false); + }); + test.skipIf(!linuxOnly)('rejects when uid_map cannot be read', () => { + expect(unmappedRootInUserNamespace(65534, () => { throw new Error('ENOENT'); })).toBe(false); + }); + test.skipIf(linuxOnly)('rejects off Linux', () => { + expect(unmappedRootInUserNamespace(65534, () => USERNS_MAP)).toBe(false); + }); +}); + +const bwrap = '/usr/bin/bwrap'; +describe('ensureDirectory inside a user namespace', () => { + // /var and /var/tmp are host root-owned (sticky 1777), so they stat as uid 65534 in the namespace; the writable-ancestor check stays satisfied. + test.skipIf(!linuxOnly || !fs.existsSync(bwrap) || !fs.existsSync('/var/tmp'))('secureDirectory succeeds under bwrap --unshare-user', () => { + const home = fs.mkdtempSync(path.join('/var/tmp', 'cso-userns-')); + try { + const script = `import { secureDirectory } from ${JSON.stringify(path.resolve(import.meta.dir, '../lib/cso/state.ts'))}; +secureDirectory(${JSON.stringify(path.join(home, 'a', 'b'))}); console.log('OK');`; + const file = path.join(home, 'probe.ts'); + fs.writeFileSync(file, script); + const r = spawnSync(bwrap, ['--unshare-user', '--ro-bind', '/', '/', '--proc', '/proc', '--dev', '/dev', '--bind', home, home, process.execPath, file], { encoding: 'utf8', timeout: 60_000 }); + expect(r.stderr).not.toContain('UNSAFE_PATH'); + expect(r.stdout).toContain('OK'); + expect(r.status).toBe(0); + } finally { fs.rmSync(home, { recursive: true, force: true }); } + }); +});