Skip to content

fix(init): validate temporary node_modules parent - #36142

Merged
nathanwhit merged 4 commits into
denoland:mainfrom
nathanwhit:fix/temp-node-modules-parent
Jul 21, 2026
Merged

nathanwhit merged 4 commits into
denoland:mainfrom
nathanwhit:fix/temp-node-modules-parent

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

Summary

  • validate the temporary node_modules root and dated directory before use
  • create Unix directories with owner-only permissions and repair permissive modes
  • fall back to a fresh random temporary directory when the preferred layout is unusable

Details

create_temp_node_modules_dir() assumed that <tmp>/deno_nm was a usable
directory and ran dated-directory cleanup before validating it. Unexpected
filesystem entries or permissive directory modes could therefore affect both
cleanup and package initialization.

The preferred root and dated directory are now checked before cleanup or use.
If the preferred layout cannot be established, initialization continues in a
fresh temporary directory without running cleanup through that path.

Validation

  • ./tools/format.js --check
  • cargo test -p deno util::temp --lib -- --nocapture
  • cargo check -p deno

AI assistance was used to help implement and test this change.

@bartlomieju

Copy link
Copy Markdown
Member

Nice hardening. Validating the root/dated dirs before running cleanup and falling back gracefully instead of hard-erroring is a clear improvement, and the two new tests cover the repair and symlink-rejection paths well.

One security nuance worth a look, given the intent of the change: ensure_secure_temp_dir has a TOCTOU window in the check → repair sequence, and the post-repair re-check verifies the mode but not ownership.

The flow is:

  1. symlink_metadata — reject symlink/non-dir, check uid == geteuid()
  2. set_permissions(path, 0o700) — this follows symlinks
  3. symlink_metadata — mode check only (no uid re-check)

If an attacker can replace the path entry between steps 1 and 2, deno could either chmod 0o700 an attacker-chosen target (symlink swap) or proceed to use an attacker-owned directory that now has mode 0o700 (dir swap — the mode re-check in step 3 passes because ownership is never re-verified).

The practical precondition is a parent the attacker can write to:

  • For the deno_nm root, that means a non-sticky /tmp. On a standard sticky /tmp, the step-1 uid check plus the sticky bit prevent replacing an entry deno owns, so it's not exploitable there.
  • For the dated subfolder, the parent is already secured to 0o700, so it's safe.

So this isn't exploitable on a correctly configured system, but since the whole point of the PR is symlink/permission safety, it may be worth closing the window:

  • fchmod on an fd opened with O_NOFOLLOW | O_DIRECTORY, or
  • re-stat and compare st_ino/st_uid after the repair rather than only the mode.

Minor, non-blocking: on a multi-user host where another user already owns /tmp/deno_nm, every run now takes the fallback path (uid mismatch → bail) and loses the dated-folder caching. That's strictly better than the pre-PR hard error and the fallback TempDir is RAII-cleaned, so it's fine — just noting it.

@bartlomieju

Copy link
Copy Markdown
Member

Solid hardening, and the fd-tied chmod + ownership check + O_NOFOLLOW/O_DIRECTORY open are the right primitives. The two new tests cover the repair and symlink-rejection paths well. Two things worth considering:

1. The fd-based validation guards only the chmod, not the destructive GC that runs by path right after. In create_secure_day_folder, ensure_secure_temp_dir validates the root via an fd and closes it on return, then attempt_temp_dir_garbage_collection(root_temp_folder, ...) re-opens the root by path (std::fs::read_dir(root_temp_folder)) and rm_rfs every non-dated entry. So on a world-writable, non-sticky TMPDIR, an attacker who wins the race to replace <tmp>/deno_nm with a symlink to a victim-owned tree after validation but before read_dir can redirect the recursive delete outside the temp area.

The comment ("Keep the check, permission repair, and final validation tied to the same directory so replacing the path cannot redirect the chmod") is accurate for the chmod, but that guarantee doesn't extend to the GC or the subsequent TempDir::new_in(&day_folder), both of which re-resolve by path. It's constrained (sticky /tmp prevents cross-user replacement, and the day-folder uid check backstops the non-GC operations), so this is defense-in-depth rather than a confirmed exploit path. But since closing this class of race is the whole point of the PR, it'd be worth either doing the GC relative to the validated fd (openat-style) or scoping the comment so the guarantee isn't over-claimed.

2. Fallback temp dirs (<tmp>/deno_nm_*) are never reclaimed by GC. The success path nests the TempDir under deno_nm/<date>/, which attempt_temp_dir_garbage_collection sweeps, but the fallback creates deno_nm_XXXX directly in <tmp>. TempDir cleans it up on drop, but if the process is killed on the fallback path — which is the ownership-mismatch case that recurs every run for an affected user — these leak permanently, since nothing sweeps the deno_nm_ prefix. Minor, but the fallback reintroduces the un-reclaimed-temp problem the dated layout exists to avoid; a periodic sweep of that prefix (or reusing the dated layout for the fallback where possible) would close it.

Neither is blocking.

@nathanwhit
nathanwhit merged commit bee9c4c into denoland:main Jul 21, 2026
136 checks passed
bartlomieju pushed a commit that referenced this pull request Jul 23, 2026
## Summary

- validate the temporary `node_modules` root and dated directory before
use
- create Unix directories with owner-only permissions and repair
permissive modes
- fall back to a fresh random temporary directory when the preferred
layout is unusable

## Details

`create_temp_node_modules_dir()` assumed that `<tmp>/deno_nm` was a
usable
directory and ran dated-directory cleanup before validating it.
Unexpected
filesystem entries or permissive directory modes could therefore affect
both
cleanup and package initialization.

The preferred root and dated directory are now checked before cleanup or
use.
If the preferred layout cannot be established, initialization continues
in a
fresh temporary directory without running cleanup through that path.

## Validation

- `./tools/format.js --check`
- `cargo test -p deno util::temp --lib -- --nocapture`
- `cargo check -p deno`

AI assistance was used to help implement and test this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants