Skip to content

lua-lsm: enforce per-module VM execution deadlines - #39

Open
LetsWalkInLine wants to merge 4 commits into
openanolis:lua-lsmfrom
LetsWalkInLine:pr/vm-execution-budget
Open

LetsWalkInLine wants to merge 4 commits into
openanolis:lua-lsmfrom
LetsWalkInLine:pr/vm-execution-budget

Conversation

@LetsWalkInLine

@LetsWalkInLine LetsWalkInLine commented Nov 9, 2026 •

Copy link
Copy Markdown

概述

当前 Lua 策略中的长循环可能持续占用 hook 执行上下文。本 PR 为每个模块处理一次 LSM hook 引入单调时钟 deadline,超时沿保护调用终止执行,保留普通 Lua 错误的捕获语义。

依赖 #38 的执行失败映射与状态恢复,可等待 #38 合并后,对本PR进行rebase再进行合并。这里不包含独立的字符串限制 PR

实现

  • 每模块、每次 hook 独立计时,覆盖首次懒加载、初始化、参数构造、回调及结果转换。协程共享 deadline 和指令计数,被调度出去的时间也计入预算。
  • 普通 task 默认 20 ms,原子上下文默认 2 ms;无 preempt count 时保守采用短预算。解释器每 256 条字节码进行独立检查,不依赖 debug hook,并在 VM、C 返回及分发完成边界核对时间。
  • 使用持续传播的 LUA_ERRTIME,超时不能被 pcall/xpcall、错误处理器、debug hook 或 coroutine 捕获后继续执行。普通错误仍可捕获;超时复用 lua-lsm: propagate policy execution failures #38 的失败映射,errno 类 hook 返回 -EIO。
  • deadline 保留至下一模块入口或保护调用完成,覆盖 C 返回和 GC 回调。模块退场清理 Lua debug hook,避免其影响后续模块。
  • inode_getsecurity 的分配输出暂存到最终检查成功后再交付,失败时释放,避免完成阶段超时造成泄漏。
  • 补充策略作者文档,说明预算、恢复及资源清理约束。

验证

当前公开测试分支已在字符串限制、错误处理和本次 VM 预算改动的组合上
完成构建和运行:

  • 39/39 KUnit 全部通过,其中预算相关测试覆盖可靠终止、
    deadline精确边界、普通/原子/softirq上下文、协程与回调共享预算、
    调度等待、C返回检查、return hook隔离、完成阶段GC及getsecurity输出释放。
  • budget guest 通过真实 open(2) 验证八类超时、精确 EIO、
    普通错误仍可捕获、25轮重复恢复、其他 task、首次懒加载重试及卸载恢复。
  • network guest 通过 netns/veth UDP 实际触发接收 hook,
    观察到 softirq 上下文中的超时返回 -EIO;卸载重载后,
    同接收路径返回成功并实际收到数据包。
  • 字符串组合回归验证普通额度错误仍可捕获,dump边界及恢复保持正确。
  • 最新组合内核构建无 compiler warning/error。

测试源码与复现

测试在 fork 的 pr/lua-lsm-tests 分支独立维护,后续计划单独提交上游。
本次引用的固定测试提交为 b48e84b972754fe1277fb1451059880cee6ac351:

最新组合在 x86_64 PREEMPT_LAZY、QEMU TCG 下实际构建并运行:
39/39 KUnit、六种 guest 场景及四个宿主工具测试全部通过。
guest 均由宿主 watchdog 保护。benchmark 留待独立交付。

边界

这是安全检查点上的合作式执行预算。C-only 工作可能超过 deadline 后才返回。注册、VM 创建及 teardown 不属于每模块 hook 预算;策略数据和任意 C helper 资源不提供通用回滚。

Benchmark

在已包含字符串限制与错误处理的基线上,对比引入VM预算前后的表现。测量在Ryzen 9 9950X3D、Ubuntu、QEMU/KVM环境完成,使用相同内核配置、GCC 13.3、-Os,交错运行五轮。

  • 最小allow策略的warm openat均值由 723ns升至1039ns(+316ns,+43.7%);32次gsub回调策略由 2.439μs升至3.809μs(+56%)。
  • 使用20ms/2ms默认预算、256条字节码检查间隔,普通与关闭抢占上下文中的纯Lua、pcall、gsub回调路径,完整hook耗时减预算的均值约 2.3–2.6μs,观测P99不超过 3.6μs。
  • 协程路径存在较长尾部:上述差值的P99约为 1.54ms(task)和0.80ms(atomic)。

[Benchmark工具与采样入口](https://github.com/LetsWalkInLine/lua-lsm-kernel/tree/19773f12803780b4a7151cd59f16f6e40a22c366/tools/testing/selftests/lua_lsm)。

Protect the complete active hook module walk, including lazy loading,
environment and argument setup, callbacks and result conversion. Stop
on uncaught errors and map failures according to each hook contract
instead of retaining an allow or not-handled default.

Preserve protected Lua error status across loader failures and restore
the dispatch stack, environments and debug-hook state before returning
the VM. Keep ordinary policy-caught errors recoverable and document
predicate and notification semantics.

Signed-off-by: LetsWalkInLine <1327604853@qq.com>
Give each module handling a hook a monotonic wall-time deadline, including
its lazy load, callbacks and coroutines. Check time independently of debug
hooks every 256 bytecode dispatches, at VM entry and every exit, on C
returns, and at module completion.

Propagate a sticky, allocation-free LUA_ERRTIME through existing protected
frames before Lua can catch a timeout. Preserve ordinary error semantics
and reuse the dispatcher's failure mapping and state restoration.

Use 20 ms in normal task context and 2 ms in atomic contexts, selected from
cold and warm guest measurements. Document the cooperative C-only boundary
and policy authors' execution and cleanup constraints.

Validated with 21 independent and 36 combined KUnit cases, watchdog guests,
257 supported dispatcher builds and baseline/64/256/1024 measurements.
Tests and development evidence are maintained separately.

Depends-on: openanolis#38
Keep the last module's deadline active until protected dispatch, C-frame
returns and GC complete. Check a module without clearing its deadline,
and only end it at the next module or public dispatch exit. Clear policy
debug hooks before leaving each module so return hooks cannot escape their
budget or affect later modules.

Stage allocated inode_getsecurity output in the outer C call frame until
all protected completion checks succeed. Free it on dispatch failure or
negative results, preserving the caller's buffer and the alloc=false path.

Add the policy-author contract for these completion and ownership rules.
Separate regression evidence covers delayed return hooks, GC at C return,
allocation-time and completion-time expiry, output frees and VM recovery.
The previous implementation fails the finite negative controls; the fixed
candidate passes 25 independent and 40 combined KUnit cases, watchdog
guests and builds of all 257 supported dispatchers.

Depends-on: openanolis#38
Drop the module-tail lua_budget_check call and its dedicated public API.
No Lua execution follows before the next module's lua_budget_end or the
protected C-return and public completion checks. Keep the existing active
deadline lifecycle, sticky timeout state and shared instruction counter.

Remove the unread nil slot left by traceback handling. Environment access
uses relative indices and result conversion records its stack top, so only
the depth assertion and stack-layout comments need adjustment.

Keep getsecurity staging and its shared-dispatch macro specialization.
Validated with the existing 25 independent and 40 combined KUnit cases,
five watchdog guests and all 257 supported dispatcher builds; tests and
source/configuration/image evidence remain separate from production.

Depends-on: openanolis#38
@chenzongyao200127 chenzongyao200127 added concurrency This will triger concurrency ci test and removed concurrency This will triger concurrency ci test labels Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

concurrency This will triger concurrency ci test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants