Thread Tools Display Modes
Prev Previous Post   Next Post Next
08-31-11, 03:25 PM   #1
tecu
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 30
tecu tries to learn - c++, lua - creating a sandbox

(sorry if this is too off-topic)

i have a ridiculous amount of free time at the moment (unemployed), so i figured i'd be productive and spend some of it learning things. to that end i'm working on a project that includes opengl, c++, and lua. doing this is a little silly: my skill level with opengl and c++ is basically at the beginner level, and the project itself doesn't have a solid goal beyond 'learn stuff'; but you probably don't care about all that.

anyway, i spent a short time yesterday trying to figure out the basics of integrating lua and c++ before thinking, 'I NEED TO CREATE A SANDBOX.' and what i'm interested in creating (and why i'm asking this here) is something similar to the wow lua environment:

- include base, math, string, and table libs (though no coroutine)
- disable reading from or writing to anything outside the lua environment

so, after reading the entire internet, i've seen examples of how to create custom environment tables and how to use them for loaded script files. i mostly undrestand these, but it seems like to do this right (while still maintaining the featureset i want) would require knowledge i don't have yet and far more effort than what it would take to, say, just nuking anything in the global table that isn't in my whitelist:

Code:
//!! = things to deal with if using a custom env table
//!  = create replacement tables for these if using a custom env table
const std::string whitelist_keys[] = {
	"_VERSION",
	"_G",			//!!	point to new env table
	"assert",
	"error",
	"getfenv",		//!!	exclude or create custom func
	"getmetatable",		//!!	exclude or create custom func
	"ipairs",
	"loadstring",		//!!	wizards can get to the real _G with this
	"next",
	"pairs",
	"pcall",
	"print",
	"rawequal",
	"rawget",
	"rawset",
	"select",
	"setfenv",		//!!	exclude or create custom func
	"setmetatable",		//!!	this is flagged as unsafe but i don't know why
	"tonumber",
	"tostring",
	"type",
	"unpack",
	"xpcall",
	// libs:
	"math",			//!	create replacement table
	"string",		//!	''
	"table"			//!	''
};
so to do that i'd just do
Code:
lua_pushnil( state );
lua_setglobal( state, "collectgarbage" );
and so on for whatever i find in the global table that is not in my whitelist.

QUESTIONS:
- where's a good place to ask this sort of question? i was thinking of setting up camp on stackoverflow or something.
- will what i described be reasonably secure? i'll probably limit the max number of operations or max execution time too, but is there anything else i should watch out for?
- are there any big drawbacks that come to mind when messing with the global table like this?
- any other thoughts?

wellp. thanks for taking the time to read this, and thanks in advance for any feedback!
  Reply With Quote
 

WoWInterface » General Discussion » Chit-Chat » tecu tries to learn - c++, lua - creating a sandbox


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off