Address Cody review round 4: CODY-006 state-directory ownership

The state directory must now be owned by the bridge's effective uid. A
directory owned by anyone else is refused with StateDirectoryError before
any file is created or opened, and is left exactly as found, even when its
mode is already 0700. Mode enforcement (exactly 0700) is unchanged and runs
after the ownership check.

Tests: the real /root as an unprivileged user; a deterministic injected-uid
case proving no database/WAL/SHM is created and the directory is not
mutated; a foreign-owned loose-mode directory left unmodified; a user-
namespace fixture using the unmodified production path.

Checks: typecheck clean; npm test 198 passed; client build ok;
test:real-stt 11 passed; test:real-hermes 9 passed; test:simulator 6
passed; git diff --check clean.

Not reviewed. No physical-device, droplet or production evidence.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-18 20:40:06 -08:00
co-authored by Claude Fable 5.2
parent 1507cea077
commit f02715fb64
7 changed files with 255 additions and 17 deletions
+21 -10
View File
@@ -93,21 +93,29 @@ export class StateDirectoryError extends Error {
}
/**
* Make the directory that holds the database owner-only (0700), whether it was just created
* or already existed, and confirm it. A directory that cannot be restricted (not ours, or on
* a filesystem without permissions) is refused: the bridge does not keep credentials where
* other users can list or replace files.
* Make sure the directory that holds the database is the bridge's own: owned by the user the
* bridge runs as, and owner-only (0700), whether it was just created or already existed.
*
* Ownership is checked first and is never "fixed": "0700" only means private if the owner is
* us. A directory that belongs to anyone else is refused exactly as found, even when its mode
* is already 0700, and nothing is created in it. There is no trusted-other-owner option.
*/
export function secureStateDir(dir: string): void {
export function secureStateDir(dir: string, effectiveUid: number | undefined = process.geteuid?.()): void {
if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: DIR_MODE });
const who = effectiveUid === undefined ? 'the user it runs as' : `uid ${effectiveUid}, the user it runs as`;
const explain = (why: string) =>
new StateDirectoryError(
`The state directory ${dir} ${why}. The bridge keeps device credentials there and needs a ` +
`directory of its own, owned by the user it runs as, with mode 0700. ` +
`Create one (for example: install -d -m 0700 ${dir}) or point HERMEVEN_DB_PATH at one.`,
`directory of its own, owned by ${who}, with mode 0700. Create one (for example: ` +
`install -d -m 0700 -o <service-user> -g <service-group> ${dir}) or point HERMEVEN_DB_PATH at one.`,
);
let info = statSync(dir);
if (!info.isDirectory()) throw explain('is not a directory');
// `effectiveUid` is undefined only on platforms without POSIX users, where there is no
// ownership to compare.
if (effectiveUid !== undefined && info.uid !== effectiveUid) {
throw explain(`is owned by uid ${info.uid}, not by the bridge, and has been left untouched`);
}
if ((info.mode & 0o777) !== DIR_MODE) {
try {
chmodSync(dir, DIR_MODE);
@@ -117,12 +125,15 @@ export function secureStateDir(dir: string): void {
info = statSync(dir);
if ((info.mode & 0o777) !== DIR_MODE) throw explain('did not keep mode 0700 after it was set');
}
// Re-checked after any change: the directory we are about to use is ours and private.
if (effectiveUid !== undefined && info.uid !== effectiveUid) throw explain('changed owner while being secured');
}
export function openDb(path: string): Db {
export function openDb(path: string, options: { effectiveUid?: number } = {}): Db {
if (path !== ':memory:') {
// Before anything is created inside it.
secureStateDir(dirname(path));
// Before anything is created or opened inside it. `effectiveUid` exists for tests; the
// bridge always uses the real effective uid of the process.
secureStateDir(dirname(path), options.effectiveUid ?? process.geteuid?.());
// Create the file ourselves, owner-only, BEFORE SQLite opens it. SQLite gives the WAL and
// SHM files the same permissions as the database file, so they are never world-readable,
// not even for a moment.