FAQ - Help and questions

Do build scripts and procedural macros run code on my machine when I build?

#security#cargo#supply-chain#build-scriptspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Yes. A build.rs file in your project or any dependency is compiled and run during cargo build with your user's permissions, and procedural macros run inside the compiler. Reading a dependency's build script and macros is part of reviewing it, and untrusted code should be built in a sandbox.

Checked with rustc 1.93.0, cargo 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Downloading and compiling a crate is not passive. Two mechanisms run arbitrary code during the build:

  • A build script (build.rs) is a normal Rust program that Cargo builds and runs before compiling the crate. It exists for good reasons (compiling C code, generating bindings), but it can read files, use the network and start programs, with the same permissions as the user who typed cargo build.
  • A procedural macro is a crate that the compiler loads and runs while it expands your code.

The demonstration is harmless: a project whose build.rs only prints messages through Cargo's cargo::warning channel.

build.rsrust
fn main() {
    // A build script is an ordinary program. It runs on your machine, with your permissions, during `cargo build`.
    let name = std::env::var("CARGO_PKG_NAME").unwrap();
    println!("cargo::warning=build.rs is running for package {name}");
    println!("cargo::warning=it can read your environment: PATH is set = {}", std::env::var_os("PATH").is_some());
}

Running cargo build on it:

text
   Compiling bs v0.1.0 (C:\...\bs)
warning: [email protected]: build.rs is running for package bs
warning: [email protected]: it can read your environment: PATH is set = true
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s

The messages appear while the project is built, before any program of yours is run. A malicious build script could do far more than print.

What to do about it#

  1. Treat every new dependency that has a build.rs or is a proc-macro crate as code you are running; review it as described in the thread on reviewing a new dependency.
  2. Do not build untrusted projects on a machine that holds secrets. Use a container, a virtual machine or a throw-away account, with no access to your SSH keys or tokens.
  3. Keep CI runners free of long-lived credentials, and give builds no network when the dependencies are vendored or cached (cargo vendor, --offline and --locked).
  4. Pin versions with Cargo.lock so a new release of a dependency does not run new code without review.
  5. Be aware that the same applies to an IDE: opening a project can run cargo check, which runs build scripts and macros. Open unknown projects in a safe environment first.

Common mistakes#

  • Believing that cargo check is "just checking": it still runs build scripts and procedural macros.
  • Reviewing the library code but not build.rs.
  • Giving the build environment production credentials "for convenience".

Can I list the crates in my tree that have a build script?#

Cargo has no single option for it. In the JSON from cargo metadata, a package with a build script has a target whose kind is custom-build (checked on the test project above). You can also look for build.rs in the sources that cargo vendor copies into one folder.

Does vendoring remove the risk?#

It removes surprise updates and the network at build time. The vendored code still runs when you build it, so it still needs review.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet