How to harden Rust servers?

Your daily reminder that, while Rust's safe APIs are memory-safe, that doesn't extend to /proc/self/mem and so, if there is a path traversal to that, secrets could still leak.

So, what hardening approaches are there for Rust? I typically use axum, but would be glad to hear about any framework. If the approach is #[!forbid(unsafe_code)][1], even better.


  1. I use that attribute for code that requires superuser rights, for instance ↩︎

On Linux, it's basically seccomp. It's not ideal (difficult to get all syscalls you need and none that could be abused), but you can set up listening during startup and then block open completely.

Well one method to preventing path traversal attacks is only using hard-coded paths using openat2 with the RESOLVE_NO_SYMLINKS flag.
(this method is likely too extreme of a measure, but it technically prevents path traversal attacks)

I use satitize_path() for the purpose.

Your reminder that secrets leak is not a memory safety violation, and safe Rust can do that. See https://github.com/rust-lang/rfcs/pull/4001 for example.

It's way, way simpler than that. You don't need freeze. You can just return the secret directly, the same way it's memory-safe to upload your password to the wrong site, to mint a certificate where the secret number is 4, to delete all the files on your machine, to submit a limit order to sell BRK.A for $1, ...

If you're worried about secrets being exposed by OS APIs you should be using something like containers, seccomp, AppArmor or SELinux to tighten access to what your application can access.

I'm well aware. I linked to the RFC because people have commented on it with this "concern".