Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable some lua functions #306

Merged
merged 18 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions contract/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ static void preloadModules(lua_State *L) {
luaopen_db(L);
}

if (vm_is_hardfork(L, 4)) {
lua_getglobal(L, "_G");
// disable getmetatable
lua_pushnil(L);
lua_setfield(L, -2, "getmetatable");
// disable setmetatable
lua_pushnil(L);
lua_setfield(L, -2, "setmetatable");
// disable rawget
lua_pushnil(L);
lua_setfield(L, -2, "rawget");
// disable rawset
lua_pushnil(L);
lua_setfield(L, -2, "rawset");
// disable rawequal
lua_pushnil(L);
lua_setfield(L, -2, "rawequal");
lua_pop(L, 1);
// disable string.dump
lua_getglobal(L, "string");
lua_pushnil(L);
lua_setfield(L, -2, "dump");
lua_pop(L, 1);
}

#ifdef MEASURE
lua_register(L, "nsec", nsec);
luaopen_jit(L);
Expand Down
46 changes: 46 additions & 0 deletions contract/vm_dummy/test_files/disabled-functions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

function check_disabled_functions()

-- check the disabled modules
assert(os == nil, "os is available")
assert(io == nil, "io is available")
assert(debug == nil, "debug is available")
assert(jit == nil, "jit is available")
assert(ffi == nil, "ffi is available")
assert(coroutine == nil, "coroutine is available")
assert(package == nil, "package is available")

-- check the disabled functions
assert(collectgarbage == nil, "collectgarbage is available")
assert(gcinfo == nil, "gcinfo is available")
assert(module == nil, "module is available")
assert(require == nil, "require is available")
assert(dofile == nil, "dofile is available")
assert(load == nil, "load is available")
assert(loadlib == nil, "loadlib is available")
assert(loadfile == nil, "loadfile is available")
assert(loadstring == nil, "loadstring is available")
assert(print == nil, "print is available")
assert(getmetatable == nil, "getmetatable is available")
assert(setmetatable == nil, "setmetatable is available")
assert(rawget == nil, "rawget is available")
assert(rawset == nil, "rawset is available")
assert(rawequal == nil, "rawequal is available")
assert(string.dump == nil, "string.dump is available")

local success, result = pcall(function() newproxy() end)
assert(success == false and result:match(".* 'newproxy' not supported"), "newproxy is available")
local success, result = pcall(function() setfenv() end)
assert(success == false and result:match(".* 'setfenv' not supported"), "setfenv is available")
local success, result = pcall(function() getfenv() end)
assert(success == false and result:match(".* 'getfenv' not supported"), "getfenv is available")

-- make sure the tostring does not return a pointer
local tab = {}
assert(not tostring(type):match("0x[%x]+"), "tostring returns a pointer for function")
assert(not tostring(system):match("0x[%x]+"), "tostring returns a pointer for internal table")
assert(not tostring(tab):match("0x[%x]+"), "tostring returns a pointer for table")

end

abi.register(check_disabled_functions)
10 changes: 7 additions & 3 deletions contract/vm_dummy/test_files/feature_isolation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ function override_functions()
-- override contract.balance
contract.balance = function() return "123" end

-- override the __add metamethod on bignum module
getmetatable(bignum.number(0)).__add = function(x,y) return x-y end
if getmetatable ~= nil then
-- override the __add metamethod on bignum module
getmetatable(bignum.number(0)).__add = function(x,y) return x-y end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't review deeper code about getmemtable, but it doesn't look like addtion rather substraction. Anyway, if this code did not cause any problem, we can leave this as is, since this commit don't change the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this test overrides the add (+) operator with a subtraction (-) to check if that feature of overriding functions is possible. the feature is disabled by this PR

end

end

Expand All @@ -37,7 +39,9 @@ function check_local_overridden_functions()
assert2(test_sender() == "overridden", "system.getSender() override failed")
assert2(test_origin() == "overridden", "system.getOrigin() override failed")
assert2(test_balance() == "123", "contract.balance() override failed")
assert2(test_bignum() == bignum.number(3), "metamethod override failed")
if getmetatable ~= nil then
assert2(test_bignum() == bignum.number(3), "metamethod override failed")
end

end

Expand Down
Loading
Loading