130 points · by coolreader18 · 6y ago · 75 comments
One I ran into in the wild recently is that in older versions of Lua, exceptions in GC finalizers (the `__gc` metamethod) can trigger a segfault. In those same versions of Lua, the bytecode format is notoriously dangerous to load.
I wonder whether this will be a large component of newer scripting language implementations. Do these safety issues warrant use of memory safe languages like Rust, or use of existing sandboxed VM implementations like WebAssembly?
Could you expand on this? Are the dangers such that they could be avoided with a bytecode verifier somewhat like Java's? Things like checking that the stack can never underflow, and that at the merge points of branches the stack always has the same depth.
5.1: https://www.lua.org/wshop11/Cawley.pdf
5.2: https://apocrypha.numin.it/talks/lua_bytecode_exploitation.p...
Lua used to have a built-in bytecode verifier as I understand it, but it never reached the point to where it was enough to safeguard the VM.
Here's a trivial example:
/usr/bin/gawk 'for (i = ) in steve kemp rocks'
I found fuzz-testing like this very very useful when writing my own BASIC interpreter, and playing with scripting languages though. It's almost magical how quickly problems are found!I don't know what the stance of other python runtimes are, but you should probably just use a sandbox at OS level which is likely to be tested far more thoroughly.
[0]: https://devblogs.microsoft.com/oldnewthing/20060508-22/?p=31...
> I now think that putting a sandbox directly in Python cannot be secure. To build a secure sandbox, the whole Python process must be put in an external sandbox.
https://mail.python.org/pipermail/python-dev/2013-November/1...
https://web.archive.org/web/20190113115213/https://blogs.msd...
CPython bytecode will segfault Python if it’s slightly incorrect. And that’s fine, it’s not a security risk and it’s not worth the performance overhead of validating wonky bytecode.
What about Javascript running on V8?
[0] https://github.com/nodejs/node/pulls?utf8=%E2%9C%93&q=is%3Ap...
It's a plausible approach to a fully-safe, near-native-speed plugin architecture.
A program fragment is safe if it does not cause untrapped errors to occur. Languages where all program fragments are safe are called safe languages. Therefore, safe languages rule out the most insidious form of execution errors: the ones that may go unnoticed.
...
It is useful to distinguish between two kinds of execution errors: the ones that cause the computation to stop immediately, and the ones that go unnoticed (for a while) and later cause arbitrary behavior. The former are called trapped errors, whereas the latter are untrapped errors.
Type Systems, Luca Cardelli
https://scholar.google.com/scholar?cluster=90442457768317510...
And practically speaking seg faults are easy to debug and fix.
I see a lot of abuse of the terms "safe" and "safe language" lately.
WebAssembly koolaid is strong on HN, let's wait the first exploits that escapes the runtime to assess the "fully-safe" architecture.
The way they said it, though, makes it sound like WebAssembly is implemented with full process sandboxing or something, which is patently false. It works that way in neither Chrome nor Firefox, and there are no other browsers right now.
import sys, threading
def r():
sys.stdin.buffer.read(1)
t = threading.Thread(target=r, daemon=True)
t.start() import sys, threading, time
t = threading.Thread(target=sys.stdin.read, args=(1,))
t.start()
time.sleep(1)
sys.stdin.close()
Run it then after a few seconds press enter. It doesn't segfault in Python 3, but it still doesn't behave how I'd like, because I would like the close() to unblock the read(), but it doesn't unblock the read(), the read() still hangs until it gets some input.I wouldn't have bothered filing the bug.
for x, x.__new__ in [(__import__('queue').Full, print)]: __import__('glob').iglob(0).throw(x)Someone commented below the gist with this one-liner:
(i for i in []).throw(type('E', (BaseException,), dict(__new__=lambda cls, *args: cls))())
I managed to golf it a bit down to this ;): n="__new__";(i for i in []).throw(type(n,(IOError,),{n:lambda c,*a:c})()) $ python3.7 -VV
Python 3.7.5 (default, Nov 7 2019, 10:50:52)
[GCC 8.3.0]
Some more golfing with yours: (i for i in[]).throw(type('',(IOError,),{'__new__':lambda a,*b:a})) import ctypes
ctypes.cast(1, ctypes.py_object)
Interestingly, this works: import ctypes, gc
x = 22
_id = id(x)
del x
gc.collect()
y = ctypes.cast(_id, ctypes.py_object).value
assert y == 22How about in Rust, then? [0]
Bugs happen in every language. When memory corruption occurs, you can segfault.
[0] https://users.rust-lang.org/t/rust-guarantees-no-segfaults-w...
At a guess, you want something with dependent types?
Like Idris, or Haskell. You already have a Haskell example. This [0] release of Idris fixed a segfault when concatenating strings.
Maybe you meant a language that is proven from the ground up. Like CakeML. You can find a segfault example here [1].
Maybe you meant a language with an algebraic type system like Ada. You can find a segfault example here [2].
Maybe you meant something like Dotty (Research for the next version of Scala). You can find a segfault example here [3].
In short: You'll need to describe what you believe to be a "proper" type system, and name the languages you think fit that description, or no one can have a conversation with you.
[0] https://www.idris-lang.org/idris-1-1-1-released/
[1] https://github.com/CakeML/cakeml/issues/438
[2] https://stackoverflow.com/questions/56227629/segmentation-fa...
Apparently neither Rust, Haskell nor Go fit.