kernel stack overflow: handle_supercmd 与 supercmd_echo 各持 4KB 栈缓冲区嵌套,event 上报改写 argv 时栈耗尽 panic
Maintainers usually reply within 4 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- c, linux
- Domain
- operating-systems, security
Research direction
Start in kernel/patch/common/supercmd.c, reading supercmd_echo, handle_supercmd, and the event-to-echo path. Inspect the generated stack frames and existing cleanup paths, then exercise the execve hook under COW and memory-reclaim pressure. Done means the overlapping large stack allocations are removed or safely shared and the relevant stress scenario no longer reaches kernel stack overflow.
Written by the indexing model from the issue text.
Description
概要
handle_supercmd 与 supercmd_echo 各自在栈上持有一个 char buffer[4096],且 handle_supercmd 的 buffer 在调用 supercmd_echo 时仍然存活,两个 4KB 缓冲区必然同时在内核栈上。在 execve 前置 Hook 的深调用链中,supercmd_echo 改写 argv 触发 COW 缺页 + 内存回收时,会导致 Kernel panic - not syncing: kernel stack overflow。
该问题最初在下游 fork KernelPatch-Aster 中复现并做了符号还原(详见 LyraVoid/KernelPatch-Aster#1),经对照,上游当前 main(a308d88)的 supercmd.c 与下游崩溃版本仅在 superkey 参数解析处有差异(arg1 多 2 字节),两个 4KB 栈缓冲区及其调用关系完全相同,因此在此报告。
上游相关代码(main @ a308d88)
supercmd_echo内的char buffer[4096](L43–L47)handle_supercmd内的char buffer[4096](L426)event分支设置"report success"(L476–L479) →echo:标签处调用supercmd_echo(L498–L500)
handle_supercmd 的 buffer 在整个函数生命周期内存活,version 分支、echo: 标签等多处都会调用 supercmd_echo,两个 4KB 缓冲区叠加不可避免。
下游实测崩溃(KernelPatch-Aster)
设备后台发生 kernel stack overflow(CPU 2,Comm apd)。对已安装的 kpimg 做符号还原,帧大小取自 kpimg 实际指令:
| 返回地址 | 函数+偏移 | 栈帧 |
|---|---|---|
| 0xfffffff406abfa24 | compat_copy_to_user+0x84 | 64 B |
| 0xfffffff406abeb20 | set_user_arg_ptr+0x50 | 48 B |
| 0xfffffff406abc7f0 | supercmd_echo+0x120 | 4288 B |
| 0xfffffff406abcc34 | handle_supercmd+0x400 | 5056 B |
| 0xfffffff406abb9ac | handle_before_execve+0x30c | 336 B |
| 0xfffffff406abe098 | syscall_dispatch_before+0x208 | 560 B |
supercmd_echo入口:mov x12,#0x10c0; sub sp,sp,x12→ 4288 Bhandle_supercmd入口:mov x12,#0x13c0; sub sp,sp,x12→ 5056 B
仅这两帧就占 9344 字节,已识别的 KernelPatch 帧合计至少 10352 字节(16KB 内核栈的约 63%),尚未计入 Hook 跳板、系统调用入口、缺页、内存回收和厂商调度函数。
完整路径:
execve (su event ...)
→ syscall_dispatch_before
→ handle_before_execve
→ handle_supercmd (event 分支成功)
→ supercmd_echo("report success")
→ set_user_arg_ptr(argv[0])
→ compat_copy_to_user → __arch_copy_to_user
→ do_wp_page → 内存分配/直接回收 → shrink_slab
→ 厂商调度函数
→ stack overflow(越过栈底 320 字节)
崩溃时 MemAvailable 约 2.28 GiB,属于内核栈耗尽而非整机内存不足。厂商调度函数只是最终撞到栈保护边界的位置,首要原因是 supercmd 路径上两个 4KB 栈缓冲区叠加。
修复建议
- 消除两个 4KB 栈缓冲区的叠加:改为带错误清理的堆分配(如
kmalloc/vmalloc),或重构为handle_supercmd与supercmd_echo共享同一缓冲区。不建议简单改成静态全局缓冲区,会引入并发问题。 - 考虑为事件上报提供不经过
supercmd_echo/ argv 改写的返回通道,减少后台频繁上报走这条深栈路径。 - 编译期加入大栈帧检查(如
-Wframe-larger-than=),并在 COW 缺页 + 内存回收压力场景下实测。
说明
- 崩溃复现与符号还原基于下游 Aster 构建的 kpimg;上游未单独复现,但两个 4KB 缓冲区及调用路径与下游相同(下游
e6078ee与上游a308d88的supercmd.c仅差 superkey 解析处几行)。 - 尚未用修改后的构建做复现对照,无法确认上述修改能完全消除问题。
- 如有需要可补充
last-kmsg、符号还原记录与相关区段反汇编。
- Dominant language
- C
- Stars
- 1.6k
- Forks
- 401
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 4
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bmax121/KernelPatch
-
booloopOpen
Difficulty 5/5 Over a week Newbie friendliness 6/100
bmax121/KernelPatch#346 ·
Maintainers usually reply within 4 days
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
bmax121/KernelPatch#344 ·
Maintainers usually reply within 4 days
-
Patch Failed (Redmi Note 9 Merlin) (4.19.325-cip136-st20-g26d86a5027fb)Possibly taken @bmax121 claimed this 24 days ago. Open
bmax121/KernelPatch#343 · 1 assignee ·
Maintainers usually reply within 4 days
-
official LineageOS on nicepool: APpatch patching fails but only if done via recovery sideloadMay be free again @bmax121 claimed this 43 days ago, and no pull request is open. Open
bmax121/KernelPatch#332 · 1 assignee ·
Maintainers usually reply within 4 days
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
bmax121/KernelPatch#329 · 2 comments ·
Maintainers usually reply within 4 days
All issues in bmax121/KernelPatch
Similar issues
-
bug severity: medium
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
AlessandroSangiuliano/Uros#1014 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
OpenXiangShan/NEMU#1314 ·
Maintainers usually reply within 1 day
-
`get_options()` repeats the first byte when hex-encoding octet stringsPossibly taken @rue19 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
OpenPrinting/cups#1830 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
micropython/micropython#20020 ·
Maintainers usually reply within 1 day