Skip to content

FS: fix symlink race in recursive rmdir - #1168

Open
basavaraj-sm05 wants to merge 1 commit into
nginx:masterfrom
basavaraj-sm05:fs-rmtree-symlink-race
Open

basavaraj-sm05 wants to merge 1 commit into
nginx:masterfrom
basavaraj-sm05:fs-rmtree-symlink-race

Conversation

@basavaraj-sm05

Copy link
Copy Markdown
Contributor

Proposed changes

fs.rmdirSync(), fs.rmdir() and fs.promises.rmdir() with {recursive: true} hand the tree to njs_ftw() / qjs_ftw(), which work on full paths: an entry is classified with lstat(), opened with a plain open(path, O_RDONLY) and finally removed with remove(path). Every one of those calls resolves the whole path again, so this is a time-of-check/time-of-use symlink race (CWE-367), the same class as the remove_dir_all issue in Rust (CVE-2022-21658) and os.RemoveAll in Go. If a script removes a directory that another, less trusted local user can write into (an upload or spool area, a per-user cache under /tmp), that user can swap a subdirectory for a symlink right after the lstat() and the walk then reads and deletes the symlink target with the privileges of the nginx worker.

I confirmed it with a scratch build that only adds a sleep(1) after the lstat() of tree/sub to make the window reliable. During the sleep the test runs mv tree/sub sub.moved && ln -s ../out tree/sub, where out/ sits outside the tree and holds f0..f4. On master rmdirSync('tree', {recursive: true}) reports success and all five files in out/ are gone, on both the njs and QuickJS engines. With this patch the same run stops with ELOOP and out/ is untouched.

The fix keeps the existing walker but makes every step relative to the directory that is already open: fstatat(AT_SYMLINK_NOFOLLOW) for the check, openat(O_DIRECTORY | O_NOFOLLOW) to descend and unlinkat() (with AT_REMOVEDIR for directories) in the rmtree callback. Nothing re-resolves a path an attacker can swap any more, and a swapped symlink can only make the walk fail, never redirect it. Since the walker no longer builds full paths, the NJS_MAX_PATH copy and length check inside it went away too. The change is identical in external/njs_fs_module.c and external/qjs_fs_module.c.

I didn't add a regression test because the race needs a second process acting in the middle of the walk, which the JS test harness can't express. The existing test/fs/rmdir.t.mjs (nested tree removal, symlink to a directory, missing path) passes on both engines, and I also checked a tree containing symlinks to an outside directory and file, a dangling link and a trailing slash in the path: the links are removed and their targets survive. These runs were on a Cygwin build of the CLI with QuickJS linked in; the few other make js_test failures there (fs permission and WebCrypto tests) fail the same way on master.

Checklist

Before creating a PR, run through this checklist and mark each as complete:

  • I have read the CONTRIBUTING document
  • If applicable, I have added tests that prove my fix is effective or that my feature works
  • If applicable, I have checked that any relevant tests pass after adding my changes

@xeioex

xeioex commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

@basavaraj-sm05

Thanks for the patch! The race is real, and switching njs_ftw() / qjs_ftw() to descriptor-relative fstatat(), openat(), and unlinkat() is the right approach.

While testing it, I noticed a few edge cases in the original commit (9af5406) that still needed to be handled:

  • The FTW_MOUNT and cycle checks ran before openat(), so they used st from fstatat() rather than fstat() on the directory descriptor actually opened for traversal.
  • For the root of the walk (parent == NULL), calling fstatat(AT_FDCWD, path, ...) before openat() resolved the input path twice and would also remove a non-directory via unlinkat(..., 0) if the root entry was replaced after the initial rmdir(path). Opening the root directly with openat(..., O_RDONLY | O_DIRECTORY | O_NOFOLLOW) and taking st from fstat(dfd) avoids the extra lookup and ensures the root is a directory.
  • When rmdir(path) failed with ENOTEMPTY on a path ending in . or .. (for example sub/..), *_file_tree_walk() entered *_ftw() and deleted the parent directory's contents before failing on unlinkat(). Trailing slashes on the root path also needed to be stripped in pathbuf so O_NOFOLLOW applies to the root entry itself.
    I rebased the branch onto master and amended the commit to cover these cases, preserve errno across closedir() / close() on the error path, and add a regression test in test/fs/rmdir.t.mjs

@xeioex
xeioex force-pushed the fs-rmtree-symlink-race branch 2 times, most recently from f59288a to 4f44c5a Compare December 2, 2026 03:09
Recursive rmdir checked directory entries and then opened and removed
entries by full path. A concurrent change could redirect the walk and
cause it to remove entries outside the requested tree.

Look up, open and remove entries relative to the already opened parent
directory with fstatat(), openat() and unlinkat(). Open each directory
without following symlinks and use the opened descriptor to check
filesystem boundaries and cycles. Strip trailing slashes and reject "."
and ".." before starting the walk.
@xeioex
xeioex force-pushed the fs-rmtree-symlink-race branch from 4f44c5a to a5b7300 Compare December 2, 2026 03:09
@basavaraj-sm05

Copy link
Copy Markdown
Contributor Author

The sub/.. case is the one I am glad you caught. That one does not even need a race: *_file_tree_walk() happily entered the parent and emptied it before unlinkat() refused, so rejecting "." and ".." and stripping trailing slashes before the walk starts is the right place to handle it. Taking st from fstat(dfd) instead of the fstatat() result also makes the FTW_MOUNT and cycle checks mean what they say, since the descriptor is what the traversal actually uses, and opening the root directly removes the double lookup I had left in.

I pulled your amended commit and went over it on macOS, which is a different platform from the Cygwin build I originally tested on, so it exercises the darwin *at() paths. It builds clean with -Werror under clang, and test/fs passes on both engines, including the new rmdir assertions. I also re-ran the race reproducer against it: with a 1s sleep spliced in after the fstatat() of tree/sub and that directory swapped for a symlink to an outside directory during the window, master reports success and all five files outside the tree are gone on both engines, while your version leaves them untouched.

One small platform note in case an errno ever ends up asserted: darwin returns ENOTDIR there rather than the ELOOP Linux gives for openat(O_DIRECTORY | O_NOFOLLOW) on a symlink. Nothing in the test depends on it today. Nothing left on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

2 participants