From: Marek VavruĊĦa Date: Wed, 29 Apr 2015 14:12:36 +0000 (+0200) Subject: daemon/engine: check for bad upvalues in module trampoline X-Git-Tag: v1.0.0-beta1~225^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7902ecad18cd7eca3aed52dbf066ee02dde3c823;p=thirdparty%2Fknot-resolver.git daemon/engine: check for bad upvalues in module trampoline --- diff --git a/daemon/engine.c b/daemon/engine.c index 7aea7fb52..211e9bf6c 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -71,6 +71,10 @@ static int l_trampoline(lua_State *L) struct kr_module *module = lua_touserdata(L, lua_upvalueindex(1)); void* callback = lua_touserdata(L, lua_upvalueindex(2)); struct engine *engine = engine_luaget(L); + if (!module) { + lua_pushstring(L, "module closure missing upvalue"); + lua_error(L); + } /* Now we only have property callback or config, * if we expand the callables, we might need a callback_type. @@ -172,8 +176,7 @@ void engine_deinit(struct engine *engine) kr_cache_close(engine->resolver.cache); } -/** Execute current chunk in the sandbox */ -static int l_sandboxcall(lua_State *L, int argc) +int engine_pcall(lua_State *L, int argc) { #if LUA_VERSION_NUM >= 502 lua_getglobal(L, "_SANDBOX"); @@ -193,7 +196,7 @@ int engine_cmd(struct engine *engine, const char *str) lua_pushstring(engine->L, str); /* Check result. */ - if (l_sandboxcall(engine->L, 1) != 0) { + if (engine_pcall(engine->L, 1) != 0) { fprintf(stderr, "%s\n", lua_tostring(engine->L, -1)); lua_pop(engine->L, 1); return kr_error(EINVAL); @@ -207,7 +210,7 @@ int engine_cmd(struct engine *engine, const char *str) (luaL_loadbuffer((L), (arr), (len), (name)) || lua_pcall((L), 0, LUA_MULTRET, 0)) /** Load file in a sandbox environment. */ #define l_dosandboxfile(L, filename) \ - (luaL_loadfile((L), (filename)) || l_sandboxcall((L), 0)) + (luaL_loadfile((L), (filename)) || engine_pcall((L), 0)) static int engine_loadconf(struct engine *engine) { @@ -276,7 +279,7 @@ static int register_properties(struct engine *engine, struct kr_module *module) /* Register module in Lua env */ lua_getglobal(engine->L, "modules_register"); lua_getglobal(engine->L, module->name); - if (l_sandboxcall(engine->L, 1) != 0) { + if (engine_pcall(engine->L, 1) != 0) { lua_pop(engine->L, 1); } diff --git a/daemon/engine.h b/daemon/engine.h index bb1f9c37c..3918ee4b3 100644 --- a/daemon/engine.h +++ b/daemon/engine.h @@ -40,5 +40,9 @@ void engine_stop(struct engine *engine); int engine_register(struct engine *engine, const char *module); int engine_unregister(struct engine *engine, const char *module); void engine_lualib(struct engine *engine, const char *name, int (*lib_cb) (struct lua_State *)); + +/** Execute current chunk in the sandbox */ +int engine_pcall(struct lua_State *L, int argc); + /** Return engine light userdata. */ struct engine *engine_luaget(struct lua_State *L);