jingfei195887 opened a new pull request, #21825:
URL: https://github.com/apache/nuttx/pull/21825
## Summary
* `open_pseudodir()` reads `inode->i_child` and takes a reference on it
with no lock held, and `seek_pseudodir()` reads `fd_root->i_child`
before it takes the inode tree lock. On SMP a concurrent
`inode_insert()`/`inode_unlink()` can change `i_child` between the read
and the reference, so the directory cursor (`pdir->next`) can point at an
inode it holds no reference on. A concurrent `inode_remove()` then frees
it and the next `readdir()` uses the freed inode.
* Take the first child and its reference under `inode_lock()` in
`open_pseudodir()`, and read the seek starting point under the lock in
`seek_pseudodir()`. This completes #21690, which put `read_pseudodir()`
under the same lock.
## Impact
* New or changed feature: NO.
* Impact on user: NO, only the pseudo file system directory cursor.
* Impact on build: NO.
* Impact on hardware: NO.
* Impact on documentation: NO.
* Impact on security: NO.
* Impact on compatibility: NO.
* Locking: the callers (`file_vopen()` -> `dir_allocate()`, and the
directory `open` used by `dup()`) hold no inode tree lock when they reach
`open_pseudodir()`, so taking `inode_lock()` there cannot self-deadlock.
## Testing
I confirm that changes are verified on local setup and works as intended:
* Build Host: Ubuntu 22.04 x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
* Target: `sim:smp` (4 CPUs) and `sim:nsh`, master 562db43781 (master is now
26adb69b76; no new commit touches
fs/vfs/fs_dir.c). Verified on
sim only.
On `sim:smp` one thread registers and unregisters drivers under `/dev` in
a loop while the main thread runs `opendir()`/`readdir()`/`closedir()` on
`/dev` 200 times. The race is not reproduced on sim: master finished all 3
runs and this PR all 13 runs, with no crash or hang on either side.
* `./tools/checkpatch.sh -m -g master..HEAD`: all checks pass.
* `sim:nsh` Make and CMake builds pass with no new warnings; `ostest`
exits with status 0.
## PR verification Self-Check
* [x] This PR introduces only one functional change.
* [x] I have updated all required description fields above.
* [x] My PR adheres to Contributing Guidelines and Documentation (git commit
title and message, coding standard, etc).
* [ ] My PR is still work in progress (not ready for review).
* [x] My PR is ready for review and can be safely merged into a codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]