Skip to content

[dfs][9pfs] bound getdents parsing to received reply size - #12035

Open
cms19859230182-lang wants to merge 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/9pfs-getdents-rx-bounds
Open

cms19859230182-lang wants to merge 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/9pfs-getdents-rx-bounds

Conversation

@cms19859230182-lang

@cms19859230182-lang cms19859230182-lang commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

中文

问题

9PFS getdents 与读路径信任对端长度,可能越过实际接收数据解析;同时,新增畸形回复拒绝后,p9_transaction() 的失败若被 read/write 循环忽略,会在 count 不变时持锁空转重发(#11289)。

改动

  • 校验 rx_size 基础头长与 conn->msg_size 上界,严格拒绝超出实际接收数据的 Rread.count。
  • 先用 16 位 name_len 校验完整记录、stat_size 和 rx_size 边界,再将文件名拷贝限制到本地 d_name 缓冲。
  • dfs_9pfs_read() 与 dfs_9pfs_write() 在 transaction 失败时设置对应文件系统错误并立即 break,随后释放连接锁;已有成功字节时与该函数原有畸形回复路径采用相同的错误返回语义。
  • 保留此前已审查的记录边界与本地名称缓冲检查。

验证

静态确认两个 transaction 失败分支都会设置错误、退出循环并释放锁,原有 RX 与名称边界检查仍在;clang-format 22.1.8 检查全部改动行,git diff --check 通过。未搭恶意 9P 服务端。

English

Problem

9PFS getdents and read paths trusted peer lengths beyond the received reply. After malformed-reply rejection was added, ignoring a failed p9_transaction() in the read/write loops could also resend forever with an unchanged count while holding the lock (#11289).

Changes

  • Validate the basic rx_size header and conn->msg_size bound, and strictly reject Rread.count values exceeding the received payload.
  • Validate the untruncated 16-bit name_len, each record, stat_size, and rx_size before capping the name copy to the local d_name buffer.
  • On transaction failure, dfs_9pfs_read() and dfs_9pfs_write() now set the filesystem error and break immediately before releasing the connection lock. If earlier bytes succeeded, this follows the same error-return semantics as the existing malformed-reply paths.
  • Preserve the previously reviewed record-bound and local-name-buffer checks.

Test plan

Statically verified that both transaction-failure branches set an error, leave the loop, and release the lock, while the prior RX and name bounds checks remain. Checked all changed lines with clang-format 22.1.8 and ran git diff --check. No malicious 9P peer.

Fix #11289

@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/9pfs-getdents-rx-bounds
    Enter PR branch fix/9pfs-getdents-rx-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/9pfs-getdents-rx-bounds 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/9pfs-getdents-rx-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 Nov 11, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/dfs/dfs_v1/filesystems/9pfs/dfs_9pfs.c

📊 Current Review Status (Last Updated: 2026-11-25 17:36 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.

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

结论:边界收紧方向正确,但新增的拒绝路径会导致读/写循环无限重试,恶意 9P 服务端仍能挂起客户端,需要修复后再合并。

[P1] p9_transaction() 返回错误后 dfs_9pfs_read() / dfs_9pfs_write() 会空转

p9_transaction() 新增了短包/超长包拒绝:

if (rx_size < P9_MSG_TAG + sizeof(rt_uint16_t) || rx_size > conn->msg_size)

但 dfs_9pfs_read() 只在 if (!(rc = p9_transaction(...))) 的成功分支处理回复;rc != 0 时既不 break 也不递减 count,while ((ssize_t)count > 0) 会一直重发 Tread:

if (!(rc = p9_transaction(conn, size, &rx_size)))

dfs_9pfs_write() 的同一位置(L806)也有同样问题。服务端持续返回短包、超长包或错误 tag 时,客户端会持锁空转。建议失败分支设置错误并 break,例如 tcount = p9_to_fs_err(rc); break;(写路径同理),并补一个畸形回复的单测。

其余检查(name_len 先按 16 位校验再截断、Rread.count 超界拒绝、d_name 本地缓冲上限)未发现问题。

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

@cms19859230182-lang
cms19859230182-lang force-pushed the fix/9pfs-getdents-rx-bounds branch from 86ae5bd to ad71b1e Compare November 12, 2026 05:02
@cms19859230182-lang

Copy link
Copy Markdown
Contributor Author

@BernardXiong 已修复该 P1,tip:ad71b1e7。

  • dfs_9pfs_read() / dfs_9pfs_write() 均在 p9_transaction() 失败的 else 中执行 tcount = p9_to_fs_err(rc); break;,随后释放 conn->lock,不会再以不变的 count 持锁空转。
  • 若此前已有成功字节,与函数内原有畸形回复错误路径一样返回该错误,未引入新的部分进度语义。
  • 原有 16 位 name_len、Rread.count 和 d_name 上限检查均保留。

clang-format 22.1.8 改动行与 git diff --check 通过;未搭恶意 9P 服务端。

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

结论:上一轮的 P1 已修复,未发现新的阻塞问题。

已验证 head ad71b1e7:

  • dfs_9pfs_read() / dfs_9pfs_write() 在 p9_transaction() 失败时都走 else { tcount = p9_to_fs_err(rc); break; },释放 conn->lock 后返回,不再持锁空转。
  • dfs_9pfs_getdents() 对 ret、stat_size、name_len 同时用「回复长度」和「记录长度」做了上界校验;name_len 截断到 DIRENT_NAME_MAX 与 d_namlen(rt_uint8_t)容量内,循环里的 ret -= stat_size 也有 stat_size > ret 提前 break,不会下溢。

两条可选建议(不阻塞合并):

  1. [P2] 写路径没有拿到实际回复长度。L811-L813 p9_transaction(conn, size, RT_NULL) 的通用校验只能保证回复 ≥ 7 字节;短 Rwrite(7–10 字节)不会被拒绝,get_rx_value32_of(conn, P9_MSG_WRITE_COUNT) 会读到 rx_buffer 里的旧数据。建议像读路径一样传入 &rx_size,并在 rx_size < P9_MSG_WRITE_COUNT + sizeof(rt_uint32_t) 时返回 P9_READ_UNEXPECTED_DATA。

  2. [P2] 部分写入时位置推进不一致。L815-L820 fd->pos += set 而 count -= got; tcount += got:服务端返回 got < set 时位置会跳过未写入的数据,下一轮写到错误偏移。该写法在 master 上已存在,但本 PR 处理的正是不可信回复,建议一并改成 fd->pos += got。

Summary (EN): The P1 from the previous round is fixed — both read and write now break out of the loop on p9_transaction() failure, and getdents bounds are checked against both the record size and the received reply size. Two non-blocking P2 follow-ups: pass &rx_size in dfs_9pfs_write() so a short Rwrite is rejected before reading the count field, and advance fd->pos by got instead of set on partial writes.

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

复核当前提交:read/write 的 transaction 失败路径已设置错误并退出循环,原持锁空转 P1 已解决;getdents 的回复及记录长度校验闭合,现有 CI/CLA 通过,代码可以合并,单一有效提交适合 rebase。原审查者最新复核也明确无阻塞问题,但 GitHub 仍保留其早期 REQUEST_CHANGES,请原审查者更新审批状态后合并。本次不撤销他人的修改请求。

@Rbb666
Rbb666 requested a review from BernardXiong November 16, 2026 03:29
}
else
{
tcount = p9_to_fs_err(rc);

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.

前一段写入成功后,下一次请求失败会覆盖 tcount,但文件偏移已经前进。调用方按错误重试可能重复写入。请仅在 tcount == 0 时返回错误,否则返回已传输的字节数;读路径同样处理。

@cms19859230182-lang

Copy link
Copy Markdown
Contributor Author

同意 short read/write 这条。仅 tcount==0 时报错,否则返回已传字节;改完贴 tip。

@cms19859230182-lang
cms19859230182-lang force-pushed the fix/9pfs-getdents-rx-bounds branch from ad71b1e to 8642050 Compare November 24, 2026 19:59
Fix RT-Thread#11289

Close the remaining RX bounds gaps after RT-Thread#11918:
- Validate p9_transaction() replies against the basic header size and
  conn->msg_size, and bound Rerror strings by the received payload.
- Reject oversized Rread counts instead of clamping them. Validate each
  getdents stat record and its untruncated 16-bit name length against both
  the record and the received reply before capping the local name copy.

dfs_9pfs_read() and dfs_9pfs_write() now follow POSIX short transfer
semantics: a failure after some bytes were already moved returns the
transferred count and only maps the error when nothing was transferred.
Reporting an error after partial progress made the caller retry from the
original offset and duplicate the write.

The loops also advance the caller buffer by the transferred count, so a
multi-chunk transfer no longer overwrites the first chunk with the second
one, advance fd->pos by the count a short write actually accepted, treat a
zero-byte Rwrite reply as an error instead of spinning on it, and validate
Rwrite replies against the received size before reading the count field.

Initialize rc in both loops: with a zero-length request the loop body never
runs, so the short-transfer check at the end read an uninitialized value
(cppcheck uninitvar) and read()/write() reported -EIO instead of returning 0
for a zero-length transfer.
@cms19859230182-lang
cms19859230182-lang force-pushed the fix/9pfs-getdents-rx-bounds branch from 8642050 to 3f74a7a Compare November 25, 2026 09:36

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.

[Bug] 9PFS client trusts server-controlled stat/name lengths in getdents path, leading to out-of-bounds read

3 participants