Repository navigation
[drivers][ivshmem] Validate the shared memory window - #11993
BernardXiong wants to merge 2 commits 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-14 00:05 CST)
📝 Review Instructions
|
|
The shared memory address and size in the ivshmem PCI config space (0x40..0x4f) are controlled by the device. ivshmem_pci_probe() used them verbatim as a JailHouse fallback when BAR2 was absent, so a malicious device could make the kernel map an arbitrary physical window and expose it to user space through /dev/ivshmemN. Only use the config space fallback when RT_IVSHMEM_JAILHOUSE is enabled, and validate the window (non-zero size, no overflow and representable by native pointer and size types) before mapping. Read the four config words into 64-bit locals instead of writing through 32-bit aliases of the pointer and size fields, and check the config space read result. Fixes RT-Thread#11992
0bc5e16 to
6bee4a6
Compare
| } | ||
|
|
||
| /* The whole window must be representable with native pointer and size types */ | ||
| if (size - 1 > max_addr - addr) |
There was a problem hiding this comment.
[P2] 单独检查 size 是否能由 rt_size_t 表示
此处只验证最后一个地址可表示,并不等价于长度可表示。32 位配置下,addr=0、size=0x100000000 会通过检查(size-1 恰好等于 max_addr),随后第 181 行转换成 rt_size_t 得到 0,仍被作为有效窗口交给 rt_ioremap()。这与函数“排除零长度并保证 native size 可表示”的契约相反。请在转换前显式拒绝 size > (rt_uint64_t)(rt_size_t)-1,并覆盖零长度、4 GiB 长度及接近地址上界的测试;映射前也应确保页对齐后的映射长度不会溢出。
| * The JailHouse config words are controlled by the device and are only | ||
| * used when the platform explicitly opts in. | ||
| */ | ||
| if (shmem_res->flags != PCI_BUS_REGION_F_NONE && |
There was a problem hiding this comment.
[P2] 共享内存窗口必须拒绝 I/O 类型的 BAR
flags != PCI_BUS_REGION_F_NONE 同样接受 PCI_BUS_REGION_F_IO。PCI 核心会根据设备声明把这类 BAR 分配到 I/O region,但下面无条件把其 base 当作物理内存交给 rt_ioremap(),之后还会暴露给 /dev/ivshmemN;在端口 I/O 与物理内存分离的平台上,这不是对应的共享内存资源。请显式限定为 memory / prefetchable-memory BAR,并把 I/O BAR 作为无效共享内存窗口拒绝(或仅在明确启用的 JailHouse 回退策略下处理),补一个 size 非零但 flags=PCI_BUS_REGION_F_IO 的负向验证。
ivshmem_shmem_range_valid() only checked the last address of the window, so a 4 GiB length on a 32-bit platform passed validation and was truncated to zero when converted to rt_size_t. Reject lengths that rt_size_t cannot represent and page-aligned mapping lengths that would overflow. Only memory and prefetchable memory BARs may describe the shared memory window; an I/O BAR was accepted before and then passed to rt_ioremap().
35d9bd6 to
9226535
Compare
|
已在新提交
非阻塞项:PR 标题已改为小写 English: both inline findings are addressed in |
Summary
When BAR2 is absent,
ivshmem_pci_probe()takes the JailHouse fallback and copies the shared memory address and size from the device-controlled PCI config words at0x40..0x4f. The PCI core never validates these values, andrt_ioremap()maps whatever physical range it is given. A malicious or ill-formatted ivshmem device can therefore make the kernel map an arbitrary physical window (for example kernel RAM) and expose it to user space through/dev/ivshmemN, giving unprivileged processes arbitrary kernel memory read/write.Changes
RT_IVSHMEM_JAILHOUSEoption is enabled. It defaults ton, and its help text documents that the device must be trusted. Without it, a valid BAR2 is required.rt_ioremap(): non-zero size, no address overflow, and the whole range must be representable with native pointer/size types.shmemandshmem_sizefields (which also corrupted adjacent fields on 32-bit builds).Fixes #11992
Testing
Changed files pass
clang-format22.1.8. The ivshmem driver compiles in both configurations (RT_IVSHMEM_JAILHOUSE=nand=y) forbsp/qemu-virt64-aarch64withRT_USING_PCI,RT_PCI_MSIandRT_USING_IVSHMEMenabled. CI will confirm the full build.