Repository navigation
[components][lwp] harden sys_getaddrinfo user pointer handling - #11977
Conversation
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_lwpReviewers: @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-03 00:58 CST)
📝 Review Instructions
|
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
|
sys_getaddrinfo() validated the top-level res buffer, then wrote the resolved socket address through res->ai_addr, a user-controlled nested pointer that was never checked. On Smart/MMU builds a user program could therefore turn getaddrinfo() into an arbitrary kernel write. The hints fields were also read directly from user memory after the access check, leaving a TOCTOU window. Copy res and hints into kernel temporaries with lwp_get_from_user() so all nested pointers come from one validated snapshot. Write the nested sockaddr and the outer addrinfo back with lwp_put_to_user(), which validates the destination range and never dereferences an unchecked user pointer in kernel context. Also fix the servname copy check (a size_t result can never be < 0), allocate the kernel addrinfo with sizeof(*k_hints), and release k_res on the new error paths. Co-Authored-By: Claude Code <noreply@anthropic.com>
bf71c44 to
b732f6c
Compare
拉取/合并请求描述:(PR description)
为什么提交这份PR (why to submit this PR)
这是对 #11454 的重做(原 PR 已冲突且过时)。
sys_getaddrinfo()只校验了用户传入的顶层res缓冲区,随后通过res->ai_addr这个用户可控的嵌套指针写入解析出的 socket 地址。在 Smart/MMU 构建下,用户程序可以借此把一次getaddrinfo()调用变成任意内核写。另外hints的字段在顶层校验后仍直接从用户内存读取,存在 TOCTOU 窗口。你的解决方案是什么 (what is your solution)
基于最新 master 重新实现(
sys_getaddrinfo()已随 syscall 拆分迁移到components/lwp/syscall/syscall_netdb.c):lwp_get_from_user()将res、hints一次性拷贝到内核临时结构体,后续所有嵌套指针都来自这份已校验的快照,消除 TOCTOU;lwp_put_to_user()写回,目标地址范围由现有 helper 校验,内核不再解引用未检查的用户指针;musl_addrinfo同样通过lwp_put_to_user()写回;servname拷贝的返回值判断(size_t与 0 比较恒为假)、按sizeof(*k_hints)分配内核 addrinfo,并在新增的错误路径上释放k_res。改动只涉及一个文件,复用现有的
lwp_get_from_user()/lwp_put_to_user()/sockaddr_tomusl(),未引入新的 helper。请提供验证的bsp和config (provide the config and bsp)
BSP: bsp/k230(RT-Smart)
.config: CONFIG_RT_USING_SMART=y, CONFIG_ARCH_MM_MMU=y, CONFIG_RT_USING_SAL=y, CONFIG_SAL_USING_POSIX=y
action: 已使用 K230 SDK
riscv64-unknown-linux-musl工具链本地编译通过(syscall_netdb.o,包含ARCH_MM_MMU路径);完整构建由 CI 验证。当前拉取/合并请求的状态 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🤖 Generated with Claude Code