rust-memory-safety-examples 2.0.0

Comprehensive educational examples demonstrating memory-safe programming patterns with CVE case studies and benchmarks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! # Rust Memory Safety Examples v2.0
//!
//! Comprehensive educational examples demonstrating memory-safe programming patterns
//! in Rust for financial systems, critical infrastructure, and security-sensitive applications.
//!
//! ## Purpose
//!
//! This library provides clear, documented examples of how Rust's ownership system
//! prevents common memory safety vulnerabilities that affect C/C++ systems. Version 2.0
//! adds CVE case studies, secure programming patterns, and performance benchmarks.
//!
//! ## What's New in v2.0
//!
//! - **CVE Case Studies**: Real-world vulnerability analysis (Heartbleed, Baron Samedit, etc.)
//! - **Secure Patterns**: Type-state, capability-based security, secret wrappers
//! - **Performance Benchmarks**: Measure safety overhead with criterion benchmarks
//! - **Enhanced Documentation**: Comprehensive explanations with industry references
//!
//! ## Comparative Examples
//!
//! Each module includes:
//! - Vulnerable C/C++ code patterns (commented examples)
//! - Safe Rust equivalents
//! - Explanations of how Rust prevents the vulnerability
//! - Real-world CVE references
//!
//! ## Running Examples
//!
//! ```bash
//! # Run CVE case studies
//! cargo run --example cve_case_studies
//!
//! # Run secure patterns demonstration
//! cargo run --example secure_patterns
//!
//! # Run benchmarks
//! cargo bench
//! ```
//!
//! ## Alignment with Federal Guidance
//!
//! These examples align with 2024-2025 CISA/FBI/NSA guidance recommending memory-safe
//! languages for critical infrastructure to eliminate 70% of security vulnerabilities.
//!
//! ## Industry Research References
//!
//! - Microsoft Security: ~70% of CVEs are memory safety issues
//! - Google Chrome: ~70% of high-severity bugs are memory safety issues
//! - CISA: Memory safety roadmap for critical infrastructure (2024)
//! - NSA: Software Memory Safety Cybersecurity Information Sheet

#![allow(clippy::empty_line_after_doc_comments)]
#![allow(clippy::mixed_attributes_style)]
#![allow(dead_code)]
#![allow(clippy::vec_init_then_push)]
#![allow(clippy::approx_constant)]
#![allow(clippy::useless_vec)]

pub mod buffer_overflow_prevention;
pub mod data_race_prevention;
pub mod use_after_free_prevention;

/// Module demonstrating buffer overflow prevention
pub mod buffer_overflow {
    //! Buffer overflow prevention through bounds checking
    //!
    //! In C/C++, buffer overflows are a major security vulnerability.
    //! Rust prevents these at compile time and runtime.

    /// Safe array access - Rust prevents buffer overflows
    pub fn safe_array_access() {
        let data = vec![1, 2, 3, 4, 5];

        // Safe access with bounds checking
        if let Some(&value) = data.get(10) {
            println!("Value: {}", value);
        } else {
            println!("Index out of bounds - safely handled!");
        }

        // Panics instead of undefined behavior (can be caught)
        // let value = data[10]; // Would panic with clear error message
    }

    /// Safe string handling - no buffer overflows
    pub fn safe_string_handling() {
        let mut buffer = String::new();

        // Rust automatically resizes, no fixed buffer overflow
        for i in 0..1000 {
            buffer.push_str(&format!("Item {}, ", i));
        }

        println!("Buffer safely holds {} bytes", buffer.len());
    }

    /// Comparing C vs Rust buffer handling
    pub fn compare_c_vs_rust() {
        // C code (UNSAFE):
        // char buffer[10];
        // strcpy(buffer, "This is way too long"); // Buffer overflow!

        // Rust equivalent (SAFE):
        let buffer = "This is way too long".to_string();
        let truncated: String = buffer.chars().take(10).collect();

        println!("C: Buffer overflow vulnerability");
        println!("Rust: Safe truncation - {}", truncated);
    }
}

/// Module demonstrating use-after-free prevention
pub mod use_after_free {
    //! Use-after-free prevention through ownership
    //!
    //! Use-after-free is impossible in safe Rust due to the ownership system.

    /// Ownership prevents use-after-free
    pub fn ownership_prevents_uaf() {
        let data = vec![1, 2, 3, 4, 5];

        // Transfer ownership
        let owned_data = data;
        // data is now invalid

        // This would not compile:
        // println!("{:?}", data); // ERROR: value used after move

        println!("Rust prevents use-after-free at compile time");
        println!("Data safely owned: {:?}", owned_data);
    }

    /// Borrowing prevents dangling references
    pub fn borrowing_prevents_dangling() {
        let data = vec![1, 2, 3, 4, 5];

        // Borrow the data
        let reference = &data;

        // Can't drop data while reference exists
        // drop(data); // ERROR: cannot move out of `data` because it is borrowed

        println!("Reference is valid: {:?}", reference);
        // data dropped here, after reference is done
    }

    /// Comparing C vs Rust lifetime management
    pub fn compare_c_vs_rust() {
        // C code (UNSAFE):
        // int* ptr;
        // {
        //     int x = 42;
        //     ptr = &x;
        // }
        // printf("%d", *ptr); // Use-after-free!

        // Rust equivalent (SAFE - won't compile):
        /*
        let ptr: &i32;
        {
            let x = 42;
            ptr = &x; // ERROR: `x` does not live long enough
        }
        */

        println!("C: Use-after-free vulnerability");
        println!("Rust: Compile-time prevention of dangling pointers");
    }
}

/// Module demonstrating data race prevention
///
/// Data race prevention through ownership and type system
///
/// Rust prevents data races at compile time through the type system.
pub mod data_race {
    use std::sync::{Arc, Mutex};
    use std::thread;

    /// Arc and Mutex for safe concurrent access
    pub fn safe_concurrent_access() {
        let counter = Arc::new(Mutex::new(0));
        let mut handles = vec![];

        for _ in 0..10 {
            let counter_clone = Arc::clone(&counter);
            let handle = thread::spawn(move || {
                let mut num = counter_clone.lock().unwrap();
                *num += 1;
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        println!("Final count (safe): {}", *counter.lock().unwrap());
        println!("No data races possible!");
    }

    /// Send and Sync traits prevent data races
    pub fn type_system_prevents_races() {
        // This would not compile (Rc is not Send):
        // use std::rc::Rc;
        // let data = Rc::new(vec![1, 2, 3]);
        // thread::spawn(move || {
        //     println!("{:?}", data); // ERROR: Rc cannot be sent between threads
        // });

        println!("Rust's type system prevents data races at compile time");
    }

    /// Comparing C vs Rust concurrency
    pub fn compare_c_vs_rust() {
        // C code (UNSAFE):
        // int counter = 0;
        // // Multiple threads incrementing counter without synchronization
        // // Result: Data race, undefined behavior

        // Rust equivalent (SAFE):
        safe_concurrent_access();

        println!("\nC: Data races cause undefined behavior");
        println!("Rust: Compile-time prevention of data races");
    }
}

/// Module demonstrating integer overflow protection
pub mod integer_overflow {
    //! Integer overflow detection and prevention

    /// Checked arithmetic prevents silent overflows
    pub fn checked_arithmetic() {
        let a: u32 = 4_000_000_000;
        let b: u32 = 1_000_000_000;

        // Silent overflow in C (undefined behavior)
        // In Rust debug mode: panics
        // In Rust release mode with checked_add: returns None

        match a.checked_add(b) {
            Some(result) => println!("Result: {}", result),
            None => println!("Overflow detected and handled safely!"),
        }
    }

    /// Saturating arithmetic for financial calculations
    pub fn saturating_arithmetic() {
        let balance: u32 = 1000;
        let withdrawal: u32 = 2000;

        // Saturating subtraction (clamps at 0)
        let new_balance = balance.saturating_sub(withdrawal);

        println!("Balance after withdrawal: {} (saturated)", new_balance);
    }
}

/// Module demonstrating null pointer dereference prevention
pub mod null_pointer {
    //! Null pointer prevention through Option<T>

    /// Option<T> eliminates null pointer dereferences
    pub fn option_prevents_null() {
        fn find_user(id: u32) -> Option<String> {
            if id == 1 {
                Some("Alice".to_string())
            } else {
                None
            }
        }

        // Must explicitly handle None case
        match find_user(1) {
            Some(name) => println!("Found user: {}", name),
            None => println!("User not found"),
        }

        // Can't accidentally dereference null
        // let name = find_user(99); // Type is Option<String>
        // println!("{}", name); // ERROR: can't print Option directly
    }

    /// Comparing C vs Rust null handling
    pub fn compare_c_vs_rust() {
        // C code (UNSAFE):
        // char* ptr = find_user(99); // Returns NULL
        // printf("%s", ptr); // Null pointer dereference!

        // Rust equivalent (SAFE):
        option_prevents_null();

        println!("\nC: Null pointer dereferences cause crashes");
        println!("Rust: Option<T> forces handling of null cases");
    }
}

/// Module demonstrating double-free prevention
pub mod double_free {
    //! Double-free prevention through ownership
    //!
    //! Double-free errors (freeing the same memory twice) are impossible in Rust
    //! because the ownership system ensures memory is freed exactly once.

    /// Ownership ensures single free
    pub fn ownership_prevents_double_free() {
        let data = vec![1, 2, 3, 4, 5];

        // Data is automatically freed when it goes out of scope
        // Trying to manually free twice would not compile:
        // drop(data);
        // drop(data); // ERROR: use of moved value

        println!("Rust prevents double-free through ownership");
        println!("Data will be freed exactly once: {:?}", data);
    } // data freed here automatically

    /// Box demonstrates single ownership
    pub fn box_single_ownership() {
        let boxed_value = Box::new(42);

        // Transfer ownership
        let moved_box = boxed_value;

        // This would not compile:
        // drop(boxed_value); // ERROR: value used after move

        println!("Boxed value freed exactly once: {}", moved_box);
    } // moved_box freed here

    /// Comparing C vs Rust memory management
    pub fn compare_c_vs_rust() {
        // C code (UNSAFE):
        // int* ptr = malloc(sizeof(int));
        // free(ptr);
        // free(ptr); // Double-free!

        // Rust equivalent (SAFE - won't compile):
        ownership_prevents_double_free();

        println!("\nC: Double-free vulnerabilities");
        println!("Rust: Compile-time prevention of double-free");
    }
}

/// Module demonstrating uninitialized memory prevention
pub mod uninitialized_memory {
    //! Uninitialized memory prevention through initialization requirements

    /// All variables must be initialized
    pub fn initialization_required() {
        // This would not compile:
        // let x: i32;
        // println!("{}", x); // ERROR: use of possibly uninitialized variable

        // Must initialize
        let x: i32 = 42;
        println!("Value is always initialized: {}", x);
    }

    /// Uninitialized array prevention
    pub fn array_initialization() {
        // C code (UNSAFE):
        // int arr[100];
        // printf("%d", arr[0]); // Reading uninitialized memory!

        // Rust equivalent (SAFE):
        let arr = vec![0; 100]; // Initialized to zero
        println!("Array element (initialized): {}", arr[0]);

        // Or must explicitly initialize each element
        let arr2: Vec<i32> = (0..100).map(|i| i * 2).collect();
        println!("Array with values: {} elements", arr2.len());
    }

    /// Struct initialization must be complete
    pub fn struct_initialization() {
        struct User {
            id: u32,
            name: String,
            email: String,
        }

        // This would not compile:
        // let user = User {
        //     id: 1,
        //     name: "Alice".to_string(),
        //     // ERROR: missing field `email`
        // };

        // Must initialize all fields
        let user = User {
            id: 1,
            name: "Alice".to_string(),
            email: "alice@example.com".to_string(),
        };

        println!("User struct fully initialized: {}", user.name);
    }

    /// Comparing C vs Rust initialization
    pub fn compare_c_vs_rust() {
        println!("C: Uninitialized memory contains garbage values");
        println!("Rust: Compiler enforces initialization before use");

        initialization_required();
        array_initialization();
        struct_initialization();
    }
}

/// Module demonstrating memory leak prevention with RAII
///
/// Memory leak prevention through RAII (Resource Acquisition Is Initialization)
pub mod memory_leak {
    use std::fs::File;
    use std::io::Write;

    /// RAII ensures resources are cleaned up
    pub fn raii_file_handling() {
        // File automatically closed when it goes out of scope
        {
            let mut file = File::create("/tmp/test.txt").ok();
            if let Some(ref mut f) = file {
                let _ = f.write_all(b"Hello, RAII!");
            }
            // File automatically closed here
        }

        println!("File handle automatically closed (RAII)");
    }

    /// Drop trait for custom cleanup
    pub fn drop_trait_cleanup() {
        struct DatabaseConnection {
            id: u32,
        }

        impl Drop for DatabaseConnection {
            fn drop(&mut self) {
                println!("Closing database connection: {}", self.id);
            }
        }

        {
            let _conn = DatabaseConnection { id: 1 };
            println!("Database connection open");
            // Automatically cleaned up at end of scope
        }

        println!("Connection automatically closed via Drop trait");
    }

    /// Comparing C vs Rust resource management
    pub fn compare_c_vs_rust() {
        // C code (RISK):
        // FILE* f = fopen("test.txt", "w");
        // // Forgot to call fclose(f) - memory/resource leak!

        // Rust equivalent (SAFE):
        raii_file_handling();

        println!("\nC: Easy to forget resource cleanup (leaks)");
        println!("Rust: RAII ensures automatic cleanup");
    }
}

/// Module demonstrating type confusion prevention
pub mod type_confusion {
    //! Type confusion prevention through strong typing

    /// Strong typing prevents type confusion
    pub fn strong_typing_prevents_confusion() {
        let integer: i32 = 42;
        let float: f64 = 3.14;

        // This would not compile:
        // let result = integer + float; // ERROR: mismatched types

        // Must explicitly convert
        let result = integer as f64 + float;
        println!("Explicit conversion required: {}", result);
    }

    /// Newtype pattern for type safety
    pub fn newtype_pattern() {
        struct UserId(u32);
        struct ProductId(u32);

        let user = UserId(123);
        let product = ProductId(456);

        // This would not compile:
        // if user == product { } // ERROR: mismatched types

        println!("NewType pattern prevents mixing different ID types");
        println!("User ID: {}, Product ID: {}", user.0, product.0);
    }

    /// Enum prevents invalid states
    pub fn enum_prevents_invalid_states() {
        enum ConnectionState {
            Disconnected,
            Connecting,
            Connected { session_id: String },
            Error { message: String },
        }

        let state = ConnectionState::Connected {
            session_id: "abc123".to_string(),
        };

        // Can't access session_id unless in Connected state
        match state {
            ConnectionState::Connected { session_id } => {
                println!("Connected with session: {}", session_id);
            }
            _ => println!("Not connected"),
        }
    }

    /// Comparing C vs Rust type safety
    pub fn compare_c_vs_rust() {
        println!("C: Type confusion through casting and unions");
        println!("Rust: Strong type system prevents confusion at compile time");

        strong_typing_prevents_confusion();
        newtype_pattern();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_buffer_overflow_examples() {
        buffer_overflow::safe_array_access();
        buffer_overflow::safe_string_handling();
        buffer_overflow::compare_c_vs_rust();
    }

    #[test]
    fn test_use_after_free_examples() {
        use_after_free::ownership_prevents_uaf();
        use_after_free::borrowing_prevents_dangling();
        use_after_free::compare_c_vs_rust();
    }

    #[test]
    fn test_data_race_examples() {
        data_race::safe_concurrent_access();
        data_race::type_system_prevents_races();
        data_race::compare_c_vs_rust();
    }

    #[test]
    fn test_integer_overflow_examples() {
        integer_overflow::checked_arithmetic();
        integer_overflow::saturating_arithmetic();
    }

    #[test]
    fn test_null_pointer_examples() {
        null_pointer::option_prevents_null();
        null_pointer::compare_c_vs_rust();
    }

    #[test]
    fn test_double_free_examples() {
        double_free::ownership_prevents_double_free();
        double_free::box_single_ownership();
        double_free::compare_c_vs_rust();
    }

    #[test]
    fn test_uninitialized_memory_examples() {
        uninitialized_memory::initialization_required();
        uninitialized_memory::array_initialization();
        uninitialized_memory::struct_initialization();
        uninitialized_memory::compare_c_vs_rust();
    }

    #[test]
    fn test_memory_leak_examples() {
        memory_leak::raii_file_handling();
        memory_leak::drop_trait_cleanup();
        memory_leak::compare_c_vs_rust();
    }

    #[test]
    fn test_type_confusion_examples() {
        type_confusion::strong_typing_prevents_confusion();
        type_confusion::newtype_pattern();
        type_confusion::enum_prevents_invalid_states();
        type_confusion::compare_c_vs_rust();
    }
}