Skip to content

[HUST CSE][finsh] validate backtrace thread address#11343

Open
Telecaster2147 wants to merge 1 commit intoRT-Thread:masterfrom
Telecaster2147:fix/finsh-backtrace-validate-thread
Open

[HUST CSE][finsh] validate backtrace thread address#11343
Telecaster2147 wants to merge 1 commit intoRT-Thread:masterfrom
Telecaster2147:fix/finsh-backtrace-validate-thread

Conversation

@Telecaster2147
Copy link
Copy Markdown
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

当前 finsh 的 backtrace [thread_address] 命令直接把用户输入解析成地址后,再用该地址调用 rt_object_get_type() 判断对象类型。这个实现默认调用方传入的是一个真实有效的线程对象地址;一旦用户传入任意无效地址、悬空地址或者并非 RT-Thread 对象的地址,命令路径就可能在对象类型判断前后访问非法内存,导致异常行为甚至崩溃。

这个问题出现在 shell 命令入口,属于运行时输入校验不足,不适合继续依赖“用户总是输入合法线程地址”来规避。更安全的做法是把用户输入当作不可信数据处理,只在当前线程对象表中确认该地址确实对应一个真实线程对象后,才继续执行 backtrace。

你的解决方案是什么 (what is your solution)

本 PR 在 src/kservice.c 中对 cmd_backtrace() 做了针对性的输入校验和线程查找重构:

  1. 新增 cmd_backtrace_parse_pid()

    • 统一处理 argv[1] 的地址解析;
    • 对空指针、非数字输入、尾部残留字符以及 0 值做显式拒绝;
    • 避免旧实现只检查 end_ptr == argv[1] 带来的宽松解析。
  2. 新增 cmd_backtrace_find_thread()cmd_backtrace_match_thread()

    • 不再把用户输入地址直接当对象指针做类型判断;
    • 改为遍历 RT_Object_Class_Thread 对象列表,只在输入地址与当前系统里真实存在的线程对象地址完全匹配时,才返回目标线程。
  3. 调整 cmd_backtrace() 主流程:

    • 输入非法时直接打印 Invalid input
    • 地址不对应现有线程对象时打印 Invalid pid
    • 只有查找到真实线程对象后,才调用 rt_backtrace_thread(target)

请提供验证的bsp和config (provide the config and bsp)

  • BSP: bsp/qemu-vexpress-a9

  • .config:

    • 使用 qemu-vexpress-a9 当前主线配置作为验证基线;
    • 配置中启用了 RT_USING_CONSOLERT_USING_FINSH 和 backtrace 相关选项,可覆盖本次修复的命令路径;
    • 本补丁未新增 Kconfig 选项,也未引入新的配置依赖。
  • action: 暂无

补充说明:

  • 已完成本地编译验证,bsp/qemu-vexpress-a9 可成功生成 rtthread.elfrtthread.bin
  • 已完成 QEMU 运行验证,系统可以正常启动到 msh />
  • 运行 backtrace 0x1 时,修复后命令能够稳定返回 Invalid pid: 1,未出现非法地址访问导致的异常;
  • 运行 backtrace <有效线程地址> 时,命令能够进入正常的线程 backtrace 路径。

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@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

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:fix/finsh-backtrace-validate-thread
  • 设置PR number为 \ Set the PR number to:11343
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 fix/finsh-backtrace-validate-thread 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/finsh-backtrace-validate-thread 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 added the Kernel PR has src relate code label Apr 22, 2026
@github-actions
Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837

Changed Files (Click to expand)
  • src/kservice.c

📊 Current Review Status (Last Updated: 2026-04-22 15:55 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kernel PR has src relate code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant