jingfei195887 opened a new pull request, #21868:
URL: https://github.com/apache/nuttx/pull/21868
## Summary
- **fs/inode: fix fdlist use-after-free during sync.**
`sync()` walks every task's fdlist through `nxsched_foreach()`
(`task_fssync()`). An exiting task stays in the pid hash from
`group_leave()` until `nxsched_releasepid()`, but `fdlist_free()` freed
the descriptor rows and `fl_fds` while leaving `fl_rows`/`fl_fds` pointing
at them. A concurrent `sync()` then read the freed table and faulted on
`f_refs` through a stale file pointer.
`fdlist_free()` now detaches the table under `fl_lock` (resetting the list
to its empty built-in row) and tears down the private copy without the
lock. `fdlist_get_by_index()` re-checks the row bound under the lock and
returns the file, so a concurrent reader sees an empty list instead of a
freed row.
- **fs/inode: copy the descriptor for other tasks' fd lists.**
`fdlist_get2()` returns `fdp`, a pointer into the descriptor table, after
dropping `fl_lock`. `fdlist_dump()` (run for every task by the assert
dump) and procfs `/proc/<pid>/group/fd` walk another task's list and then
read `fdp->f_backtrace`; if that task exits in between, `fdlist_free()`
has already freed the row. Add `fdlist_get_copy()` (fs-internal, in
`fs/inode/inode.h`), which copies the descriptor while `fl_lock` is held,
and use it in both readers. `fdlist_get2()` keeps returning `fdp` for
callers working on their own list.
## Impact
- New feature / API change: NO public API change. `fdlist_get_copy()` is
declared in the fs-internal `fs/inode/inode.h`; `fdlist_get2()` keeps its
signature and return values.
- Behavior: a reader racing with `fdlist_free()` gets `-EBADF` / no file
instead of touching freed memory.
- Build, hardware, documentation, security, compatibility: NO.
## Testing
Host: Ubuntu x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0. The
functional runs below used master 1033417331; the branch was then rebased
onto d9b891b2be (no conflicts, no change to the patched code) and the
build checks were rerun there.
`sim:smp` (4 CPUs) with `CONFIG_FS_LINKS` and `CONFIG_MM_FILL_ALLOCATIONS`:
a thread calls `sync()` in a loop while the main thread creates 2000 tasks,
each of which `dup()`s 40 descriptors (several fd blocks) and exits.
Three runs:
| | master | this PR |
|---|---|---|
| boots that reached the test | 3 | 3 |
| crashed (SIGSEGV) | 3 | 0 |
| finished 2000 tasks | 0 | 3 (11621, 14646, 17354 `sync()` calls) |
Four more fdsync runs of this PR under gdb also finished (one boot ran it
three times and powered off normally). The master crash, from gdb:
```
inode_free (inode=0xbea0) at inode/fs_inodefree.c:59
inode_release () at inode/fs_inoderelease.c:59
file_close () at vfs/fs_close.c:147
file_put () at inode/fs_files.c:918
task_fssync () at inode/fs_files.c:299
nxsched_foreach () at sched/sched_foreach.c:75
sync () at inode/fs_files.c:1032
```
Separately, `sim:smp` sometimes hung in the serial console while NSH was
reading input: on master 2 of 5 boots hung before the test started; on this
PR 2 of 5 hung before the test and the 3 that finished hung afterwards,
reading `poweroff`. In the hung state one CPU, in the timer interrupt,
waits for the console lock in `uart_poll_notify()`, while the NSH reader
releasing that lock waits for `g_cpu_irqlock` in `sched_unlock()`. This is
in the serial/SMP path, also seen on master, and unrelated to this change.
Second commit, `sim:smp` with `CONFIG_FS_BACKTRACE=8`: a thread reads
`/proc/<pid>/group/fd` of a task that `dup()`s 40 descriptors and exits,
1000 tasks in a row, counting backtrace columns that show the heap fill
patterns (freed rows):
This race was not reproduced on sim. `proc_read()` holds the critical
section across `proc_groupfd()`, and an exiting task also frees its fd list
inside the critical section (`exit()` enters it before `nxtask_exithook()`).
The two only interleave when the reader blocks between the lookup and the
`f_backtrace` read. Both commits read no freed rows (first commit: 17552
and 17499 reads in 2 runs; second commit: 18069, 18382, 19397, 18763 and
17291 reads in 5 runs). The second commit is a fix by inspection: the row
pointer no longer leaves `fl_lock`.
- `./tools/checkpatch.sh -g master..HEAD`: all checks pass.
- `sim:nsh` Make and CMake builds pass with no new warnings in the changed
files, both with default options and with `CONFIG_FS_BACKTRACE=8`;
`ostest` exits with status 0. Each commit builds on its own.
- Verified on sim only.
## 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]