Answer#
Rust's guarantee is about memory: code written without unsafe cannot read or write memory it does not own, cannot use a value after it was freed, and cannot have two threads change the same data without synchronisation. That removes a big source of serious bugs. It says nothing about whether your program does the right thing with the data it handles.
| Problem | Does safe Rust prevent it? | Where to look |
|---|---|---|
| Buffer overflow, use after free, data race | Yes (outside unsafe) | The Rust Book, ownership chapters |
| SQL injection, command injection | No: a query is just a string | The thread on parameterised queries |
| Path traversal in file names from users | No | The thread on path traversal |
| Weak or missing authentication, broken sessions | No | The password, session and CSRF threads |
| Secrets in source code or logs | No | The threads on secrets and logs |
| A vulnerable or malicious dependency | No | The cargo audit, cargo deny and supply-chain threads |
| Integer overflow giving a wrong result | Partly: it panics in debug builds and wraps in release builds | The thread on integer overflow |
| A panic that an attacker can trigger repeatedly | No: it is memory safe, but it is still an outage | The thread on panics and untrusted data |
Bugs inside unsafe blocks, or in C libraries you call | No: the compiler trusts you there | The threads on reviewing unsafe |
The point of the table is the middle column. Rust moves the work: you spend less time on memory errors and can spend it on the rows below them.
How to do it#
- Treat everything that crosses a trust boundary (network, files, environment, command line, other processes) as hostile until it is parsed and checked.
- Keep secrets out of source and logs, and hash passwords with a memory-hard algorithm.
- Run
cargo auditandcargo denyin CI so known-vulnerable dependencies are caught. - Keep
unsafesmall, documented and reviewed. - Run the service with the least privilege it needs.
Common mistakes#
- Assuming "it is written in Rust" is a security review. A Rust service can still have an open admin page or a guessable token.
- Using
unsafecasually for speed without measuring. The memory guarantee ends exactly there. - Forgetting that dependencies run with your program's permissions, and some of their code runs at build time.
Related questions#
Can a Rust program still crash from untrusted input?#
Yes. Indexing out of range, unwrap on None and arithmetic overflow in debug builds all panic. That is memory safe, but an attacker who can trigger it repeatedly can take the service down.
Does unsafe turn the safety checks off everywhere?#
No. It allows a small set of extra operations (such as dereferencing a raw pointer). The borrow checker and type checker keep working for everything else in the block. The responsibility for those few operations moves to you.
Sources#
- The Rust Book, "Unsafe Rust": https://doc.rust-lang.org/book/ch20-01-unsafe-rust.html
- The Rustonomicon, "Meet Safe and Unsafe": https://doc.rust-lang.org/nomicon/meet-safe-and-unsafe.html
- OWASP Top 10: https://owasp.org/www-project-top-ten/