r/rust • miri • Jul 19 '18

Thoughts on Compile-Time Function Evaluation and Type Systems

https://www.ralfj.de/blog/2018/07/19/const.html
73 Upvotes

63 comments sorted by

View all comments

Show parent comments

29

u/matthieum [he/him] Jul 19 '18

TL;DR: It can be made safe (with some effort) in terms of soundness, but it'd be a big security flaw.


It is actually possible to safely read external files, query external servers, etc...

However, in order for it to be safe, then it must be guaranteed that such operations behave as pure functions: if called with the same arguments, they must return the same result.

This could be implemented, for example, by simply building a huge cache, in which the result of each such query - arguments pair is stored until end of computation, and any subsequent query simply reuse the result from the cache rather than performing a "live" read.

This is more difficult, implementation-wise, but still quite feasible.


Beyond safety, it's also an additional hurdle for incremental compilation. In essence, each result of a read/query is its own source file, and therefore should it change then there are ripple effects: some stuff need be re-compiled.

This requires that for incremental compilation, the cache that I mentioned above must be saved as part of the incremental meta-data. Wholly.

Then, each time the compiler is invoked, it must first perform all queries again and compare their output with the cached ones to know whether they changed or not.

Still feasible.


At this point, however, we have to question whether it is useful, whether it could be surprising and what would be the priority of such a feature:

  • Useful: possibly.

  • Surprising: in some instances, extremely. Reading /dev/random multiple times in the same compilation process always returns the same result. Yet it most likely changes across each invocation of the compiler (causing incremental compilation to recompile all dependencies).

  • Priority: extremely low; it enables nothing that build.rs cannot do.


And then we need to talk about good engineering practices and security.

In terms of engineering practices, I would put out there a reminder that reproducible builds rock, and any kind of input that is NOT committed prevents them. Therefore, I would argue for a two-steps build process if such a thing is necessary:

  1. Read whatever you need from network/disk/... and write that to a file committed in the repository (or several),

  2. Build from those files.

And at this point, simply ensuring that the file is valid Rust code is sufficient to NOT need to read files during the compilation.

In terms of security, including arbitrary I/O in the compiler opens up a huge attack surface. You could literally have "innocent" looking Rust code reads files of your disk (hint: ~/.ssh/id_rsa?) and upload them to a random webserver.

It's already possible today as part of build.rs, however it's also trivial to check whether a crate has a build.rs or not1 whereas auditing arbitrary Rust code to check for file/network access would be a huge pain.

1 Note: Is there a cargo feature to explicit whitelist which crate - version pair is allowed to execute a build.rs script as part of its build? If not, we need one before this is exploited.

2

u/DannoHung Jul 19 '18

It's already possible today as part of build.rs, however it's also trivial to check whether a crate has a build.rs or not1 whereas auditing arbitrary Rust code to check for file/network access would be a huge pain.

Does rustc guarantee that the include macro can't read from files not in the source code repository? Does it also refuse to read symbolic links that point outside the source repository?

That said. This seems sort of on the paranoid end of security considerations.

2

u/matthieum [he/him] Jul 20 '18

That said. This seems sort of on the paranoid end of security considerations.

Just a week or so ago there was an uproar on the NPM community because a popular NPM module had been subverted to exfiltrate NPM keys.

You'd build your code, as usual, and in the background the npm utility would download the new version of this module, which would exfiltrate your NPM keys and publish them on 3rd party server.

This is not paranoia for the sake of paranoia; just learning from (recent) history.

Link: ESLint compromised, may have stolen your credentials.

1

u/DannoHung Jul 20 '18

My point is just that attacking the build machine seems small potatoes compared to attacking a production server or an end-user.

1

u/matthieum [he/him] Jul 20 '18

My point is just that attacking the build machine seems small potatoes compared to attacking a production server or an end-user.

Yes... and no.

Exfiltrating the cargo credentials means that you can now publish updates of any of the crates the author has access to. The viral behavior of replicating the exploit to gather credentials is not that useful in itself; once you have the credentials, however, you can publish updates on any crate of your choice and then have code run on production servers.

This is trojan-horse attack, in essence. Imagine:

  1. Check out burntsushi's week-end projects,

  2. Publish a crate to help solve one of the problems he's complaining about,

  3. Do a PR on his week-end project which uses your new crate to solve the problem,

  4. Wait until integrated,

  5. Publish an update to your crate, which can steal credentials,

  6. Wait until burntsushi rebuilds his week-end project => credentials in,

  7. Publish a minor update to the regex crate, or maybe to ripgrep (now distributed in VS code), which includes code that steal CC numbers or install bitcoin miners in the background,

  8. Profit.

Exploiting build.rs or a procedural macro is about getting your foot in on a popular crate's author's machine. Your PR on regex or ripgrep would never be integrated otherwise, severely limited your target audience.

1

u/DannoHung Jul 20 '18

I guess! What a weird vector for spearfishing though!