Repository navigation
lua-lsm: enforce per-module VM execution deadlines - #39
Open
LetsWalkInLine wants to merge 4 commits into
Open
LetsWalkInLine wants to merge 4 commits into
LetsWalkInLine wants to merge 4 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
当前 Lua 策略中的长循环可能持续占用 hook 执行上下文。本 PR 为每个模块处理一次 LSM hook 引入单调时钟 deadline,超时沿保护调用终止执行,保留普通 Lua 错误的捕获语义。
依赖 #38 的执行失败映射与状态恢复,可等待 #38 合并后,对本PR进行rebase再进行合并。这里不包含独立的字符串限制 PR
实现
LUA_ERRTIME,超时不能被 pcall/xpcall、错误处理器、debug hook 或 coroutine 捕获后继续执行。普通错误仍可捕获;超时复用 lua-lsm: propagate policy execution failures #38 的失败映射,errno 类 hook 返回-EIO。inode_getsecurity的分配输出暂存到最终检查成功后再交付,失败时释放,避免完成阶段超时造成泄漏。验证
当前公开测试分支已在字符串限制、错误处理和本次 VM 预算改动的组合上
完成构建和运行:
deadline精确边界、普通/原子/softirq上下文、协程与回调共享预算、
调度等待、C返回检查、return hook隔离、完成阶段GC及getsecurity输出释放。
budgetguest 通过真实 open(2) 验证八类超时、精确 EIO、普通错误仍可捕获、25轮重复恢复、其他 task、首次懒加载重试及卸载恢复。
networkguest 通过 netns/veth UDP 实际触发接收 hook,观察到 softirq 上下文中的超时返回 -EIO;卸载重载后,
同接收路径返回成功并实际收到数据包。
测试源码与复现
测试在 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,交错运行五轮。[Benchmark工具与采样入口](https://github.com/LetsWalkInLine/lua-lsm-kernel/tree/19773f12803780b4a7151cd59f16f6e40a22c366/tools/testing/selftests/lua_lsm)。