|
| 1 | +use lazorkit_program::auth::secp256r1::slothashes::SlotHashes; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn test_slot_hashes_oob_read() { |
| 5 | + // 1. Setup Mock Data |
| 6 | + // num_entries: 2 (u64) |
| 7 | + // entry 0: slot 100, hash [1; 32] |
| 8 | + // entry 1: slot 99, hash [2; 32] |
| 9 | + let mut data = Vec::new(); |
| 10 | + data.extend_from_slice(&2u64.to_le_bytes()); // len = 2 |
| 11 | + |
| 12 | + // Entry 0 |
| 13 | + data.extend_from_slice(&100u64.to_le_bytes()); |
| 14 | + data.extend_from_slice(&[1u8; 32]); |
| 15 | + |
| 16 | + // Entry 1 |
| 17 | + data.extend_from_slice(&99u64.to_le_bytes()); |
| 18 | + data.extend_from_slice(&[2u8; 32]); |
| 19 | + |
| 20 | + // Interpret data as &[u8] |
| 21 | + let data_slice: &[u8] = &data; |
| 22 | + |
| 23 | + // Safety: we constructed data correctly |
| 24 | + let slot_hashes = unsafe { SlotHashes::new_unchecked(data_slice) }; |
| 25 | + |
| 26 | + // 2. Verify Valid Access |
| 27 | + let hash_0 = slot_hashes.get_slot_hash(0).unwrap(); |
| 28 | + assert_eq!(hash_0.height, 100); |
| 29 | + |
| 30 | + let hash_1 = slot_hashes.get_slot_hash(1).unwrap(); |
| 31 | + assert_eq!(hash_1.height, 99); |
| 32 | + |
| 33 | + // 3. Verify OOB Access (The Bug) |
| 34 | + println!("Trying to access OOB index 2..."); |
| 35 | + // This call accesses index 2. |
| 36 | + // Length is 2. |
| 37 | + // Current Buggy Logic: 2 > 2 is FALSE. |
| 38 | + // So it PROCEEDS to unsafe code and returns Ok or Panics. |
| 39 | + // We expect it to be Err(PermissionDenied). |
| 40 | + |
| 41 | + let result = slot_hashes.get_slot_hash(2); |
| 42 | + |
| 43 | + // If the bug is present, result.is_ok() will be true (or panic). |
| 44 | + // If fixed, result.is_err() will be true. |
| 45 | + assert!( |
| 46 | + result.is_err(), |
| 47 | + "Index equal to length should be an error! (OOB Read)" |
| 48 | + ); |
| 49 | +} |
0 commit comments