You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/todo/rust-basics.md
+68Lines changed: 68 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -320,4 +320,72 @@ fn main() {
320
320
```
321
321
322
322
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
+
usestd::ptr;
338
+
339
+
fnvuln_copy(src:&[u8]) ->Vec<u8> {
340
+
letmutdst=Vec::with_capacity(4);
341
+
unsafe {
342
+
// ❌ copies *src.len()* bytes, the destination only reserves 4.
0 commit comments