Skip to content

Commit 62d3244

Browse files
fvincenzokees
authored andcommitted
kasan: Add strscpy() test to trigger tag fault on arm64
When we invoke strscpy() with a maximum size of N bytes, it assumes that: - It can always read N bytes from the source. - It always write N bytes (zero-padded) to the destination. On aarch64 with Memory Tagging Extension enabled if we pass an N that is bigger then the source buffer, it would previously trigger an MTE fault. Implement a KASAN KUnit test that triggers the issue with the previous implementation of read_word_at_a_time() on aarch64 with MTE enabled. Cc: Will Deacon <[email protected]> Signed-off-by: Vincenzo Frascino <[email protected]> Signed-off-by: Catalin Marinas <[email protected]> Co-developed-by: Peter Collingbourne <[email protected]> Signed-off-by: Peter Collingbourne <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Link: https://linux-review.googlesource.com/id/If88e396b9e7c058c1a4b5a252274120e77b1898a Reviewed-by: Catalin Marinas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent d94c12b commit 62d3244

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

mm/kasan/kasan_test_c.c

+20
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,7 @@ static void kasan_memcmp(struct kunit *test)
15671567
static void kasan_strings(struct kunit *test)
15681568
{
15691569
char *ptr;
1570+
char *src;
15701571
size_t size = 24;
15711572

15721573
/*
@@ -1578,6 +1579,25 @@ static void kasan_strings(struct kunit *test)
15781579
ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
15791580
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
15801581

1582+
src = kmalloc(KASAN_GRANULE_SIZE, GFP_KERNEL | __GFP_ZERO);
1583+
strscpy(src, "f0cacc1a0000000", KASAN_GRANULE_SIZE);
1584+
1585+
/*
1586+
* Make sure that strscpy() does not trigger KASAN if it overreads into
1587+
* poisoned memory.
1588+
*
1589+
* The expected size does not include the terminator '\0'
1590+
* so it is (KASAN_GRANULE_SIZE - 2) ==
1591+
* KASAN_GRANULE_SIZE - ("initial removed character" + "\0").
1592+
*/
1593+
KUNIT_EXPECT_EQ(test, KASAN_GRANULE_SIZE - 2,
1594+
strscpy(ptr, src + 1, KASAN_GRANULE_SIZE));
1595+
1596+
/* strscpy should fail if the first byte is unreadable. */
1597+
KUNIT_EXPECT_KASAN_FAIL(test, strscpy(ptr, src + KASAN_GRANULE_SIZE,
1598+
KASAN_GRANULE_SIZE));
1599+
1600+
kfree(src);
15811601
kfree(ptr);
15821602

15831603
/*

0 commit comments

Comments
 (0)