Skip to content

Commit 830edec

Browse files
authored
Merge pull request #1154 from HackTricks-wiki/research_update_src_todo_rust-basics_20250719_082358
Research Update Enhanced src/todo/rust-basics.md
2 parents d5e0254 + fbb226c commit 830edec

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/todo/rust-basics.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,72 @@ fn main() {
320320
```
321321

322322

323+
### Security Essentials
324+
325+
Rust provides strong memory-safety guarantees by default, but you can still introduce critical vulnerabilities through `unsafe` code, dependency issues or logic mistakes. The following mini-cheatsheet gathers the primitives you will most commonly touch during offensive or defensive security reviews of Rust software.
326+
327+
#### Unsafe code & memory safety
328+
329+
`unsafe` blocks opt-out of the compiler’s aliasing and bounds checks, so **all traditional memory-corruption bugs (OOB, use-after-free, double free, etc.) can appear again**. A quick audit checklist:
330+
331+
* Look for `unsafe` blocks, `extern "C"` functions, calls to `ptr::copy*`, `std::mem::transmute`, `MaybeUninit`, raw pointers or `ffi` modules.
332+
* Validate every pointer arithmetic and length argument passed to low-level functions.
333+
* Prefer `#![forbid(unsafe_code)]` (crate-wide) or `#[deny(unsafe_op_in_unsafe_fn)]` (1.68 +) to fail compilation when someone re-introduces `unsafe`.
334+
335+
Example overflow created with raw pointers:
336+
```rust
337+
use std::ptr;
338+
339+
fn vuln_copy(src: &[u8]) -> Vec<u8> {
340+
let mut dst = Vec::with_capacity(4);
341+
unsafe {
342+
// ❌ copies *src.len()* bytes, the destination only reserves 4.
343+
ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len());
344+
dst.set_len(src.len());
345+
}
346+
dst
347+
}
348+
```
349+
Running Miri is an inexpensive way to detect UB at test time:
350+
```bash
351+
rustup component add miri
352+
cargo miri test # hunts for OOB / UAF during unit tests
353+
```
354+
355+
#### Auditing dependencies with RustSec / cargo-audit
356+
357+
Most real-world Rust vulns live in third-party crates. The RustSec advisory DB (community-powered) can be queried locally:
358+
```bash
359+
cargo install cargo-audit
360+
cargo audit # flags vulnerable versions listed in Cargo.lock
361+
```
362+
Integrate it in CI and fail on `--deny warnings`.
363+
364+
`cargo deny check advisories` offers similar functionality plus licence and ban-list checks.
365+
366+
#### Supply-chain verification with cargo-vet (2024)
367+
368+
`cargo vet` records a review hash for every crate you import and prevents unnoticed upgrades:
369+
```bash
370+
cargo install cargo-vet
371+
cargo vet init # generates vet.toml
372+
cargo vet --locked # verifies packages referenced in Cargo.lock
373+
```
374+
The tool is being adopted by the Rust project infrastructure and a growing number of orgs to mitigate poisoned-package attacks.
375+
376+
#### Fuzzing your API surface (cargo-fuzz)
377+
378+
Fuzz tests easily catch panics, integer overflows and logic bugs that might become DoS or side-channel issues:
379+
```bash
380+
cargo install cargo-fuzz
381+
cargo fuzz init # creates fuzz_targets/
382+
cargo fuzz run fuzz_target_1 # builds with libFuzzer & runs continuously
383+
```
384+
Add the fuzz target to your repo and run it in your pipeline.
385+
386+
## References
387+
388+
- RustSec Advisory Database – <https://rustsec.org>
389+
- Cargo-vet: "Auditing your Rust Dependencies" – <https://mozilla.github.io/cargo-vet/>
390+
323391
{{#include ../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)