rexec.py 19.6 KB
Newer Older
1
"""Restricted execution facilities.
Guido van Rossum's avatar
Guido van Rossum committed
2

3 4
The class RExec exports methods r_exec(), r_eval(), r_execfile(), and
r_import(), which correspond roughly to the built-in operations
5 6 7 8 9 10 11 12 13
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().

14 15 16 17
XXX To do:
- r_open should allow writing tmp dir
- r_exec etc. with explicit globals/locals? (Use rexec("exec ... in ...")?)

18 19 20 21
"""


import sys
Guido van Rossum's avatar
Guido van Rossum committed
22 23
import __builtin__
import os
24
import ihooks
Guido van Rossum's avatar
Guido van Rossum committed
25
import imp
26

27
__all__ = ["RExec"]
28

29 30
class FileBase:

31 32
    ok_file_methods = ('fileno', 'flush', 'isatty', 'read', 'readline',
            'readlines', 'seek', 'tell', 'write', 'writelines')
33 34 35 36


class FileWrapper(FileBase):

37
    # XXX This is just like a Bastion -- should use that!
38

39 40 41 42 43 44 45 46
    def __init__(self, f):
        self.f = f
        for m in self.ok_file_methods:
            if not hasattr(self, m) and hasattr(f, m):
                setattr(self, m, getattr(f, m))

    def close(self):
        self.flush()
47 48 49 50


TEMPLATE = """
def %s(self, *args):
51
        return apply(getattr(self.mod, self.name).%s, args)
52 53 54 55
"""

class FileDelegate(FileBase):

56 57 58 59 60 61
    def __init__(self, mod, name):
        self.mod = mod
        self.name = name

    for m in FileBase.ok_file_methods + ('close',):
        exec TEMPLATE % (m, m)
62 63


64 65
class RHooks(ihooks.Hooks):

66
    def __init__(self, *args):
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        # 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"
        ihooks.Hooks.__init__(self, verbose)
        self.rexec = rexec
82

83
    def set_rexec(self, rexec):
84 85
        # Called by RExec instance to complete initialization
        self.rexec = rexec
86

Guido van Rossum's avatar
Guido van Rossum committed
87 88 89
    def get_suffixes(self):
        return self.rexec.get_suffixes()

90
    def is_builtin(self, name):
91
        return self.rexec.is_builtin(name)
92 93

    def init_builtin(self, name):
94 95
        m = __import__(name)
        return self.rexec.copy_except(m, ())
96 97 98 99

    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"
100
    def load_package(self, *args): raise SystemError, "don't use this"
101

102
    def load_dynamic(self, name, filename, file):
103
        return self.rexec.load_dynamic(name, filename, file)
104 105

    def add_module(self, name):
106
        return self.rexec.add_module(name)
107 108

    def modules_dict(self):
109
        return self.rexec.modules
110 111

    def default_path(self):
112
        return self.rexec.modules['sys'].path
113 114


115 116 117
# XXX Backwards compatibility
RModuleLoader = ihooks.FancyModuleLoader
RModuleImporter = ihooks.ModuleImporter
118 119 120


class RExec(ihooks._Verbose):
121
    """Basic restricted execution framework.
122

123 124 125 126 127 128 129 130 131 132
    Code executed in this restricted environment will only have access to
    modules and functions that are deemed safe; you can subclass RExec to
    add or remove capabilities as desired.

    The RExec class can prevent code from performing unsafe operations like
    reading or writing disk files, or using TCP/IP sockets.  However, it does
    not protect against code using extremely large amounts of memory or
    processor time.

    """
133

134
    ok_path = tuple(sys.path)           # That's a policy decision
135

136
    ok_builtin_modules = ('audioop', 'array', 'binascii',
137 138 139
                          'cmath', 'errno', 'imageop',
                          'marshal', 'math', 'md5', 'operator',
                          'parser', 'regex', 'pcre', 'rotor', 'select',
140
                          'sha', '_sre', 'strop', 'struct', 'time')
141 142

    ok_posix_names = ('error', 'fstat', 'listdir', 'lstat', 'readlink',
143 144
                      'stat', 'times', 'uname', 'getpid', 'getppid',
                      'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')
145 146

    ok_sys_names = ('ps1', 'ps2', 'copyright', 'version',
147
                    'platform', 'exit', 'maxint')
148

149
    nok_builtin_names = ('open', 'file', 'reload', '__import__')
150

Guido van Rossum's avatar
Guido van Rossum committed
151 152
    ok_file_types = (imp.C_EXTENSION, imp.PY_SOURCE)

153
    def __init__(self, hooks = None, verbose = 0):
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        """Returns an instance of the RExec class.

        The hooks parameter is an instance of the RHooks class or a subclass
        of it.  If it is omitted or None, the default RHooks class is
        instantiated.

        Whenever the RExec module searches for a module (even a built-in one)
        or reads a module's code, it doesn't actually go out to the file
        system itself.  Rather, it calls methods of an RHooks instance that
        was passed to or created by its constructor.  (Actually, the RExec
        object doesn't make these calls --- they are made by a module loader
        object that's part of the RExec object.  This allows another level of
        flexibility, which can be useful when changing the mechanics of
        import within the restricted environment.)

        By providing an alternate RHooks object, we can control the file
        system accesses made to import a module, without changing the
        actual algorithm that controls the order in which those accesses are
        made.  For instance, we could substitute an RHooks object that
        passes all filesystem requests to a file server elsewhere, via some
        RPC mechanism such as ILU.  Grail's applet loader uses this to support
        importing applets from a URL for a directory.

        If the verbose parameter is true, additional debugging output may be
        sent to standard output.

        """
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        ihooks._Verbose.__init__(self, verbose)
        # XXX There's a circular reference here:
        self.hooks = hooks or RHooks(verbose)
        self.hooks.set_rexec(self)
        self.modules = {}
        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)
        self.ok_builtin_modules = tuple(list)
        self.set_trusted_path()
        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)
200

201
    def set_trusted_path(self):
202 203 204
        # 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)
205 206

    def load_dynamic(self, name, filename, file):
207 208 209 210 211 212 213 214
        if name not in self.ok_dynamic_modules:
            raise ImportError, "untrusted dynamic module: %s" % name
        if sys.modules.has_key(name):
            src = sys.modules[name]
        else:
            src = imp.load_dynamic(name, filename, file)
        dst = self.copy_except(src, [])
        return dst
215

216
    def make_initial_modules(self):
217 218
        self.make_main()
        self.make_osname()
219 220 221

    # Helpers for RHooks

Guido van Rossum's avatar
Guido van Rossum committed
222 223 224 225 226
    def get_suffixes(self):
        return [item   # (suff, mode, type)
                for item in imp.get_suffixes()
                if item[2] in self.ok_file_types]

227
    def is_builtin(self, mname):
228
        return mname in self.ok_builtin_modules
229 230 231 232

    # The make_* methods create specific built-in modules

    def make_builtin(self):
233 234 235
        m = self.copy_except(__builtin__, self.nok_builtin_names)
        m.__import__ = self.r_import
        m.reload = self.r_reload
236
        m.open = m.file = self.r_open
237 238

    def make_main(self):
239
        m = self.add_module('__main__')
240 241

    def make_osname(self):
242 243 244 245 246 247
        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
248 249

    def make_sys(self):
250 251 252 253
        m = self.copy_only(sys, self.ok_sys_names)
        m.modules = self.modules
        m.argv = ['RESTRICTED']
        m.path = map(None, self.ok_path)
254
        m.exc_info = self.r_exc_info
255 256 257 258
        m = self.modules['sys']
        l = self.modules.keys() + list(self.ok_builtin_modules)
        l.sort()
        m.builtin_module_names = tuple(l)
259 260 261 262

    # The copy_* methods copy existing modules with some changes

    def copy_except(self, src, exceptions):
263 264 265 266 267 268 269 270 271
        dst = self.copy_none(src)
        for name in dir(src):
            setattr(dst, name, getattr(src, name))
        for name in exceptions:
            try:
                delattr(dst, name)
            except AttributeError:
                pass
        return dst
272 273

    def copy_only(self, src, names):
274 275 276 277 278 279 280 281
        dst = self.copy_none(src)
        for name in names:
            try:
                value = getattr(src, name)
            except AttributeError:
                continue
            setattr(dst, name, value)
        return dst
282 283

    def copy_none(self, src):
284 285 286
        m = self.add_module(src.__name__)
        m.__doc__ = src.__doc__
        return m
287 288 289 290

    # Add a module -- return an existing module or create one

    def add_module(self, mname):
291 292 293
        m = self.modules.get(mname)
        if m is None:
            self.modules[mname] = m = self.hooks.new_module(mname)
294 295
        m.__builtins__ = self.modules['__builtin__']
        return m
Guido van Rossum's avatar
Guido van Rossum committed
296

297 298 299
    # The r* methods are public interfaces

    def r_exec(self, code):
300 301 302 303 304 305 306
        """Execute code within a restricted environment.

        The code parameter must either be a string containing one or more
        lines of Python code, or a compiled code object, which will be
        executed in the restricted environment's __main__ module.

        """
307 308
        m = self.add_module('__main__')
        exec code in m.__dict__
309 310

    def r_eval(self, code):
311 312 313 314 315 316 317 318
        """Evaluate code within a restricted environment.

        The code parameter must either be a string containing a Python
        expression, or a compiled code object, which will be evaluated in
        the restricted environment's __main__ module.  The value of the
        expression or code object will be returned.

        """
319 320
        m = self.add_module('__main__')
        return eval(code, m.__dict__)
321 322

    def r_execfile(self, file):
323 324 325 326
        """Execute the Python code in the file in the restricted
        environment's __main__ module.

        """
327
        m = self.add_module('__main__')
328
        execfile(file, m.__dict__)
329 330

    def r_import(self, mname, globals={}, locals={}, fromlist=[]):
331 332 333 334 335 336 337 338
        """Import a module, raising an ImportError exception if the module
        is considered unsafe.

        This method is implicitly called by code executing in the
        restricted environment.  Overriding this method in a subclass is
        used to change the policies enforced by a restricted environment.

        """
339
        return self.importer.import_module(mname, globals, locals, fromlist)
Guido van Rossum's avatar
Guido van Rossum committed
340

341
    def r_reload(self, m):
342 343 344 345 346 347 348
        """Reload the module object, re-parsing and re-initializing it.

        This method is implicitly called by code executing in the
        restricted environment.  Overriding this method in a subclass is
        used to change the policies enforced by a restricted environment.

        """
349
        return self.importer.reload(m)
350 351

    def r_unload(self, m):
352 353 354 355 356 357 358 359 360
        """Unload the module.

        Removes it from the restricted environment's sys.modules dictionary.

        This method is implicitly called by code executing in the
        restricted environment.  Overriding this method in a subclass is
        used to change the policies enforced by a restricted environment.

        """
361
        return self.importer.unload(m)
Tim Peters's avatar
Tim Peters committed
362

363 364
    # The s_* methods are similar but also swap std{in,out,err}

365 366
    def make_delegate_files(self):
        s = self.modules['sys']
367 368 369
        self.delegate_stdin = FileDelegate(s, 'stdin')
        self.delegate_stdout = FileDelegate(s, 'stdout')
        self.delegate_stderr = FileDelegate(s, 'stderr')
370 371 372 373
        self.restricted_stdin = FileWrapper(sys.stdin)
        self.restricted_stdout = FileWrapper(sys.stdout)
        self.restricted_stderr = FileWrapper(sys.stderr)

374
    def set_files(self):
375 376 377 378
        if not hasattr(self, 'save_stdin'):
            self.save_files()
        if not hasattr(self, 'delegate_stdin'):
            self.make_delegate_files()
379
        s = self.modules['sys']
380 381 382 383 384 385
        s.stdin = self.restricted_stdin
        s.stdout = self.restricted_stdout
        s.stderr = self.restricted_stderr
        sys.stdin = self.delegate_stdin
        sys.stdout = self.delegate_stdout
        sys.stderr = self.delegate_stderr
386 387

    def reset_files(self):
388
        self.restore_files()
389
        s = self.modules['sys']
390 391 392
        self.restricted_stdin = s.stdin
        self.restricted_stdout = s.stdout
        self.restricted_stderr = s.stderr
Tim Peters's avatar
Tim Peters committed
393

394 395 396 397 398 399

    def save_files(self):
        self.save_stdin = sys.stdin
        self.save_stdout = sys.stdout
        self.save_stderr = sys.stderr

400
    def restore_files(self):
401 402 403
        sys.stdin = self.save_stdin
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
Tim Peters's avatar
Tim Peters committed
404

405
    def s_apply(self, func, args=(), kw=None):
406 407 408 409 410 411 412
        self.save_files()
        try:
            self.set_files()
            if kw:
                r = apply(func, args, kw)
            else:
                r = apply(func, args)
413
        finally:
414
            self.restore_files()
415
        return r
Tim Peters's avatar
Tim Peters committed
416

417
    def s_exec(self, *args):
418 419 420 421 422 423 424 425 426 427 428
        """Execute code within a restricted environment.

        Similar to the r_exec() method, but the code will be granted access
        to restricted versions of the standard I/O streams sys.stdin,
        sys.stderr, and sys.stdout.

        The code parameter must either be a string containing one or more
        lines of Python code, or a compiled code object, which will be
        executed in the restricted environment's __main__ module.

        """
429
        return self.s_apply(self.r_exec, args)
Tim Peters's avatar
Tim Peters committed
430

431
    def s_eval(self, *args):
432 433 434 435 436 437 438 439 440 441 442 443
        """Evaluate code within a restricted environment.

        Similar to the r_eval() method, but the code will be granted access
        to restricted versions of the standard I/O streams sys.stdin,
        sys.stderr, and sys.stdout.

        The code parameter must either be a string containing a Python
        expression, or a compiled code object, which will be evaluated in
        the restricted environment's __main__ module.  The value of the
        expression or code object will be returned.

        """
444
        return self.s_apply(self.r_eval, args)
Tim Peters's avatar
Tim Peters committed
445

446
    def s_execfile(self, *args):
447 448 449 450 451 452 453 454
        """Execute the Python code in the file in the restricted
        environment's __main__ module.

        Similar to the r_execfile() method, but the code will be granted
        access to restricted versions of the standard I/O streams sys.stdin,
        sys.stderr, and sys.stdout.

        """
455
        return self.s_apply(self.r_execfile, args)
Tim Peters's avatar
Tim Peters committed
456

457
    def s_import(self, *args):
458 459 460 461 462 463 464 465 466 467 468 469
        """Import a module, raising an ImportError exception if the module
        is considered unsafe.

        This method is implicitly called by code executing in the
        restricted environment.  Overriding this method in a subclass is
        used to change the policies enforced by a restricted environment.

        Similar to the r_import() method, but has access to restricted
        versions of the standard I/O streams sys.stdin, sys.stderr, and
        sys.stdout.

        """
470
        return self.s_apply(self.r_import, args)
Tim Peters's avatar
Tim Peters committed
471

472
    def s_reload(self, *args):
473 474 475 476 477 478 479 480 481 482 483
        """Reload the module object, re-parsing and re-initializing it.

        This method is implicitly called by code executing in the
        restricted environment.  Overriding this method in a subclass is
        used to change the policies enforced by a restricted environment.

        Similar to the r_reload() method, but has access to restricted
        versions of the standard I/O streams sys.stdin, sys.stderr, and
        sys.stdout.

        """
484
        return self.s_apply(self.r_reload, args)
Tim Peters's avatar
Tim Peters committed
485

486
    def s_unload(self, *args):
487 488 489 490 491 492 493 494 495 496 497 498 499
        """Unload the module.

        Removes it from the restricted environment's sys.modules dictionary.

        This method is implicitly called by code executing in the
        restricted environment.  Overriding this method in a subclass is
        used to change the policies enforced by a restricted environment.

        Similar to the r_unload() method, but has access to restricted
        versions of the standard I/O streams sys.stdin, sys.stderr, and
        sys.stdout.

        """
500
        return self.s_apply(self.r_unload, args)
Tim Peters's avatar
Tim Peters committed
501

502
    # Restricted open(...)
Tim Peters's avatar
Tim Peters committed
503

504
    def r_open(self, file, mode='r', buf=-1):
505 506 507 508 509 510 511 512 513 514 515 516
        """Method called when open() is called in the restricted environment.

        The arguments are identical to those of the open() function, and a
        file object (or a class instance compatible with file objects)
        should be returned.  RExec's default behaviour is allow opening
        any file for reading, but forbidding any attempt to write a file.

        This method is implicitly called by code executing in the
        restricted environment.  Overriding this method in a subclass is
        used to change the policies enforced by a restricted environment.

        """
517 518
        if mode not in ('r', 'rb'):
            raise IOError, "can't open files for writing in restricted mode"
519
        return open(file, mode, buf)
520

521 522 523 524 525 526 527
    # Restricted version of sys.exc_info()

    def r_exc_info(self):
        ty, va, tr = sys.exc_info()
        tr = None
        return ty, va, tr

Guido van Rossum's avatar
Guido van Rossum committed
528 529

def test():
530
    import getopt, traceback
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    opts, args = getopt.getopt(sys.argv[1:], 'vt:')
    verbose = 0
    trusted = []
    for o, a in opts:
        if o == '-v':
            verbose = verbose+1
        if o == '-t':
            trusted.append(a)
    r = RExec(verbose=verbose)
    if trusted:
        r.ok_builtin_modules = r.ok_builtin_modules + tuple(trusted)
    if args:
        r.modules['sys'].argv = args
        r.modules['sys'].path.insert(0, os.path.dirname(args[0]))
    else:
        r.modules['sys'].path.insert(0, "")
    fp = sys.stdin
    if args and args[0] != '-':
549
        try:
550 551 552 553 554 555
            fp = open(args[0])
        except IOError, msg:
            print "%s: can't open file %s" % (sys.argv[0], `args[0]`)
            return 1
    if fp.isatty():
        print "*** RESTRICTED *** Python", sys.version
556 557 558
        print 'Type "help", "copyright", "credits" or "license" ' \
              'for more information.'

559
        while 1:
560
            try:
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
                try:
                    s = raw_input('>>> ')
                except EOFError:
                    print
                    break
                if s and s[0] != '#':
                    s = s + '\n'
                    c = compile(s, '<stdin>', 'single')
                    r.s_exec(c)
            except SystemExit, n:
                return n
            except:
                traceback.print_exc()
    else:
        text = fp.read()
        fp.close()
        c = compile(text, fp.name, 'exec')
        try:
            r.s_exec(c)
580
        except SystemExit, n:
581
            return n
582 583
        except:
            traceback.print_exc()
584
            return 1
585

Guido van Rossum's avatar
Guido van Rossum committed
586 587

if __name__ == '__main__':
588
    sys.exit(test())