Skip to content

Commit fe5c0a6

Browse files
pccgregkh
authored andcommitted
kasan: fix unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled
commit f649dc0 upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 140cfd9 commit fe5c0a6

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
@@ -449,8 +449,20 @@ static char global_array[10];
449449

450450
static void kasan_global_oob(struct kunit *test)
451451
{
452-
volatile int i = 3;
453-
char *p = &global_array[ARRAY_SIZE(global_array) + i];
452+
/*
453+
* Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
454+
* from failing here and panicing the kernel, access the array via a
455+
* volatile pointer, which will prevent the compiler from being able to
456+
* determine the array bounds.
457+
*
458+
* This access uses a volatile pointer to char (char *volatile) rather
459+
* than the more conventional pointer to volatile char (volatile char *)
460+
* because we want to prevent the compiler from making inferences about
461+
* the pointer itself (i.e. its array bounds), not the data that it
462+
* refers to.
463+
*/
464+
char *volatile array = global_array;
465+
char *p = &array[ARRAY_SIZE(global_array) + 3];
454466

455467
/* Only generic mode instruments globals. */
456468
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
@@ -479,8 +491,9 @@ static void ksize_unpoisons_memory(struct kunit *test)
479491
static void kasan_stack_oob(struct kunit *test)
480492
{
481493
char stack_array[10];
482-
volatile int i = OOB_TAG_OFF;
483-
char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
494+
/* See comment in kasan_global_oob. */
495+
char *volatile array = stack_array;
496+
char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
484497

485498
if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
486499
kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
@@ -494,7 +507,9 @@ static void kasan_alloca_oob_left(struct kunit *test)
494507
{
495508
volatile int i = 10;
496509
char alloca_array[i];
497-
char *p = alloca_array - 1;
510+
/* See comment in kasan_global_oob. */
511+
char *volatile array = alloca_array;
512+
char *p = array - 1;
498513

499514
/* Only generic mode instruments dynamic allocas. */
500515
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
@@ -514,7 +529,9 @@ static void kasan_alloca_oob_right(struct kunit *test)
514529
{
515530
volatile int i = 10;
516531
char alloca_array[i];
517-
char *p = alloca_array + i;
532+
/* See comment in kasan_global_oob. */
533+
char *volatile array = alloca_array;
534+
char *p = array + i;
518535

519536
/* Only generic mode instruments dynamic allocas. */
520537
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {

0 commit comments

Comments
 (0)