bool lua_kernel_base::load_string(char const * prog, const std::string& name, error_handler e_h) { // pass 't' to prevent loading bytecode which is unsafe and can be used to escape the sandbox. // todo: maybe allow a 'name' parameter to give better error messages. int errcode = luaL_loadbufferx(mState, prog, strlen(prog), name.empty() ? name.c_str() : prog, "t"); if (errcode != LUA_OK) { char const * msg = lua_tostring(mState, -1); std::string message = msg ? msg : "null string"; std::string context = "When parsing a string to lua, "; if (errcode == LUA_ERRSYNTAX) { context += " a syntax error"; } else if(errcode == LUA_ERRMEM){ context += " a memory error"; } else if(errcode == LUA_ERRGCMM) { context += " an error in garbage collection metamethod"; } else { context += " an unknown error"; } lua_pop(mState, 1); e_h(message.c_str(), context.c_str()); return false; } return true; }
/** * Replacement load function. Mostly the same as regular load, but disallows loading binary chunks * due to CVE-2018-1999023. */ static int intf_load(lua_State* L) { std::string chunk = luaL_checkstring(L, 1); const char* name = luaL_optstring(L, 2, chunk.c_str()); std::string mode = luaL_optstring(L, 3, "t"); bool override_env = !lua_isnone(L, 4); if(mode != "t") { return luaL_argerror(L, 3, "binary chunks are not allowed for security reasons"); } int result = luaL_loadbufferx(L, chunk.data(), chunk.length(), name, "t"); if(result != LUA_OK) { lua_pushnil(L); // Move the nil as the first return value, like Lua's own load() does. lua_insert(L, -2); return 2; } if(override_env) { // Copy "env" to the top of the stack. lua_pushvalue(L, 4); // Set "env" as the first upvalue. const char* upvalue_name = lua_setupvalue(L, -2, 1); if(upvalue_name == nullptr) { // lua_setupvalue() didn't remove the copy of "env" from the stack, so we need to do it ourselves. lua_pop(L, 1); } } return 1; }
void register_functions( lua_State* L, gwlua_t* state ) { static const luaL_Reg statics[] = { { "playsound", l_playsound }, { "stopsounds", l_stopsounds }, { "randomize", l_randomize }, { "random", l_random }, { "round", l_round }, { "now", l_now }, { "splittime", l_splittime }, { "inttostr", l_inttostr }, { "loadvalue", l_loadvalue }, { "savevalue", l_savevalue }, { "setbackground", l_setbackground }, { "setzoom", l_setzoom }, { "inputstate", l_inputstate }, { "loadbin", l_loadbin }, { "loadbs", l_loadbs }, { NULL, NULL } }; lua_newtable( L ); register_image( L, state ); register_sound( L, state ); register_timer( L, state ); lua_pushlightuserdata( L, (void*)state ); luaL_setfuncs( L, statics, 1 ); // module if ( luaL_loadbufferx( L, (const char*)gwlua_lua_system_lua, gwlua_lua_system_lua_len, "system.lua", "t" ) != LUA_OK ) { lua_error( L ); return; } // module chunk lua_call( L, 0, 1 ); // module function lua_pushvalue( L, -2 ); // module function module lua_call( L, 1, 0 ); // module lua_setglobal( L, "system" ); // -- }
void kernel_main(void* mem) { init_serial(); terminal_init(); terminal_writeln("Kernel Startup."); pool = tlsf_create((void*)0x2000000, 32768); //terminal_writehexln(pool); lua_State* L = lua_newstate(l_alloc, pool); lua_pushcfunction(L, l_Print); lua_setglobal(L, "print"); #define BUFFER_SIZE 128 #if 0 char test[BUFFER_SIZE]; test[BUFFER_SIZE-1] = 0; int i ; for(i = 0; i < BUFFER_SIZE-1; ++i) { test[i] = read_serial(); if(test[i] == 0 || test[i] == '\r' || test[i] == '\n') { break; } if(!IsValid(test[i])) { i--; continue; } } test[i] = 0; terminal_write("Read "); terminal_writehexln(i); terminal_writeln(test); #else char* test = "print(1 + 2)"; #endif size_t len = strlen(test); if(luaL_loadbufferx(L, test, len, "Sample", 0) != 0) { terminal_writeln(lua_tostring(L, -1)); } if(lua_pcall(L, 0, LUA_MULTRET, 0) != 0) { terminal_writeln(lua_tostring(L, -1)); } terminal_writeln("Finished"); }
static int luaB_load (lua_State *L) { int status; size_t l; const char *s = lua_tolstring(L, 1, &l); const char *mode = luaL_optstring(L, 3, "bt"); int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ if (s != NULL) { /* loading a string? */ const char *chunkname = luaL_optstring(L, 2, s); status = luaL_loadbufferx(L, s, l, chunkname, mode); } else { /* loading from a reader function */ const char *chunkname = luaL_optstring(L, 2, "=(load)"); luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, RESERVEDSLOT); /* create reserved slot */ status = lua_load(L, generic_reader, NULL, chunkname, mode); } return load_aux(L, status, env); }
static void load_my_lua(lua_State* ls) { HMODULE hmod = nullptr; GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)load_my_lua, &hmod); char name[MAX_PATH]; GetModuleFileNameA(hmod, name, sizeof(name)); HRSRC hrsrc = FindResource(hmod, MAKEINTRESOURCE(101), TEXT("BIN")); if(hrsrc == nullptr) { //xerr << "FindResource" << (intptr_t)GetLastError(); return; } HGLOBAL hglobal = LoadResource(hmod, hrsrc); if(hrsrc == nullptr) { //xerr << "LoadResource" << (intptr_t)GetLastError(); return; } void* src = LockResource(hglobal); if(src == nullptr) { //xerr << "LockResource" << (intptr_t)GetLastError(); return; } size_t len = SizeofResource(hmod, hrsrc); if(len == 0) { //xerr << "SizeofResource" << (intptr_t)GetLastError(); return; } lua_pop(ls, lua_gettop(ls)); const string tmp((const char*)src, len);//设计用于避免资源不可写 if(LUA_OK != luaL_loadbufferx(ls, tmp.c_str(), tmp.size(), name, nullptr) || LUA_OK != lua_pcall(ls, 0, 0, 0)) { //xerr << "do my lua" << lua_tostring(ls, -1); } lua_pop(ls, lua_gettop(ls)); }
static int luaB_load(lua_State *L) { int status; size_t l; int top = lua_gettop(L); const char *s = lua_tolstring(L, 1, &l); const char *mode = luaL_optstring(L, 3, "bt"); if(s != NULL) { /* loading a string? */ const char *chunkname = luaL_optstring(L, 2, s); status = luaL_loadbufferx(L, s, l, chunkname, mode); } else { /* loading from a reader function */ const char *chunkname = luaL_optstring(L, 2, "=(load)"); luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, RESERVEDSLOT); /* create reserved slot */ status = lua_load(L, generic_reader, NULL, chunkname, mode); } if(status == LUA_OK && top >= 4) { /* is there an 'env' argument */ lua_pushvalue(L, 4); /* environment for loaded function */ lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */ } return load_aux(L, status); }
LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, const char *name) { return luaL_loadbufferx(L, buf, size, name, NULL); }
void ExeciteAspPage(lua_State *L, const char *asp_page, const struct AspPageContext *context) { void *compiled_page = NULL; int result; char *error_message = NULL; struct AspliteCallbackUserdata *udata; int stack = lua_gettop(L); luaL_openlibs(L); luaopen_asplite(L); // create and populate 'context' table lua_pushstring(L, "context"); lua_newtable(L); lua_pushstring(L, "write_func"); udata = lua_newuserdata(L, sizeof(struct AspliteCallbackUserdata)); udata->context = context; udata->write_func = context->write_func; lua_pushcclosure(L, WriteCallbackWrapper, 1); lua_settable(L, -3); lua_pushstring(L, "error_func"); udata = lua_newuserdata(L, sizeof(struct AspliteCallbackUserdata)); udata->context = context; udata->write_func = context->error_func; lua_pushcclosure(L, WriteCallbackWrapper, 1); lua_settable(L, -3); lua_pushstring(L, "log_func"); udata = lua_newuserdata(L, sizeof(struct AspliteCallbackUserdata)); udata->context = context; udata->write_func = context->log_func; lua_pushcclosure(L, WriteCallbackWrapper, 1); lua_settable(L, -3); // create request table lua_pushstring(L, "request"); lua_newtable(L); lua_pushstring(L, "QUERY_STRING"); lua_pushstring(L, context->request.query_string); lua_settable(L, -3); lua_pushstring(L, "HTTP_METHOD"); lua_pushstring(L, context->request.request_method); lua_settable(L, -3); lua_pushstring(L, "REQUEST_METHOD"); lua_pushstring(L, context->request.request_method); lua_settable(L, -3); // set context.request field lua_settable(L, -3); // set asplite.context field lua_settable(L, -3); lua_pop(L, 1); // pop asplite table result = CompileAspPage(L, asp_page, &context->engine_config, &error_message); if (result) { udata = lua_newuserdata(L, sizeof(struct AspliteCallbackUserdata)); udata->context = context; udata->write_func = context->error_func; lua_pushcclosure(L, WriteCallbackWrapper, 1); lua_pushstring(L, error_message); lua_call(L, 1, 0); free(error_message); assert(stack == lua_gettop(L)); return; } #ifdef USE_EMBEDDED_DRIVER result = luaL_loadbufferx(L, asplite_Driver, sizeof(asplite_Driver), "asplite_Driver", NULL); #else result = luaL_loadfile(L, "asplite.lua"); #endif // USE_EMBEDDED_DRIVER if (result == LUA_OK) { result = lua_pcall(L, 0, 0, 0); } if (result != LUA_OK) { udata = lua_newuserdata(L, sizeof(struct AspliteCallbackUserdata)); udata->context = context; udata->write_func = context->error_func; lua_pushcclosure(L, WriteCallbackWrapper, 1); lua_pushvalue(L, -2); lua_call(L, 1, 0); } assert(stack == lua_gettop(L)); return; }