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
25
import imp
26

27
__all__ = ["RExec"]
28

29 30
class FileBase:

31
    ok_file_methods = ('fileno', 'flush', 'isatty', 'read', 'readline',
Gustavo Niemeyer's avatar
Gustavo Niemeyer committed
32 33
            'readlines', 'seek', 'tell', 'write', 'writelines', 'xreadlines',
            '__iter__')
34 35 36 37


class FileWrapper(FileBase):

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

40 41 42 43 44 45 46
    def __init__(self, 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 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

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
                          'cmath', 'errno', 'imageop',
                          'marshal', 'math', 'md5', 'operator',
139
                          'parser', 'regex', 'select',
Gustavo Niemeyer's avatar
Gustavo Niemeyer committed
140
                          'sha', '_sre', 'strop', 'struct', 'time',
141
                          '_weakref')
142 143

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

147 148 149
    ok_sys_names = ('byteorder', 'copyright', 'exit', 'getdefaultencoding',
                    'getrefcount', 'hexversion', 'maxint', 'maxunicode',
                    'platform', 'ps1', 'ps2', 'version', 'version_info')
150

151
    nok_builtin_names = ('open', 'file', 'reload', '__import__')
152

153 154
    ok_file_types = (imp.C_EXTENSION, imp.PY_SOURCE)

155
    def __init__(self, hooks = None, verbose = 0):
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 181 182
        """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.

        """
183 184 185

        raise RuntimeError, "This code is not secure in Python 2.2 and 2.3"

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        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)
205

206
    def set_trusted_path(self):
207 208 209
        # 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)
210 211

    def load_dynamic(self, name, filename, file):
212 213
        if name not in self.ok_dynamic_modules:
            raise ImportError, "untrusted dynamic module: %s" % name
214
        if name in sys.modules:
215 216 217 218 219
            src = sys.modules[name]
        else:
            src = imp.load_dynamic(name, filename, file)
        dst = self.copy_except(src, [])
        return dst
220

221
    def make_initial_modules(self):
222 223
        self.make_main()
        self.make_osname()
224 225 226

    # Helpers for RHooks

227 228 229 230 231
    def get_suffixes(self):
        return [item   # (suff, mode, type)
                for item in imp.get_suffixes()
                if item[2] in self.ok_file_types]

232
    def is_builtin(self, mname):
233
        return mname in self.ok_builtin_modules
234 235 236 237

    # The make_* methods create specific built-in modules

    def make_builtin(self):
238 239 240
        m = self.copy_except(__builtin__, self.nok_builtin_names)
        m.__import__ = self.r_import
        m.reload = self.r_reload
241
        m.open = m.file = self.r_open
242 243

    def make_main(self):
244
        m = self.add_module('__main__')
245 246

    def make_osname(self):
247 248 249 250 251 252
        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
253 254

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

    # The copy_* methods copy existing modules with some changes

    def copy_except(self, src, exceptions):
268 269 270 271 272 273 274 275 276
        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
277 278

    def copy_only(self, src, names):
279 280 281 282 283 284 285 286
        dst = self.copy_none(src)
        for name in names:
            try:
                value = getattr(src, name)
            except AttributeError:
                continue
            setattr(dst, name, value)
        return dst
287 288

    def copy_none(self, src):
289 290 291
        m = self.add_module(src.__name__)
        m.__doc__ = src.__doc__
        return m
292 293 294 295

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

    def add_module(self, mname):
296 297 298
        m = self.modules.get(mname)
        if m is None:
            self.modules[mname] = m = self.hooks.new_module(mname)
299 300
        m.__builtins__ = self.modules['__builtin__']
        return m
Guido van Rossum's avatar
Guido van Rossum committed
301

302 303 304
    # The r* methods are public interfaces

    def r_exec(self, code):
305 306 307 308 309 310 311
        """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.

        """
312 313
        m = self.add_module('__main__')
        exec code in m.__dict__
314 315

    def r_eval(self, code):
316 317 318 319 320 321 322 323
        """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.

        """
324 325
        m = self.add_module('__main__')
        return eval(code, m.__dict__)
326 327

    def r_execfile(self, file):
328 329 330 331
        """Execute the Python code in the file in the restricted
        environment's __main__ module.

        """
332
        m = self.add_module('__main__')
333
        execfile(file, m.__dict__)
334 335

    def r_import(self, mname, globals={}, locals={}, fromlist=[]):
336 337 338 339 340 341 342 343
        """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.

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

346
    def r_reload(self, m):
347 348 349 350 351 352 353
        """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.

        """
354
        return self.importer.reload(m)
355 356

    def r_unload(self, m):
357 358 359 360 361 362 363 364 365
        """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.

        """
366
        return self.importer.unload(m)
Tim Peters's avatar
Tim Peters committed
367

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

370 371
    def make_delegate_files(self):
        s = self.modules['sys']
372 373 374
        self.delegate_stdin = FileDelegate(s, 'stdin')
        self.delegate_stdout = FileDelegate(s, 'stdout')
        self.delegate_stderr = FileDelegate(s, 'stderr')
375 376 377 378
        self.restricted_stdin = FileWrapper(sys.stdin)
        self.restricted_stdout = FileWrapper(sys.stdout)
        self.restricted_stderr = FileWrapper(sys.stderr)

379
    def set_files(self):
380 381 382 383
        if not hasattr(self, 'save_stdin'):
            self.save_files()
        if not hasattr(self, 'delegate_stdin'):
            self.make_delegate_files()
384
        s = self.modules['sys']
385 386 387 388 389 390
        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
391 392

    def reset_files(self):
393
        self.restore_files()
394
        s = self.modules['sys']
395 396 397
        self.restricted_stdin = s.stdin
        self.restricted_stdout = s.stdout
        self.restricted_stderr = s.stderr
Tim Peters's avatar
Tim Peters committed
398

399 400 401 402 403 404

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

405
    def restore_files(self):
406 407 408
        sys.stdin = self.save_stdin
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
Tim Peters's avatar
Tim Peters committed
409

410
    def s_apply(self, func, args=(), kw={}):
411 412 413
        self.save_files()
        try:
            self.set_files()
414
            r = func(*args, **kw)
415
        finally:
416
            self.restore_files()
417
        return r
Tim Peters's avatar
Tim Peters committed
418

419
    def s_exec(self, *args):
420 421 422 423 424 425 426 427 428 429 430
        """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.

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

433
    def s_eval(self, *args):
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.
Tim Peters's avatar
Tim Peters committed
444

445
        """
446
        return self.s_apply(self.r_eval, args)
447

448
    def s_execfile(self, *args):
449 450 451 452 453 454 455 456
        """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.

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

459
    def s_import(self, *args):
460 461 462 463 464 465 466 467 468 469 470 471
        """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.

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

474
    def s_reload(self, *args):
475 476 477 478 479 480 481 482 483 484 485
        """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.

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

488
    def s_unload(self, *args):
489 490 491 492 493 494 495 496 497 498 499 500 501
        """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.

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

504
    # Restricted open(...)
Tim Peters's avatar
Tim Peters committed
505

506
    def r_open(self, file, mode='r', buf=-1):
507 508 509 510 511 512 513 514 515 516 517 518
        """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.

        """
Gustavo Niemeyer's avatar
Gustavo Niemeyer committed
519
        mode = str(mode)
520 521
        if mode not in ('r', 'rb'):
            raise IOError, "can't open files for writing in restricted mode"
522
        return open(file, mode, buf)
523

524 525 526 527 528 529 530
    # 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
531 532

def test():
533
    import getopt, traceback
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    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] != '-':
552
        try:
553 554
            fp = open(args[0])
        except IOError, msg:
555
            print "%s: can't open file %r" % (sys.argv[0], args[0])
556 557
            return 1
    if fp.isatty():
558 559 560 561
        try:
            import readline
        except ImportError:
            pass
562
        import code
563 564 565 566
        class RestrictedConsole(code.InteractiveConsole):
            def runcode(self, co):
                self.locals['__builtins__'] = r.modules['__builtin__']
                r.s_apply(code.InteractiveConsole.runcode, (self, co))
567
        try:
568
            RestrictedConsole(r.modules['__main__'].__dict__).interact()
569 570
        except SystemExit, n:
            return n
571 572 573 574 575 576
    else:
        text = fp.read()
        fp.close()
        c = compile(text, fp.name, 'exec')
        try:
            r.s_exec(c)
577
        except SystemExit, n:
578
            return n
579 580
        except:
            traceback.print_exc()
581
            return 1
582

Guido van Rossum's avatar
Guido van Rossum committed
583 584

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