Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <will@kernel.org>
Signed-off-by: Vincenzo Frascino <vincenzo.frascino@arm.com>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
Co-developed-by: Peter Collingbourne <pcc@google.com>
Signed-off-by: Peter Collingbourne <pcc@google.com>
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Link: https://linux-review.googlesource.com/id/If88e396b9e7c058c1a4b5a252274120e77b1898a
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Link: https://lore.kernel.org/r/20250403000703.2584581-3-pcc@google.com
Signed-off-by: Kees Cook <kees@kernel.org>

authored by

Vincenzo Frascino and committed by
Kees Cook
62d32440 d94c12bd

+20
+20
mm/kasan/kasan_test_c.c
··· 1567 1567 static void kasan_strings(struct kunit *test) 1568 1568 { 1569 1569 char *ptr; 1570 + char *src; 1570 1571 size_t size = 24; 1571 1572 1572 1573 /* ··· 1579 1578 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); 1580 1579 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); 1581 1580 1581 + src = kmalloc(KASAN_GRANULE_SIZE, GFP_KERNEL | __GFP_ZERO); 1582 + strscpy(src, "f0cacc1a0000000", KASAN_GRANULE_SIZE); 1583 + 1584 + /* 1585 + * Make sure that strscpy() does not trigger KASAN if it overreads into 1586 + * poisoned memory. 1587 + * 1588 + * The expected size does not include the terminator '\0' 1589 + * so it is (KASAN_GRANULE_SIZE - 2) == 1590 + * KASAN_GRANULE_SIZE - ("initial removed character" + "\0"). 1591 + */ 1592 + KUNIT_EXPECT_EQ(test, KASAN_GRANULE_SIZE - 2, 1593 + strscpy(ptr, src + 1, KASAN_GRANULE_SIZE)); 1594 + 1595 + /* strscpy should fail if the first byte is unreadable. */ 1596 + KUNIT_EXPECT_KASAN_FAIL(test, strscpy(ptr, src + KASAN_GRANULE_SIZE, 1597 + KASAN_GRANULE_SIZE)); 1598 + 1599 + kfree(src); 1582 1600 kfree(ptr); 1583 1601 1584 1602 /*