kasan_test_rust.rs 592 B

12345678910111213141516171819202122
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Helper crate for KASAN testing.
  3. //!
  4. //! Provides behavior to check the sanitization of Rust code.
  5. use core::ptr::addr_of_mut;
  6. use kernel::prelude::*;
  7. /// Trivial UAF - allocate a big vector, grab a pointer partway through,
  8. /// drop the vector, and touch it.
  9. #[no_mangle]
  10. pub extern "C" fn kasan_test_rust_uaf() -> u8 {
  11. let mut v: KVec<u8> = KVec::new();
  12. for _ in 0..4096 {
  13. v.push(0x42, GFP_KERNEL).unwrap();
  14. }
  15. let ptr: *mut u8 = addr_of_mut!(v[2048]);
  16. drop(v);
  17. // SAFETY: Incorrect, on purpose.
  18. unsafe { *ptr }
  19. }