1995-08-04 00:59:03 -03:00
|
|
|
"""Restricted execution facilities.
|
1995-01-12 08:29:47 -04:00
|
|
|
|
1996-05-28 20:07:17 -03:00
|
|
|
The class RExec exports methods r_exec(), r_eval(), r_execfile(), and
|
|
|
|
r_import(), which correspond roughly to the built-in operations
|
1995-08-04 00:59:03 -03:00
|
|
|
exec, eval(), execfile() and import, but executing the code in an
|
|
|
|
environment that only exposes those built-in operations that are
|
|
|
|
deemed safe. To this end, a modest collection of 'fake' modules is
|
|
|
|
created which mimics the standard modules by the same names. It is a
|
|
|
|
policy decision which built-in modules and operations are made
|
|
|
|
available; this module provides a reasonable default, but derived
|
|
|
|
classes can change the policies e.g. by overriding or extending class
|
|
|
|
variables like ok_builtin_modules or methods like make_sys().
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
XXX To do:
|
|
|
|
- r_open should allow writing tmp dir
|
|
|
|
- r_exec etc. with explicit globals/locals? (Use rexec("exec ... in ...")?)
|
|
|
|
|
1995-08-04 00:59:03 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
1995-01-12 08:29:47 -04:00
|
|
|
import __builtin__
|
|
|
|
import os
|
1995-08-04 00:59:03 -03:00
|
|
|
import marshal
|
|
|
|
import ihooks
|
|
|
|
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
class FileBase:
|
|
|
|
|
|
|
|
ok_file_methods = ('fileno', 'flush', 'isatty', 'read', 'readline',
|
|
|
|
'readlines', 'seek', 'tell', 'write', 'writelines')
|
|
|
|
|
|
|
|
|
|
|
|
class FileWrapper(FileBase):
|
|
|
|
|
1996-06-28 14:28:51 -03:00
|
|
|
# XXX This is just like a Bastion -- should use that!
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
def __init__(self, f):
|
|
|
|
self.f = f
|
|
|
|
for m in self.ok_file_methods:
|
1996-09-30 15:50:44 -03:00
|
|
|
if not hasattr(self, m) and hasattr(f, m):
|
1995-08-07 17:19:27 -03:00
|
|
|
setattr(self, m, getattr(f, m))
|
|
|
|
|
1996-09-25 15:47:39 -03:00
|
|
|
def close(self):
|
1995-08-07 17:19:27 -03:00
|
|
|
self.flush()
|
|
|
|
|
|
|
|
|
|
|
|
TEMPLATE = """
|
|
|
|
def %s(self, *args):
|
|
|
|
return apply(getattr(self.mod, self.name).%s, args)
|
|
|
|
"""
|
|
|
|
|
|
|
|
class FileDelegate(FileBase):
|
|
|
|
|
|
|
|
def __init__(self, mod, name):
|
|
|
|
self.mod = mod
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
for m in FileBase.ok_file_methods + ('close',):
|
|
|
|
exec TEMPLATE % (m, m)
|
|
|
|
|
|
|
|
|
1995-08-04 00:59:03 -03:00
|
|
|
class RHooks(ihooks.Hooks):
|
|
|
|
|
1996-05-28 20:07:17 -03:00
|
|
|
def __init__(self, *args):
|
|
|
|
# Hacks to support both old and new interfaces:
|
|
|
|
# old interface was RHooks(rexec[, verbose])
|
|
|
|
# new interface is RHooks([verbose])
|
|
|
|
verbose = 0
|
|
|
|
rexec = None
|
|
|
|
if args and type(args[-1]) == type(0):
|
|
|
|
verbose = args[-1]
|
|
|
|
args = args[:-1]
|
|
|
|
if args and hasattr(args[0], '__class__'):
|
|
|
|
rexec = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
if args:
|
|
|
|
raise TypeError, "too many arguments"
|
1995-08-04 00:59:03 -03:00
|
|
|
ihooks.Hooks.__init__(self, verbose)
|
|
|
|
self.rexec = rexec
|
|
|
|
|
1996-05-28 20:07:17 -03:00
|
|
|
def set_rexec(self, rexec):
|
|
|
|
# Called by RExec instance to complete initialization
|
|
|
|
self.rexec = rexec
|
|
|
|
|
1995-08-04 00:59:03 -03:00
|
|
|
def is_builtin(self, name):
|
|
|
|
return self.rexec.is_builtin(name)
|
|
|
|
|
|
|
|
def init_builtin(self, name):
|
|
|
|
m = __import__(name)
|
|
|
|
return self.rexec.copy_except(m, ())
|
|
|
|
|
|
|
|
def init_frozen(self, name): raise SystemError, "don't use this"
|
|
|
|
def load_source(self, *args): raise SystemError, "don't use this"
|
|
|
|
def load_compiled(self, *args): raise SystemError, "don't use this"
|
|
|
|
|
1996-05-28 20:07:17 -03:00
|
|
|
def load_dynamic(self, name, filename, file):
|
|
|
|
return self.rexec.load_dynamic(name, filename, file)
|
1995-08-04 00:59:03 -03:00
|
|
|
|
|
|
|
def add_module(self, name):
|
|
|
|
return self.rexec.add_module(name)
|
|
|
|
|
|
|
|
def modules_dict(self):
|
|
|
|
return self.rexec.modules
|
|
|
|
|
|
|
|
def default_path(self):
|
|
|
|
return self.rexec.modules['sys'].path
|
|
|
|
|
|
|
|
|
|
|
|
class RModuleLoader(ihooks.FancyModuleLoader):
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
def load_module(self, name, stuff):
|
|
|
|
file, filename, info = stuff
|
|
|
|
m = ihooks.FancyModuleLoader.load_module(self, name, stuff)
|
|
|
|
m.__filename__ = filename
|
|
|
|
return m
|
1995-08-04 00:59:03 -03:00
|
|
|
|
|
|
|
|
|
|
|
class RModuleImporter(ihooks.ModuleImporter):
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
def reload(self, module, path=None):
|
|
|
|
if path is None and hasattr(module, '__filename__'):
|
1995-08-10 16:40:39 -03:00
|
|
|
head, tail = os.path.split(module.__filename__)
|
1995-08-11 10:56:04 -03:00
|
|
|
path = [os.path.join(head, '')]
|
1995-08-07 17:19:27 -03:00
|
|
|
return ihooks.ModuleImporter.reload(self, module, path)
|
1995-08-04 00:59:03 -03:00
|
|
|
|
|
|
|
|
|
|
|
class RExec(ihooks._Verbose):
|
|
|
|
|
|
|
|
"""Restricted Execution environment."""
|
|
|
|
|
|
|
|
ok_path = tuple(sys.path) # That's a policy decision
|
|
|
|
|
1996-09-25 15:47:39 -03:00
|
|
|
ok_builtin_modules = ('audioop', 'array', 'binascii',
|
|
|
|
'cmath', 'errno', 'imageop',
|
1997-03-21 17:18:16 -04:00
|
|
|
'math', 'md5', 'operator',
|
1996-09-25 15:47:39 -03:00
|
|
|
'parser', 'regex', 'rotor', 'select',
|
1995-08-04 00:59:03 -03:00
|
|
|
'strop', 'struct', 'time')
|
|
|
|
|
|
|
|
ok_posix_names = ('error', 'fstat', 'listdir', 'lstat', 'readlink',
|
|
|
|
'stat', 'times', 'uname', 'getpid', 'getppid',
|
|
|
|
'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')
|
|
|
|
|
|
|
|
ok_sys_names = ('ps1', 'ps2', 'copyright', 'version',
|
|
|
|
'platform', 'exit', 'maxint')
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
nok_builtin_names = ('open', 'reload', '__import__')
|
1995-08-04 00:59:03 -03:00
|
|
|
|
|
|
|
def __init__(self, hooks = None, verbose = 0):
|
|
|
|
ihooks._Verbose.__init__(self, verbose)
|
|
|
|
# XXX There's a circular reference here:
|
1996-05-28 20:07:17 -03:00
|
|
|
self.hooks = hooks or RHooks(verbose)
|
|
|
|
self.hooks.set_rexec(self)
|
1995-08-04 00:59:03 -03:00
|
|
|
self.modules = {}
|
1996-05-28 20:07:17 -03:00
|
|
|
self.ok_dynamic_modules = self.ok_builtin_modules
|
|
|
|
list = []
|
|
|
|
for mname in self.ok_builtin_modules:
|
|
|
|
if mname in sys.builtin_module_names:
|
|
|
|
list.append(mname)
|
1996-12-12 17:31:52 -04:00
|
|
|
self.ok_builtin_modules = tuple(list)
|
1996-05-28 20:07:17 -03:00
|
|
|
self.set_trusted_path()
|
1995-08-04 00:59:03 -03:00
|
|
|
self.make_builtin()
|
|
|
|
self.make_initial_modules()
|
|
|
|
# make_sys must be last because it adds the already created
|
|
|
|
# modules to its builtin_module_names
|
|
|
|
self.make_sys()
|
|
|
|
self.loader = RModuleLoader(self.hooks, verbose)
|
|
|
|
self.importer = RModuleImporter(self.loader, verbose)
|
|
|
|
|
1996-05-28 20:07:17 -03:00
|
|
|
def set_trusted_path(self):
|
|
|
|
# Set the path from which dynamic modules may be loaded.
|
|
|
|
# Those dynamic modules must also occur in ok_builtin_modules
|
|
|
|
self.trusted_path = filter(os.path.isabs, sys.path)
|
|
|
|
|
|
|
|
def load_dynamic(self, name, filename, file):
|
|
|
|
if name not in self.ok_dynamic_modules:
|
|
|
|
raise ImportError, "untrusted dynamic module: %s" % name
|
|
|
|
if sys.modules.has_key(name):
|
1996-05-28 20:34:10 -03:00
|
|
|
src = sys.modules[name]
|
1996-05-28 20:07:17 -03:00
|
|
|
else:
|
|
|
|
import imp
|
|
|
|
src = imp.load_dynamic(name, filename, file)
|
|
|
|
dst = self.copy_except(src, [])
|
|
|
|
return dst
|
|
|
|
|
1995-08-04 00:59:03 -03:00
|
|
|
def make_initial_modules(self):
|
|
|
|
self.make_main()
|
|
|
|
self.make_osname()
|
|
|
|
|
|
|
|
# Helpers for RHooks
|
|
|
|
|
|
|
|
def is_builtin(self, mname):
|
|
|
|
return mname in self.ok_builtin_modules
|
|
|
|
|
|
|
|
# The make_* methods create specific built-in modules
|
|
|
|
|
|
|
|
def make_builtin(self):
|
|
|
|
m = self.copy_except(__builtin__, self.nok_builtin_names)
|
|
|
|
m.__import__ = self.r_import
|
1995-08-07 17:19:27 -03:00
|
|
|
m.reload = self.r_reload
|
|
|
|
m.open = self.r_open
|
1995-08-04 00:59:03 -03:00
|
|
|
|
|
|
|
def make_main(self):
|
|
|
|
m = self.add_module('__main__')
|
|
|
|
|
|
|
|
def make_osname(self):
|
|
|
|
osname = os.name
|
|
|
|
src = __import__(osname)
|
|
|
|
dst = self.copy_only(src, self.ok_posix_names)
|
|
|
|
dst.environ = e = {}
|
|
|
|
for key, value in os.environ.items():
|
|
|
|
e[key] = value
|
|
|
|
|
|
|
|
def make_sys(self):
|
|
|
|
m = self.copy_only(sys, self.ok_sys_names)
|
|
|
|
m.modules = self.modules
|
|
|
|
m.argv = ['RESTRICTED']
|
|
|
|
m.path = map(None, self.ok_path)
|
|
|
|
m = self.modules['sys']
|
1996-12-12 17:31:52 -04:00
|
|
|
l = self.modules.keys() + list(self.ok_builtin_modules)
|
|
|
|
l.sort()
|
|
|
|
m.builtin_module_names = tuple(l)
|
1995-08-04 00:59:03 -03:00
|
|
|
|
|
|
|
# The copy_* methods copy existing modules with some changes
|
|
|
|
|
|
|
|
def copy_except(self, src, exceptions):
|
|
|
|
dst = self.copy_none(src)
|
|
|
|
for name in dir(src):
|
1996-05-28 20:07:17 -03:00
|
|
|
setattr(dst, name, getattr(src, name))
|
|
|
|
for name in exceptions:
|
|
|
|
try:
|
|
|
|
delattr(dst, name)
|
1996-08-20 17:25:08 -03:00
|
|
|
except AttributeError:
|
1996-05-28 20:07:17 -03:00
|
|
|
pass
|
1995-08-04 00:59:03 -03:00
|
|
|
return dst
|
|
|
|
|
|
|
|
def copy_only(self, src, names):
|
|
|
|
dst = self.copy_none(src)
|
|
|
|
for name in names:
|
|
|
|
try:
|
|
|
|
value = getattr(src, name)
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
setattr(dst, name, value)
|
|
|
|
return dst
|
|
|
|
|
|
|
|
def copy_none(self, src):
|
|
|
|
return self.add_module(src.__name__)
|
|
|
|
|
|
|
|
# Add a module -- return an existing module or create one
|
|
|
|
|
|
|
|
def add_module(self, mname):
|
|
|
|
if self.modules.has_key(mname):
|
|
|
|
return self.modules[mname]
|
|
|
|
self.modules[mname] = m = self.hooks.new_module(mname)
|
|
|
|
m.__builtins__ = self.modules['__builtin__']
|
1995-01-12 08:29:47 -04:00
|
|
|
return m
|
|
|
|
|
1995-08-04 00:59:03 -03:00
|
|
|
# The r* methods are public interfaces
|
|
|
|
|
|
|
|
def r_exec(self, code):
|
|
|
|
m = self.add_module('__main__')
|
|
|
|
exec code in m.__dict__
|
|
|
|
|
|
|
|
def r_eval(self, code):
|
|
|
|
m = self.add_module('__main__')
|
|
|
|
return eval(code, m.__dict__)
|
|
|
|
|
|
|
|
def r_execfile(self, file):
|
|
|
|
m = self.add_module('__main__')
|
|
|
|
return execfile(file, m.__dict__)
|
|
|
|
|
|
|
|
def r_import(self, mname, globals={}, locals={}, fromlist=[]):
|
|
|
|
return self.importer.import_module(mname, globals, locals, fromlist)
|
1995-01-12 08:29:47 -04:00
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
def r_reload(self, m):
|
|
|
|
return self.importer.reload(m)
|
1995-08-08 23:32:08 -03:00
|
|
|
|
|
|
|
def r_unload(self, m):
|
|
|
|
return self.importer.unload(m)
|
1995-08-07 17:19:27 -03:00
|
|
|
|
|
|
|
# The s_* methods are similar but also swap std{in,out,err}
|
|
|
|
|
1996-06-28 14:28:51 -03:00
|
|
|
def make_delegate_files(self):
|
|
|
|
s = self.modules['sys']
|
|
|
|
self.delegate_stdin = FileDelegate(s, 'stdin')
|
|
|
|
self.delegate_stdout = FileDelegate(s, 'stdout')
|
|
|
|
self.delegate_stderr = FileDelegate(s, 'stderr')
|
|
|
|
self.restricted_stdin = FileWrapper(sys.stdin)
|
|
|
|
self.restricted_stdout = FileWrapper(sys.stdout)
|
|
|
|
self.restricted_stderr = FileWrapper(sys.stderr)
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
def set_files(self):
|
1996-06-28 14:28:51 -03:00
|
|
|
if not hasattr(self, 'save_stdin'):
|
|
|
|
self.save_files()
|
|
|
|
if not hasattr(self, 'delegate_stdin'):
|
|
|
|
self.make_delegate_files()
|
1995-08-07 17:19:27 -03:00
|
|
|
s = self.modules['sys']
|
1996-06-28 14:28:51 -03:00
|
|
|
s.stdin = self.restricted_stdin
|
|
|
|
s.stdout = self.restricted_stdout
|
1996-10-02 11:57:28 -03:00
|
|
|
s.stderr = self.restricted_stderr
|
1996-06-28 14:28:51 -03:00
|
|
|
sys.stdin = self.delegate_stdin
|
|
|
|
sys.stdout = self.delegate_stdout
|
|
|
|
sys.stderr = self.delegate_stderr
|
|
|
|
|
|
|
|
def reset_files(self):
|
|
|
|
self.restore_files()
|
|
|
|
s = self.modules['sys']
|
|
|
|
self.restricted_stdin = s.stdin
|
|
|
|
self.restricted_stdout = s.stdout
|
1996-10-02 11:57:28 -03:00
|
|
|
self.restricted_stderr = s.stderr
|
1996-06-28 14:28:51 -03:00
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
|
|
|
|
def save_files(self):
|
|
|
|
self.save_stdin = sys.stdin
|
|
|
|
self.save_stdout = sys.stdout
|
|
|
|
self.save_stderr = sys.stderr
|
|
|
|
|
1996-06-28 14:28:51 -03:00
|
|
|
def restore_files(self):
|
|
|
|
sys.stdin = self.save_stdin
|
1995-08-07 17:19:27 -03:00
|
|
|
sys.stdout = self.save_stdout
|
|
|
|
sys.stderr = self.save_stderr
|
|
|
|
|
1996-09-25 15:47:39 -03:00
|
|
|
def s_apply(self, func, args=(), kw=None):
|
1995-08-07 17:19:27 -03:00
|
|
|
self.save_files()
|
|
|
|
try:
|
|
|
|
self.set_files()
|
1996-09-25 15:47:39 -03:00
|
|
|
if kw:
|
|
|
|
r = apply(func, args, kw)
|
|
|
|
else:
|
|
|
|
r = apply(func, args)
|
1995-08-07 17:19:27 -03:00
|
|
|
finally:
|
|
|
|
self.restore_files()
|
|
|
|
|
|
|
|
def s_exec(self, *args):
|
|
|
|
self.s_apply(self.r_exec, args)
|
|
|
|
|
|
|
|
def s_eval(self, *args):
|
|
|
|
self.s_apply(self.r_eval, args)
|
|
|
|
|
|
|
|
def s_execfile(self, *args):
|
|
|
|
self.s_apply(self.r_execfile, args)
|
|
|
|
|
|
|
|
def s_import(self, *args):
|
|
|
|
self.s_apply(self.r_import, args)
|
|
|
|
|
|
|
|
def s_reload(self, *args):
|
|
|
|
self.s_apply(self.r_reload, args)
|
|
|
|
|
1995-08-08 23:32:08 -03:00
|
|
|
def s_unload(self, *args):
|
|
|
|
self.s_apply(self.r_unload, args)
|
|
|
|
|
1995-08-07 17:19:27 -03:00
|
|
|
# Restricted open(...)
|
|
|
|
|
|
|
|
def r_open(self, file, mode='r', buf=-1):
|
|
|
|
if mode not in ('r', 'rb'):
|
|
|
|
raise IOError, "can't open files for writing in restricted mode"
|
1995-08-08 23:32:08 -03:00
|
|
|
return open(file, mode, buf)
|
1995-08-07 17:19:27 -03:00
|
|
|
|
1995-01-12 08:29:47 -04:00
|
|
|
|
|
|
|
def test():
|
1995-08-04 00:59:03 -03:00
|
|
|
import traceback
|
1996-08-20 17:25:08 -03:00
|
|
|
r = RExec(verbose=('-v' in sys.argv[1:]))
|
1995-08-04 00:59:03 -03:00
|
|
|
print "*** RESTRICTED *** Python", sys.version
|
|
|
|
print sys.copyright
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
s = raw_input('>>> ')
|
|
|
|
except EOFError:
|
|
|
|
print
|
|
|
|
break
|
|
|
|
if s and s[0] != '#':
|
|
|
|
s = s + '\n'
|
|
|
|
c = compile(s, '<stdin>', 'single')
|
|
|
|
r.r_exec(c)
|
|
|
|
except SystemExit, n:
|
|
|
|
sys.exit(n)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
1995-01-12 08:29:47 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
1995-08-04 00:59:03 -03:00
|
|
|
test()
|