1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Runtime test cases for CONFIG_FORTIFY_SOURCE. For additional memcpy() 4 * testing see FORTIFY_MEM_* tests in LKDTM (drivers/misc/lkdtm/fortify.c). 5 * 6 * For corner cases with UBSAN, try testing with: 7 * 8 * ./tools/testing/kunit/kunit.py run --arch=x86_64 \ 9 * --kconfig_add CONFIG_FORTIFY_SOURCE=y \ 10 * --kconfig_add CONFIG_UBSAN=y \ 11 * --kconfig_add CONFIG_UBSAN_TRAP=y \ 12 * --kconfig_add CONFIG_UBSAN_BOUNDS=y \ 13 * --kconfig_add CONFIG_UBSAN_LOCAL_BOUNDS=y \ 14 * --make_options LLVM=1 fortify 15 */ 16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 17 18 /* We don't need to fill dmesg with the fortify WARNs during testing. */ 19 #ifdef DEBUG 20 # define FORTIFY_REPORT_KUNIT(x...) __fortify_report(x) 21 # define FORTIFY_WARN_KUNIT(x...) WARN_ONCE(x) 22 #else 23 # define FORTIFY_REPORT_KUNIT(x...) do { } while (0) 24 # define FORTIFY_WARN_KUNIT(x...) do { } while (0) 25 #endif 26 27 /* Redefine fortify_panic() to track failures. */ 28 void fortify_add_kunit_error(int write); 29 #define fortify_panic(func, write, avail, size, retfail) do { \ 30 FORTIFY_REPORT_KUNIT(FORTIFY_REASON(func, write), avail, size); \ 31 fortify_add_kunit_error(write); \ 32 return (retfail); \ 33 } while (0) 34 35 /* Redefine fortify_warn_once() to track memcpy() failures. */ 36 #define fortify_warn_once(chk_func, x...) do { \ 37 bool __result = chk_func; \ 38 FORTIFY_WARN_KUNIT(__result, x); \ 39 if (__result) \ 40 fortify_add_kunit_error(1); \ 41 } while (0) 42 43 #include <kunit/device.h> 44 #include <kunit/test.h> 45 #include <kunit/test-bug.h> 46 #include <linux/device.h> 47 #include <linux/slab.h> 48 #include <linux/string.h> 49 #include <linux/vmalloc.h> 50 51 /* Handle being built without CONFIG_FORTIFY_SOURCE */ 52 #ifndef __compiletime_strlen 53 # define __compiletime_strlen __builtin_strlen 54 #endif 55 56 static struct kunit_resource read_resource; 57 static struct kunit_resource write_resource; 58 static int fortify_read_overflows; 59 static int fortify_write_overflows; 60 61 static const char array_of_10[] = "this is 10"; 62 static const char *ptr_of_11 = "this is 11!"; 63 static const char * const unchanging_12 = "this is 12!!"; 64 static char array_unknown[] = "compiler thinks I might change"; 65 66 void fortify_add_kunit_error(int write) 67 { 68 struct kunit_resource *resource; 69 struct kunit *current_test; 70 71 current_test = kunit_get_current_test(); 72 if (!current_test) 73 return; 74 75 resource = kunit_find_named_resource(current_test, 76 write ? "fortify_write_overflows" 77 : "fortify_read_overflows"); 78 if (!resource) 79 return; 80 81 (*(int *)resource->data)++; 82 kunit_put_resource(resource); 83 } 84 85 static void fortify_test_known_sizes(struct kunit *test) 86 { 87 char stack[80] = "Test!"; 88 89 KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(stack))); 90 KUNIT_EXPECT_EQ(test, __compiletime_strlen(stack), 5); 91 92 KUNIT_EXPECT_TRUE(test, __is_constexpr(__builtin_strlen("88888888"))); 93 KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8); 94 95 KUNIT_EXPECT_TRUE(test, __is_constexpr(__builtin_strlen(array_of_10))); 96 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10); 97 98 KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(ptr_of_11))); 99 KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11); 100 101 KUNIT_EXPECT_TRUE(test, __is_constexpr(__builtin_strlen(unchanging_12))); 102 KUNIT_EXPECT_EQ(test, __compiletime_strlen(unchanging_12), 12); 103 104 KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(array_unknown))); 105 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX); 106 107 /* Externally defined and dynamically sized string pointer: */ 108 KUNIT_EXPECT_FALSE(test, __is_constexpr(__builtin_strlen(test->name))); 109 KUNIT_EXPECT_EQ(test, __compiletime_strlen(test->name), SIZE_MAX); 110 } 111 112 /* This is volatile so the optimizer can't perform DCE below. */ 113 static volatile int pick; 114 115 /* Not inline to keep optimizer from figuring out which string we want. */ 116 static noinline size_t want_minus_one(int pick) 117 { 118 const char *str; 119 120 switch (pick) { 121 case 1: 122 str = "4444"; 123 break; 124 case 2: 125 str = "333"; 126 break; 127 default: 128 str = "1"; 129 break; 130 } 131 return __compiletime_strlen(str); 132 } 133 134 static void fortify_test_control_flow_split(struct kunit *test) 135 { 136 KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX); 137 } 138 139 #define KUNIT_EXPECT_BOS(test, p, expected, name) \ 140 KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \ 141 expected, \ 142 "__alloc_size() not working with __bos on " name "\n") 143 144 #if !__has_builtin(__builtin_dynamic_object_size) 145 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \ 146 /* Silence "unused variable 'expected'" warning. */ \ 147 KUNIT_EXPECT_EQ(test, expected, expected) 148 #else 149 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \ 150 KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \ 151 expected, \ 152 "__alloc_size() not working with __bdos on " name "\n") 153 #endif 154 155 /* If the execpted size is a constant value, __bos can see it. */ 156 #define check_const(_expected, alloc, free) do { \ 157 size_t expected = (_expected); \ 158 void *p = alloc; \ 159 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \ 160 KUNIT_EXPECT_BOS(test, p, expected, #alloc); \ 161 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \ 162 free; \ 163 } while (0) 164 165 /* If the execpted size is NOT a constant value, __bos CANNOT see it. */ 166 #define check_dynamic(_expected, alloc, free) do { \ 167 size_t expected = (_expected); \ 168 void *p = alloc; \ 169 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \ 170 KUNIT_EXPECT_BOS(test, p, SIZE_MAX, #alloc); \ 171 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \ 172 free; \ 173 } while (0) 174 175 /* Assortment of constant-value kinda-edge cases. */ 176 #define CONST_TEST_BODY(TEST_alloc) do { \ 177 /* Special-case vmalloc()-family to skip 0-sized allocs. */ \ 178 if (strcmp(#TEST_alloc, "TEST_vmalloc") != 0) \ 179 TEST_alloc(check_const, 0, 0); \ 180 TEST_alloc(check_const, 1, 1); \ 181 TEST_alloc(check_const, 128, 128); \ 182 TEST_alloc(check_const, 1023, 1023); \ 183 TEST_alloc(check_const, 1025, 1025); \ 184 TEST_alloc(check_const, 4096, 4096); \ 185 TEST_alloc(check_const, 4097, 4097); \ 186 } while (0) 187 188 static volatile size_t zero_size; 189 static volatile size_t unknown_size = 50; 190 191 #if !__has_builtin(__builtin_dynamic_object_size) 192 #define DYNAMIC_TEST_BODY(TEST_alloc) \ 193 kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() support\n") 194 #else 195 #define DYNAMIC_TEST_BODY(TEST_alloc) do { \ 196 size_t size = unknown_size; \ 197 \ 198 /* \ 199 * Expected size is "size" in each test, before it is then \ 200 * internally incremented in each test. Requires we disable \ 201 * -Wunsequenced. \ 202 */ \ 203 TEST_alloc(check_dynamic, size, size++); \ 204 /* Make sure incrementing actually happened. */ \ 205 KUNIT_EXPECT_NE(test, size, unknown_size); \ 206 } while (0) 207 #endif 208 209 #define DEFINE_ALLOC_SIZE_TEST_PAIR(allocator) \ 210 static void fortify_test_alloc_size_##allocator##_const(struct kunit *test) \ 211 { \ 212 CONST_TEST_BODY(TEST_##allocator); \ 213 } \ 214 static void fortify_test_alloc_size_##allocator##_dynamic(struct kunit *test) \ 215 { \ 216 DYNAMIC_TEST_BODY(TEST_##allocator); \ 217 } 218 219 #define TEST_kmalloc(checker, expected_size, alloc_size) do { \ 220 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \ 221 void *orig; \ 222 size_t len; \ 223 \ 224 checker(expected_size, kmalloc(alloc_size, gfp), \ 225 kfree(p)); \ 226 checker(expected_size, \ 227 kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \ 228 kfree(p)); \ 229 checker(expected_size, kzalloc(alloc_size, gfp), \ 230 kfree(p)); \ 231 checker(expected_size, \ 232 kzalloc_node(alloc_size, gfp, NUMA_NO_NODE), \ 233 kfree(p)); \ 234 checker(expected_size, kcalloc(1, alloc_size, gfp), \ 235 kfree(p)); \ 236 checker(expected_size, kcalloc(alloc_size, 1, gfp), \ 237 kfree(p)); \ 238 checker(expected_size, \ 239 kcalloc_node(1, alloc_size, gfp, NUMA_NO_NODE), \ 240 kfree(p)); \ 241 checker(expected_size, \ 242 kcalloc_node(alloc_size, 1, gfp, NUMA_NO_NODE), \ 243 kfree(p)); \ 244 checker(expected_size, kmalloc_array(1, alloc_size, gfp), \ 245 kfree(p)); \ 246 checker(expected_size, kmalloc_array(alloc_size, 1, gfp), \ 247 kfree(p)); \ 248 checker(expected_size, \ 249 kmalloc_array_node(1, alloc_size, gfp, NUMA_NO_NODE), \ 250 kfree(p)); \ 251 checker(expected_size, \ 252 kmalloc_array_node(alloc_size, 1, gfp, NUMA_NO_NODE), \ 253 kfree(p)); \ 254 \ 255 orig = kmalloc(alloc_size, gfp); \ 256 KUNIT_EXPECT_TRUE(test, orig != NULL); \ 257 checker((expected_size) * 2, \ 258 krealloc(orig, (alloc_size) * 2, gfp), \ 259 kfree(p)); \ 260 orig = kmalloc(alloc_size, gfp); \ 261 KUNIT_EXPECT_TRUE(test, orig != NULL); \ 262 checker((expected_size) * 2, \ 263 krealloc_array(orig, 1, (alloc_size) * 2, gfp), \ 264 kfree(p)); \ 265 orig = kmalloc(alloc_size, gfp); \ 266 KUNIT_EXPECT_TRUE(test, orig != NULL); \ 267 checker((expected_size) * 2, \ 268 krealloc_array(orig, (alloc_size) * 2, 1, gfp), \ 269 kfree(p)); \ 270 \ 271 len = 11; \ 272 /* Using memdup() with fixed size, so force unknown length. */ \ 273 if (!__builtin_constant_p(expected_size)) \ 274 len += zero_size; \ 275 checker(len, kmemdup("hello there", len, gfp), kfree(p)); \ 276 } while (0) 277 DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc) 278 279 /* Sizes are in pages, not bytes. */ 280 #define TEST_vmalloc(checker, expected_pages, alloc_pages) do { \ 281 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \ 282 checker((expected_pages) * PAGE_SIZE, \ 283 vmalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \ 284 checker((expected_pages) * PAGE_SIZE, \ 285 vzalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \ 286 checker((expected_pages) * PAGE_SIZE, \ 287 __vmalloc((alloc_pages) * PAGE_SIZE, gfp), vfree(p)); \ 288 } while (0) 289 DEFINE_ALLOC_SIZE_TEST_PAIR(vmalloc) 290 291 /* Sizes are in pages (and open-coded for side-effects), not bytes. */ 292 #define TEST_kvmalloc(checker, expected_pages, alloc_pages) do { \ 293 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \ 294 size_t prev_size; \ 295 void *orig; \ 296 \ 297 checker((expected_pages) * PAGE_SIZE, \ 298 kvmalloc((alloc_pages) * PAGE_SIZE, gfp), \ 299 kvfree(p)); \ 300 checker((expected_pages) * PAGE_SIZE, \ 301 kvmalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \ 302 kvfree(p)); \ 303 checker((expected_pages) * PAGE_SIZE, \ 304 kvzalloc((alloc_pages) * PAGE_SIZE, gfp), \ 305 kvfree(p)); \ 306 checker((expected_pages) * PAGE_SIZE, \ 307 kvzalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \ 308 kvfree(p)); \ 309 checker((expected_pages) * PAGE_SIZE, \ 310 kvcalloc(1, (alloc_pages) * PAGE_SIZE, gfp), \ 311 kvfree(p)); \ 312 checker((expected_pages) * PAGE_SIZE, \ 313 kvcalloc((alloc_pages) * PAGE_SIZE, 1, gfp), \ 314 kvfree(p)); \ 315 checker((expected_pages) * PAGE_SIZE, \ 316 kvmalloc_array(1, (alloc_pages) * PAGE_SIZE, gfp), \ 317 kvfree(p)); \ 318 checker((expected_pages) * PAGE_SIZE, \ 319 kvmalloc_array((alloc_pages) * PAGE_SIZE, 1, gfp), \ 320 kvfree(p)); \ 321 \ 322 prev_size = (expected_pages) * PAGE_SIZE; \ 323 orig = kvmalloc(prev_size, gfp); \ 324 KUNIT_EXPECT_TRUE(test, orig != NULL); \ 325 checker(((expected_pages) * PAGE_SIZE) * 2, \ 326 kvrealloc(orig, ((alloc_pages) * PAGE_SIZE) * 2, gfp), \ 327 kvfree(p)); \ 328 } while (0) 329 DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc) 330 331 #define TEST_devm_kmalloc(checker, expected_size, alloc_size) do { \ 332 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; \ 333 const char dev_name[] = "fortify-test"; \ 334 struct device *dev; \ 335 void *orig; \ 336 size_t len; \ 337 \ 338 /* Create dummy device for devm_kmalloc()-family tests. */ \ 339 dev = kunit_device_register(test, dev_name); \ 340 KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), \ 341 "Cannot register test device\n"); \ 342 \ 343 checker(expected_size, devm_kmalloc(dev, alloc_size, gfp), \ 344 devm_kfree(dev, p)); \ 345 checker(expected_size, devm_kzalloc(dev, alloc_size, gfp), \ 346 devm_kfree(dev, p)); \ 347 checker(expected_size, \ 348 devm_kmalloc_array(dev, 1, alloc_size, gfp), \ 349 devm_kfree(dev, p)); \ 350 checker(expected_size, \ 351 devm_kmalloc_array(dev, alloc_size, 1, gfp), \ 352 devm_kfree(dev, p)); \ 353 checker(expected_size, \ 354 devm_kcalloc(dev, 1, alloc_size, gfp), \ 355 devm_kfree(dev, p)); \ 356 checker(expected_size, \ 357 devm_kcalloc(dev, alloc_size, 1, gfp), \ 358 devm_kfree(dev, p)); \ 359 \ 360 orig = devm_kmalloc(dev, alloc_size, gfp); \ 361 KUNIT_EXPECT_TRUE(test, orig != NULL); \ 362 checker((expected_size) * 2, \ 363 devm_krealloc(dev, orig, (alloc_size) * 2, gfp), \ 364 devm_kfree(dev, p)); \ 365 \ 366 len = 4; \ 367 /* Using memdup() with fixed size, so force unknown length. */ \ 368 if (!__builtin_constant_p(expected_size)) \ 369 len += zero_size; \ 370 checker(len, devm_kmemdup(dev, "Ohai", len, gfp), \ 371 devm_kfree(dev, p)); \ 372 \ 373 kunit_device_unregister(test, dev); \ 374 } while (0) 375 DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc) 376 377 static const char * const test_strs[] = { 378 "", 379 "Hello there", 380 "A longer string, just for variety", 381 }; 382 383 #define TEST_realloc(checker) do { \ 384 gfp_t gfp = GFP_KERNEL; \ 385 size_t len; \ 386 int i; \ 387 \ 388 for (i = 0; i < ARRAY_SIZE(test_strs); i++) { \ 389 len = strlen(test_strs[i]); \ 390 KUNIT_EXPECT_EQ(test, __builtin_constant_p(len), 0); \ 391 checker(len, kmemdup_array(test_strs[i], 1, len, gfp), \ 392 kfree(p)); \ 393 checker(len, kmemdup(test_strs[i], len, gfp), \ 394 kfree(p)); \ 395 } \ 396 } while (0) 397 static void fortify_test_realloc_size(struct kunit *test) 398 { 399 TEST_realloc(check_dynamic); 400 } 401 402 /* 403 * We can't have an array at the end of a structure or else 404 * builds without -fstrict-flex-arrays=3 will report them as 405 * being an unknown length. Additionally, add bytes before 406 * and after the string to catch over/underflows if tests 407 * fail. 408 */ 409 struct fortify_padding { 410 unsigned long bytes_before; 411 char buf[32]; 412 unsigned long bytes_after; 413 }; 414 /* Force compiler into not being able to resolve size at compile-time. */ 415 static volatile int unconst; 416 417 static void fortify_test_strlen(struct kunit *test) 418 { 419 struct fortify_padding pad = { }; 420 int i, end = sizeof(pad.buf) - 1; 421 422 /* Fill 31 bytes with valid characters. */ 423 for (i = 0; i < sizeof(pad.buf) - 1; i++) 424 pad.buf[i] = i + '0'; 425 /* Trailing bytes are still %NUL. */ 426 KUNIT_EXPECT_EQ(test, pad.buf[end], '\0'); 427 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 428 429 /* String is terminated, so strlen() is valid. */ 430 KUNIT_EXPECT_EQ(test, strlen(pad.buf), end); 431 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 432 433 /* Make string unterminated, and recount. */ 434 pad.buf[end] = 'A'; 435 end = sizeof(pad.buf); 436 KUNIT_EXPECT_EQ(test, strlen(pad.buf), end); 437 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 438 } 439 440 static void fortify_test_strnlen(struct kunit *test) 441 { 442 struct fortify_padding pad = { }; 443 int i, end = sizeof(pad.buf) - 1; 444 445 /* Fill 31 bytes with valid characters. */ 446 for (i = 0; i < sizeof(pad.buf) - 1; i++) 447 pad.buf[i] = i + '0'; 448 /* Trailing bytes are still %NUL. */ 449 KUNIT_EXPECT_EQ(test, pad.buf[end], '\0'); 450 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 451 452 /* String is terminated, so strnlen() is valid. */ 453 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf)), end); 454 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 455 /* A truncated strnlen() will be safe, too. */ 456 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf) / 2), 457 sizeof(pad.buf) / 2); 458 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 459 460 /* Make string unterminated, and recount. */ 461 pad.buf[end] = 'A'; 462 end = sizeof(pad.buf); 463 /* Reading beyond with strncpy() will fail. */ 464 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 1), end); 465 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 466 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 2), end); 467 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 468 469 /* Early-truncated is safe still, though. */ 470 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end), end); 471 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 472 473 end = sizeof(pad.buf) / 2; 474 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end), end); 475 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 476 } 477 478 static void fortify_test_strcpy(struct kunit *test) 479 { 480 struct fortify_padding pad = { }; 481 char src[sizeof(pad.buf) + 1] = { }; 482 int i; 483 484 /* Fill 31 bytes with valid characters. */ 485 for (i = 0; i < sizeof(src) - 2; i++) 486 src[i] = i + '0'; 487 488 /* Destination is %NUL-filled to start with. */ 489 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); 490 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 491 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 492 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 493 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 494 495 /* Legitimate strcpy() 1 less than of max size. */ 496 KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src) 497 == pad.buf); 498 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 499 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 500 /* Only last byte should be %NUL */ 501 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 502 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 503 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 504 505 src[sizeof(src) - 2] = 'A'; 506 /* But now we trip the overflow checking. */ 507 KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src) 508 == pad.buf); 509 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 510 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); 511 /* Trailing %NUL -- thanks to FORTIFY. */ 512 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 513 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 514 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 515 /* And we will not have gone beyond. */ 516 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 517 518 src[sizeof(src) - 1] = 'A'; 519 /* And for sure now, two bytes past. */ 520 KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src) 521 == pad.buf); 522 /* 523 * Which trips both the strlen() on the unterminated src, 524 * and the resulting copy attempt. 525 */ 526 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 527 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); 528 /* Trailing %NUL -- thanks to FORTIFY. */ 529 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 530 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 531 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 532 /* And we will not have gone beyond. */ 533 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 534 } 535 536 static void fortify_test_strncpy(struct kunit *test) 537 { 538 struct fortify_padding pad = { }; 539 char src[] = "Copy me fully into a small buffer and I will overflow!"; 540 541 /* Destination is %NUL-filled to start with. */ 542 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); 543 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 544 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 545 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 546 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 547 548 /* Legitimate strncpy() 1 less than of max size. */ 549 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, 550 sizeof(pad.buf) + unconst - 1) 551 == pad.buf); 552 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 553 /* Only last byte should be %NUL */ 554 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 555 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 556 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 557 558 /* Legitimate (though unterminated) max-size strncpy. */ 559 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, 560 sizeof(pad.buf) + unconst) 561 == pad.buf); 562 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 563 /* No trailing %NUL -- thanks strncpy API. */ 564 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 565 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 566 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 567 /* But we will not have gone beyond. */ 568 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 569 570 /* Now verify that FORTIFY is working... */ 571 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, 572 sizeof(pad.buf) + unconst + 1) 573 == pad.buf); 574 /* Should catch the overflow. */ 575 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); 576 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 577 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 578 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 579 /* And we will not have gone beyond. */ 580 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 581 582 /* And further... */ 583 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, 584 sizeof(pad.buf) + unconst + 2) 585 == pad.buf); 586 /* Should catch the overflow. */ 587 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); 588 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 589 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 590 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 591 /* And we will not have gone beyond. */ 592 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 593 } 594 595 static void fortify_test_strscpy(struct kunit *test) 596 { 597 struct fortify_padding pad = { }; 598 char src[] = "Copy me fully into a small buffer and I will overflow!"; 599 600 /* Destination is %NUL-filled to start with. */ 601 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); 602 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 603 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 604 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 605 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 606 607 /* Legitimate strscpy() 1 less than of max size. */ 608 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, 609 sizeof(pad.buf) + unconst - 1), 610 -E2BIG); 611 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 612 /* Keeping space for %NUL, last two bytes should be %NUL */ 613 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 614 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 615 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 616 617 /* Legitimate max-size strscpy. */ 618 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, 619 sizeof(pad.buf) + unconst), 620 -E2BIG); 621 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 622 /* A trailing %NUL will exist. */ 623 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 624 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 625 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 626 627 /* Now verify that FORTIFY is working... */ 628 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, 629 sizeof(pad.buf) + unconst + 1), 630 -E2BIG); 631 /* Should catch the overflow. */ 632 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); 633 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 634 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 635 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 636 /* And we will not have gone beyond. */ 637 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 638 639 /* And much further... */ 640 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, 641 sizeof(src) * 2 + unconst), 642 -E2BIG); 643 /* Should catch the overflow. */ 644 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); 645 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 646 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 647 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 648 /* And we will not have gone beyond. */ 649 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 650 } 651 652 static void fortify_test_strcat(struct kunit *test) 653 { 654 struct fortify_padding pad = { }; 655 char src[sizeof(pad.buf) / 2] = { }; 656 char one[] = "A"; 657 char two[] = "BC"; 658 int i; 659 660 /* Fill 15 bytes with valid characters. */ 661 for (i = 0; i < sizeof(src) - 1; i++) 662 src[i] = i + 'A'; 663 664 /* Destination is %NUL-filled to start with. */ 665 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); 666 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 667 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 668 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 669 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 670 671 /* Legitimate strcat() using less than half max size. */ 672 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, src) == pad.buf); 673 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 674 /* Legitimate strcat() now 2 bytes shy of end. */ 675 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, src) == pad.buf); 676 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 677 /* Last two bytes should be %NUL */ 678 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 679 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 680 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 681 682 /* Add one more character to the end. */ 683 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, one) == pad.buf); 684 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 685 /* Last byte should be %NUL */ 686 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 687 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 688 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 689 690 /* And this one char will overflow. */ 691 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, one) == pad.buf); 692 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); 693 /* Last byte should be %NUL thanks to FORTIFY. */ 694 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 695 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 696 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 697 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 698 699 /* And adding two will overflow more. */ 700 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, two) == pad.buf); 701 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); 702 /* Last byte should be %NUL thanks to FORTIFY. */ 703 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 704 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 705 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 706 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 707 } 708 709 static void fortify_test_strncat(struct kunit *test) 710 { 711 struct fortify_padding pad = { }; 712 char src[sizeof(pad.buf)] = { }; 713 int i, partial; 714 715 /* Fill 31 bytes with valid characters. */ 716 partial = sizeof(src) / 2 - 1; 717 for (i = 0; i < partial; i++) 718 src[i] = i + 'A'; 719 720 /* Destination is %NUL-filled to start with. */ 721 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); 722 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 723 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 724 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 725 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 726 727 /* Legitimate strncat() using less than half max size. */ 728 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, partial) == pad.buf); 729 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 730 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 731 /* Legitimate strncat() now 2 bytes shy of end. */ 732 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, partial) == pad.buf); 733 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 734 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 735 /* Last two bytes should be %NUL */ 736 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 737 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 738 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 739 740 /* Add one more character to the end. */ 741 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf); 742 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 743 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 744 /* Last byte should be %NUL */ 745 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 746 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 747 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 748 749 /* And this one char will overflow. */ 750 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf); 751 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 752 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); 753 /* Last byte should be %NUL thanks to FORTIFY. */ 754 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 755 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 756 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 757 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 758 759 /* And adding two will overflow more. */ 760 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 2) == pad.buf); 761 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 762 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); 763 /* Last byte should be %NUL thanks to FORTIFY. */ 764 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 765 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 766 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 767 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 768 769 /* Force an unterminated destination, and overflow. */ 770 pad.buf[sizeof(pad.buf) - 1] = 'A'; 771 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf); 772 /* This will have tripped both strlen() and strcat(). */ 773 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 774 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 3); 775 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 776 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 777 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 778 /* But we should not go beyond the end. */ 779 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 780 } 781 782 static void fortify_test_strlcat(struct kunit *test) 783 { 784 struct fortify_padding pad = { }; 785 char src[sizeof(pad.buf)] = { }; 786 int i, partial; 787 int len = sizeof(pad.buf) + unconst; 788 789 /* Fill 15 bytes with valid characters. */ 790 partial = sizeof(src) / 2 - 1; 791 for (i = 0; i < partial; i++) 792 src[i] = i + 'A'; 793 794 /* Destination is %NUL-filled to start with. */ 795 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); 796 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 797 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 798 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 799 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 800 801 /* Legitimate strlcat() using less than half max size. */ 802 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len), partial); 803 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 804 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 805 /* Legitimate strlcat() now 2 bytes shy of end. */ 806 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len), partial * 2); 807 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 808 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 809 /* Last two bytes should be %NUL */ 810 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 811 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 812 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 813 814 /* Add one more character to the end. */ 815 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "Q", len), partial * 2 + 1); 816 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 817 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); 818 /* Last byte should be %NUL */ 819 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 820 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 821 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 822 823 /* And this one char will overflow. */ 824 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "V", len * 2), len); 825 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 826 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); 827 /* Last byte should be %NUL thanks to FORTIFY. */ 828 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 829 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 830 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 831 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 832 833 /* And adding two will overflow more. */ 834 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "QQ", len * 2), len + 1); 835 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 836 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); 837 /* Last byte should be %NUL thanks to FORTIFY. */ 838 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 839 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 840 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 841 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 842 843 /* Force an unterminated destination, and overflow. */ 844 pad.buf[sizeof(pad.buf) - 1] = 'A'; 845 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "TT", len * 2), len + 2); 846 /* This will have tripped both strlen() and strlcat(). */ 847 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 848 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); 849 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 850 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); 851 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); 852 /* But we should not go beyond the end. */ 853 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 854 855 /* Force an unterminated source, and overflow. */ 856 memset(src, 'B', sizeof(src)); 857 pad.buf[sizeof(pad.buf) - 1] = '\0'; 858 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len * 3), len - 1 + sizeof(src)); 859 /* This will have tripped both strlen() and strlcat(). */ 860 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 3); 861 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 3); 862 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); 863 /* But we should not go beyond the end. */ 864 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); 865 } 866 867 /* Check for 0-sized arrays... */ 868 struct fortify_zero_sized { 869 unsigned long bytes_before; 870 char buf[0]; 871 unsigned long bytes_after; 872 }; 873 874 #define __fortify_test(memfunc) \ 875 static void fortify_test_##memfunc(struct kunit *test) \ 876 { \ 877 struct fortify_zero_sized zero = { }; \ 878 struct fortify_padding pad = { }; \ 879 char srcA[sizeof(pad.buf) + 2]; \ 880 char srcB[sizeof(pad.buf) + 2]; \ 881 size_t len = sizeof(pad.buf) + unconst; \ 882 \ 883 memset(srcA, 'A', sizeof(srcA)); \ 884 KUNIT_ASSERT_EQ(test, srcA[0], 'A'); \ 885 memset(srcB, 'B', sizeof(srcB)); \ 886 KUNIT_ASSERT_EQ(test, srcB[0], 'B'); \ 887 \ 888 memfunc(pad.buf, srcA, 0 + unconst); \ 889 KUNIT_EXPECT_EQ(test, pad.buf[0], '\0'); \ 890 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 891 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \ 892 memfunc(pad.buf + 1, srcB, 1 + unconst); \ 893 KUNIT_EXPECT_EQ(test, pad.buf[0], '\0'); \ 894 KUNIT_EXPECT_EQ(test, pad.buf[1], 'B'); \ 895 KUNIT_EXPECT_EQ(test, pad.buf[2], '\0'); \ 896 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 897 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \ 898 memfunc(pad.buf, srcA, 1 + unconst); \ 899 KUNIT_EXPECT_EQ(test, pad.buf[0], 'A'); \ 900 KUNIT_EXPECT_EQ(test, pad.buf[1], 'B'); \ 901 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 902 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \ 903 memfunc(pad.buf, srcA, len - 1); \ 904 KUNIT_EXPECT_EQ(test, pad.buf[1], 'A'); \ 905 KUNIT_EXPECT_EQ(test, pad.buf[len - 1], '\0'); \ 906 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 907 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \ 908 memfunc(pad.buf, srcA, len); \ 909 KUNIT_EXPECT_EQ(test, pad.buf[1], 'A'); \ 910 KUNIT_EXPECT_EQ(test, pad.buf[len - 1], 'A'); \ 911 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); \ 912 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 913 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \ 914 memfunc(pad.buf, srcA, len + 1); \ 915 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 916 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); \ 917 memfunc(pad.buf + 1, srcB, len); \ 918 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 919 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); \ 920 \ 921 /* Reset error counter. */ \ 922 fortify_write_overflows = 0; \ 923 /* Copy nothing into nothing: no errors. */ \ 924 memfunc(zero.buf, srcB, 0 + unconst); \ 925 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 926 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \ 927 memfunc(zero.buf, srcB, 1 + unconst); \ 928 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \ 929 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); \ 930 } 931 __fortify_test(memcpy) 932 __fortify_test(memmove) 933 934 static void fortify_test_memscan(struct kunit *test) 935 { 936 char haystack[] = "Where oh where is my memory range?"; 937 char *mem = haystack + strlen("Where oh where is "); 938 char needle = 'm'; 939 size_t len = sizeof(haystack) + unconst; 940 941 KUNIT_ASSERT_PTR_EQ(test, memscan(haystack, needle, len), 942 mem); 943 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 944 /* Catch too-large range. */ 945 KUNIT_ASSERT_PTR_EQ(test, memscan(haystack, needle, len + 1), 946 NULL); 947 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 948 KUNIT_ASSERT_PTR_EQ(test, memscan(haystack, needle, len * 2), 949 NULL); 950 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 951 } 952 953 static void fortify_test_memchr(struct kunit *test) 954 { 955 char haystack[] = "Where oh where is my memory range?"; 956 char *mem = haystack + strlen("Where oh where is "); 957 char needle = 'm'; 958 size_t len = sizeof(haystack) + unconst; 959 960 KUNIT_ASSERT_PTR_EQ(test, memchr(haystack, needle, len), 961 mem); 962 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 963 /* Catch too-large range. */ 964 KUNIT_ASSERT_PTR_EQ(test, memchr(haystack, needle, len + 1), 965 NULL); 966 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 967 KUNIT_ASSERT_PTR_EQ(test, memchr(haystack, needle, len * 2), 968 NULL); 969 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 970 } 971 972 static void fortify_test_memchr_inv(struct kunit *test) 973 { 974 char haystack[] = "Where oh where is my memory range?"; 975 char *mem = haystack + 1; 976 char needle = 'W'; 977 size_t len = sizeof(haystack) + unconst; 978 979 /* Normal search is okay. */ 980 KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len), 981 mem); 982 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 983 /* Catch too-large range. */ 984 KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len + 1), 985 NULL); 986 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 987 KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len * 2), 988 NULL); 989 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 990 } 991 992 static void fortify_test_memcmp(struct kunit *test) 993 { 994 char one[] = "My mind is going ..."; 995 char two[] = "My mind is going ... I can feel it."; 996 size_t one_len = sizeof(one) + unconst - 1; 997 size_t two_len = sizeof(two) + unconst - 1; 998 999 /* We match the first string (ignoring the %NUL). */ 1000 KUNIT_ASSERT_EQ(test, memcmp(one, two, one_len), 0); 1001 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 1002 /* Still in bounds, but no longer matching. */ 1003 KUNIT_ASSERT_LT(test, memcmp(one, two, one_len + 1), 0); 1004 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 1005 1006 /* Catch too-large ranges. */ 1007 KUNIT_ASSERT_EQ(test, memcmp(one, two, one_len + 2), INT_MIN); 1008 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 1009 1010 KUNIT_ASSERT_EQ(test, memcmp(two, one, two_len + 2), INT_MIN); 1011 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 1012 } 1013 1014 static void fortify_test_kmemdup(struct kunit *test) 1015 { 1016 char src[] = "I got Doom running on it!"; 1017 char *copy; 1018 size_t len = sizeof(src) + unconst; 1019 1020 /* Copy is within bounds. */ 1021 copy = kmemdup(src, len, GFP_KERNEL); 1022 KUNIT_EXPECT_NOT_NULL(test, copy); 1023 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 1024 kfree(copy); 1025 1026 /* Without %NUL. */ 1027 copy = kmemdup(src, len - 1, GFP_KERNEL); 1028 KUNIT_EXPECT_NOT_NULL(test, copy); 1029 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 1030 kfree(copy); 1031 1032 /* Tiny bounds. */ 1033 copy = kmemdup(src, 1, GFP_KERNEL); 1034 KUNIT_EXPECT_NOT_NULL(test, copy); 1035 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); 1036 kfree(copy); 1037 1038 /* Out of bounds by 1 byte. */ 1039 copy = kmemdup(src, len + 1, GFP_KERNEL); 1040 KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR); 1041 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); 1042 kfree(copy); 1043 1044 /* Way out of bounds. */ 1045 copy = kmemdup(src, len * 2, GFP_KERNEL); 1046 KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR); 1047 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); 1048 kfree(copy); 1049 1050 /* Starting offset causing out of bounds. */ 1051 copy = kmemdup(src + 1, len, GFP_KERNEL); 1052 KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR); 1053 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 3); 1054 kfree(copy); 1055 } 1056 1057 static int fortify_test_init(struct kunit *test) 1058 { 1059 if (!IS_ENABLED(CONFIG_FORTIFY_SOURCE)) 1060 kunit_skip(test, "Not built with CONFIG_FORTIFY_SOURCE=y"); 1061 1062 fortify_read_overflows = 0; 1063 kunit_add_named_resource(test, NULL, NULL, &read_resource, 1064 "fortify_read_overflows", 1065 &fortify_read_overflows); 1066 fortify_write_overflows = 0; 1067 kunit_add_named_resource(test, NULL, NULL, &write_resource, 1068 "fortify_write_overflows", 1069 &fortify_write_overflows); 1070 return 0; 1071 } 1072 1073 static struct kunit_case fortify_test_cases[] = { 1074 KUNIT_CASE(fortify_test_known_sizes), 1075 KUNIT_CASE(fortify_test_control_flow_split), 1076 KUNIT_CASE(fortify_test_alloc_size_kmalloc_const), 1077 KUNIT_CASE(fortify_test_alloc_size_kmalloc_dynamic), 1078 KUNIT_CASE(fortify_test_alloc_size_vmalloc_const), 1079 KUNIT_CASE(fortify_test_alloc_size_vmalloc_dynamic), 1080 KUNIT_CASE(fortify_test_alloc_size_kvmalloc_const), 1081 KUNIT_CASE(fortify_test_alloc_size_kvmalloc_dynamic), 1082 KUNIT_CASE(fortify_test_alloc_size_devm_kmalloc_const), 1083 KUNIT_CASE(fortify_test_alloc_size_devm_kmalloc_dynamic), 1084 KUNIT_CASE(fortify_test_realloc_size), 1085 KUNIT_CASE(fortify_test_strlen), 1086 KUNIT_CASE(fortify_test_strnlen), 1087 KUNIT_CASE(fortify_test_strcpy), 1088 KUNIT_CASE(fortify_test_strncpy), 1089 KUNIT_CASE(fortify_test_strscpy), 1090 KUNIT_CASE(fortify_test_strcat), 1091 KUNIT_CASE(fortify_test_strncat), 1092 KUNIT_CASE(fortify_test_strlcat), 1093 /* skip memset: performs bounds checking on whole structs */ 1094 KUNIT_CASE(fortify_test_memcpy), 1095 KUNIT_CASE(fortify_test_memmove), 1096 KUNIT_CASE(fortify_test_memscan), 1097 KUNIT_CASE(fortify_test_memchr), 1098 KUNIT_CASE(fortify_test_memchr_inv), 1099 KUNIT_CASE(fortify_test_memcmp), 1100 KUNIT_CASE(fortify_test_kmemdup), 1101 {} 1102 }; 1103 1104 static struct kunit_suite fortify_test_suite = { 1105 .name = "fortify", 1106 .init = fortify_test_init, 1107 .test_cases = fortify_test_cases, 1108 }; 1109 1110 kunit_test_suite(fortify_test_suite); 1111 1112 MODULE_DESCRIPTION("Runtime test cases for CONFIG_FORTIFY_SOURCE"); 1113 MODULE_LICENSE("GPL"); 1114