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
19 changes: 18 additions & 1 deletion lib/cso/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,23 @@ export function stateRoot(env: Record<string, string | undefined> = 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;
Expand All @@ -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');
Expand Down
47 changes: 47 additions & 0 deletions test/cso-state-userns.test.ts
Original file line number Diff line number Diff line change
@@ -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 }); }
});
});
Loading