Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kasan: rework krealloc tests

This patch reworks KASAN-KUnit tests for krealloc() to:

1. Check both slab and page_alloc based krealloc() implementations.
2. Allow at least one full granule to fit between old and new sizes for
each KASAN mode, and check accesses to that granule accordingly.

Link: https://lkml.kernel.org/r/c707f128a2bb9f2f05185d1eb52192cf179cf4fa.1612546384.git.andreyknvl@google.com
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Reviewed-by: Marco Elver <elver@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Branislav Rankov <Branislav.Rankov@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Peter Collingbourne <pcc@google.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andrey Konovalov and committed by
Linus Torvalds
b87c28b9 200072ce

+81 -10
+81 -10
lib/test_kasan.c
··· 252 252 kfree(ptr); 253 253 } 254 254 255 - static void kmalloc_oob_krealloc_more(struct kunit *test) 255 + static void krealloc_more_oob_helper(struct kunit *test, 256 + size_t size1, size_t size2) 256 257 { 257 258 char *ptr1, *ptr2; 258 - size_t size1 = 17; 259 - size_t size2 = 19; 259 + size_t middle; 260 + 261 + KUNIT_ASSERT_LT(test, size1, size2); 262 + middle = size1 + (size2 - size1) / 2; 260 263 261 264 ptr1 = kmalloc(size1, GFP_KERNEL); 262 265 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); ··· 267 264 ptr2 = krealloc(ptr1, size2, GFP_KERNEL); 268 265 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); 269 266 270 - KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x'); 267 + /* All offsets up to size2 must be accessible. */ 268 + ptr2[size1 - 1] = 'x'; 269 + ptr2[size1] = 'x'; 270 + ptr2[middle] = 'x'; 271 + ptr2[size2 - 1] = 'x'; 272 + 273 + /* Generic mode is precise, so unaligned size2 must be inaccessible. */ 274 + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) 275 + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x'); 276 + 277 + /* For all modes first aligned offset after size2 must be inaccessible. */ 278 + KUNIT_EXPECT_KASAN_FAIL(test, 279 + ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x'); 280 + 271 281 kfree(ptr2); 272 282 } 273 283 274 - static void kmalloc_oob_krealloc_less(struct kunit *test) 284 + static void krealloc_less_oob_helper(struct kunit *test, 285 + size_t size1, size_t size2) 275 286 { 276 287 char *ptr1, *ptr2; 277 - size_t size1 = 17; 278 - size_t size2 = 15; 288 + size_t middle; 289 + 290 + KUNIT_ASSERT_LT(test, size2, size1); 291 + middle = size2 + (size1 - size2) / 2; 279 292 280 293 ptr1 = kmalloc(size1, GFP_KERNEL); 281 294 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); ··· 299 280 ptr2 = krealloc(ptr1, size2, GFP_KERNEL); 300 281 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); 301 282 302 - KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x'); 283 + /* Must be accessible for all modes. */ 284 + ptr2[size2 - 1] = 'x'; 285 + 286 + /* Generic mode is precise, so unaligned size2 must be inaccessible. */ 287 + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) 288 + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x'); 289 + 290 + /* For all modes first aligned offset after size2 must be inaccessible. */ 291 + KUNIT_EXPECT_KASAN_FAIL(test, 292 + ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x'); 293 + 294 + /* 295 + * For all modes all size2, middle, and size1 should land in separate 296 + * granules and thus the latter two offsets should be inaccessible. 297 + */ 298 + KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE), 299 + round_down(middle, KASAN_GRANULE_SIZE)); 300 + KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE), 301 + round_down(size1, KASAN_GRANULE_SIZE)); 302 + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x'); 303 + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x'); 304 + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x'); 305 + 303 306 kfree(ptr2); 307 + } 308 + 309 + static void krealloc_more_oob(struct kunit *test) 310 + { 311 + krealloc_more_oob_helper(test, 201, 235); 312 + } 313 + 314 + static void krealloc_less_oob(struct kunit *test) 315 + { 316 + krealloc_less_oob_helper(test, 235, 201); 317 + } 318 + 319 + static void krealloc_pagealloc_more_oob(struct kunit *test) 320 + { 321 + /* page_alloc fallback in only implemented for SLUB. */ 322 + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); 323 + 324 + krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201, 325 + KMALLOC_MAX_CACHE_SIZE + 235); 326 + } 327 + 328 + static void krealloc_pagealloc_less_oob(struct kunit *test) 329 + { 330 + /* page_alloc fallback in only implemented for SLUB. */ 331 + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); 332 + 333 + krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235, 334 + KMALLOC_MAX_CACHE_SIZE + 201); 304 335 } 305 336 306 337 static void kmalloc_oob_16(struct kunit *test) ··· 1046 977 KUNIT_CASE(pagealloc_oob_right), 1047 978 KUNIT_CASE(pagealloc_uaf), 1048 979 KUNIT_CASE(kmalloc_large_oob_right), 1049 - KUNIT_CASE(kmalloc_oob_krealloc_more), 1050 - KUNIT_CASE(kmalloc_oob_krealloc_less), 980 + KUNIT_CASE(krealloc_more_oob), 981 + KUNIT_CASE(krealloc_less_oob), 982 + KUNIT_CASE(krealloc_pagealloc_more_oob), 983 + KUNIT_CASE(krealloc_pagealloc_less_oob), 1051 984 KUNIT_CASE(kmalloc_oob_16), 1052 985 KUNIT_CASE(kmalloc_uaf_16), 1053 986 KUNIT_CASE(kmalloc_oob_in_memset),