@@ -216,6 +216,12 @@ static void kmalloc_oob_16(struct kunit *test)
216
216
u64 words [2 ];
217
217
} * ptr1 , * ptr2 ;
218
218
219
+ /* This test is specifically crafted for the generic mode. */
220
+ if (!IS_ENABLED (CONFIG_KASAN_GENERIC )) {
221
+ kunit_info (test , "CONFIG_KASAN_GENERIC required\n" );
222
+ return ;
223
+ }
224
+
219
225
ptr1 = kmalloc (sizeof (* ptr1 ) - 3 , GFP_KERNEL );
220
226
KUNIT_ASSERT_NOT_ERR_OR_NULL (test , ptr1 );
221
227
@@ -227,6 +233,23 @@ static void kmalloc_oob_16(struct kunit *test)
227
233
kfree (ptr2 );
228
234
}
229
235
236
+ static void kmalloc_uaf_16 (struct kunit * test )
237
+ {
238
+ struct {
239
+ u64 words [2 ];
240
+ } * ptr1 , * ptr2 ;
241
+
242
+ ptr1 = kmalloc (sizeof (* ptr1 ), GFP_KERNEL );
243
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , ptr1 );
244
+
245
+ ptr2 = kmalloc (sizeof (* ptr2 ), GFP_KERNEL );
246
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , ptr2 );
247
+ kfree (ptr2 );
248
+
249
+ KUNIT_EXPECT_KASAN_FAIL (test , * ptr1 = * ptr2 );
250
+ kfree (ptr1 );
251
+ }
252
+
230
253
static void kmalloc_oob_memset_2 (struct kunit * test )
231
254
{
232
255
char * ptr ;
@@ -429,6 +452,12 @@ static void kasan_global_oob(struct kunit *test)
429
452
volatile int i = 3 ;
430
453
char * p = & global_array [ARRAY_SIZE (global_array ) + i ];
431
454
455
+ /* Only generic mode instruments globals. */
456
+ if (!IS_ENABLED (CONFIG_KASAN_GENERIC )) {
457
+ kunit_info (test , "CONFIG_KASAN_GENERIC required" );
458
+ return ;
459
+ }
460
+
432
461
KUNIT_EXPECT_KASAN_FAIL (test , * (volatile char * )p );
433
462
}
434
463
@@ -467,6 +496,12 @@ static void kasan_alloca_oob_left(struct kunit *test)
467
496
char alloca_array [i ];
468
497
char * p = alloca_array - 1 ;
469
498
499
+ /* Only generic mode instruments dynamic allocas. */
500
+ if (!IS_ENABLED (CONFIG_KASAN_GENERIC )) {
501
+ kunit_info (test , "CONFIG_KASAN_GENERIC required" );
502
+ return ;
503
+ }
504
+
470
505
if (!IS_ENABLED (CONFIG_KASAN_STACK )) {
471
506
kunit_info (test , "CONFIG_KASAN_STACK is not enabled" );
472
507
return ;
@@ -481,6 +516,12 @@ static void kasan_alloca_oob_right(struct kunit *test)
481
516
char alloca_array [i ];
482
517
char * p = alloca_array + i ;
483
518
519
+ /* Only generic mode instruments dynamic allocas. */
520
+ if (!IS_ENABLED (CONFIG_KASAN_GENERIC )) {
521
+ kunit_info (test , "CONFIG_KASAN_GENERIC required" );
522
+ return ;
523
+ }
524
+
484
525
if (!IS_ENABLED (CONFIG_KASAN_STACK )) {
485
526
kunit_info (test , "CONFIG_KASAN_STACK is not enabled" );
486
527
return ;
@@ -551,6 +592,9 @@ static void kasan_memchr(struct kunit *test)
551
592
return ;
552
593
}
553
594
595
+ if (OOB_TAG_OFF )
596
+ size = round_up (size , OOB_TAG_OFF );
597
+
554
598
ptr = kmalloc (size , GFP_KERNEL | __GFP_ZERO );
555
599
KUNIT_ASSERT_NOT_ERR_OR_NULL (test , ptr );
556
600
@@ -573,6 +617,9 @@ static void kasan_memcmp(struct kunit *test)
573
617
return ;
574
618
}
575
619
620
+ if (OOB_TAG_OFF )
621
+ size = round_up (size , OOB_TAG_OFF );
622
+
576
623
ptr = kmalloc (size , GFP_KERNEL | __GFP_ZERO );
577
624
KUNIT_ASSERT_NOT_ERR_OR_NULL (test , ptr );
578
625
memset (arr , 0 , sizeof (arr ));
@@ -619,69 +666,85 @@ static void kasan_strings(struct kunit *test)
619
666
KUNIT_EXPECT_KASAN_FAIL (test , kasan_int_result = strnlen (ptr , 1 ));
620
667
}
621
668
622
- static void kasan_bitops (struct kunit * test )
669
+ static void kasan_bitops_modify (struct kunit * test , int nr , void * addr )
670
+ {
671
+ KUNIT_EXPECT_KASAN_FAIL (test , set_bit (nr , addr ));
672
+ KUNIT_EXPECT_KASAN_FAIL (test , __set_bit (nr , addr ));
673
+ KUNIT_EXPECT_KASAN_FAIL (test , clear_bit (nr , addr ));
674
+ KUNIT_EXPECT_KASAN_FAIL (test , __clear_bit (nr , addr ));
675
+ KUNIT_EXPECT_KASAN_FAIL (test , clear_bit_unlock (nr , addr ));
676
+ KUNIT_EXPECT_KASAN_FAIL (test , __clear_bit_unlock (nr , addr ));
677
+ KUNIT_EXPECT_KASAN_FAIL (test , change_bit (nr , addr ));
678
+ KUNIT_EXPECT_KASAN_FAIL (test , __change_bit (nr , addr ));
679
+ }
680
+
681
+ static void kasan_bitops_test_and_modify (struct kunit * test , int nr , void * addr )
682
+ {
683
+ KUNIT_EXPECT_KASAN_FAIL (test , test_and_set_bit (nr , addr ));
684
+ KUNIT_EXPECT_KASAN_FAIL (test , __test_and_set_bit (nr , addr ));
685
+ KUNIT_EXPECT_KASAN_FAIL (test , test_and_set_bit_lock (nr , addr ));
686
+ KUNIT_EXPECT_KASAN_FAIL (test , test_and_clear_bit (nr , addr ));
687
+ KUNIT_EXPECT_KASAN_FAIL (test , __test_and_clear_bit (nr , addr ));
688
+ KUNIT_EXPECT_KASAN_FAIL (test , test_and_change_bit (nr , addr ));
689
+ KUNIT_EXPECT_KASAN_FAIL (test , __test_and_change_bit (nr , addr ));
690
+ KUNIT_EXPECT_KASAN_FAIL (test , kasan_int_result = test_bit (nr , addr ));
691
+
692
+ #if defined(clear_bit_unlock_is_negative_byte )
693
+ KUNIT_EXPECT_KASAN_FAIL (test , kasan_int_result =
694
+ clear_bit_unlock_is_negative_byte (nr , addr ));
695
+ #endif
696
+ }
697
+
698
+ static void kasan_bitops_generic (struct kunit * test )
623
699
{
700
+ long * bits ;
701
+
702
+ /* This test is specifically crafted for the generic mode. */
703
+ if (!IS_ENABLED (CONFIG_KASAN_GENERIC )) {
704
+ kunit_info (test , "CONFIG_KASAN_GENERIC required\n" );
705
+ return ;
706
+ }
707
+
624
708
/*
625
709
* Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
626
710
* this way we do not actually corrupt other memory.
627
711
*/
628
- long * bits = kzalloc (sizeof (* bits ) + 1 , GFP_KERNEL );
712
+ bits = kzalloc (sizeof (* bits ) + 1 , GFP_KERNEL );
629
713
KUNIT_ASSERT_NOT_ERR_OR_NULL (test , bits );
630
714
631
715
/*
632
716
* Below calls try to access bit within allocated memory; however, the
633
717
* below accesses are still out-of-bounds, since bitops are defined to
634
718
* operate on the whole long the bit is in.
635
719
*/
636
- KUNIT_EXPECT_KASAN_FAIL (test , set_bit (BITS_PER_LONG , bits ));
637
-
638
- KUNIT_EXPECT_KASAN_FAIL (test , __set_bit (BITS_PER_LONG , bits ));
639
-
640
- KUNIT_EXPECT_KASAN_FAIL (test , clear_bit (BITS_PER_LONG , bits ));
641
-
642
- KUNIT_EXPECT_KASAN_FAIL (test , __clear_bit (BITS_PER_LONG , bits ));
643
-
644
- KUNIT_EXPECT_KASAN_FAIL (test , clear_bit_unlock (BITS_PER_LONG , bits ));
645
-
646
- KUNIT_EXPECT_KASAN_FAIL (test , __clear_bit_unlock (BITS_PER_LONG , bits ));
647
-
648
- KUNIT_EXPECT_KASAN_FAIL (test , change_bit (BITS_PER_LONG , bits ));
649
-
650
- KUNIT_EXPECT_KASAN_FAIL (test , __change_bit (BITS_PER_LONG , bits ));
720
+ kasan_bitops_modify (test , BITS_PER_LONG , bits );
651
721
652
722
/*
653
723
* Below calls try to access bit beyond allocated memory.
654
724
*/
655
- KUNIT_EXPECT_KASAN_FAIL (test ,
656
- test_and_set_bit (BITS_PER_LONG + BITS_PER_BYTE , bits ));
657
-
658
- KUNIT_EXPECT_KASAN_FAIL (test ,
659
- __test_and_set_bit (BITS_PER_LONG + BITS_PER_BYTE , bits ));
660
-
661
- KUNIT_EXPECT_KASAN_FAIL (test ,
662
- test_and_set_bit_lock (BITS_PER_LONG + BITS_PER_BYTE , bits ));
725
+ kasan_bitops_test_and_modify (test , BITS_PER_LONG + BITS_PER_BYTE , bits );
663
726
664
- KUNIT_EXPECT_KASAN_FAIL ( test ,
665
- test_and_clear_bit ( BITS_PER_LONG + BITS_PER_BYTE , bits ));
727
+ kfree ( bits );
728
+ }
666
729
667
- KUNIT_EXPECT_KASAN_FAIL (test ,
668
- __test_and_clear_bit (BITS_PER_LONG + BITS_PER_BYTE , bits ));
730
+ static void kasan_bitops_tags (struct kunit * test )
731
+ {
732
+ long * bits ;
669
733
670
- KUNIT_EXPECT_KASAN_FAIL (test ,
671
- test_and_change_bit (BITS_PER_LONG + BITS_PER_BYTE , bits ));
734
+ /* This test is specifically crafted for the tag-based mode. */
735
+ if (IS_ENABLED (CONFIG_KASAN_GENERIC )) {
736
+ kunit_info (test , "CONFIG_KASAN_SW_TAGS required\n" );
737
+ return ;
738
+ }
672
739
673
- KUNIT_EXPECT_KASAN_FAIL (test ,
674
- __test_and_change_bit (BITS_PER_LONG + BITS_PER_BYTE , bits ));
740
+ /* Allocation size will be rounded to up granule size, which is 16. */
741
+ bits = kzalloc (sizeof (* bits ), GFP_KERNEL );
742
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , bits );
675
743
676
- KUNIT_EXPECT_KASAN_FAIL ( test ,
677
- kasan_int_result =
678
- test_bit ( BITS_PER_LONG + BITS_PER_BYTE , bits ) );
744
+ /* Do the accesses past the 16 allocated bytes. */
745
+ kasan_bitops_modify ( test , BITS_PER_LONG , & bits [ 1 ]);
746
+ kasan_bitops_test_and_modify ( test , BITS_PER_LONG + BITS_PER_BYTE , & bits [ 1 ] );
679
747
680
- #if defined(clear_bit_unlock_is_negative_byte )
681
- KUNIT_EXPECT_KASAN_FAIL (test ,
682
- kasan_int_result = clear_bit_unlock_is_negative_byte (
683
- BITS_PER_LONG + BITS_PER_BYTE , bits ));
684
- #endif
685
748
kfree (bits );
686
749
}
687
750
@@ -728,6 +791,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {
728
791
KUNIT_CASE (kmalloc_oob_krealloc_more ),
729
792
KUNIT_CASE (kmalloc_oob_krealloc_less ),
730
793
KUNIT_CASE (kmalloc_oob_16 ),
794
+ KUNIT_CASE (kmalloc_uaf_16 ),
731
795
KUNIT_CASE (kmalloc_oob_in_memset ),
732
796
KUNIT_CASE (kmalloc_oob_memset_2 ),
733
797
KUNIT_CASE (kmalloc_oob_memset_4 ),
@@ -751,7 +815,8 @@ static struct kunit_case kasan_kunit_test_cases[] = {
751
815
KUNIT_CASE (kasan_memchr ),
752
816
KUNIT_CASE (kasan_memcmp ),
753
817
KUNIT_CASE (kasan_strings ),
754
- KUNIT_CASE (kasan_bitops ),
818
+ KUNIT_CASE (kasan_bitops_generic ),
819
+ KUNIT_CASE (kasan_bitops_tags ),
755
820
KUNIT_CASE (kmalloc_double_kzfree ),
756
821
KUNIT_CASE (vmalloc_oob ),
757
822
{}
0 commit comments