Skip to content

Commit ab51280

Browse files
xairytorvalds
authored andcommitted
kasan: test: rework kmalloc_oob_right
Patch series "kasan: test: avoid crashing the kernel with HW_TAGS", v2. KASAN tests do out-of-bounds and use-after-free accesses. Running the tests works fine for the GENERIC mode, as it uses qurantine and redzones. But the HW_TAGS mode uses neither, and running the tests might crash the kernel. Rework the tests to avoid corrupting kernel memory. This patch (of 8): Rework kmalloc_oob_right() to do these bad access checks: 1. An unaligned access one byte past the requested kmalloc size (can only be detected by KASAN_GENERIC). 2. An aligned access into the first out-of-bounds granule that falls within the aligned kmalloc object. 3. Out-of-bounds access past the aligned kmalloc object. Test #3 deliberately uses a read access to avoid corrupting memory. Otherwise, this test might lead to crashes with the HW_TAGS mode, as it neither uses quarantine nor redzones. Link: https://lkml.kernel.org/r/cover.1628779805.git.andreyknvl@gmail.com Link: https://lkml.kernel.org/r/474aa8b7b538c6737a4c6d0090350af2e1776bef.1628779805.git.andreyknvl@gmail.com Signed-off-by: Andrey Konovalov <andreyknvl@gmail.com> Reviewed-by: Marco Elver <elver@google.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Alexander Potapenko <glider@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c9d1af2 commit ab51280

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/test_kasan.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,28 @@ static void kasan_test_exit(struct kunit *test)
122122
static void kmalloc_oob_right(struct kunit *test)
123123
{
124124
char *ptr;
125-
size_t size = 123;
125+
size_t size = 128 - KASAN_GRANULE_SIZE - 5;
126126

127127
ptr = kmalloc(size, GFP_KERNEL);
128128
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
129129

130-
KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
130+
/*
131+
* An unaligned access past the requested kmalloc size.
132+
* Only generic KASAN can precisely detect these.
133+
*/
134+
if (IS_ENABLED(CONFIG_KASAN_GENERIC))
135+
KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x');
136+
137+
/*
138+
* An aligned access into the first out-of-bounds granule that falls
139+
* within the aligned kmalloc object.
140+
*/
141+
KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y');
142+
143+
/* Out-of-bounds access past the aligned kmalloc object. */
144+
KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] =
145+
ptr[size + KASAN_GRANULE_SIZE + 5]);
146+
131147
kfree(ptr);
132148
}
133149

0 commit comments

Comments
 (0)