Skip to content

fix: elevated runs with a preserved HOME create a root-owned update cache in the user home #70

Closed

Description

@abruption

What happened

When codex-unlock runs as root while HOME still points to a regular user's home (for example an elevated shell that preserves HOME), the automatic update refresh creates the update cache inside that user's home owned by UID 0. Afterwards, update checks by the owning user fail permanently.

The cache location is derived from XDG_CACHE_HOME or homedir() ($HOME). ensurePrivateDirectory creates it with mkdirSync(..., { recursive: true, mode: 0o700 }) and compares ownership only with the current UID, so a UID 0 process accepts a root-owned directory inside another user's home.

Reproduced in an isolated Linux container (no network, no host mounts, synthetic home, empty CODEX_HOME), using docker exec --user 0 and --user 1000:1000 with the same HOME to model "elevated process with preserved HOME". sudo itself was not exercised.

Scenario Result
A. User's ~/.cache exists (UID 1000, 0700); UID 0 runs interactive list Refresh child creates ~/.cache/codex-unlock as UID 0/0700. As UID 1000, two refreshes return cache_directory_is_unsafe with no fetch; check-update --json exits 3 with update check failed: cache_directory_is_unsafe
B. ~/.cache does not exist; same sequence Both ~/.cache and ~/.cache/codex-unlock are created as UID 0/0700; UID 1000 gets EACCES and cache_directory_unavailable
C. Control: UID 1000 only Both refreshes updated, cache owned by UID 1000
D. Control: UID 0, non-interactive list No cache created (refresh is scheduled only for interactive runs)

In scenario B, other XDG tools of that user are also affected because ~/.cache itself becomes root-owned. list --json as the user kept working (exit 0) in all scenarios.

What you expected

An elevated process does not create or modify cache directories in another user's home. The update cache still fails closed for unsafe ownership, but the automatic notice/refresh path does not cause that state.

Environment

  • codex-unlock 0.4.0 at 6227e6c (src/update.ts and src/cli.ts identical to the v0.4.0 release commit)
  • Linux aarch64 container, Node.js 24, node:24-bookworm-slim

Source evidence

  • Cache root from XDG_CACHE_HOME or homedir():

    codex-unlock/src/update.ts

    Lines 271 to 275 in 6227e6c

    userHome: string = homedir(),
    ): UpdateCacheLocation {
    const configured = environment.XDG_CACHE_HOME;
    const root = configured && isAbsolute(configured) ? configured : join(userHome, ".cache");
    const directory = join(root, "codex-unlock");
  • Recursive creation and current-UID-only ownership check:

    codex-unlock/src/update.ts

    Lines 180 to 188 in 6227e6c

    function ensurePrivateDirectory(path: string, uid: number): string | null {
    try {
    mkdirSync(path, { recursive: true, mode: 0o700 });
    const initial = lstatSync(path);
    if (
    !initial.isDirectory() ||
    initial.isSymbolicLink() ||
    initial.uid !== uid
    ) {
  • Automatic refresh from interactive commands:

    codex-unlock/src/cli.ts

    Lines 257 to 262 in 6227e6c

    function finishAutomaticAdvisory(advisory: PreparedUpdateAdvisory): void {
    emitHumanUpdateNotice(advisory.humanNotice);
    if (advisory.scheduleRefresh) {
    scheduleUpdateRefresh({ cliPath: process.argv[1] });
    }
    }
  • Detached refresh child inherits the environment:

    codex-unlock/src/update.ts

    Lines 801 to 814 in 6227e6c

    export function scheduleUpdateRefresh(
    options: ScheduleUpdateRefreshOptions,
    ): boolean {
    try {
    const spawnImpl = options.spawnImpl ?? spawn;
    const child = spawnImpl(
    options.executable ?? process.execPath,
    [options.cliPath, UPDATE_REFRESH_ARG],
    {
    detached: true,
    stdio: "ignore",
    env: options.environment ?? process.env,
    },
    );

Acceptance criteria

  • When running as UID 0 (or when the resolved cache root or home is not owned by the current UID), automatic update notices and refreshes are skipped without creating directories.
  • Explicit check-update in that situation fails with a clear reason instead of creating directories in another user's home.
  • Existing cache directories are never automatically chown/chmod-repaired.
  • Tests cover UID 0 with a foreign-owned HOME, with and without an existing ~/.cache.

Scope and limitations

  • Requires the user to run the tool with elevated privileges while HOME is preserved; no privilege escalation is involved. How often common sudo configurations preserve HOME was not measured.
  • Fails closed: the cache is rejected rather than trusted, and diagnostic commands are unaffected.

Checked

Activity

  1. added this to the v0.4.1 milestone on Sep 28, 2026
  2. added
    bug
    on Sep 28, 2026
  3. added a commit that references this issue on Sep 29, 2026
    d15c9f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions