Skip to content

Commit f649dc0

Browse files
pcctorvalds
authored andcommitted
kasan: fix unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled
These tests deliberately access these arrays out of bounds, which will cause the dynamic local bounds checks inserted by CONFIG_UBSAN_LOCAL_BOUNDS to fail and panic the kernel. To avoid this problem, access the arrays via volatile pointers, which will prevent the compiler from being able to determine the array bounds. These accesses use volatile pointers to char (char *volatile) rather than the more conventional pointers to volatile char (volatile char *) because we want to prevent the compiler from making inferences about the pointer itself (i.e. its array bounds), not the data that it refers to. Link: https://lkml.kernel.org/r/20210507025915.1464056-1-pcc@google.com Link: https://linux-review.googlesource.com/id/I90b1713fbfa1bf68ff895aef099ea77b98a7c3b9 Signed-off-by: Peter Collingbourne <pcc@google.com> Tested-by: Alexander Potapenko <glider@google.com> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> Cc: Peter Collingbourne <pcc@google.com> Cc: George Popescu <georgepope@android.com> Cc: Elena Petrova <lenaptr@google.com> Cc: Evgenii Stepanov <eugenis@google.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9ddb3c1 commit f649dc0

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

lib/test_kasan.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,20 @@ static char global_array[10];
654654

655655
static void kasan_global_oob(struct kunit *test)
656656
{
657-
volatile int i = 3;
658-
char *p = &global_array[ARRAY_SIZE(global_array) + i];
657+
/*
658+
* Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
659+
* from failing here and panicing the kernel, access the array via a
660+
* volatile pointer, which will prevent the compiler from being able to
661+
* determine the array bounds.
662+
*
663+
* This access uses a volatile pointer to char (char *volatile) rather
664+
* than the more conventional pointer to volatile char (volatile char *)
665+
* because we want to prevent the compiler from making inferences about
666+
* the pointer itself (i.e. its array bounds), not the data that it
667+
* refers to.
668+
*/
669+
char *volatile array = global_array;
670+
char *p = &array[ARRAY_SIZE(global_array) + 3];
659671

660672
/* Only generic mode instruments globals. */
661673
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
@@ -703,8 +715,9 @@ static void ksize_uaf(struct kunit *test)
703715
static void kasan_stack_oob(struct kunit *test)
704716
{
705717
char stack_array[10];
706-
volatile int i = OOB_TAG_OFF;
707-
char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
718+
/* See comment in kasan_global_oob. */
719+
char *volatile array = stack_array;
720+
char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
708721

709722
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
710723

@@ -715,7 +728,9 @@ static void kasan_alloca_oob_left(struct kunit *test)
715728
{
716729
volatile int i = 10;
717730
char alloca_array[i];
718-
char *p = alloca_array - 1;
731+
/* See comment in kasan_global_oob. */
732+
char *volatile array = alloca_array;
733+
char *p = array - 1;
719734

720735
/* Only generic mode instruments dynamic allocas. */
721736
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
@@ -728,7 +743,9 @@ static void kasan_alloca_oob_right(struct kunit *test)
728743
{
729744
volatile int i = 10;
730745
char alloca_array[i];
731-
char *p = alloca_array + i;
746+
/* See comment in kasan_global_oob. */
747+
char *volatile array = alloca_array;
748+
char *p = array + i;
732749

733750
/* Only generic mode instruments dynamic allocas. */
734751
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);

0 commit comments

Comments
 (0)