Repository navigation
FS: fix symlink race in recursive rmdir - #1168
basavaraj-sm05 wants to merge 1 commit into
Conversation
9af5406 to
1b801fb
Compare
|
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:
|
f59288a to
4f44c5a
Compare
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.
4f44c5a to
a5b7300
Compare
|
The 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 One small platform note in case an errno ever ends up asserted: darwin returns ENOTDIR there rather than the ELOOP Linux gives for |
Proposed changes
fs.rmdirSync(),fs.rmdir()andfs.promises.rmdir()with{recursive: true}hand the tree tonjs_ftw()/qjs_ftw(), which work on full paths: an entry is classified withlstat(), opened with a plainopen(path, O_RDONLY)and finally removed withremove(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 theremove_dir_allissue in Rust (CVE-2022-21658) andos.RemoveAllin 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 thelstat()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 thelstat()oftree/subto make the window reliable. During the sleep the test runsmv tree/sub sub.moved && ln -s ../out tree/sub, whereout/sits outside the tree and holdsf0..f4. On masterrmdirSync('tree', {recursive: true})reports success and all five files inout/are gone, on both the njs and QuickJS engines. With this patch the same run stops withELOOPandout/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 andunlinkat()(withAT_REMOVEDIRfor 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, theNJS_MAX_PATHcopy and length check inside it went away too. The change is identical inexternal/njs_fs_module.candexternal/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 othermake js_testfailures 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:
CONTRIBUTINGdocument