| 1 |
"""Generation of sandboxing stand-alone executable from RPython code. |
| 2 |
In place of real calls to any external function, this code builds |
| 3 |
trampolines that marshal their input arguments, dump them to STDOUT, |
| 4 |
and wait for an answer on STDIN. Enable with 'translate.py --sandbox'. |
| 5 |
""" |
| 6 |
import py |
| 7 |
|
| 8 |
from rpython.rlib import rmarshal, types |
| 9 |
from rpython.rlib.signature import signature |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from rpython.rlib import rposix |
| 17 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 18 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 19 |
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator |
| 20 |
from rpython.tool.ansi_print import AnsiLogger |
| 21 |
|
| 22 |
log = AnsiLogger("sandbox") |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
ll_read_not_sandboxed = rposix.external('read', |
| 28 |
[rffi.INT, rffi.CCHARP, rffi.SIZE_T], |
| 29 |
rffi.SIZE_T, |
| 30 |
sandboxsafe=True, |
| 31 |
_nowrapper=True) |
| 32 |
|
| 33 |
ll_write_not_sandboxed = rposix.external('write', |
| 34 |
[rffi.INT, rffi.CCHARP, rffi.SIZE_T], |
| 35 |
rffi.SIZE_T, |
| 36 |
sandboxsafe=True, |
| 37 |
_nowrapper=True) |
| 38 |
|
| 39 |
|
| 40 |
@signature(types.int(), types.ptr(rffi.CCHARP.TO), types.int(), |
| 41 |
returns=types.none()) |
| 42 |
def writeall_not_sandboxed(fd, buf, length): |
| 43 |
fd = rffi.cast(rffi.INT, fd) |
| 44 |
while length > 0: |
| 45 |
size = rffi.cast(rffi.SIZE_T, length) |
| 46 |
count = rffi.cast(lltype.Signed, ll_write_not_sandboxed(fd, buf, size)) |
| 47 |
if count <= 0: |
| 48 |
raise IOError |
| 49 |
length -= count |
| 50 |
buf = lltype.direct_ptradd(lltype.direct_arrayitems(buf), count) |
| 51 |
buf = rffi.cast(rffi.CCHARP, buf) |
| 52 |
|
| 53 |
|
| 54 |
class FdLoader(rmarshal.Loader): |
| 55 |
def __init__(self, fd): |
| 56 |
rmarshal.Loader.__init__(self, "") |
| 57 |
self.fd = fd |
| 58 |
self.buflen = 4096 |
| 59 |
|
| 60 |
def need_more_data(self): |
| 61 |
buflen = self.buflen |
| 62 |
with lltype.scoped_alloc(rffi.CCHARP.TO, buflen) as buf: |
| 63 |
buflen = rffi.cast(rffi.SIZE_T, buflen) |
| 64 |
fd = rffi.cast(rffi.INT, self.fd) |
| 65 |
count = ll_read_not_sandboxed(fd, buf, buflen) |
| 66 |
count = rffi.cast(lltype.Signed, count) |
| 67 |
if count <= 0: |
| 68 |
raise IOError |
| 69 |
self.buf += ''.join([buf[i] for i in range(count)]) |
| 70 |
self.buflen *= 2 |
| 71 |
|
| 72 |
def sandboxed_io(buf): |
| 73 |
STDIN = 0 |
| 74 |
STDOUT = 1 |
| 75 |
|
| 76 |
with lltype.scoped_alloc(rffi.CCHARP.TO, len(buf)) as p: |
| 77 |
for i in range(len(buf)): |
| 78 |
p[i] = buf[i] |
| 79 |
writeall_not_sandboxed(STDOUT, p, len(buf)) |
| 80 |
|
| 81 |
loader = FdLoader(STDIN) |
| 82 |
|
| 83 |
error = load_int(loader) |
| 84 |
if error != 0: |
| 85 |
reraise_error(error, loader) |
| 86 |
else: |
| 87 |
|
| 88 |
return loader |
| 89 |
|
| 90 |
def reraise_error(error, loader): |
| 91 |
if error == 1: |
| 92 |
raise OSError(load_int(loader), "external error") |
| 93 |
elif error == 2: |
| 94 |
raise IOError |
| 95 |
elif error == 3: |
| 96 |
raise OverflowError |
| 97 |
elif error == 4: |
| 98 |
raise ValueError |
| 99 |
elif error == 5: |
| 100 |
raise ZeroDivisionError |
| 101 |
elif error == 6: |
| 102 |
raise MemoryError |
| 103 |
elif error == 7: |
| 104 |
raise KeyError |
| 105 |
elif error == 8: |
| 106 |
raise IndexError |
| 107 |
else: |
| 108 |
raise RuntimeError |
| 109 |
|
| 110 |
|
| 111 |
@signature(types.str(), returns=types.impossible()) |
| 112 |
def not_implemented_stub(msg): |
| 113 |
STDERR = 2 |
| 114 |
with rffi.scoped_str2charp(msg + '\n') as buf: |
| 115 |
writeall_not_sandboxed(STDERR, buf, len(msg) + 1) |
| 116 |
raise RuntimeError(msg) |
| 117 |
|
| 118 |
def make_stub(fnname, msg): |
| 119 |
"""Build always-raising stub function to replace unsupported external.""" |
| 120 |
log.WARNING(msg) |
| 121 |
|
| 122 |
def execute(*args): |
| 123 |
not_implemented_stub(msg) |
| 124 |
execute.__name__ = 'sandboxed_%s' % (fnname,) |
| 125 |
return execute |
| 126 |
|
| 127 |
def sig_ll(fnobj): |
| 128 |
FUNCTYPE = lltype.typeOf(fnobj) |
| 129 |
args_s = [lltype_to_annotation(ARG) for ARG in FUNCTYPE.ARGS] |
| 130 |
s_result = lltype_to_annotation(FUNCTYPE.RESULT) |
| 131 |
return args_s, s_result |
| 132 |
|
| 133 |
dump_string = rmarshal.get_marshaller(str) |
| 134 |
load_int = rmarshal.get_loader(int) |
| 135 |
|
| 136 |
def get_sandbox_stub(fnobj, rtyper): |
| 137 |
fnname = fnobj._name |
| 138 |
args_s, s_result = sig_ll(fnobj) |
| 139 |
msg = "Not implemented: sandboxing for external function '%s'" % (fnname,) |
| 140 |
execute = make_stub(fnname, msg) |
| 141 |
return _annotate(rtyper, execute, args_s, s_result) |
| 142 |
|
| 143 |
def make_sandbox_trampoline(fnname, args_s, s_result): |
| 144 |
"""Create a trampoline function with the specified signature. |
| 145 |
|
| 146 |
The trampoline is meant to be used in place of real calls to the external |
| 147 |
function named 'fnname'. It marshals its input arguments, dumps them to |
| 148 |
STDOUT, and waits for an answer on STDIN. |
| 149 |
""" |
| 150 |
try: |
| 151 |
dump_arguments = rmarshal.get_marshaller(tuple(args_s)) |
| 152 |
load_result = rmarshal.get_loader(s_result) |
| 153 |
except (rmarshal.CannotMarshal, rmarshal.CannotUnmarshall) as e: |
| 154 |
msg = "Cannot sandbox function '%s': %s" % (fnname, e) |
| 155 |
execute = make_stub(fnname, msg) |
| 156 |
else: |
| 157 |
def execute(*args): |
| 158 |
|
| 159 |
buf = [] |
| 160 |
dump_string(buf, fnname) |
| 161 |
dump_arguments(buf, args) |
| 162 |
|
| 163 |
loader = sandboxed_io(buf) |
| 164 |
|
| 165 |
result = load_result(loader) |
| 166 |
loader.check_finished() |
| 167 |
return result |
| 168 |
execute.__name__ = 'sandboxed_%s' % (fnname,) |
| 169 |
return execute |
| 170 |
|
| 171 |
|
| 172 |
def _annotate(rtyper, f, args_s, s_result): |
| 173 |
ann = MixLevelHelperAnnotator(rtyper) |
| 174 |
graph = ann.getgraph(f, args_s, s_result) |
| 175 |
ann.finish() |
| 176 |
return graph |