Proc macro sandboxing
Proc macro sandboxing
we’re working on a third party solution for this. Should have some updates that sandbox cargo builds shortly.
github.com/phylum-dev/birdcage
It’s a cross-platform sandbox that works on Linux via Landlock and macOS via Seatbelt. We’ve rolled this into our CLI (github.com/phylum-dev/cli) so you can do thinks like:
phylumFor example for npm, which currently uses the sandbox:
phylum npm installWe’re adding this to cargo to similarly sandbox crate installations. Would love feedback and thoughts on our sandbox!
A Rust procedural macro (proc macro) is a metaprogramming feature in Rust that allows you to define custom syntax extensions and code transformations. They operate on the abstract syntax tree (AST) of Rust code during compilation and can generate or modify code based on annotations or custom syntax.
Sandboxing a Rust proc macro refers to restricting the capabilities of the macro to improve security and prevent potentially harmful code execution. There are several reasons why someone might want to sandbox a proc macro:
Security: Untrusted code can be executed during the macro expansion process. To prevent malicious code execution or code that could access sensitive information, sandboxing techniques are employed.
Preventing unintended side effects: Some proc macros might inadvertently introduce side effects like file I/O or network requests. Sandboxing can limit these actions to ensure the macro only performs intended transformations.
Resource control: To manage system resources, a sandboxed proc macro can be configured to run within resource limits, preventing excessive memory or CPU usage.
Isolation: Sandboxing helps keep the macro’s execution isolated from the rest of the compilation process, reducing the risk of interfering with other parts of the code.
Sandboxing a Rust proc macro typically involves using crates like sandbox or cap-std to restrict the macro’s capabilities and limit its access to the system. This ensures that the macro operates within a controlled environment, enhancing the overall safety of code compilation and execution.
I didn’t get it either.
Seems to me if your code will be this unpredictable, you should only run it on an air gapped machine
Did I say that? It’s obvious that it’s a fairly nuanced as topics go, and GPT is not great at nuance. It doesn’t seem like it’s totally wrong though.
Anyhow I don’t rust, so it’s kinda irrelevant, just an interesting topic.
It’s just compile time code execution.
The difference between those macros („procedural macros“) and regular macros is that while regular macros are pretty much only templated code that is unfolded, proc macros contain code that is run at compile time, so they are more powerful but also more dangerous from a security perspective as you would expect just compiling a program to be safe.
Also: is copy pasting ChatGPT answers a thing now even when you, as you said, don’t even know what it means??
As long as it’s annotated as such I don’t mind, even if it’s wrong. And if it’s wrong you’re more likely to get people to actually respond via a “I’m but actually” type response
I personally don’t think they do, but an argument can certainly be made. Rust proc macros can run arbitrary code at compile time. Build scripts can also do this.
This means, adding a dependency in Cargo.toml is often enough for that dependency to run arbitrary code (as rust-analyzer will likely immediately compile it).
In practice, I don’t think this is much worse than a dependency being able to run arbitrary code at runtime, but some people clearly do.
I don’t know if it is a huge issue but it is definitely a nice to have. There are a few examples I can think of:
I’m sure there are more. For me personally it isn’t a huge priority or concern but I would definitely appreciate it. If people are surprised that building a project can compromise their machine than they will likely build things assuming that it won’t. Sure, in an ideal world everyone would do their research but in general the safer things are the better.
Analyzing without running might lead to bad situations, in which code behaves differently on runtime vs what the compiler / rust-analyzer might expect.
Imagine a malicious dependency. You add the thing with cargo, and the rust analyzer picks it up. The malicious code was carefully crafted to stay undetected, especially in static code analysis. The rust analyzer would think that the code does different things than it actually will. Could potentially lead to problematic behavior, idk.
Not sure how realistic that scenario is, or how exploitable.