Repository navigation
[dfs][9pfs] bound getdents parsing to received reply size - #12035
cms19859230182-lang wants to merge 1 commit into
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-11-25 03:59 CST)
📝 Review Instructions
|
BernardXiong
left a comment
There was a problem hiding this comment.
结论:边界收紧方向正确,但新增的拒绝路径会导致读/写循环无限重试,恶意 9P 服务端仍能挂起客户端,需要修复后再合并。
[P1] p9_transaction() 返回错误后 dfs_9pfs_read() / dfs_9pfs_write() 会空转
p9_transaction() 新增了短包/超长包拒绝:
但 dfs_9pfs_read() 只在 if (!(rc = p9_transaction(...))) 的成功分支处理回复;rc != 0 时既不 break 也不递减 count,while ((ssize_t)count > 0) 会一直重发 Tread:
dfs_9pfs_write() 的同一位置(L806)也有同样问题。服务端持续返回短包、超长包或错误 tag 时,客户端会持锁空转。建议失败分支设置错误并 break,例如 tcount = p9_to_fs_err(rc); break;(写路径同理),并补一个畸形回复的单测。
其余检查(name_len 先按 16 位校验再截断、Rread.count 超界拒绝、d_name 本地缓冲上限)未发现问题。
|
86ae5bd to
ad71b1e
Compare
|
@BernardXiong 已修复该 P1,tip:
|
BernardXiong
left a comment
There was a problem hiding this comment.
结论:上一轮的 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,不会下溢。
两条可选建议(不阻塞合并):
-
[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。 -
[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
left a comment
There was a problem hiding this comment.
复核当前提交:read/write 的 transaction 失败路径已设置错误并退出循环,原持锁空转 P1 已解决;getdents 的回复及记录长度校验闭合,现有 CI/CLA 通过,代码可以合并,单一有效提交适合 rebase。原审查者最新复核也明确无阻塞问题,但 GitHub 仍保留其早期 REQUEST_CHANGES,请原审查者更新审批状态后合并。本次不撤销他人的修改请求。
| } | ||
| else | ||
| { | ||
| tcount = p9_to_fs_err(rc); |
There was a problem hiding this comment.
前一段写入成功后,下一次请求失败会覆盖 tcount,但文件偏移已经前进。调用方按错误重试可能重复写入。请仅在 tcount == 0 时返回错误,否则返回已传输的字节数;读路径同样处理。
|
同意 short read/write 这条。仅 tcount==0 时报错,否则返回已传字节;改完贴 tip。 |
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.
ad71b1e to
8642050
Compare
中文
问题
9PFS
getdents与读路径信任对端长度,可能越过实际接收数据解析;同时,新增畸形回复拒绝后,p9_transaction()的失败若被read/write循环忽略,会在count不变时持锁空转重发(#11289)。改动
rx_size基础头长与conn->msg_size上界,严格拒绝超出实际接收数据的Rread.count。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
getdentsand read paths trusted peer lengths beyond the received reply. After malformed-reply rejection was added, ignoring a failedp9_transaction()in the read/write loops could also resend forever with an unchanged count while holding the lock (#11289).Changes
rx_sizeheader andconn->msg_sizebound, and strictly rejectRread.countvalues exceeding the received payload.name_len, each record,stat_size, andrx_sizebefore capping the name copy to the locald_namebuffer.dfs_9pfs_read()anddfs_9pfs_write()now set the filesystem error andbreakimmediately before releasing the connection lock. If earlier bytes succeeded, this follows the same error-return semantics as the existing malformed-reply paths.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.8and rangit diff --check. No malicious 9P peer.Fix #11289