Live data from Hacker News

Show HN: I built a Rust crate for running unsafe code safely

github.com

31–40 of 72 posts

Re: Show HN: I built a Rust crate for running unsafe code safely

#31

If you can afford to sacrifice that much performance just to run some potentially unsafe code, then you can probably afford to not be writing Rust in the first place and instead use a garbage-collected language.

I think it is basically a garbage collector, just one that operates on a per-function level instead of at the general level of the program

Re: Show HN: I built a Rust crate for running unsafe code safely

#33
post #8

Rather design the application from the start to use multiple processes, OS IPC and actual OS sandboxing APIs. Pseudo sandboxing on the fly is an old idea and with its own issues, as proven by classical UNIX approach to launching daemons.

What are the sandboxing APIs you’d recommend on Linux, Mac, & Windows? I haven’t been able to find any comprehensive references online.

My starting point would be Chromium's documentation, as - presumably - chrome is one of the most widely used and battle tested, user-facing, third party sandboxes running on end user machines.

Windows: https://chromium.googlesource.com/chromium/src/+/main/docs/d...

Linux: https://chromium.googlesource.com/chromium/src/+/main/sandbo...

OS X: https://chromium.googlesource.com/chromium/src/+/main/sandbo...

With the caveat that I wouldn't necessairly assume this is the cutting edge at this point, and there might be other resources to invest in for server-side sandboxing involving containers or hypervisors, and that I've only actually engaged with the Windows APIs based on that reading.

I wrote `firehazard` ( https://docs.rs/firehazard/ , https://github.com/MaulingMonkey/firehazard/tree/master/exam... ) to experiment with wrapping the Windows APIs, document edge cases, etc. - although if the long list of warnings in the readme doesn't scare you away, it'll hopefully at least confirm I hesitate to recommend my own code ;)

Re: Show HN: I built a Rust crate for running unsafe code safely

#34

Earlier quoted context omitted.

macOS provides native sandboxing; you can use capabilities at the app level[1] or the sandbox-exec CLI to wrap an existing tool. For Windows, you probably want WSB[2] or AppContainer isolation[3]. For Linux, the low-level primitives for sandboxing are seccomp and namespaces. You can use tools like Firejail and bubblewrap to wrap individual tool invocations, similar to sandbox-exec on macOS. [1]: https://developer.app…

Linux also has Landlock now. macOS sandboxing is notoriously under-documented, has sharp edges, and is nowhere near as expressive as Linux sandboxing.

To save a search; https://docs.kernel.org/userspace-api/landlock.html

Re: Show HN: I built a Rust crate for running unsafe code safely

#35

Earlier quoted context omitted.

macOS provides native sandboxing; you can use capabilities at the app level[1] or the sandbox-exec CLI to wrap an existing tool. For Windows, you probably want WSB[2] or AppContainer isolation[3]. For Linux, the low-level primitives for sandboxing are seccomp and namespaces. You can use tools like Firejail and bubblewrap to wrap individual tool invocations, similar to sandbox-exec on macOS. [1]: https://developer.app…

Linux also has Landlock now. macOS sandboxing is notoriously under-documented, has sharp edges, and is nowhere near as expressive as Linux sandboxing.

Thanks! Landlock is the one I couldn't remember.

Agreed about macOS's sandboxing being under-documented.

Re: Show HN: I built a Rust crate for running unsafe code safely

#36

Earlier quoted context omitted.

Well, you can close all file descriptors (except the pipe used for sending the return value back to the parent), re-mmap all files with MAP_PRIVATE, and then use SECCOMP_SET_MODE_STRICT to isolate the child process. But at that point, what are you even doing? Probably nothing useful. If there were a Quick Fix for safety, we'd probably have discovered it by now.

> use SECCOMP_SET_MODE_STRICT to isolate the child process. But at that > point, what are you even doing? Probably nothing useful. The classic example of a fully-seccomp'd subprocess is decoding / decompression. If you want to execute ffmpeg on untrusted user input then seccomp is a sandbox that allows full-power SIMD, and the code has no reason to perform syscalls other than read/write to its input/output stream. On…

The classic example of a fully-seccomp'd subprocess is decoding / decompression.

Yes. I've run JPEG 2000 decoders in a subprocess for that reason.

Re: Show HN: I built a Rust crate for running unsafe code safely

#37
There is a way to sandbox native code without forking to a new process, and it looks like this

https://hacks.mozilla.org/2020/02/securing-firefox-with-weba...

Firefox employs processes for sandboxing but for small components they are not worth the overhead. For those they employed this curious idea: first compile the potentially unsafe code to wasm (any other VM would work), then compile the wasm code to C (using the wasm2c tool). Then use this new C source normally in your program.

All UB in the original code becomes logical bugs in the wasm, that can output incorrect values but not corrupt memory or do things that UB can do. Firefox does this to encapsulate C code, but it can be done with Rust too

Re: Show HN: I built a Rust crate for running unsafe code safely

#38

Earlier quoted context omitted.

Well, you can close all file descriptors (except the pipe used for sending the return value back to the parent), re-mmap all files with MAP_PRIVATE, and then use SECCOMP_SET_MODE_STRICT to isolate the child process. But at that point, what are you even doing? Probably nothing useful. If there were a Quick Fix for safety, we'd probably have discovered it by now.

> use SECCOMP_SET_MODE_STRICT to isolate the child process. But at that > point, what are you even doing? Probably nothing useful. The classic example of a fully-seccomp'd subprocess is decoding / decompression. If you want to execute ffmpeg on untrusted user input then seccomp is a sandbox that allows full-power SIMD, and the code has no reason to perform syscalls other than read/write to its input/output stream. On…

Well, it seems that lately this kind of task wants to write/mmap to a GPU, and poke at font files and interpret them.

Re: Show HN: I built a Rust crate for running unsafe code safely

#39
post #9

This is cool from a theoretical perspective, but `fork()` can be prohibitively expensive, at least on the hot path. This is a cool tool that should be used with care.

Which pretty much makes this whole thing pointless since a lot of unsafe code exists purely for performance reasons.

That's what I was thinking; isn't 1 millisecond kind of an eternity in terms of computer performance?

I'm sure there's still value in this project, but I'm not sure I'm versed enough in Rust to know what that is.

Post reply on HN