v3: Lua m.setvar() returns -1 on errors, so the script gets stack garbage
维护者通常 1 天内回复
评估
调研方向
从 src/engine/lua.cc 中的 Lua::setvar() 开始,这里的三个错误分支都返回 -1(位于第 343、351 和 366 行附近)。与 getvar() 进行比较,后者在任何 C++ 局部变量存在之前就调用了 luaL_checkstring()。完成的标准是:错误分支返回 0(或压入 nil 并返回 1);两处 luaL_checkstring() 调用移动到 std::string 局部变量之前;修正拼写错误;并且回归测试运行两个复现脚本并检查返回值。
由索引模型根据 Issue 内容生成。
描述
@airween, I found this while reviewing #3730. It's in the same file but a separate problem, so I'm filing it on its own.
Problem
Lua::setvar() (src/engine/lua.cc, v3/master 9d45c6d7) returns -1 on its three error paths:
- line 343: wrong number of arguments;
- line 351:
luaL_checkstring()returnedNULL; - line 366: no
.in the name, so no collection.
A lua_CFunction must return the number of values it pushed onto the stack. Lua doesn't check for a negative count in release builds, so the call returns whatever happens to be on the stack.
Reproduction
I used two scripts run with exec: in phase 1 (macOS, Lua 5.5.1):
-- nodot.lua
function main()
local r = m.setvar("novar", "x");
m.log(4, "after setvar, got: " .. tostring(r));
return nil;
end
-- onearg.lua
function main()
local r = m.setvar("tx.a");
m.log(4, "after setvar, got: " .. tostring(r));
return nil;
end
Debug log:
[8] m.setvar: Must specify a collection using dot character - ie m.setvar(tx.myvar,mydata)
[4] after setvar, got: userdata: 0x16f5b2498
...
[8] m.setvar: Failed m.setvar funtion must has 2 arguments
[4] after setvar, got: [
In the first case the script gets back the __transaction pointer that setvar() pushed. In the second it gets a stray string. Nothing crashed here, but this is undefined behaviour, and the result depends on the Lua version and build.
A related, smaller point
luaL_checkstring() raises a Lua error, using longjmp with the usual C build of Lua, when an argument isn't a string, e.g. m.setvar("tx.a", {}). In setvar() that happens after the three std::string locals have been constructed, so their destructors are skipped. They're still empty at that point, so nothing leaks today, but it's fragile. getvar(), getvars() and log() call luaL_checkstring() before any C++ object exists, so they're fine.
Suggested fix
- Return
0(no results) on the three error paths, aslog()does. Or, if a script should see the failure, pushnilorfalseand return1. - Move the two
luaL_checkstring()calls before thestd::stringlocals, as ingetvar(). - While there: fix the typo in the debug message ("funtion must has" → "function must have").
A regression case with the two scripts above, checking that m.setvar() returns nil (or false), would cover it.
- 主要语言
- C++
- 星标
- 9.8k
- 派生
- 1.8k
- 平均合并
- 1 天 6 小时
- 30 天内合并 PR
- 20
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
owasp-modsecurity/ModSecurity 的其他 Issue
-
v3: "General failure, action: 0x..." prints a pointer instead of the action name可能已有人在做 @Nikhil-jaiswal007 于 4 天前认领。 未关闭3.x bug
难度 2/5 1-3 小时 新手友好度 78/100
owasp-modsecurity/ModSecurity#3708 ·
维护者通常 1 天内回复
-
v2: t:utf8toUnicode drops the last hex digit of code points above U+FFFF (regression since 2.9.9)未关闭
难度 2/5 1-3 小时 新手友好度 78/100
owasp-modsecurity/ModSecurity#3684 ·
维护者通常 1 天内回复
-
2.x bug Platform - IIS pr available windows
难度 1/5 1 小时以内 新手友好度 90/100
owasp-modsecurity/ModSecurity#3623 · 2 条评论 ·
维护者通常 1 天内回复
-
2.x bug Platform - IIS pr available windows
难度 2/5 1-3 小时 新手友好度 82/100
owasp-modsecurity/ModSecurity#3621 · 2 条评论 ·
维护者通常 1 天内回复
-
2.x bug Platform - IIS pr available windows
难度 2/5 1-3 小时 新手友好度 84/100
owasp-modsecurity/ModSecurity#3619 · 2 条评论 ·
维护者通常 1 天内回复
查看 owasp-modsecurity/ModSecurity 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
meshcore-dev/MeshCore#3760 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
deepseek-ai/DeepGEMM-Ascend#29 ·
维护者通常 1 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 70/100
EchoTools/nevr-runtime#2042 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
emscripten-core/emscripten#28160 · 1 条评论 ·
维护者通常 1 天内回复
-
Download size and speed show scientific notation between 1000 and 1023 units可能已有人在做 @RaphaelFakhri 今天认领。 未关闭
难度 2/5 1 小时以内 新手友好度 88/100
kiwix/kiwix-desktop#1570 ·
维护者通常 1 天内回复