Repository navigation
[HUST CSE][dfs] fix stack buffer overflow on long path components in devtmpfs - #11842
HANYAODONG wants to merge 7 commits into
Conversation
…devtmpfs
_path_separate() and _get_subdir() copied an unbounded path component into
caller-provided buffers, so a single component longer than DIRENT_NAME_MAX
(256) bytes overran the 256-byte file_name[]/subdir_name[] stack arrays used
by devtmpfs_create_vnode() and devtmpfs_file_lookup().
dentry->pathname can be up to DFS_PATH_MAX - 4 = 4092 bytes because
_dentry_create() only strips the mount point prefix, so the overflow length
and its contents are attacker controlled. It is reachable from
open("/dev/<long name>", O_CREAT) - including from user mode through
sys_open() - and from the msh command mkdir /dev/<long name>.
On bsp/qemu-vexpress-a9 an over-long single component produced a data abort
whose backtrace contained 0x41414141, i.e. the saved return address
overwritten with the attacker supplied path bytes.
The tmpfs implementation already validates these lengths and returns
-ENAMETOOLONG (dfs_tmpfs.c), this brings devtmpfs in line with it:
- pass parent_size/file_size to _path_separate() and name_size to
_get_subdir(), rejecting oversized components with -ENAMETOOLONG
- check the return value at both call sites; create_vnode() and
file_lookup() now fail cleanly instead of corrupting the stack
[HUST CSE]
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-24 22:51 CST)
📝 Review Instructions
|
BernardXiong
left a comment
There was a problem hiding this comment.
Review / 审查
结论 / Conclusion: no blocking issue found; the bounds-checking fix is directionally correct.
The new parent_size/file_size/name_size checks prevent the reported stack overwrite, and the vnode cleanup path is present. The change is small and keeps devtmpfs consistent with tmpfs.
建议 / Suggestions
-
components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c:63still dereferencespath[0]before validatingpath. This is probably protected by the VFS dentry invariant, but checkingpath == RT_NULLbefore the assertion would make this helper robust against malformed internal callers and consistent with the tmpfs implementation.if (path == RT_NULL || parent_path == RT_NULL || file_name == RT_NULL ...) return -EINVAL; RT_ASSERT(path[0] == '/');
-
Please add boundary regression tests for component lengths 254, 255, and 256 bytes, covering both vnode creation and lookup. The test should verify that an overlong component cannot modify neighboring stack data and that the expected error is observable by the caller.
The PR description already contains a useful reproduction and validation record. / PR 描述中的复现和验证信息比较完整。
|
…ndary tests Address the review of RT-Thread#11842. _path_separate() dereferenced path[0] in RT_ASSERT before any of its arguments had been validated, so the helper was not robust against a NULL path from a malformed internal caller. Move the argument checks ahead of the assertion, which also makes the helper consistent with the tmpfs one. Add a boundary regression test for the path components of devtmpfs, with the cases the review asked for: - component lengths DIRENT_NAME_MAX - 2 (254) and DIRENT_NAME_MAX - 1 (255) must still create the node, - DIRENT_NAME_MAX (256) and 300 bytes must be rejected with an error the caller can observe, and must not create anything, - both entry points are covered for every length: devtmpfs_file_lookup() through dfs_file_stat() and devtmpfs_create_vnode() through open(), - a guard pattern around the call verifies that an overlong component does not modify neighbouring stack data. The test lives in components/dfs/utest/tc_devtmpfs_path.c behind its own Kconfig option (RT_UTEST_TC_USING_DEVTMPFS_PATH, depends on DFS v2) so it does not change any existing utest configuration. Verified on bsp/qemu-vexpress-a9 with the real utest framework compiled into the image: the fixed tree passes all five cases (0 failure), while the tree without the fix fails at the 256-byte case and then aborts with the 0x41414141 backtrace, which is the overflow this patch prevents. [HUST CSE]
|
Thanks for the review! Both suggestions are addressed in 6b5e2f7. 1. Validate before dereference - 2. Boundary tests - added Verified on
|
The new test file failed the "Check File Format and License" workflow because the macro alignment did not match the repository .clang-format. Run clang-format on it; whitespace only, no functional change.
|
Pushed a4163bf to fix the clang-format violations that the "Check File |
|
All review points are addressed in a4163bf, and the change is verified This is ready from my side. The workflow suites on this PR are waiting for Thanks! |
@BernardXiong since the review is already positive, the only thing missing on Both suggestions from the review were addressed in 6b5e2f7 and a4163bf. |
BernardXiong
left a comment
There was a problem hiding this comment.
Review / 审查
结论 / Conclusion: 修复方向正确,未发现阻塞性问题;构建/测试检查尚待 CI 运行。
_path_separate() / _get_subdir() 现在接收缓冲区大小并在拷贝前返回 -ENAMETOOLONG,两个调用点都检查返回值并走既有清理路径;devtmpfs.c:63-133 与 tmpfs 的实现保持一致。测试覆盖 254/255/256/300 字节分量及短名称回归,边界选择与 DIRENT_NAME_MAX 的终止符语义一致。
非阻塞建议 / Suggestions
- devtmpfs.c:130 的
name[0] = '\0'发生在name指针自增之后,实际清空的是当前位置而非首字节;调用方已memset,当前无影响,但建议保存起始指针或删除该行,避免误导。 - Kconfig:44-51 只依赖
RT_USING_DFS_V2,建议同时依赖RT_USING_DFS_DEVFS(以及RT_USING_UTESTCASES),否则未挂载 devtmpfs 时该测试仍会被编入,只能依赖 init 阶段跳过。 - tc_devtmpfs_path.c:84 注释称挂载点取自
RT_UTEST_DFS_MNT_PATH,但tc_build_path()实际硬编码/dev;请统一注释或改为读取配置。
CI:本 head 的 7 个工作流此前因首次贡献者处于 action_required。我已静态核对全部 4 个文件,未涉及 workflow、构建脚本、依赖或外部下载,并批准了对应运行;构建/测试结果出来前不给出 APPROVE。
Approved runs: utest_auto_run, pkgs_test, MemBrowse, Format/License, Static analysis, BSP static build, ToolsCI.
Three non-blocking points from the second review. 1. _get_subdir(): the -ENAMETOOLONG path did name[0] = '\0' after the loop had already advanced name, so it cleared the current position instead of the first byte. Keep the start pointer and reset through it. Callers memset() the buffer today, so behaviour is unchanged, but the intent is now what the code says. 2. utest Kconfig: the case needs devtmpfs mounted on /dev and the utest case framework, so also depend on RT_USING_DFS_DEVFS and RT_USING_UTESTCASES. Otherwise it is compiled when devtmpfs is not mounted and can only be skipped at run time. 3. tc_devtmpfs_path.c: the comment claimed the mount point comes from RT_UTEST_DFS_MNT_PATH while tc_build_path() always uses /dev. Correct the comment: devtmpfs_init() always mounts devtmpfs on /dev, and that option only selects where the generic DFS API tests mount their own filesystem.
@BernardXiong 感谢第二轮评审。三条建议都已在 bcf1cd7 处理:
本地验证(bsp/qemu-vexpress-a9,scons -c 后重新编译):text 段 553567 字节;QEMU 上 另外想麻烦你一件事:PR 顶部目前仍显示 “7 workflows awaiting approval”,当前 head |
BernardXiong
left a comment
There was a problem hiding this comment.
Review / 审查 — bcf1cd73
结论 / Conclusion: APPROVE
增量复核确认三点均已落实:_get_subdir() 在入口保存 name_start,失败路径用它复位首字节(devtmpfs.c:117、devtmpfs.c:131);测试 Kconfig 增加 RT_USING_DFS_DEVFS 与 RT_USING_UTESTCASES 依赖(Kconfig:48-49);测试注释与固定 /dev 挂载点一致(tc_devtmpfs_path.c:84)。边界用例覆盖 254/255/256/300 字节分量。
本 head 的 utest、BSP 静态构建、静态分析等 7 个工作流全部通过,未发现阻塞问题。
All three earlier suggestions are addressed at this head, and all seven workflows (utest, BSP static build, static analysis, etc.) passed.
|
@BernardXiong 目前此pr7个 workflow 全部通过,您这边已经 approve, 如果需要补充信息或者调整什么内容麻烦您告知,谢谢。 |
| (void)dfs_file_stat(tc_path, &st); | ||
|
|
||
| /* creation path: this is where _path_separate() runs */ | ||
| fd = open(tc_path, O_CREAT | O_RDWR, 0644); |
There was a problem hiding this comment.
成功用例留下了 /dev 节点。第二轮 open(O_CREAT) 会直接打开已有节点,跳过要测的创建路径。请用 O_EXCL 确认本轮创建成功,结束时只清理本轮创建的节点,再连续运行两轮。
| /* a normal, short component must keep working (no regression) */ | ||
| static void test_devtmpfs_component_short(void) | ||
| { | ||
| uassert_int_equal(tc_roundtrip(8, RT_TRUE), 0); |
There was a problem hiding this comment.
这个用例首次创建节点时,devtmpfs_create_vnode() 的两个局部数组默认就占 4352 字节,超过 32 位默认 utest 线程的 4096 字节栈。请把路径缓冲移出栈并处理分配失败,或给用例配置足够的执行栈。
… frames
Two points from review.
1. The success cases left the /dev node behind, so a later round's
open(O_CREAT) simply reopened it and devtmpfs_create_vnode() was never
entered again. Open with O_CREAT | O_EXCL so that a successful open proves
this round created the node, remove only the node this round created, and
run every component length twice.
2. devtmpfs_create_vnode() keeps parent_path[DFS_PATH_MAX] and
file_name[DIRENT_NAME_MAX] on its own frame: 4352 bytes before anything
else is accounted for. The utest framework runs test cases on a 4096 byte
thread, which cannot hold that frame. The filesystem calls now run on a
worker thread with an 8 KB stack, and both a failed rt_thread_create() and
a worker that does not finish in time are reported as test failures instead
of quietly corrupting the utest stack. tc_path was already static, so no
path buffer had to move.
Verified with the real utest framework on bsp/qemu-vexpress-a9:
utest_run components.dfs.devtmpfs_path_tc 2
both loops PASSED, and ls /dev afterwards shows no node left behind.
With devtmpfs.c reverted to the unfixed version the same case fails:
test_devtmpfs_component_256 asserts and test_devtmpfs_component_300 reaches
0x41414141 followed by a data abort, so the case still catches the defect.
|
@Rbb666 感谢评审,两条都成立,已在 98b9cf3 修改。
验证(bsp/qemu-vexpress-a9,真实 utest 框架编入固件): utest_run components.dfs.devtmpfs_path_tc 2 反证(把 devtmpfs.c 换回未修复版本,同一用例重跑): 另外这个 head 的 workflow 需要重新批准才能跑,麻烦一并放行,谢谢。 |
Rbb666
left a comment
There was a problem hiding this comment.
复核了 98b9cf3d。测试已改为独占创建,并在 8 KiB 栈的工作线程中执行。关于节点清理、边界用例的结果汇总和工作线程,还有五条行内修改建议。
| if (expect_ok) | ||
| { | ||
| /* drop whatever a previous round left behind, so that O_EXCL below | ||
| * really exercises the creation path */ | ||
| (void)unlink(tc_path); |
There was a problem hiding this comment.
[P1] 保留测试前已存在的节点
请移除创建前的 unlink(tc_path)。如果路径已存在,应让 open(..., O_CREAT | O_EXCL, ...) 报告 EEXIST 并保留原节点。当前写法会先删除原节点,再把本轮创建判为成功。
| { | ||
| /* every round removes the node it created; this is only a safety net in | ||
| * case a case aborted half way through */ | ||
| (void)unlink(tc_path); |
There was a problem hiding this comment.
[P2] 清理时确认节点由本轮创建
请把清理放到 tc_roundtrip() 的统一出口。独占创建成功后立即记录节点归属,确保后续检查失败时也能关闭描述符并删除本轮创建的节点。这里的 tc_path 只记录最后尝试的路径,无法据此判断归属。
| UTEST_UNIT_RUN(test_devtmpfs_component_short); | ||
| UTEST_UNIT_RUN(test_devtmpfs_component_254); | ||
| UTEST_UNIT_RUN(test_devtmpfs_component_255); | ||
| UTEST_UNIT_RUN(test_devtmpfs_component_256); | ||
| UTEST_UNIT_RUN(test_devtmpfs_component_300); |
There was a problem hiding this comment.
[P2] 保留每个边界用例的失败结果
请在一次 UTEST_UNIT_RUN 中遍历这些长度,记录失败的长度并在最后统一断言。框架每次进入新的测试单元都会清零 failed_num。在主机内存模拟中,只让 8 字节名称创建失败,能看到该项触发断言,但最后一个用例通过后,整组仍报告 PASSED。
| if (tc_ctx.finished != RT_TRUE) | ||
| { | ||
| rt_kprintf("worker did not finish within %d ms\n", TC_WORKER_TIMEOUT_MS); | ||
| return -101; |
There was a problem hiding this comment.
[P2] 避免超时线程改写下一轮结果
超时返回后,工作线程仍可能继续运行。如果下一轮已复用 tc_ctx 和 tc_path,旧线程就可能把下一轮标记为完成。请用完成通知同步结果,并在旧线程结束后再复用这两份状态。
| * issued from a worker thread with a stack large enough for it. | ||
| */ | ||
| #define TC_WORKER_STACK_SIZE 8192 | ||
| #define TC_WORKER_PRIORITY 20 |
There was a problem hiding this comment.
[P2] 工作线程优先级应适配 8 级配置
请将固定值 20 改为 RT_THREAD_PRIORITY_MAX / 2。内核配置为 8 级优先级时,有效范围是 0~7,当前值会在创建工作线程时触发优先级断言。
… rounds Five points from the re-review. 1. Do not unlink before creating. A node that already exists is left alone: open(O_CREAT | O_EXCL) now reports it and the test fails instead of deleting somebody else's node and then counting its own creation. 2. Ownership and cleanup moved to a single exit path in tc_roundtrip(). created is set right after the exclusive create succeeds, so the descriptor is closed and the node removed even when a later check fails. utest_tc_cleanup() no longer unlinks tc_path, which only held the last attempted path and could not prove ownership. 3. All lengths run inside one UTEST_UNIT_RUN(). utest_unit_run() resets failed_num on every unit, so the five separate units could let a later passing unit report PASSED while an earlier one failed. Failures are now counted per length and asserted once at the end. 4. Each round takes its own job slot, and a worker that misses its timeout stops the sweep instead of letting the next round reuse the shared state. Four slots are rotated so a late worker can never overwrite a newer result, and the path buffer is only used while no worker is outstanding. 5. TC_WORKER_PRIORITY is RT_THREAD_PRIORITY_MAX / 2 instead of a fixed 20, which would be out of range on kernels configured with eight levels. Verified on bsp/qemu-vexpress-a9 with the real utest framework: utest_run components.dfs.devtmpfs_path_tc 2 both loops PASSED, and ls /dev afterwards shows no node left behind. Aggregate assertion checked by injecting a failure for the 254 byte length only (temporary hook, not committed): the case reports case 254 byte component, round 1 failed with -99 [ FAILED ] components.dfs.devtmpfs_path_tc even though the remaining lengths pass, so an early failure is no longer masked. With devtmpfs.c reverted to the unfixed version the 256 byte case is reported as "expected rejection, open() succeeded" and the following lookup and unlink on the overlong path end in 0x41414141 / data abort, so the case still catches the defect.
|
@Rbb666 感谢二次复核,五条都成立,已在 557c58d 处理。
验证(bsp/qemu-vexpress-a9,真实 utest 框架): 反证:换回未修复的 devtmpfs.c 后,256 那一档报 "expected rejection, open() 另外这个 head 的 workflow 需要重新批准,麻烦一并放行,谢谢。 |
BernardXiong
left a comment
There was a problem hiding this comment.
复核范围:bcf1cd73 → 557c58dd 的增量,仅 components/dfs/utest/tc_devtmpfs_path.c(+222/-54)。
已确认 Rbb666 在 98b9cf3 上的五点意见在该 head 中处理:
- 创建前不再 unlink 既有节点,改用
O_CREAT | O_EXCL证明创建路径; - 清理集中到单一出口,只删除本轮创建的节点;
- 所有长度合并进一个 utest 单元,避免后续通过单元覆盖先前失败;
- 用 8 KiB 工作线程执行,避开
devtmpfs_create_vnode()的栈帧; - 失败结果聚合,超时后停止后续轮次。
增量静态审查未发现新的阻塞问题。
验证缺口:该 head 仅 license/cla、assign-reviewers、labeler、notify-format 通过,没有编译/utest 运行记录(unknown),因此本轮不据此给出 APPROVE。合并前建议触发一次包含 dfs utest 的构建。
[
为什么提交这份PR (why to submit this PR)
components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c中的_path_separate()与_get_subdir()没有接收缓冲区长度参数,会把任意长度的路径分量直接
rt_memcpy进调用者的固定大小栈数组:_path_separate()(:82-83)把最后一级分量拷进file_name[DIRENT_NAME_MAX](256 字节),随后还在
file_name[path_q - path_p]写一个 NUL;_get_subdir()(:104)逐字节写subdir_name[DIRENT_NAME_MAX](256 字节)。DIRENT_NAME_MAX = 256(components/libc/compilers/common/include/dirent.h:57),而
dentry->pathname最长可达DFS_PATH_MAX - 4 = 4092字节(
_dentry_create()只剥掉挂载点/dev前缀,dfs_dentry.c:74-85)。因此单个路径分量超过 255 字节时即发生栈缓冲区溢出,越界可达约 3.8 KB,
写入的内容与长度均由调用者控制,能够覆盖保存的返回地址。
这不是理论问题:上游
tmpfs的同名函数dfs_tmpfs.c:55-140已经加上了parent_size/file_size/name_size校验并返回-ENAMETOOLONG,只有 devfs 这一份漏了,属于同类修复的漏项。
你的解决方案是什么 (what is your solution)
让 devfs 与 tmpfs 的实现保持一致:
_path_separate()增加parent_size/file_size参数,越界时返回-ENAMETOOLONG;_get_subdir()增加name_size参数,越界时返回-ENAMETOOLONG;devtmpfs_create_vnode()失败时走原有的dfs_vnode_destroy()清理路径并返回
NULL,devtmpfs_file_lookup()失败时返回NULL。改动 1 file changed, 48 insertions(+), 8 deletions(-),不改变正常路径行为。
请提供验证的bsp和config (provide the config and bsp)
BSP: bsp/qemu-vexpress-a9
.config: 使用该 BSP 默认配置,未额外改动;相关项为
CONFIG_RT_USING_DFS=y
CONFIG_RT_USING_DFS_V2=y
CONFIG_RT_USING_DFS_DEVFS=y
action: 尚未触发 fork 上的 action;本地已完成等价的编译验证,见下方"验证情况"。
如需 CI 链接,可在本 PR 建立后补充。
验证情况:
使用 arm-none-eabi-gcc 13.3.0 在 bsp/qemu-vexpress-a9 上编译链接通过
(text 552603, data 34384, bss 79600);
在 QEMU (vexpress-a9) 上端到端复现,触发方式为
open("/dev/<300 x 'A'>", O_CREAT|O_RDWR, 0666):修复前:
backtrace: ... 41414141 <- 'A' 覆盖了返回地址
data abort: Exception: pc :0x60011718
用 arm-none-eabi-addr2line 解析该 pc,得到
devtmpfs_file_lookup components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c:220
即
_path_separate()的越界写破坏了devtmpfs_create_vnode()的栈帧。修复后,同一输入:
open() = -1, errno = ENOENT
RETURNED NORMALLY -- kernel stack survived
调用链:
open("/dev/<超长分量>", O_CREAT|O_RDWR, 0666)→
dfs_file_open()(dfs_file.c:565)→dfs_dentry_lookup()(:596)查找失败→
O_CREAT分支 →dfs_dentry_create()(:668)→devtmpfs_create_vnode()(:487)→
_path_separate():82 越界;第二条入口是dfs_file_mknod()(:903)。Shell 等价操作:
mkdir /dev/<超长名>。]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up