Skip to content

[HUST CSE][dfs-v1]fix: validate tmpfs path components and ramfs entry names#11304

Open
Telecaster2147 wants to merge 2 commits intoRT-Thread:masterfrom
Telecaster2147:security/dfs-name-bounds-hardening
Open

[HUST CSE][dfs-v1]fix: validate tmpfs path components and ramfs entry names#11304
Telecaster2147 wants to merge 2 commits intoRT-Thread:masterfrom
Telecaster2147:security/dfs-name-bounds-hardening

Conversation

@Telecaster2147
Copy link
Copy Markdown
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

这份 PR主要处理 DFS V1 里两个实际可达的固定长度名字问题:tmpfs 的路径越界写,以及 ramfs 的文件名未保证 NUL结尾。前者会在路径查找和创建过程中把超长名字写进固定大小缓冲区,后者会把一个不完整的伪字符串留在目录项里,后续再进入 rt_strcmp()一类字符串操作时,读取范围就不再受控。

你的解决方案是什么 (what is your solution)

这个 PR 分成两部分,分别对应 tmpfs 和 ramfs。

  1. tmpfs 部分:

    • _path_separate() 增加缓冲区大小参数,在复制 parent_path和 file_name 之前先检查长度,避免把超长路径分量写出界。
    • _get_subdir() 增加 name_size 参数,在逐字节复制子目录名时实时检查剩余空间,保证最后一定能写入 \0。
    • 新增 _tmpfs_path_validate(),对空指针和非绝对路径做运行时校验,减少 release 配置下仅靠断言带来的不确定性。
    • dfs_tmpfs_lookup()dfs_tmpfs_open()dfs_tmpfs_rename() 这些关键入口接住返回值,路径非法或名字过长时直接返回,不再继续走后面的字符串比较和目录项写入。
  2. ramfs 部分:

    • 新增 _ramfs_set_name(),统一处理从路径得到文件名这一步。
    • 这个 helper 会检查路径是否为空、是否以 / 开头、去掉前导 / 后是否为空、名字里是否还包含 /、名字长度是否超出 RAMFS_NAME_MAX - 1。
    • 通过后再把名字写进 dirent->name,并补上 \0。这样后续 dfs_ramfs_lookup() 里的 rt_strcmp(dirent->name, subpath) 才能在受控范围内工作。
    • dfs_ramfs_open()dfs_ramfs_rename() 都改成调用这个 helper,并在失败时返回真实错误码。创建新文件失败时还会释放刚申请的 dirent,避免留下无用分配。
  3. 测试:

    • 给 tmpfs 补了一个 utest,用长文件名打开 /tmp/...,确认接口返回 -1errno == ENAMETOOLONG

请提供验证的bsp和config (provide the config and bsp)

  • BSP: bsp/qemu-vexpress-a9
  • .config:

为了验证 DFS v1 这条补丁链路,本地使用了下面这组配置:

  • CONFIG_RT_USING_DFS_V1=y
  • # CONFIG_RT_USING_DFS_V2 is not set
  • CONFIG_RT_USING_DFS_TMPFS=y
  • CONFIG_RT_USING_DFS_RAMFS=y

默认的 qemu-vexpress-a9 配置走的是 DFS v2,而这次修复落在 components/dfs/dfs_v1,所以验证时需要切到 DFS v1 才能覆盖到目标代码路径。

  • action:暂无

]

当前拉取/合并请求的状态 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
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@Telecaster2147 Telecaster2147 requested a review from Rbb666 as a code owner March 30, 2026 13:09
@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

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:security/dfs-name-bounds-hardening
  • 设置PR number为 \ Set the PR number to:11304
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 security/dfs-name-bounds-hardening 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the security/dfs-name-bounds-hardening 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
Copy link
Copy Markdown

github-actions bot commented Mar 30, 2026

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/dfs/dfs_v1/filesystems/ramfs/dfs_ramfs.c
  • components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c

📊 Current Review Status (Last Updated: 2026-03-31 15:53 CST)

  • Maihuanyi Pending Review

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens DFS v1 tmpfs/ramfs pathname handling to prevent fixed-size buffer overflows and unterminated entry names, and adds a tmpfs utest to verify long-name rejection.
本 PR 加固 DFS v1 的 tmpfs/ramfs 路径与名字处理,避免固定大小缓冲区越界与目录项名字未 NUL 结尾的问题,并补充 tmpfs 的长文件名拒绝用例。

Changes:

  • tmpfs: add runtime path validation, size-aware path splitting, and bounded subdir parsing to reject overlong components.
  • tmpfs: ensure tmpfs entry names are always NUL-terminated when copied.
  • ramfs: introduce a helper to validate and safely copy entry names; add tmpfs utest for ENAMETOOLONG behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
examples/utest/testcases/tmpfs/tmpfs.c Adds a utest that attempts to create a file with an overlong name under /tmp and asserts the expected failure.
components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c Adds tmpfs path validation, passes buffer sizes into helpers, and tightens name copying/termination.
components/dfs/dfs_v1/filesystems/ramfs/dfs_ramfs.c Adds _ramfs_set_name() to validate/sanitize names and ensure dirent->name is always NUL-terminated.

@Telecaster2147 Telecaster2147 force-pushed the security/dfs-name-bounds-hardening branch from fd241fb to f239638 Compare March 31, 2026 07:28
@Telecaster2147
Copy link
Copy Markdown
Contributor Author

@Rbb666 您好,copilot提供的审查意见已经全部解决

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants