Skip to content Skip to footer
0 items - $0.00 0

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

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

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

15 Comments

  • Post Author
    djha-skin
    Posted April 6, 2025 at 2:28 pm

    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.

  • Post Author
    null_investor
    Posted April 6, 2025 at 2:38 pm

    Forking and this package can be useful if you know that the unsafe code is really unsafe and have no hope of making it better.

    But I wouldn't use this often. I'd be willing to bet that you'd lose all performance benefits of using Rust versus something like Python or Ruby that uses forking extensively for parallelism.

  • Post Author
    dijit
    Posted April 6, 2025 at 2:41 pm

    this seems like a good place to ask, I don’t write very much unsafe Rust code… but when I do, it’s because I’m calling the Win32 API.

    Tools like valgrind do not work on windows, and I am nowhere near smart enough to know the entire layout of memory that should exist.

    When using Windows and calling system system functions, there’s a lot of casting involved; to convert wide characters and DWORDS to rust primitives for example. And given that I don’t have a good debugging situation, I’m terrified that I’m corrupting or leaking memory.

    does anyone know any good tools that work on windows to help me out here?

  • Post Author
    teknopaul
    Posted April 6, 2025 at 2:42 pm

    Hammer, nut.

    Clever trick tho if you are in a bind.

  • Post Author
    pjmlp
    Posted April 6, 2025 at 2:49 pm

    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.

  • Post Author
    woodruffw
    Posted April 6, 2025 at 3:12 pm

    I don't think this meets the definition of "safe" in "safe" Rust: "safe" doesn't just mean "won't crash due to spatial memory errors," it means that the code is in fact spatially and temporally memory safe.

    In other words: this won't detect memory unsafety that doesn't result in an abnormal exit or other detectable fault. If I'm writing an exploit, my entire goal is to perform memory corruption without causing a fault; that's why Rust's safety property is much stronger than crash-freeness.

  • Post Author
    m00dy
    Posted April 6, 2025 at 3:16 pm

    >>We call this trick the "fork and free" pattern. It's pretty nifty.

    It should be called "fork and see" pattern instead :D

  • Post Author
    slashdev
    Posted April 6, 2025 at 3:35 pm

    I'd love to know what horrible library / code the author was using where sandboxing it like this seemed like the best alternative.

  • Post Author
    destroycom
    Posted April 6, 2025 at 3:43 pm

    This isn't mentioned anywhere on the page, but fork is generally not a great API for these kinds of things. In a multi-threaded application, any code between the fork and exec syscalls should be async-signal-safe. Since the memory is replicated in full at the time of the call, the current state of mutexes is also replicated and if some thread was holding them at the time, there is a risk of a deadlock. A simple print! or anything that allocates memory can lead to a freeze. There's also an issue of user-space buffers, again printing something may write to a user-space buffer that, if not flushed, will be lost after the callback completes.

  • Post Author
    corank
    Posted April 6, 2025 at 4:11 pm

    > It forces functions to be memory pure (pure with respect to memory), even if they aren't.

    What if the unsafe code is not supposed to be pure but mutates some memory? For example, does this allow implementing a doubly-linked list?

  • Post Author
    wavemode
    Posted April 6, 2025 at 4:22 pm

    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.

  • Post Author
    jesprenj
    Posted April 6, 2025 at 4:58 pm

    Why use a pipe to communicate instead of shared memory?

  • Post Author
    nextaccountic
    Posted April 6, 2025 at 5:17 pm

    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

  • Post Author
    loeg
    Posted April 6, 2025 at 5:43 pm

    As a joke, it's funny. Obviously you would not want to actually deploy this. I feel like most comments are too quick to criticize using this in prod (don't!) and missing the point.

  • Post Author
    Svetlitski
    Posted April 6, 2025 at 8:01 pm

    This is likely to violate async-signal-safety [1] in any non-trivial program, unless used with extreme care. Running code in between a fork() and an exec() is fraught with peril; it's not hard to end up in a situation where you deadlock because you forked a multi-threaded process where one of the existing threads held a lock at the time of forking, among other hazards.

    [1] https://man7.org/linux/man-pages/man7/signal-safety.7.html

Leave a comment

In the Shadows of Innovation”

© 2025 HackTech.info. All Rights Reserved.

Sign Up to Our Newsletter

Be the first to know the latest updates

Whoops, you're not connected to Mailchimp. You need to enter a valid Mailchimp API key.