// SPDX-License-Identifier: GPL-2.0 /* * Runtime test cases for CONFIG_FORTIFY_SOURCE. For additional memcpy() * testing see FORTIFY_MEM_* tests in LKDTM (drivers/misc/lkdtm/fortify.c). * * For corner cases with UBSAN, try testing with: * * ./tools/testing/kunit/kunit.py run --arch=x86_64 \ * --kconfig_add CONFIG_FORTIFY_SOURCE=y \ * --kconfig_add CONFIG_UBSAN=y \ * --kconfig_add CONFIG_UBSAN_TRAP=y \ * --kconfig_add CONFIG_UBSAN_BOUNDS=y \ * --kconfig_add CONFIG_UBSAN_LOCAL_BOUNDS=y \ * --make_options LLVM=1 fortify
*/ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
/* We don't need to fill dmesg with the fortify WARNs during testing. */ #ifdef DEBUG # define FORTIFY_REPORT_KUNIT(x...) __fortify_report(x) # define FORTIFY_WARN_KUNIT(x...) WARN_ONCE(x) #else # define FORTIFY_REPORT_KUNIT(x...) do { } while (0) # define FORTIFY_WARN_KUNIT(x...) do { } while (0) #endif
#define KUNIT_EXPECT_BOS(test, p, expected, name) \
KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \
expected, \ "__alloc_size() not working with __bos on " name "\n")
#if !__has_builtin(__builtin_dynamic_object_size) #define KUNIT_EXPECT_BDOS(test, p, expected, name) \ /* Silence "unused variable 'expected'" warning. */ \
KUNIT_EXPECT_EQ(test, expected, expected) #else #define KUNIT_EXPECT_BDOS(test, p, expected, name) \
KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \
expected, \ "__alloc_size() not working with __bdos on " name "\n") #endif
/* If the execpted size is a constant value, __bos can see it. */ #define check_const(_expected, alloc, free) do { \
size_t expected = (_expected); \ void *p = alloc; \
KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc" failed?!\n"); \
KUNIT_EXPECT_BOS(test, p, expected, #alloc); \
KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
free; \
} while (0)
/* If the execpted size is NOT a constant value, __bos CANNOT see it. */ #define check_dynamic(_expected, alloc, free) do { \
size_t expected = (_expected); \ void *p = alloc; \
KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc" failed?!\n"); \
KUNIT_EXPECT_BOS(test, p, SIZE_MAX, #alloc); \
KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
free; \
} while (0)
staticconstchar * const test_strs[] = { "", "Hello there", "A longer string, just for variety",
};
#define TEST_realloc(checker) do { \
gfp_t gfp = GFP_KERNEL; \
size_t len; \ int i; \
\ for (i = 0; i < ARRAY_SIZE(test_strs); i++) { \
len = strlen(test_strs[i]); \
KUNIT_EXPECT_EQ(test, __builtin_constant_p(len), 0); \
checker(len, kmemdup_array(test_strs[i], 1, len, gfp), \
kfree(p)); \
checker(len, kmemdup(test_strs[i], len, gfp), \
kfree(p)); \
} \
} while (0) staticvoid fortify_test_realloc_size(struct kunit *test)
{
TEST_realloc(check_dynamic);
}
/* * We can't have an array at the end of a structure or else * builds without -fstrict-flex-arrays=3 will report them as * being an unknown length. Additionally, add bytes before * and after the string to catch over/underflows if tests * fail.
*/ struct fortify_padding { unsignedlong bytes_before; char buf[32]; unsignedlong bytes_after;
};
staticvoid fortify_test_strlen(struct kunit *test)
{ struct fortify_padding pad = { }; int i, end = sizeof(pad.buf) - 1;
/* Fill 31 bytes with valid characters. */ for (i = 0; i < sizeof(pad.buf) - 1; i++)
pad.buf[i] = i + '0'; /* Trailing bytes are still %NUL. */
KUNIT_EXPECT_EQ(test, pad.buf[end], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* String is terminated, so strlen() is valid. */
KUNIT_EXPECT_EQ(test, strlen(pad.buf), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
/* Make string unterminated, and recount. */
pad.buf[end] = 'A';
end = sizeof(pad.buf);
KUNIT_EXPECT_EQ(test, strlen(pad.buf), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
}
staticvoid fortify_test_strnlen(struct kunit *test)
{ struct fortify_padding pad = { }; int i, end = sizeof(pad.buf) - 1;
/* Fill 31 bytes with valid characters. */ for (i = 0; i < sizeof(pad.buf) - 1; i++)
pad.buf[i] = i + '0'; /* Trailing bytes are still %NUL. */
KUNIT_EXPECT_EQ(test, pad.buf[end], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* String is terminated, so strnlen() is valid. */
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf)), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); /* A truncated strnlen() will be safe, too. */
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf) / 2), sizeof(pad.buf) / 2);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
/* Make string unterminated, and recount. */
pad.buf[end] = 'A';
end = sizeof(pad.buf); /* Reading beyond with strncpy() will fail. */
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 1), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 2), end);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
/* Legitimate strcpy() 1 less than of max size. */
KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src)
== pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Only last byte should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
src[sizeof(src) - 2] = 'A'; /* But now we trip the overflow checking. */
KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src)
== pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); /* Trailing %NUL -- thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); /* And we will not have gone beyond. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
src[sizeof(src) - 1] = 'A'; /* And for sure now, two bytes past. */
KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src)
== pad.buf); /* * Which trips both the strlen() on the unterminated src, * and the resulting copy attempt.
*/
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); /* Trailing %NUL -- thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); /* And we will not have gone beyond. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
}
staticvoid fortify_test_strncpy(struct kunit *test)
{ struct fortify_padding pad = { }; char src[] = "Copy me fully into a small buffer and I will overflow!";
size_t sizeof_buf = sizeof(pad.buf);
/* Legitimate strncpy() 1 less than of max size. */
KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, sizeof_buf - 1)
== pad.buf);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Only last byte should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof_buf - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 3], '\0');
/* Legitimate (though unterminated) max-size strncpy. */
KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, sizeof_buf)
== pad.buf);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* No trailing %NUL -- thanks strncpy API. */
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 2], '\0'); /* But we will not have gone beyond. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* Now verify that FORTIFY is working... */
KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, sizeof_buf + 1)
== pad.buf); /* Should catch the overflow. */
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1);
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 2], '\0'); /* And we will not have gone beyond. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* And further... */
KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, sizeof_buf + 2)
== pad.buf); /* Should catch the overflow. */
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2);
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 2], '\0'); /* And we will not have gone beyond. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
}
staticvoid fortify_test_strscpy(struct kunit *test)
{ struct fortify_padding pad = { }; char src[] = "Copy me fully into a small buffer and I will overflow!";
size_t sizeof_buf = sizeof(pad.buf);
size_t sizeof_src = sizeof(src);
/* Legitimate strscpy() 1 less than of max size. */
KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, sizeof_buf - 1),
-E2BIG);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Keeping space for %NUL, last two bytes should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof_buf - 1], '\0');
KUNIT_EXPECT_EQ(test, pad.buf[sizeof_buf - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof_buf - 3], '\0');
/* Legitimate strcat() using less than half max size. */
KUNIT_ASSERT_TRUE(test, strcat(pad.buf, src) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Legitimate strcat() now 2 bytes shy of end. */
KUNIT_ASSERT_TRUE(test, strcat(pad.buf, src) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Last two bytes should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
/* Add one more character to the end. */
KUNIT_ASSERT_TRUE(test, strcat(pad.buf, one) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Last byte should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
/* And this one char will overflow. */
KUNIT_ASSERT_TRUE(test, strcat(pad.buf, one) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); /* Last byte should be %NUL thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* And adding two will overflow more. */
KUNIT_ASSERT_TRUE(test, strcat(pad.buf, two) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); /* Last byte should be %NUL thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
}
staticvoid fortify_test_strncat(struct kunit *test)
{ struct fortify_padding pad = { }; char src[sizeof(pad.buf)] = { }; int i, partial;
/* Fill 31 bytes with valid characters. */
partial = sizeof(src) / 2 - 1; for (i = 0; i < partial; i++)
src[i] = i + 'A';
/* Legitimate strncat() using less than half max size. */
KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, partial) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Legitimate strncat() now 2 bytes shy of end. */
KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, partial) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Last two bytes should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
/* Add one more character to the end. */
KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Last byte should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
/* And this one char will overflow. */
KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); /* Last byte should be %NUL thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* And adding two will overflow more. */
KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 2) == pad.buf);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); /* Last byte should be %NUL thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* Force an unterminated destination, and overflow. */
pad.buf[sizeof(pad.buf) - 1] = 'A';
KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf); /* This will have tripped both strlen() and strcat(). */
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 3);
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); /* But we should not go beyond the end. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
}
staticvoid fortify_test_strlcat(struct kunit *test)
{ struct fortify_padding pad = { }; char src[sizeof(pad.buf)] = { }; int i, partial; int len = sizeof(pad.buf);
OPTIMIZER_HIDE_VAR(len);
/* Fill 15 bytes with valid characters. */
partial = sizeof(src) / 2 - 1; for (i = 0; i < partial; i++)
src[i] = i + 'A';
/* Legitimate strlcat() using less than half max size. */
KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len), partial);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Legitimate strlcat() now 2 bytes shy of end. */
KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len), partial * 2);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Last two bytes should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
/* Add one more character to the end. */
KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "Q", len), partial * 2 + 1);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); /* Last byte should be %NUL */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
/* And this one char will overflow. */
KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "V", len * 2), len);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); /* Last byte should be %NUL thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* And adding two will overflow more. */
KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "QQ", len * 2), len + 1);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); /* Last byte should be %NUL thanks to FORTIFY. */
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0');
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* Force an unterminated destination, and overflow. */
pad.buf[sizeof(pad.buf) - 1] = 'A';
KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "TT", len * 2), len + 2); /* This will have tripped both strlen() and strlcat(). */
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2);
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0');
KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); /* But we should not go beyond the end. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
/* Force an unterminated source, and overflow. */
memset(src, 'B', sizeof(src));
pad.buf[sizeof(pad.buf) - 1] = '\0';
KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len * 3), len - 1 + sizeof(src)); /* This will have tripped both strlen() and strlcat(). */
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 3);
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 3);
KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); /* But we should not go beyond the end. */
KUNIT_EXPECT_EQ(test, pad.bytes_after, 0);
}
staticvoid fortify_test_memchr_inv(struct kunit *test)
{ char haystack[] = "Where oh where is my memory range?"; char *mem = haystack + 1; char needle = 'W';
size_t len = sizeof(haystack);
OPTIMIZER_HIDE_VAR(len);
/* Normal search is okay. */
KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len),
mem);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); /* Catch too-large range. */
KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len + 1),
NULL);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len * 2),
NULL);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
}
staticvoid fortify_test_memcmp(struct kunit *test)
{ char one[] = "My mind is going ..."; char two[] = "My mind is going ... I can feel it."; volatile size_t one_len = sizeof(one) - 1; volatile size_t two_len = sizeof(two) - 1;
/* We match the first string (ignoring the %NUL). */
KUNIT_ASSERT_EQ(test, memcmp(one, two, one_len), 0);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); /* Still in bounds, but no longer matching. */
KUNIT_ASSERT_LT(test, memcmp(one, two, one_len + 1), 0);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
/* Out of bounds by 1 byte. */
copy = kmemdup(src, len + 1, GFP_KERNEL);
KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
kfree(copy);
/* Way out of bounds. */
copy = kmemdup(src, len * 2, GFP_KERNEL);
KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
kfree(copy);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.