From: Andrey Konovalov <[email protected]>
After the recent changes done to KUnit-enabled KASAN tests, non-KASAN KUnit
tests stopped being failed when KASAN report is detected.
Recover that property by failing the currently running non-KASAN KUnit test
when KASAN detects and prints a report for a bad memory access.
Note that if the bad accesses happened in a kernel thread that doesn't
have a reference to the currently running KUnit-test available via
current->kunit_test, the test won't be failed. This is a limitation of
KUnit, which doesn't yet provide a thread-agnostic way to find the
reference to the currenly running test.
Fixes: 49d9977ac909 ("kasan: check CONFIG_KASAN_KUNIT_TEST instead of CONFIG_KUNIT")
Fixes: 7f29493ba529 ("kasan: switch kunit tests to console tracepoints")
Signed-off-by: Andrey Konovalov <[email protected]>
---
mm/kasan/kasan.h | 6 ++++++
mm/kasan/kasan_test.c | 11 +++++++++++
mm/kasan/report.c | 22 ++++++++++++++++++++++
3 files changed, 39 insertions(+)
diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
index a84491bc4867..08a83a7ef77f 100644
--- a/mm/kasan/kasan.h
+++ b/mm/kasan/kasan.h
@@ -548,6 +548,12 @@ void kasan_restore_multi_shot(bool enabled);
#endif
+#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
+bool kasan_kunit_test_suite_executing(void);
+#else
+static bool kasan_kunit_test_suite_executing(void) { return false; }
+#endif
+
/*
* Exported functions for interfaces called from assembly or from generated
* code. Declared here to avoid warnings about missing declarations.
diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
index e27591ef2777..c9a615e892ed 100644
--- a/mm/kasan/kasan_test.c
+++ b/mm/kasan/kasan_test.c
@@ -32,6 +32,9 @@
#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
+/* Whether the KASAN KUnit test suite is currently being executed. */
+static bool executing;
+
static bool multishot;
/* Fields set based on lines observed in the console. */
@@ -47,6 +50,11 @@ static struct {
void *kasan_ptr_result;
int kasan_int_result;
+bool kasan_kunit_test_suite_executing(void)
+{
+ return READ_ONCE(executing);
+}
+
/* Probe for console output: obtains test_status lines of interest. */
static void probe_console(void *ignore, const char *buf, size_t len)
{
@@ -76,6 +84,8 @@ static int kasan_suite_init(struct kunit_suite *suite)
return -1;
}
+ WRITE_ONCE(executing, true);
+
/*
* Temporarily enable multi-shot mode. Otherwise, KASAN would only
* report the first detected bug and panic the kernel if panic_on_warn
@@ -94,6 +104,7 @@ static int kasan_suite_init(struct kunit_suite *suite)
static void kasan_suite_exit(struct kunit_suite *suite)
{
+ WRITE_ONCE(executing, false);
kasan_restore_multi_shot(multishot);
for_each_kernel_tracepoint(unregister_tracepoints, NULL);
tracepoint_synchronize_unregister();
diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index 31355851a5ec..e718c997ecae 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -9,6 +9,7 @@
* Andrey Konovalov <[email protected]>
*/
+#include <kunit/test.h>
#include <linux/bitops.h>
#include <linux/ftrace.h>
#include <linux/init.h>
@@ -112,10 +113,31 @@ EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
#endif
+#if IS_ENABLED(CONFIG_KUNIT)
+
+static void fail_nonkasan_kunit_test(void)
+{
+ struct kunit *test;
+
+ if (!kasan_kunit_test_suite_executing())
+ return;
+
+ test = current->kunit_test;
+ if (test)
+ kunit_set_failure(test);
+}
+
+#else /* CONFIG_KUNIT */
+
+static void fail_nonkasan_kunit_test(void) { }
+
+#endif /* CONFIG_KUNIT */
+
static DEFINE_SPINLOCK(report_lock);
static void start_report(unsigned long *flags, bool sync)
{
+ fail_nonkasan_kunit_test();
/* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
disable_trace_on_warning();
/* Do not allow LOCKDEP mangling KASAN reports. */
--
2.25.1
Hi,
I love your patch! Yet something to improve:
[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on next-20221125]
[cannot apply to linus/master v6.1-rc6]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/andrey-konovalov-linux-dev/kasan-fail-non-kasan-KUnit-tests-on-KASAN-reports/20221127-021639
base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link: https://lore.kernel.org/r/655fd7e303b852809d3a8167d28091429f969c73.1669486407.git.andreyknvl%40google.com
patch subject: [PATCH mm] kasan: fail non-kasan KUnit tests on KASAN reports
config: powerpc-allmodconfig
compiler: powerpc-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/d958adcd0a4271459e50cf8a07c6dd60c48484a4
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review andrey-konovalov-linux-dev/kasan-fail-non-kasan-KUnit-tests-on-KASAN-reports/20221127-021639
git checkout d958adcd0a4271459e50cf8a07c6dd60c48484a4
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=powerpc SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
All errors (new ones prefixed by >>):
powerpc-linux-ld: mm/kasan/report.o: in function `start_report.constprop.0':
>> report.c:(.text.start_report.constprop.0+0x14): undefined reference to `kasan_kunit_test_suite_executing'
--
0-DAY CI Kernel Test Service
https://01.org/lkp
Hi,
I love your patch! Yet something to improve:
[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on next-20221125]
[cannot apply to linus/master v6.1-rc6]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/andrey-konovalov-linux-dev/kasan-fail-non-kasan-KUnit-tests-on-KASAN-reports/20221127-021639
base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link: https://lore.kernel.org/r/655fd7e303b852809d3a8167d28091429f969c73.1669486407.git.andreyknvl%40google.com
patch subject: [PATCH mm] kasan: fail non-kasan KUnit tests on KASAN reports
config: x86_64-rhel-8.3-kunit
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/d958adcd0a4271459e50cf8a07c6dd60c48484a4
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review andrey-konovalov-linux-dev/kasan-fail-non-kasan-KUnit-tests-on-KASAN-reports/20221127-021639
git checkout d958adcd0a4271459e50cf8a07c6dd60c48484a4
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
All errors (new ones prefixed by >>):
ld: mm/kasan/report.o: in function `fail_nonkasan_kunit_test':
>> mm/kasan/report.c:122: undefined reference to `kasan_kunit_test_suite_executing'
>> ld: mm/kasan/report.c:122: undefined reference to `kasan_kunit_test_suite_executing'
pahole: .tmp_vmlinux.btf: No such file or directory
.btf.vmlinux.bin.o: file not recognized: file format not recognized
vim +122 mm/kasan/report.c
117
118 static void fail_nonkasan_kunit_test(void)
119 {
120 struct kunit *test;
121
> 122 if (!kasan_kunit_test_suite_executing())
123 return;
124
125 test = current->kunit_test;
126 if (test)
127 kunit_set_failure(test);
128 }
129
--
0-DAY CI Kernel Test Service
https://01.org/lkp