Repository navigation
[dfs][iso9660] fix: validate directory record name length - #12062
BernardXiong wants to merge 3 commits into
Conversation
The name length field of an ISO9660 directory record comes from the medium and was used without validation. For regular files the code unconditionally stripped the two-byte version suffix, so a length of 0 or 1 underflowed the size_t and a later rt_strncpy() zeroed kernel memory until it reached an unmapped page. Clamp the length to the local name buffer and only strip the suffix when the name is long enough. Verified by building bsp/qemu-virt64-riscv with DFS v1 and ISO9660 enabled; the runtime reproduction on a block device was not executed.
|
👋 感谢您对 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-25 00:06 CST)
📝 Review Instructions
|
|
| namelen = rt_le32_to_cpu(dirent->namelen); | ||
|
|
||
| /* The name length comes from the medium, never trust it */ | ||
| if (namelen >= MAX_NAMELEN) |
There was a problem hiding this comment.
这里限制了输出长度,读取前还需要确认目录记录完整、文件名位于记录范围内。请在访问 name[0] 和调用转换函数前完成校验。
This bounds the output length. Please validate the directory record and the filename span before reading name[0] or calling the decoder.
There was a problem hiding this comment.
已按建议在读取 name[0] 和调用转换函数前校验目录记录与名称范围(commit fc4cfc3d0c,dfs_iso9660.c#L278):
namelen必须非 0;- 记录长度不得小于固定头,且
namelen必须落在记录范围内; - 记录长度不得超过当前扇区剩余空间,避免
sz回绕后越界; - 校验通过后才访问
name[0]或进入iso9660_convert_*。
已在启用 RT_USING_DFS_V1 + RT_USING_DFS_ISO9660 的 bsp/qemu-virt64-riscv 上重新编译并链接通过;未执行畸形 ISO 的运行时复现。
Applied before reading name[0] or calling the decoders (commit fc4cfc3d0c, dfs_iso9660.c#L278): the name length must be non-zero and fit inside the record, the record must be at least the fixed header size, and it must not extend past the current sector's remaining bytes. Rebuilt and linked bsp/qemu-virt64-riscv with RT_USING_DFS_V1 + RT_USING_DFS_ISO9660; no runtime malformed-ISO repro was executed.
Check the record length and the name length read from the medium before reading name[0] or converting the name, so a truncated or malformed directory record cannot make the lookup read past the sector buffer.
| if (dirent->name[0] >> 1 != 0 || dnamelen != 1) | ||
| { | ||
| /* This is not "." or "..", use this record. */ | ||
| break; |
There was a problem hiding this comment.
普通目录项在这里退出内层循环。若名称不匹配,下轮仍比较同一项;枚举时也会重复返回它。请在处理完普通项后按已校验的 dlen 前移,并用同目录的两个文件验证查找和枚举。
There was a problem hiding this comment.
已修复。新增 iso9660_next_dirent(),在跳过 "."/".." 以及处理完一个普通目录项后,按已校验的 dlen 前移,并统一处理扇区切换与目录结束;查找在名称不匹配时也会前移,枚举时每个条目只返回一次。
提交:66466907ee(dfs_iso9660.c#L218-L251,循环改动见 #L300-L412)。
验证:在 bsp/qemu-virt64-riscv 上以 RT_USING_DFS_V1 + RT_USING_DFS_ISO9660 完整编译并链接通过。随后用构造的、同一目录含两个文件的 ISO 启动 QEMU 做查找/枚举验证,但该 BSP 在中断控制器初始化阶段失败(Master pic interrupt-controller@d000000 not support),未进入 msh,因此本轮未完成运行时双文件验证;该项保留为后续验证项。
Fixed by adding iso9660_next_dirent(): after skipping "."/".." or handling an ordinary record, the scan advances by the validated dlen and handles sector rollover/end-of-directory. Lookup now advances on a name mismatch, and iteration returns each entry once.
Commit 6646690. The BSP builds and links with RT_USING_DFS_V1 + RT_USING_DFS_ISO9660. A QEMU run with a two-file ISO was attempted but the BSP failed before reaching the shell (interrupt-controller init), so the runtime lookup/enumeration check is still outstanding.
Move to the next record after handling an ordinary directory entry. The previous change only advanced while skipping "." and "..", so a record whose name did not match was compared again on every pass, hanging lookup, and iteration returned the same entry repeatedly.
问题 / Problem
Issue #12061 报告的 ISO9660 目录记录解析问题:记录中的
namelen来自介质且未经验证。非目录记录会无条件去掉";1"版本后缀,当namelen为 0 或 1 时rt_size_t下溢,随后的rt_strncpy()会越界写入,导致内核崩溃。Fixes #12061.
改动 / Changes
namelen限制在本地名称缓冲区可容纳的范围内;namelen >= 2时才剥离";1"版本后缀。验证 / Verification
RT_USING_DFS_V1+RT_USING_DFS_ISO9660的bsp/qemu-virt64-riscv上完成编译与链接(scons -j8,链接成功)。未覆盖 / Not covered