How to wrap IO functions in Lua so that the user does not leave the X directory

How would you wrap the IO functions in Lua so that someone doesn't leave your top-level directory.

You place them in "MyDoc", and they have full IO access to the entire subset of MyDoc, but cannot, for example, return to C drive or anywhere else.

+4
source share
2 answers

open liolib.c. let's move on to these 3 functions

static void opencheck (lua_State *L, const char *fname, const char *mode) {
  LStream *p = newfile(L);
  p->f = fopen(fname, mode);
  if (p->f == NULL)
    luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno));
}

static int io_open (lua_State *L) {
  const char *filename = luaL_checkstring(L, 1);
  const char *mode = luaL_optstring(L, 2, "r");
  LStream *p = newfile(L);
  const char *md = mode;  /* to traverse/check mode */
  luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode");
  p->f = fopen(filename, mode);
  return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
}


static int io_popen (lua_State *L) {
  const char *filename = luaL_checkstring(L, 1);
  const char *mode = luaL_optstring(L, 2, "r");
  LStream *p = newprefile(L);
  p->f = lua_popen(L, filename, mode);
  p->closef = &io_pclose;
  return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
}

These are the features you want to edit.

the first gets the file name as a parameter fname, the second and third pull it from the lua stack as a local variable filename.

now all you have to do is

1) get your own process path

2)

3) ,

4), , opencheck luaL_error(L,"access denied to %s", fname);  return luaL_fileresult(L,0,filename);

+1

, , , , "require" "dofile" "setatable"? , , , , . , , , , , - , , "".

, API C, , , , . API C, Lua .

, Lua, . , lua, C. . , , getmetable , metatable, .

, C

  • , io, (, ) (meta);
  • io _G, ; , .
  • io , 1, , , , "".
  • , .

, , Lua 5.1 vs 5.2, Lua (, , ), -, , , Lua SO /.;)

+1

All Articles