| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import errno |
| 8 | |
| 9 | # Disabling potentially dangerous functions |
| 10 | import os as _os |
| 11 | from functools import partial |
| 12 | |
| 13 | os_funcs_to_disable = [ |
| 14 | "kill", |
| 15 | "system", |
| 16 | "putenv", |
| 17 | "remove", |
| 18 | "removedirs", |
| 19 | "rmdir", |
| 20 | "fchdir", |
| 21 | "setuid", |
| 22 | "fork", |
| 23 | "forkpty", |
| 24 | "killpg", |
| 25 | "rename", |
| 26 | "renames", |
| 27 | "truncate", |
| 28 | "replace", |
| 29 | # "unlink", # Commenting as this was blocking matpltlib from rendering plots correctly |
| 30 | "fchmod", |
| 31 | "fchown", |
| 32 | "chmod", |
| 33 | "chown", |
| 34 | "chroot", |
| 35 | "fchdir", |
| 36 | "lchflags", |
| 37 | "lchmod", |
| 38 | "lchown", |
| 39 | "chdir", |
| 40 | ] |
| 41 | |
| 42 | |
| 43 | def call_not_allowed(*args, **kwargs): |
| 44 | raise OSError(errno.EPERM, "Call are not permitted in this environment") |
| 45 | |
| 46 | |
| 47 | for func_name in os_funcs_to_disable: |
| 48 | if hasattr(_os, func_name): |
| 49 | setattr(_os, func_name, partial(call_not_allowed, _func_name=f"os.{func_name}")) |
| 50 | |
| 51 | import shutil as _shutil |
| 52 | |
| 53 | for func_name in ["rmtree", "move", "chown"]: |
| 54 | if hasattr(_shutil, func_name): |
| 55 | setattr( |
| 56 | _shutil, |
| 57 | func_name, |
| 58 | partial(call_not_allowed, _func_name=f"shutil.{func_name}"), |
| 59 | ) |
| 60 | |
| 61 | import subprocess as _subprocess |
| 62 | |
| 63 | |
| 64 | def popen_not_allowed(*args, **kwargs): |
| 65 | raise _subprocess.CalledProcessError( |
| 66 | -1, |
| 67 | args[0] if args else "unknown", |
| 68 | stderr="subprocess.Popen is not allowed in this environment", |
| 69 | ) |
| 70 | |
| 71 | |
| 72 | _subprocess.Popen = popen_not_allowed |
| 73 | |
| 74 | |
| 75 | import atexit as _atexit |
| 76 | import builtins as _builtins |
| 77 | import io as _io |
| 78 | import json as _json |
| 79 | import sys as _sys |
| 80 | |
| 81 | # NB! The following "unused" imports crucial, make sure not not to remove |
| 82 | # them with linters - they're used in code_execution.py |
| 83 | from contextlib import ( # noqa |
| 84 | contextmanager as _contextmanager, |
| 85 | redirect_stderr as _redirect_stderr, |
| 86 | redirect_stdout as _redirect_stdout, |
| 87 | ) |
| 88 | from multiprocessing.connection import Connection as _Connection |
| 89 | |
| 90 | # Mangle imports to avoid polluting model execution namespace. |
| 91 | |
| 92 | _IO_SINK = _io.StringIO() |
| 93 | _NETWORK_TIMEOUT = 5 |
| 94 | _NETWORK_CONNECTIONS = None |
| 95 | |
| 96 | |
| 97 | def _open_connections(): |
| 98 | global _NETWORK_CONNECTIONS |
| 99 | if _NETWORK_CONNECTIONS is not None: |
| 100 | # Ensure connections only opened once. |
| 101 | return _NETWORK_CONNECTIONS |
| 102 | req_w_fd, resp_r_fd = _sys.argv[1], _sys.argv[2] |
| 103 | req_con = _Connection(int(req_w_fd), readable=False) |
| 104 | resp_con = _Connection(int(resp_r_fd), writable=False) |
| 105 | _NETWORK_CONNECTIONS = (req_con, resp_con) |
| 106 | return _NETWORK_CONNECTIONS |
| 107 | |
| 108 | |
| 109 | _builtins._open_connections = _open_connections |
| 110 | |
| 111 | |
| 112 | @_atexit.register |
| 113 | def _close_connections(): |
| 114 | global _NETWORK_CONNECTIONS |
| 115 | if _NETWORK_CONNECTIONS is None: |
| 116 | return |
| 117 | for con in _NETWORK_CONNECTIONS: |
| 118 | con.close() |
| 119 | del _NETWORK_CONNECTIONS |
| 120 | |
| 121 | |
| 122 | def _network_call(request): |
| 123 | # NOTE: We communicate with the parent process in json, encoded |
| 124 | # in raw bytes. We do this because native send/recv methods use |
| 125 | # pickle which involves execution of arbitrary code. |
| 126 | _open_connections() |
| 127 | req_con, resp_con = _NETWORK_CONNECTIONS |
| 128 | |
| 129 | req_con.send_bytes(_json.dumps(request).encode("utf-8")) |
| 130 | if resp_con.poll(timeout=_NETWORK_TIMEOUT) is None: |
| 131 | raise Exception(f"Network request timed out: {_json.dumps(request)}") |
| 132 | else: |
| 133 | return _json.loads(resp_con.recv_bytes().decode("utf-8")) |