<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BadPopcorn &#187; Scripting</title>
	<atom:link href="http://badpopcorn.com/blog/category/technology/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://badpopcorn.com/blog</link>
	<description>Solutions for anything... except popcorn.</description>
	<lastBuildDate>Mon, 12 Apr 2010 15:38:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Securing Lua, Openlibs Oddity</title>
		<link>http://badpopcorn.com/blog/2007/04/25/securing-lua-openlibs-oddity/</link>
		<comments>http://badpopcorn.com/blog/2007/04/25/securing-lua-openlibs-oddity/#comments</comments>
		<pubDate>Wed, 25 Apr 2007 21:51:58 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/2007/04/25/securing-lua-openlibs-oddity/</guid>
		<description><![CDATA[Lua is a very nifty scripting language embedded into many applications: World of Warcraft being my favorite. And a common way to embed Lua into a program is to open it up, load all of Lua&#8217;s standard libraries and then execute the intended script:

  lua_State *l;
  l = lua_open();
  luaL_openlibs(l);
  luaL_dofile(l, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lua.org/">Lua</a> is a very nifty scripting language embedded into many applications: World of Warcraft being my favorite. And a common way to embed Lua into a program is to open it up, load all of Lua&#8217;s standard libraries and then execute the intended script:</p>
<blockquote><p>
  lua_State *l;<br />
  l = lua_open();<br />
  luaL_openlibs(l);<br />
  luaL_dofile(l, &#8220;SOME_LUA_SCRIPT&#8221;);<br />
  lua_close(l);
</p></blockquote>
<p>However, opening all the standard libraries exposes dangerous methods that can be maliciously used by a script. For example, A World of Warcraft plugin could be written to trash your entire harddrive if all of Lua&#8217;s standard libraries were loaded. Instead, Lua has the ability to load its libraries individually, thus being able to exclude libraries that leave holes to the operating system:<br />
<span id="more-901"></span></p>
<blockquote><p>
  lua_State *l;<br />
  l = lua_open();<br />
  luaopen_table(l);<br />
  luaopen_string(l);<br />
  luaopen_math(l);<br />
  luaL_dofile(l, &#8220;SOME_LUA_SCRIPT&#8221;);<br />
  lua_close(l);
</p></blockquote>
<p>The above code works nicely, but I lose the Lua module functionality in the &#8216;package&#8217; package. I wanted to allow packages to be loaded so I added a line (code for sandboxing package directories omitted):</p>
<blockquote><p>
  lua_State *l;<br />
  l = lua_open();<br />
  luaopen_package(l);<br />
  luaopen_table(l);<br />
  luaopen_string(l);<br />
  luaopen_math(l);<br />
  luaL_dofile(l, &#8220;SOME_LUA_SCRIPT&#8221;);<br />
  lua_close(l);
</p></blockquote>
<p>After compiling and running, I find my code no longer works (Lua circa 5.1.2)! What&#8217;s the deal? No idea, but I was able to get the &#8216;package&#8217; package to load by creating my own clone of Lua&#8217;s luaL_openlibs code. For example, the following only loads the &#8216;package&#8217; package:</p>
<blockquote><p>
static const luaL_Reg my_lualibs[] = {<br />
  // {&#8220;&#8221;, luaopen_base},<br />
  {LUA_LOADLIBNAME, luaopen_package},<br />
  // {LUA_TABLIBNAME, luaopen_table},<br />
  // {LUA_IOLIBNAME, luaopen_io},<br />
  // {LUA_OSLIBNAME, luaopen_os},<br />
  // {LUA_STRLIBNAME, luaopen_string},<br />
  // {LUA_MATHLIBNAME, luaopen_math},<br />
  // {LUA_DBLIBNAME, luaopen_debug},<br />
  {NULL, NULL}<br />
};</p>
<p>LUALIB_API void open_my_lualibs(lua_State *L) {<br />
  const luaL_Reg *lib = my_lualibs;<br />
  for (; lib->func; lib++) {<br />
    lua_pushcfunction(L, lib->func);<br />
    lua_pushstring(L, lib->name);<br />
    lua_call(L, 1, 0);<br />
  }<br />
}
</p></blockquote>
<p>Calling open_my_lualibs instead of the usual openlibs works like a charm.</p>
<p>Can anyone else reproduce this effect? Is it just me?</p>
<p>P.S. &#8211; Another way to secure Lua from functions such as &#8216;dofile&#8217;  or &#8216;loadfile&#8217; is to nil out these unsafe functions (dofile=nil) before running a user script; but I find it a tedious task that seems to be a fragile fix.</p>
<p>P.P.S. &#8211; I skipped over the lua_register(l, &#8220;function_name_in_lua&#8221;, function_pointer_in_c) bits for exposing code and data to Lua scripts. A truly paranoid person would abandon the use of Lua&#8217;s standard libraries and would register their own set of secured functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2007/04/25/securing-lua-openlibs-oddity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
