Skip to content

[HUST CSE][dfs] fix stack buffer overflow on long path components in devtmpfs - #11842

Open
HANYAODONG wants to merge 7 commits into
RT-Thread:masterfrom
HANYAODONG:fix/devtmpfs-path-bounds
Open

HANYAODONG wants to merge 7 commits into
RT-Thread:masterfrom
HANYAODONG:fix/devtmpfs-path-bounds

Conversation

@HANYAODONG

Copy link
Copy Markdown

[

为什么提交这份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 的实现保持一致:

  1. _path_separate() 增加 parent_size / file_size 参数,越界时返回 -ENAMETOOLONG;
  2. _get_subdir() 增加 name_size 参数,越界时返回 -ENAMETOOLONG;
  3. 两个调用点检查返回值: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):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

…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]
@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix/devtmpfs-path-bounds
    Enter PR branch fix/devtmpfs-path-bounds in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix/devtmpfs-path-bounds 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/devtmpfs-path-bounds branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c
  • components/dfs/utest/Kconfig
  • components/dfs/utest/SConscript
  • components/dfs/utest/tc_devtmpfs_path.c

📊 Current Review Status (Last Updated: 2026-11-24 22:51 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@CLAassistant

CLAassistant commented Sep 27, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c:63 still dereferences path[0] before validating path. This is probably protected by the VFS dentry invariant, but checking path == RT_NULL before 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] == '/');
  2. 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 描述中的复现和验证信息比较完整。

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

at32f415-start

  • RAM: .bss -8 B (-0.2%, 4,572 B / 32,768 B, total: 14% used)
  • ROM: .text +124 B (+0.2%, 65,524 B / 262,144 B, total: 25% used)

esp32-c3

  • dram0_0_seg: .dram0.bss +9,208 B, .dram0.data +204 B, .dram0.dummy -13,312 B, .stack +4 B (-1.8%, 216,832 B / 325,120 B, total: 67% used)
  • drom0_0_seg: .eh_frame +78,544 B, .eh_frame_hdr +12,376 B, .flash.rodata +27,368 B, .flash_rodata_dummy +196,608 B (+76.6%, 725,908 B / 8,388,576 B, total: 9% used)
  • iram0_0_seg: .iram0.text -13,092 B, .iram0.text_end -220 B (-17.8%, 61,440 B / 325,120 B, total: 19% used)
  • iram0_2_seg: .flash.text +199,912 B (+83.8%, 421,880 B / 8,388,576 B, total: 5% used)
  • rtc_iram_seg: .rtc.text +8 B (+60.0%, 64 B / 8,192 B, total: 1% used)

gd32105r-start

  • CODE: .text +120 B (+0.2%, 69,436 B / 262,144 B, total: 26% used)
  • DATA: .bss -4 B (-0.1%, 3,748 B / 98,304 B, total: 4% used)

hc32f334

  • FLASH: .text +120 B (+0.1%, 99,692 B / 131,072 B, total: 76% used)
  • RAM: .bss -4 B (-0.1%, 5,328 B / 32,768 B, total: 16% used)

hpmicro-hpm5301evklite

  • DLM: .bss -2,320 B, .data -464 B (+0.5%, 58,560 B / 131,072 B, total: 45% used)
  • ILM: .fast -14,384 B, .vectors -96 B (-37.9%, 23,728 B / 131,072 B, total: 18% used)
  • XPI0: .data -464 B, .fast -14,384 B, .text -33,240 B, .vectors -96 B (-38.6%, 76,788 B / 1,048,576 B, total: 7% used)

infineon-psoc6

  • flash: .text +120 B (+0.1%, 116,292 B / 262,144 B, total: 44% used)

k230

  • SRAM: .bss +62,528 B, .data +1,144 B, .eh_frame +36,800 B, .sbss +172 B, .srodata.cst8 +40 B, .stack +8 B, .text +166,696 B (+23.7%, 1,395,912 B / 60,682,240 B, total: 2% used)

loongson-ls1cdev

  • Code: .text +676 B (+0.2%, 363,980 B)
  • Data: .bss -4 B (-0.0%, 59,808 B)

nordic-nrf51822

  • FLASH: .text +124 B (+0.2%, 52,132 B / 262,144 B, total: 20% used)

nuvoton-m487

  • CODE: .text +944 B (+0.3%, 356,952 B / 524,288 B, total: 68% used)
  • DATA: .bss -4 B (-0.0%, 101,928 B / 163,840 B, total: 62% used)

qemu-virt64-aarch64

  • Code: .eh_frame -14,904 B, .text -49,968 B (-6.0%, 1,012,348 B)
  • Data: .bss +598,160 B, .data -47,560 B (+106.4%, 1,068,120 B)

raspberry-pico-rp2040

  • FLASH: .rodata +40 B, .text +96 B (+0.1%, 114,272 B / 2,097,152 B, total: 5% used)

renesas-ra2l1

  • FLASH: .text +260 B (+0.3%, 98,512 B / 262,144 B, total: 38% used)

simulator

  • Code: .eh_frame +4 B, .rela.dyn +1,704 B, .rodata +233 B, .text +2,048 B (+0.1%, 2,676,684 B)
  • Data: .data +1,824 B (+0.1%, 1,610,852 B)

stm32f407-rt-spark

  • CODE: .text +120 B (+0.1%, 86,168 B / 1,048,576 B, total: 8% used)

stm32l475-atk-pandora-llvm

  • RAM1: .stack +4 B (+0.1%, 4,244 B / 98,304 B, total: 4% used)
  • ROM: .text +152 B (+0.2%, 82,804 B / 524,288 B, total: 16% used)

wch-ch32v208w-r0

  • FLASH: .text +400 B (+0.3%, 159,976 B / 491,520 B, total: 33% used)

x86

  • Code: .rodata +32 B, .text +384 B (+0.2%, 199,293 B)
  • Data: .bss -32 B (-0.2%, 14,548 B)

xuantie-e901plus

  • DSRAM: .bss -8 B (-0.1%, 14,004 B / 524,288 B, total: 3% used)
  • ISRAM: .rodata +64 B, .text +64 B (+0.3%, 46,488 B / 131,072 B, total: 35% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

…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]
@HANYAODONG

Copy link
Copy Markdown
Author

Thanks for the review! Both suggestions are addressed in 6b5e2f7.

1. Validate before dereference - _path_separate() now checks path together with the other arguments before RT_ASSERT(path[0] == '/'). The assertion stays, but nothing is dereferenced before the check.

2. Boundary tests - added components/dfs/utest/tc_devtmpfs_path.c behind a new RT_UTEST_TC_USING_DEVTMPFS_PATH option (depends on RT_USING_DFS_V2, so no existing utest config changes). For component lengths 8, 254, 255, 256 and 300 it covers both entry points (dfs_file_stat() for lookup, open(O_CREAT) for create): 254/255 must succeed, 256/300 must be rejected and create nothing. A guard pattern around each call checks that neighbouring stack data is untouched.

Verified on bsp/qemu-vexpress-a9 with the real utest framework compiled into the image:

  • with the fix: === RESULT: PASSED (0 failure) ===
  • with the bounds checks reverted: [ASSERT FAILED] test_devtmpfs_component_256 followed by the 0x41414141 backtrace - so the test does catch the original defect.

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.
@HANYAODONG

Copy link
Copy Markdown
Author

Pushed a4163bf to fix the clang-format violations that the "Check File
Format and License" check reported on the new test file - whitespace only,
no functional change.

@HANYAODONG

Copy link
Copy Markdown
Author

All review points are addressed in a4163bf, and the change is verified
locally: clean build on bsp/qemu-vexpress-a9, boundary test passes on QEMU
(254/255 accepted, 256/300 rejected).

This is ready from my side. The workflow suites on this PR are waiting for
maintainer approval before they can run, so the CI results will show up once
they are approved.

Thanks!

@HANYAODONG

Copy link
Copy Markdown
Author

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

  1. components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c:63 still dereferences path[0] before validating path. This is probably protected by the VFS dentry invariant, but checking path == RT_NULL before 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] == '/');
  2. 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 描述中的复现和验证信息比较完整。

@BernardXiong since the review is already positive, the only thing missing on
this PR is CI: the seven workflow suites have been sitting in
action_required since Oct 17 (the earlier batch was auto-marked failed on
Nov 16 after waiting 30 days), so they never actually ran and the red checks
are not real failures. Could you approve them so the build/format results
show up?

Both suggestions from the review were addressed in 6b5e2f7 and a4163bf.
Thanks!

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review / 审查

结论 / Conclusion: 修复方向正确,未发现阻塞性问题;构建/测试检查尚待 CI 运行。

_path_separate() / _get_subdir() 现在接收缓冲区大小并在拷贝前返回 -ENAMETOOLONG,两个调用点都检查返回值并走既有清理路径;devtmpfs.c:63-133 与 tmpfs 的实现保持一致。测试覆盖 254/255/256/300 字节分量及短名称回归,边界选择与 DIRENT_NAME_MAX 的终止符语义一致。

非阻塞建议 / Suggestions

  1. devtmpfs.c:130 的 name[0] = '\0' 发生在 name 指针自增之后,实际清空的是当前位置而非首字节;调用方已 memset,当前无影响,但建议保存起始指针或删除该行,避免误导。
  2. Kconfig:44-51 只依赖 RT_USING_DFS_V2,建议同时依赖 RT_USING_DFS_DEVFS(以及 RT_USING_UTESTCASES),否则未挂载 devtmpfs 时该测试仍会被编入,只能依赖 init 阶段跳过。
  3. 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.
@HANYAODONG

Copy link
Copy Markdown
Author

Review / 审查

结论 / Conclusion: 修复方向正确,未发现阻塞性问题;构建/测试检查尚待 CI 运行。

_path_separate() / _get_subdir() 现在接收缓冲区大小并在拷贝前返回 -ENAMETOOLONG,两个调用点都检查返回值并走既有清理路径;devtmpfs.c:63-133 与 tmpfs 的实现保持一致。测试覆盖 254/255/256/300 字节分量及短名称回归,边界选择与 DIRENT_NAME_MAX 的终止符语义一致。

非阻塞建议 / Suggestions

  1. devtmpfs.c:130 的 name[0] = '\0' 发生在 name 指针自增之后,实际清空的是当前位置而非首字节;调用方已 memset,当前无影响,但建议保存起始指针或删除该行,避免误导。
  2. Kconfig:44-51 只依赖 RT_USING_DFS_V2,建议同时依赖 RT_USING_DFS_DEVFS(以及 RT_USING_UTESTCASES),否则未挂载 devtmpfs 时该测试仍会被编入,只能依赖 init 阶段跳过。
  3. 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.

@BernardXiong 感谢第二轮评审。三条建议都已在 bcf1cd7 处理:

  1. devtmpfs.c:130 —— 确认是笔误。原来的 name[0] = '\0' 写在循环体内,而循环里已经
    做过 name++,实际清空的是当前位置而非首字节。现在在函数入口保存 name_start,
    失败路径改为通过 *name_start 复位。现有调用方都先 memset,运行行为不变,但实现
    与注释表达的意思一致了。

  2. utest Kconfig:44-51 —— 已补 depends on RT_USING_DFS_DEVFS 和
    depends on RT_USING_UTESTCASES。后者确认是 components/utilities/utest/Kconfig
    里的 bool 符号,不会因符号不存在而恒为假。这样未挂载 devtmpfs 或未开启
    utestcases 时该用例不会被编进来,不必依赖运行期跳过。

  3. tc_devtmpfs_path.c:84 —— 注释与实现不一致,已改。TC_MNT_PATH 固定为 /dev,
    因为 devtmpfs_init() 总是把 devtmpfs 挂在 /dev;RT_UTEST_DFS_MNT_PATH 只决定
    通用 DFS API 测试自己的挂载点,与本用例无关。

本地验证(bsp/qemu-vexpress-a9,scons -c 后重新编译):text 段 553567 字节;QEMU 上
256 / 300 字节分量返回 -ENAMETOOLONG(errno=-2),8 / 254 / 255 正常创建节点,
边界测试 PASSED (0 failure)。新增和修改的行符合仓库 .clang-format 风格。

另外想麻烦你一件事:PR 顶部目前仍显示 “7 workflows awaiting approval”,当前 head
bcf1cd7 的 check-suites 里有 7 个 action_required、3 个 queued,Actions 中还没有
对应的运行记录,看起来是推了新提交之后需要再批准一次。方便时麻烦在 Checks 区域点
一下 Approve and run,构建和测试结果就能出来。谢谢。

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@HANYAODONG

Copy link
Copy Markdown
Author

@BernardXiong 目前此pr7个 workflow 全部通过,您这边已经 approve,
mergeable 为 clean,目前没有其他待处理项。

如果需要补充信息或者调整什么内容麻烦您告知,谢谢。

Comment thread components/dfs/utest/tc_devtmpfs_path.c Outdated
(void)dfs_file_stat(tc_path, &st);

/* creation path: this is where _path_separate() runs */
fd = open(tc_path, O_CREAT | O_RDWR, 0644);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

成功用例留下了 /dev 节点。第二轮 open(O_CREAT) 会直接打开已有节点,跳过要测的创建路径。请用 O_EXCL 确认本轮创建成功,结束时只清理本轮创建的节点,再连续运行两轮。

Comment thread components/dfs/utest/tc_devtmpfs_path.c Outdated
/* 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个用例首次创建节点时,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.
@HANYAODONG

Copy link
Copy Markdown
Author

@Rbb666 感谢评审,两条都成立,已在 98b9cf3 修改。

  1. tc_devtmpfs_path.c:127 —— 确认问题存在。成功用例跑完没有删除节点,所以第二轮的
    open(O_CREAT) 直接打开了已有节点,devtmpfs_create_vnode() 根本没被再次进入。
    现在创建改用 O_CREAT | O_EXCL:open 成功即证明是本轮创建的;用例结束只删除本轮
    创建的节点并回查确认已消失;每个分量长度连跑两轮。

  2. tc_devtmpfs_path.c:213 —— 也确认了。devtmpfs_create_vnode() 的 parent_path[4096]
    加 file_name[256],单帧就 4352 字节,而 utest 框架的线程栈是 4096
    (components/utilities/utest/utest.c:32)。现在文件系统调用放在一个 8 KB 栈的
    worker 线程里执行;rt_thread_create() 失败和工作线程超时都按用例失败上报,不再
    靠越界踩 utest 线程栈。路径缓冲 tc_path 本来就是 static,不在栈上,所以没有搬动。

验证(bsp/qemu-vexpress-a9,真实 utest 框架编入固件):

utest_run components.dfs.devtmpfs_path_tc 2
→ loop 1/2 PASSED,loop 2/2 PASSED
→ 跑完 ls /dev,没有残留的测试节点

反证(把 devtmpfs.c 换回未修复版本,同一用例重跑):
→ test_devtmpfs_component_256 断言失败
→ test_devtmpfs_component_300 出现 0x41414141 与 data abort
说明用例对缺陷仍然敏感,不是改成假通过。

另外这个 head 的 workflow 需要重新批准才能跑,麻烦一并放行,谢谢。

@HANYAODONG
HANYAODONG requested a review from Rbb666 November 24, 2026 10:52

@Rbb666 Rbb666 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复核了 98b9cf3d。测试已改为独占创建,并在 8 KiB 栈的工作线程中执行。关于节点清理、边界用例的结果汇总和工作线程,还有五条行内修改建议。

Comment thread components/dfs/utest/tc_devtmpfs_path.c Outdated
Comment on lines +152 to +156
if (expect_ok)
{
/* drop whatever a previous round left behind, so that O_EXCL below
* really exercises the creation path */
(void)unlink(tc_path);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] 保留测试前已存在的节点

请移除创建前的 unlink(tc_path)。如果路径已存在,应让 open(..., O_CREAT | O_EXCL, ...) 报告 EEXIST 并保留原节点。当前写法会先删除原节点,再把本轮创建判为成功。

Comment thread components/dfs/utest/tc_devtmpfs_path.c Outdated
{
/* 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] 清理时确认节点由本轮创建

请把清理放到 tc_roundtrip() 的统一出口。独占创建成功后立即记录节点归属,确保后续检查失败时也能关闭描述符并删除本轮创建的节点。这里的 tc_path 只记录最后尝试的路径,无法据此判断归属。

Comment thread components/dfs/utest/tc_devtmpfs_path.c Outdated
Comment on lines +378 to +382
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] 保留每个边界用例的失败结果

请在一次 UTEST_UNIT_RUN 中遍历这些长度,记录失败的长度并在最后统一断言。框架每次进入新的测试单元都会清零 failed_num。在主机内存模拟中,只让 8 字节名称创建失败,能看到该项触发断言,但最后一个用例通过后,整组仍报告 PASSED。

Comment thread components/dfs/utest/tc_devtmpfs_path.c Outdated
Comment on lines +293 to +296
if (tc_ctx.finished != RT_TRUE)
{
rt_kprintf("worker did not finish within %d ms\n", TC_WORKER_TIMEOUT_MS);
return -101;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] 避免超时线程改写下一轮结果

超时返回后,工作线程仍可能继续运行。如果下一轮已复用 tc_ctx 和 tc_path,旧线程就可能把下一轮标记为完成。请用完成通知同步结果,并在旧线程结束后再复用这两份状态。

Comment thread components/dfs/utest/tc_devtmpfs_path.c Outdated
* issued from a worker thread with a stack large enough for it.
*/
#define TC_WORKER_STACK_SIZE 8192
#define TC_WORKER_PRIORITY 20

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.
@HANYAODONG

Copy link
Copy Markdown
Author

@Rbb666 感谢二次复核,五条都成立,已在 557c58d 处理。

  1. [P1] 创建前的 unlink 已移除。节点已存在时 open(O_CREAT|O_EXCL) 会报错,用例按
    失败上报,不再删除原有节点后再把本轮创建判为成功。

  2. [P2] 清理改到 tc_roundtrip() 的统一出口:独占创建成功后立刻置位 created,后续
    任何检查失败都会先关闭描述符、再删除本轮创建的节点。utest_tc_cleanup() 里原来
    基于 tc_path 的兜底 unlink 已删除——那个变量只记录最后尝试的路径,确实无法
    证明归属。

  3. [P2] 五种长度改到同一个 UTEST_UNIT_RUN 里遍历,逐档记录失败,最后统一断言。
    已实测:临时给 254 这一档注入一个失败(临时钩子,未提交),后面几档都通过,
    结果仍然是

    case 254 byte component, round 1 failed with -99
    [ FAILED ] components.dfs.devtmpfs_path_tc

    说明前面的失败不会再被后面的通过掩盖。这条原来是真 bug,谢谢指出。

  4. [P2] 每轮用独立的 job 槽位(4 个轮转),worker 超时即中止整个长度遍历,不会让
    下一轮复用共享状态;迟到的 worker 只会写进自己的槽位,tc_path 在超时后不再使用。

    这里没有引入 rt_sem 做完成通知,是为了不给用例增加 RT_USING_SEMAPHORE 依赖
    (DFS 只 select 了 RT_USING_MUTEX)。如果项目更倾向用信号量,我可以改。

  5. [P2] 优先级改为 RT_THREAD_PRIORITY_MAX / 2,8 级配置下是 4,不会触发断言。

验证(bsp/qemu-vexpress-a9,真实 utest 框架):
utest_run components.dfs.devtmpfs_path_tc 2
→ loop 1/2 PASSED,loop 2/2 PASSED
→ 跑完 ls /dev,无残留节点

反证:换回未修复的 devtmpfs.c 后,256 那一档报 "expected rejection, open()
succeeded",随后的查找与 unlink 走到 0x41414141 / data abort,说明用例仍能抓到
缺陷。

另外这个 head 的 workflow 需要重新批准,麻烦一并放行,谢谢。

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复核范围: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 的构建。

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants