[HUST CSE][dfs-v1]fix: validate tmpfs path components and ramfs entry names#11304
[HUST CSE][dfs-v1]fix: validate tmpfs path components and ramfs entry names#11304Telecaster2147 wants to merge 2 commits intoRT-Thread:masterfrom
Conversation
|
👋 感谢您对 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-03-31 15:53 CST)
📝 Review Instructions
|
There was a problem hiding this comment.
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. |
fd241fb to
f239638
Compare
|
@Rbb666 您好,copilot提供的审查意见已经全部解决 |
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
这份 PR主要处理 DFS V1 里两个实际可达的固定长度名字问题:tmpfs 的路径越界写,以及 ramfs 的文件名未保证 NUL结尾。前者会在路径查找和创建过程中把超长名字写进固定大小缓冲区,后者会把一个不完整的伪字符串留在目录项里,后续再进入 rt_strcmp()一类字符串操作时,读取范围就不再受控。
你的解决方案是什么 (what is your solution)
这个 PR 分成两部分,分别对应 tmpfs 和 ramfs。
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()这些关键入口接住返回值,路径非法或名字过长时直接返回,不再继续走后面的字符串比较和目录项写入。ramfs 部分:
_ramfs_set_name(),统一处理从路径得到文件名这一步。/开头、去掉前导/后是否为空、名字里是否还包含/、名字长度是否超出 RAMFS_NAME_MAX - 1。\0。这样后续dfs_ramfs_lookup()里的rt_strcmp(dirent->name, subpath)才能在受控范围内工作。dfs_ramfs_open()和dfs_ramfs_rename()都改成调用这个 helper,并在失败时返回真实错误码。创建新文件失败时还会释放刚申请的 dirent,避免留下无用分配。测试:
/tmp/...,确认接口返回-1且errno == ENAMETOOLONG。请提供验证的bsp和config (provide the config and bsp)
为了验证 DFS v1 这条补丁链路,本地使用了下面这组配置:
CONFIG_RT_USING_DFS_V1=y# CONFIG_RT_USING_DFS_V2 is not setCONFIG_RT_USING_DFS_TMPFS=yCONFIG_RT_USING_DFS_RAMFS=y默认的
qemu-vexpress-a9配置走的是 DFS v2,而这次修复落在 components/dfs/dfs_v1,所以验证时需要切到 DFS v1 才能覆盖到目标代码路径。]
当前拉取/合并请求的状态 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