Skip to content

Commit d73dad4

Browse files
keestorvalds
authored andcommitted
kasan: test: bypass __alloc_size checks
Intentional overflows, as performed by the KASAN tests, are detected at compile time[1] (instead of only at run-time) with the addition of __alloc_size. Fix this by forcing the compiler into not being able to trust the size used following the kmalloc()s. [1] https://lore.kernel.org/lkml/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Dmitry Vyukov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8772716 commit d73dad4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/test_kasan.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ static void kmalloc_oob_memset_2(struct kunit *test)
440440
ptr = kmalloc(size, GFP_KERNEL);
441441
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
442442

443+
OPTIMIZER_HIDE_VAR(size);
443444
KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
444445
kfree(ptr);
445446
}
@@ -452,6 +453,7 @@ static void kmalloc_oob_memset_4(struct kunit *test)
452453
ptr = kmalloc(size, GFP_KERNEL);
453454
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
454455

456+
OPTIMIZER_HIDE_VAR(size);
455457
KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
456458
kfree(ptr);
457459
}
@@ -464,6 +466,7 @@ static void kmalloc_oob_memset_8(struct kunit *test)
464466
ptr = kmalloc(size, GFP_KERNEL);
465467
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
466468

469+
OPTIMIZER_HIDE_VAR(size);
467470
KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
468471
kfree(ptr);
469472
}
@@ -476,6 +479,7 @@ static void kmalloc_oob_memset_16(struct kunit *test)
476479
ptr = kmalloc(size, GFP_KERNEL);
477480
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
478481

482+
OPTIMIZER_HIDE_VAR(size);
479483
KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
480484
kfree(ptr);
481485
}
@@ -488,6 +492,7 @@ static void kmalloc_oob_in_memset(struct kunit *test)
488492
ptr = kmalloc(size, GFP_KERNEL);
489493
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
490494

495+
OPTIMIZER_HIDE_VAR(size);
491496
KUNIT_EXPECT_KASAN_FAIL(test,
492497
memset(ptr, 0, size + KASAN_GRANULE_SIZE));
493498
kfree(ptr);
@@ -497,7 +502,7 @@ static void kmalloc_memmove_negative_size(struct kunit *test)
497502
{
498503
char *ptr;
499504
size_t size = 64;
500-
volatile size_t invalid_size = -2;
505+
size_t invalid_size = -2;
501506

502507
/*
503508
* Hardware tag-based mode doesn't check memmove for negative size.
@@ -510,6 +515,7 @@ static void kmalloc_memmove_negative_size(struct kunit *test)
510515
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
511516

512517
memset((char *)ptr, 0, 64);
518+
OPTIMIZER_HIDE_VAR(invalid_size);
513519
KUNIT_EXPECT_KASAN_FAIL(test,
514520
memmove((char *)ptr, (char *)ptr + 4, invalid_size));
515521
kfree(ptr);

lib/test_kasan_module.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static noinline void __init copy_user_test(void)
3535
return;
3636
}
3737

38+
OPTIMIZER_HIDE_VAR(size);
39+
3840
pr_info("out-of-bounds in copy_from_user()\n");
3941
unused = copy_from_user(kmem, usermem, size + 1);
4042

0 commit comments

Comments
 (0)