LuaL_openlibs()和沙箱脚本 [英] LuaL_openlibs() and sandboxing scripts

查看:1675
本文介绍了LuaL_openlibs()和沙箱脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C / C ++应用程序中嵌入的Lua(5.1)。

I am embedding Lua (5.1) in a C/C++ application.

我使用的是 LuaL_openlibs()函数加载基础库。但是,此功能加载,我要禁用,使他们不向我的Lua脚本一些其他库。

I am using the LuaL_openlibs() function to load the base libs. However, this function loads some other libraries which I want to disable so that they are not available to my Lua scripts.

具体来说,我要禁用IO和操作系统模块。是否有一个功能,我可以打电话programmativally禁用(或卸载),这些模块,这样我可以运行Lua脚本创造一个安全沙盒环境?

Specifically, I want to disable the IO and OS modules. Is there a function I can call to programmativally disable (or unload) these modules so that I can create a safe sandbox environment for running Lua scripts?

推荐答案

我不知道如何禁用模块,但你仍然可以选择加载,而不是装载它们全部用 luaL_openlibs <哪些/ code>。 Lua的5.1手动的第7.3节说:

I don't know how to disable modules, but you can still choose which ones to load instead of loading them all with luaL_openlibs. Section 7.3 of the Lua 5.1 manual says:

luaopen _ * 函数(打开库)不能直接调用,就像一个普通的C函数。他们必须通过Lua中被调用,就像一个Lua功能。

The luaopen_* functions (to open libraries) cannot be called directly, like a regular C function. They must be called through Lua, like a Lua function.

这是,而不是直接调用该函数在Lua 5.0:

That is, instead of directly calling the function as in Lua 5.0:

luaopen_table(L);

...你把它作为其名称的C函数,并使用 lua_call 或在Lua 5.1相似的:

... you push it as a C function with its name and use lua_call or similar in Lua 5.1:

lua_pushcfunction(L, luaopen_table);
lua_pushliteral(L, LUA_TABLIBNAME);
lua_call(L, 1, 0);

您可以在列出做到这一点的功能 lualib.h

The functions you can do this with are listed in lualib.h:

Function        | Name
----------------+-----------------
luaopen_base    | ""
luaopen_table   | LUA_TABLIBNAME
luaopen_io      | LUA_IOLIBNAME
luaopen_os      | LUA_OSLIBNAME
luaopen_string  | LUA_STRLIBNAME
luaopen_math    | LUA_MATHLIBNAME
luaopen_debug   | LUA_DBLIBNAME
luaopen_package | LUA_LOADLIBNAME

这篇关于LuaL_openlibs()和沙箱脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆