From: Patricia Alfonso <trishalfo...@google.com>

Integrate KASAN into KUnit testing framework.
        - Fail tests when KASAN reports an error that is not expected
        - Use KUNIT_EXPECT_KASAN_FAIL to expect a KASAN error in KASAN
        tests
        - Expected KASAN reports pass tests and are still printed when run
        without kunit_tool (kunit_tool still bypasses the report due to the
        test passing)
        - KUnit struct in current task used to keep track of the current
        test from KASAN code

This patch makes use of "kunit: generalize kunit_resource API beyond
allocated resources" and "kunit: add support for named resources" from
Alan Maguire [1]
        - A named resource is added to a test when a KASAN report is
        expected
        - This resource contains a struct for kasan_data containing
        booleans representing if a KASAN report is expected and if a KASAN
        report is found

[1] 
https://lore.kernel.org/linux-kselftest/cafd5g46uu_5tg89uom0dj5cmq+11cwjbnsd-k_cvy6bqueu...@mail.gmail.com/T/#t

Signed-off-by: Patricia Alfonso <trishalfo...@google.com>
Signed-off-by: David Gow <david...@google.com>
Reviewed-by: Dmitry Vyukov <dvyu...@google.com>
Reviewed-by: Andrey Konovalov <andreyk...@google.com>
---
 include/kunit/test.h  |  5 +++++
 include/linux/kasan.h |  6 ++++++
 lib/kunit/test.c      | 13 +++++++-----
 lib/test_kasan.c      | 47 +++++++++++++++++++++++++++++++++++++++++--
 mm/kasan/report.c     | 32 +++++++++++++++++++++++++++++
 5 files changed, 96 insertions(+), 7 deletions(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index 59f3144f009a..3391f38389f8 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -224,6 +224,11 @@ struct kunit {
        struct list_head resources; /* Protected by lock. */
 };
 
+static inline void kunit_set_failure(struct kunit *test)
+{
+       WRITE_ONCE(test->success, false);
+}
+
 void kunit_init_test(struct kunit *test, const char *name, char *log);
 
 int kunit_run_tests(struct kunit_suite *suite);
diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index 31314ca7c635..d58db2f67f43 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -14,6 +14,12 @@ struct task_struct;
 #include <asm/kasan.h>
 #include <asm/pgtable.h>
 
+/* kasan_data struct is used in KUnit tests for KASAN expected failures */
+struct kunit_kasan_expectation {
+       bool report_expected;
+       bool report_found;
+};
+
 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
 extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index c36037200310..dcc35fd30d95 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -10,16 +10,12 @@
 #include <linux/kernel.h>
 #include <linux/kref.h>
 #include <linux/sched/debug.h>
+#include <linux/sched.h>
 
 #include "debugfs.h"
 #include "string-stream.h"
 #include "try-catch-impl.h"
 
-static void kunit_set_failure(struct kunit *test)
-{
-       WRITE_ONCE(test->success, false);
-}
-
 static void kunit_print_tap_version(void)
 {
        static bool kunit_has_printed_tap_version;
@@ -288,6 +284,10 @@ static void kunit_try_run_case(void *data)
        struct kunit_suite *suite = ctx->suite;
        struct kunit_case *test_case = ctx->test_case;
 
+#if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
+       current->kunit_test = test;
+#endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT) */
+
        /*
         * kunit_run_case_internal may encounter a fatal error; if it does,
         * abort will be called, this thread will exit, and finally the parent
@@ -602,6 +602,9 @@ void kunit_cleanup(struct kunit *test)
                spin_unlock(&test->lock);
                kunit_remove_resource(test, res);
        }
+#if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
+       current->kunit_test = NULL;
+#endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT)*/
 }
 EXPORT_SYMBOL_GPL(kunit_cleanup);
 
diff --git a/lib/test_kasan.c b/lib/test_kasan.c
index dc2c6a51d11a..842adcd30943 100644
--- a/lib/test_kasan.c
+++ b/lib/test_kasan.c
@@ -23,19 +23,62 @@
 
 #include <asm/page.h>
 
+#include <kunit/test.h>
+
 /*
  * We assign some test results to these globals to make sure the tests
  * are not eliminated as dead code.
  */
 
-int kasan_int_result;
 void *kasan_ptr_result;
+int kasan_int_result;
+
+static struct kunit_resource resource;
+static struct kunit_kasan_expectation fail_data;
+static bool multishot;
+
+static int kasan_test_init(struct kunit *test)
+{
+       /*
+        * Temporarily enable multi-shot mode and set panic_on_warn=0.
+        * Otherwise, we'd only get a report for the first case.
+        */
+       multishot = kasan_save_enable_multi_shot();
+
+       return 0;
+}
+
+static void kasan_test_exit(struct kunit *test)
+{
+       kasan_restore_multi_shot(multishot);
+}
+
+/**
+ * KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does
+ * not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do
+ * Do not use this name for a KUnit resource outside here.
+ *
+ */
+#define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \
+       fail_data.report_expected = true; \
+       fail_data.report_found = false; \
+       kunit_add_named_resource(test, \
+                               NULL, \
+                               NULL, \
+                               &resource, \
+                               "kasan_data", &fail_data); \
+       condition; \
+       KUNIT_EXPECT_EQ(test, \
+                       fail_data.report_expected, \
+                       fail_data.report_found); \
+} while (0)
+
+
 
 /*
  * Note: test functions are marked noinline so that their names appear in
  * reports.
  */
-
 static noinline void __init kmalloc_oob_right(void)
 {
        char *ptr;
diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index 80f23c9da6b0..45f3c23f54cb 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -32,6 +32,8 @@
 
 #include <asm/sections.h>
 
+#include <kunit/test.h>
+
 #include "kasan.h"
 #include "../slab.h"
 
@@ -463,12 +465,37 @@ bool report_enabled(void)
        return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
 }
 
+#if IS_ENABLED(CONFIG_KUNIT)
+static void kasan_update_kunit_status(struct kunit *cur_test)
+{
+       struct kunit_resource *resource;
+       struct kunit_kasan_expectation *kasan_data;
+
+       resource = kunit_find_named_resource(cur_test, "kasan_data");
+
+       if (!resource) {
+               kunit_set_failure(cur_test);
+               return;
+       }
+
+       kasan_data = (struct kunit_kasan_expectation *)resource->data;
+       kasan_data->report_found = true;
+       kunit_put_resource(resource);
+}
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
 void kasan_report_invalid_free(void *object, unsigned long ip)
 {
        unsigned long flags;
        u8 tag = get_tag(object);
 
        object = reset_tag(object);
+
+#if IS_ENABLED(CONFIG_KUNIT)
+       if (current->kunit_test)
+               kasan_update_kunit_status(current->kunit_test);
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
        start_report(&flags);
        pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
        print_tags(tag, object);
@@ -486,6 +513,11 @@ void __kasan_report(unsigned long addr, size_t size, bool 
is_write, unsigned lon
        void *untagged_addr;
        unsigned long flags;
 
+#if IS_ENABLED(CONFIG_KUNIT)
+       if (current->kunit_test)
+               kasan_update_kunit_status(current->kunit_test);
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
        disable_trace_on_warning();
 
        tagged_addr = (void *)addr;
-- 
2.27.0.278.ge193c7cf3a9-goog

Reply via email to