@@ -1928,6 +1928,41 @@ static void rust_uaf(struct kunit *test)
1928
1928
KUNIT_EXPECT_KASAN_FAIL (test , kasan_test_rust_uaf ());
1929
1929
}
1930
1930
1931
+ static void copy_to_kernel_nofault_oob (struct kunit * test )
1932
+ {
1933
+ char * ptr ;
1934
+ char buf [128 ];
1935
+ size_t size = sizeof (buf );
1936
+
1937
+ /*
1938
+ * This test currently fails with the HW_TAGS mode. The reason is
1939
+ * unknown and needs to be investigated.
1940
+ */
1941
+ KASAN_TEST_NEEDS_CONFIG_OFF (test , CONFIG_KASAN_HW_TAGS );
1942
+
1943
+ ptr = kmalloc (size - KASAN_GRANULE_SIZE , GFP_KERNEL );
1944
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , ptr );
1945
+ OPTIMIZER_HIDE_VAR (ptr );
1946
+
1947
+ /*
1948
+ * We test copy_to_kernel_nofault() to detect corrupted memory that is
1949
+ * being written into the kernel. In contrast,
1950
+ * copy_from_kernel_nofault() is primarily used in kernel helper
1951
+ * functions where the source address might be random or uninitialized.
1952
+ * Applying KASAN instrumentation to copy_from_kernel_nofault() could
1953
+ * lead to false positives. By focusing KASAN checks only on
1954
+ * copy_to_kernel_nofault(), we ensure that only valid memory is
1955
+ * written to the kernel, minimizing the risk of kernel corruption
1956
+ * while avoiding false positives in the reverse case.
1957
+ */
1958
+ KUNIT_EXPECT_KASAN_FAIL (test ,
1959
+ copy_to_kernel_nofault (& buf [0 ], ptr , size ));
1960
+ KUNIT_EXPECT_KASAN_FAIL (test ,
1961
+ copy_to_kernel_nofault (ptr , & buf [0 ], size ));
1962
+
1963
+ kfree (ptr );
1964
+ }
1965
+
1931
1966
static struct kunit_case kasan_kunit_test_cases [] = {
1932
1967
KUNIT_CASE (kmalloc_oob_right ),
1933
1968
KUNIT_CASE (kmalloc_oob_left ),
@@ -2000,6 +2035,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {
2000
2035
KUNIT_CASE (match_all_not_assigned ),
2001
2036
KUNIT_CASE (match_all_ptr_tag ),
2002
2037
KUNIT_CASE (match_all_mem_tag ),
2038
+ KUNIT_CASE (copy_to_kernel_nofault_oob ),
2003
2039
KUNIT_CASE (rust_uaf ),
2004
2040
{}
2005
2041
};
0 commit comments