fortify: Allow KUnit test to build without FORTIFY
authorKees Cook <keescook@chromium.org>
Sun, 26 Mar 2023 20:53:27 +0000 (13:53 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 16 May 2023 21:07:49 +0000 (14:07 -0700)
In order for CI systems to notice all the skipped tests related to
CONFIG_FORTIFY_SOURCE, allow the FORTIFY_SOURCE KUnit tests to build
with or without CONFIG_FORTIFY_SOURCE.

Signed-off-by: Kees Cook <keescook@chromium.org>
lib/Kconfig.debug
lib/fortify_kunit.c

index ce51d4d..1a630a0 100644 (file)
@@ -2645,7 +2645,7 @@ config STACKINIT_KUNIT_TEST
 
 config FORTIFY_KUNIT_TEST
        tristate "Test fortified str*() and mem*() function internals at runtime" if !KUNIT_ALL_TESTS
-       depends on KUNIT && FORTIFY_SOURCE
+       depends on KUNIT
        default KUNIT_ALL_TESTS
        help
          Builds unit tests for checking internals of FORTIFY_SOURCE as used
index c8c33cb..524132f 100644 (file)
@@ -25,6 +25,11 @@ static const char array_of_10[] = "this is 10";
 static const char *ptr_of_11 = "this is 11!";
 static char array_unknown[] = "compiler thinks I might change";
 
+/* Handle being built without CONFIG_FORTIFY_SOURCE */
+#ifndef __compiletime_strlen
+# define __compiletime_strlen __builtin_strlen
+#endif
+
 static void known_sizes_test(struct kunit *test)
 {
        KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8);
@@ -307,6 +312,14 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc)
 } while (0)
 DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc)
 
+static int fortify_test_init(struct kunit *test)
+{
+       if (!IS_ENABLED(CONFIG_FORTIFY_SOURCE))
+               kunit_skip(test, "Not built with CONFIG_FORTIFY_SOURCE=y");
+
+       return 0;
+}
+
 static struct kunit_case fortify_test_cases[] = {
        KUNIT_CASE(known_sizes_test),
        KUNIT_CASE(control_flow_split_test),
@@ -323,6 +336,7 @@ static struct kunit_case fortify_test_cases[] = {
 
 static struct kunit_suite fortify_test_suite = {
        .name = "fortify",
+       .init = fortify_test_init,
        .test_cases = fortify_test_cases,
 };