Skip to content

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

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

HANYAODONG wants to merge 3 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-10-17 21:58 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

at32f415-start

  • ROM: .text +20 B (+0.0%, 65,420 B / 262,144 B, total: 25% used)

esp32-c3

  • dram0_0_seg: .dram0.dummy +512 B (+0.2%, 221,248 B / 325,120 B, total: 68% used)
  • iram0_0_seg: .iram0.text +658 B, .iram0.text_end -146 B (+0.7%, 75,264 B / 325,120 B, total: 23% used)

gd32105r-start

  • CODE: .text +20 B (+0.0%, 69,336 B / 262,144 B, total: 26% used)

hc32f334

  • FLASH: .text +20 B (+0.0%, 99,592 B / 131,072 B, total: 76% used)

hpmicro-hpm5301evklite

  • ILM: .fast +32 B (+0.1%, 38,240 B / 131,072 B, total: 29% used)
  • XPI0: .fast +32 B (+0.0%, 125,000 B / 1,048,576 B, total: 12% used)

infineon-psoc6

  • flash: .text +20 B (+0.0%, 116,192 B / 262,144 B, total: 44% used)

k230

  • SRAM: .eh_frame +32 B (+0.0%, 1,128,547 B / 268,300,288 B, total: 0% used)

loongson-ls1cdev

  • Code: .text +56 B (+0.0%, 363,360 B)

nordic-nrf51822

  • FLASH: .text +28 B (+0.1%, 52,036 B / 262,144 B, total: 20% used)

nuvoton-m487

  • CODE: .text +168 B (+0.0%, 356,176 B / 524,288 B, total: 68% used)

qemu-virt64-aarch64

  • Code: .eh_frame +48 B, .text +544 B (+0.1%, 1,077,812 B)

raspberry-pico-rp2040

  • FLASH: .text +32 B (+0.0%, 114,168 B / 2,097,152 B, total: 5% used)

renesas-ra2l1

  • FLASH: .text +24 B (+0.0%, 98,276 B / 262,144 B, total: 37% used)

simulator

  • Code: .rela.dyn +192 B, .rodata +32 B, .text +101 B (+0.0%, 2,673,020 B)
  • Data: .data +256 B (+0.0%, 1,609,284 B)

stm32f407-rt-spark

  • CODE: .text +20 B (+0.0%, 86,068 B / 1,048,576 B, total: 8% used)
  • RAM1: .stack +4 B (+0.1%, 5,476 B / 131,072 B, total: 4% used)

stm32l475-atk-pandora-llvm

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

wch-ch32v208w-r0

  • FLASH: .text +24 B (+0.0%, 159,600 B / 491,520 B, total: 32% used)

x86

…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.

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.

3 participants