lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi!

I am learning Lua by working my way through Programming in Lua (4th edition).

In chapter 25 there's a discussion on implementing a sandbox, based on defining a "white list" (my wording) of allowable functions.

This seems straightforward in principle, but in practice I am having a hard time getting hold of some core functions that *need* to go into the whitelist.  The code below is a very pared-down example that shows one of these elusive functions in actions.

    #!/usr/bin/env lua5.3
    local loaded_chunk = assert(load('nonexistent()', "=(load)", "t", {}))
    local function callhook ()
      local info = debug.getinfo(2, "fnS")
      if info.func == loaded_chunk then return end
      error(string.format("calling disallowed function (%s:%d): %s (%s)",
                          info.short_src,
                          info.linedefined,
                          (info.name or "?"),
                          info.func))
    end
    debug.sethook(callhook, "c")
    loaded_chunk()

If I run this script on my Debian system I get the following output:

    lua5.3: ./snippet.lua:6: calling disallowed function ([C]:-1): nonexistent (function: 0x55918f70aef0)
    stack traceback:
        [C]: in function 'error'
        ./snippet.lua:6: in function <./snippet.lua:3>
        [C]: in global 'nonexistent'
        (load):1: in local 'loaded_chunk'
        ./snippet.lua:13: in main chunk
        [C]: in ?

Now, this output is somewhat disconcerting (to me at least, since it gives the impression that the true-to-its-name function `nonexistent` actually lives at 0x55918f70aef0), but the important thing is that what has triggered the call hook is a "mystery function X" called in response to the `attempt to call a nil value` error.  It is this mystery function X that I want to include into the allowed functions whitelist.

How can I determine this function and refer to it in my code?

Muito obrigado in advance!

kj