Merge tag 'hardening-v6.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[sfrench/cifs-2.6.git] / mm / kasan / report.c
index cc98dfdd3ed2f6f548e2fc6faa3cd10d22ee25e5..1d02757e90a3266a0d2f66872c68ba117115b33b 100644 (file)
@@ -9,6 +9,7 @@
  *        Andrey Konovalov <andreyknvl@gmail.com>
  */
 
+#include <kunit/test.h>
 #include <linux/bitops.h>
 #include <linux/ftrace.h>
 #include <linux/init.h>
@@ -30,8 +31,6 @@
 
 #include <asm/sections.h>
 
-#include <kunit/test.h>
-
 #include "kasan.h"
 #include "../slab.h"
 
@@ -115,40 +114,63 @@ EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
 #endif
 
 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
-static void update_kunit_status(bool sync)
+
+/*
+ * Whether the KASAN KUnit test suite is currently being executed.
+ * Updated in kasan_test.c.
+ */
+bool kasan_kunit_executing;
+
+void kasan_kunit_test_suite_start(void)
+{
+       WRITE_ONCE(kasan_kunit_executing, true);
+}
+EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_start);
+
+void kasan_kunit_test_suite_end(void)
+{
+       WRITE_ONCE(kasan_kunit_executing, false);
+}
+EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_end);
+
+static bool kasan_kunit_test_suite_executing(void)
+{
+       return READ_ONCE(kasan_kunit_executing);
+}
+
+#else /* CONFIG_KASAN_KUNIT_TEST */
+
+static inline bool kasan_kunit_test_suite_executing(void) { return false; }
+
+#endif /* CONFIG_KASAN_KUNIT_TEST */
+
+#if IS_ENABLED(CONFIG_KUNIT)
+
+static void fail_non_kasan_kunit_test(void)
 {
        struct kunit *test;
-       struct kunit_resource *resource;
-       struct kunit_kasan_status *status;
 
-       test = current->kunit_test;
-       if (!test)
+       if (kasan_kunit_test_suite_executing())
                return;
 
-       resource = kunit_find_named_resource(test, "kasan_status");
-       if (!resource) {
+       test = current->kunit_test;
+       if (test)
                kunit_set_failure(test);
-               return;
-       }
+}
 
-       status = (struct kunit_kasan_status *)resource->data;
-       WRITE_ONCE(status->report_found, true);
-       WRITE_ONCE(status->sync_fault, sync);
+#else /* CONFIG_KUNIT */
 
-       kunit_put_resource(resource);
-}
-#else
-static void update_kunit_status(bool sync) { }
-#endif
+static inline void fail_non_kasan_kunit_test(void) { }
+
+#endif /* CONFIG_KUNIT */
 
 static DEFINE_SPINLOCK(report_lock);
 
 static void start_report(unsigned long *flags, bool sync)
 {
+       fail_non_kasan_kunit_test();
        /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
        disable_trace_on_warning();
-       /* Update status of the currently running KASAN test. */
-       update_kunit_status(sync);
        /* Do not allow LOCKDEP mangling KASAN reports. */
        lockdep_off();
        /* Make sure we don't end up in loop. */