summaryrefslogtreecommitdiff
path: root/mm/kasan/kasan_test_rust.rs
diff options
context:
space:
mode:
authorMatthew Maurer <mmaurer@google.com>2024-08-20 19:48:59 +0000
committerMiguel Ojeda <ojeda@kernel.org>2024-09-16 18:04:37 +0200
commita2f11547052001bd448ccec81dd1e68409078fbb (patch)
tree8b877782043c1e767f6ecf12fe429ebe138d8ca6 /mm/kasan/kasan_test_rust.rs
parente3117404b41124c88a4d834fc3222669a880addc (diff)
kasan: rust: Add KASAN smoke test via UAFrust-6.12
Adds a smoke test to ensure that KASAN in Rust is actually detecting a Rust-native UAF. There is significant room to expand this test suite, but this will at least ensure that flags are having the intended effect. The rename from kasan_test.c to kasan_test_c.c is in order to allow the single kasan_test.ko test suite to contain both a .o file produced by the C compiler and one produced by rustc. Signed-off-by: Matthew Maurer <mmaurer@google.com> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> Link: https://lore.kernel.org/r/20240820194910.187826-5-mmaurer@google.com [ Applied empty line nit, removed double empty line, applied `rustfmt` and formatted crate comment. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'mm/kasan/kasan_test_rust.rs')
-rw-r--r--mm/kasan/kasan_test_rust.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/mm/kasan/kasan_test_rust.rs b/mm/kasan/kasan_test_rust.rs
new file mode 100644
index 000000000000..caa7175964ef
--- /dev/null
+++ b/mm/kasan/kasan_test_rust.rs
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Helper crate for KASAN testing.
+//!
+//! Provides behavior to check the sanitization of Rust code.
+
+use core::ptr::addr_of_mut;
+use kernel::prelude::*;
+
+/// Trivial UAF - allocate a big vector, grab a pointer partway through,
+/// drop the vector, and touch it.
+#[no_mangle]
+pub extern "C" fn kasan_test_rust_uaf() -> u8 {
+ let mut v: Vec<u8> = Vec::new();
+ for _ in 0..4096 {
+ v.push(0x42, GFP_KERNEL).unwrap();
+ }
+ let ptr: *mut u8 = addr_of_mut!(v[2048]);
+ drop(v);
+ unsafe { *ptr }
+}