From patchwork Sat Nov 26 18:15:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 26287 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp4776796wrr; Sat, 26 Nov 2022 10:48:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf5CwtwYjtdo6LyH3fQaNEDoFwo1ltOI+XDgIrea3JCer9DVTr6EOK3ctpLXQpZs5tTANr3p X-Received: by 2002:a17:906:dfc1:b0:78d:894d:e123 with SMTP id jt1-20020a170906dfc100b0078d894de123mr20564986ejc.112.1669488522764; Sat, 26 Nov 2022 10:48:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669488522; cv=none; d=google.com; s=arc-20160816; b=D4Y8imykaQUJMaDtihxGNXBuE/j89U4AxRdvqayh38YF2J/7uzS5XP3mdsogPxpO/x QpZHv/kYyQpwn/lShETZjUJb3VkX5wGg2kQOcjOvdSCaAE817dCtCywvBpPw1u8BHjqK bEk0C7VcQa5zk0mKf4q90FgNO14NblZ0ZMYsblouQ/PX00u1bsR/Yxu1zRTgjRqQ9pHq jo6zpyEvCK9tOEhs2LFGrSRXL2PaXPWI1VDhJlkni8Ec+q6DnfPzkWAl/QqrVILcOpQL VU5ZqgTptJoXnhpeR9ok72zJVPsIxFLTeIXYAN+Iy8T8cOpBUWCrVoL7VQU8dcAECCEW bx7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=JDvTtFdWl1YXSMfasI4Saz9+J3V+bsk5xm78bG3AgR8=; b=rPvehwP2gFm5cOgUT0bHPY22pgASjx7s1iO8zTKpb/wwGGQdsTfV/mC8w0o4ECPqB7 sCFRzIqwsS0M7uBhmzAifysVUg6Ht5IIOhisLeMbZRllNr6zDkvwlf8+jh2C1jdKccF2 wKyeHR4R64E5l8dv1MIXvfdVt1fxYjCD78V5uqpMiNVx9w5yNActnag2PBcCp4ktPDor A3cQVoQD4Rorci9Bgr/X4uywZ6Ie/ZnRoZ8rS0iJbXdmqZSU792p1PogbsO7gp/CTKZa /T+m54E5F961UbTxzputbtucXfSkVQ6q2CmH6KITVEUBQl6kcCHkaG32hit19jy2xARy n+vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=DCYD0KPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs10-20020a0564020c4a00b00458bbd3a0d2si6201045edb.602.2022.11.26.10.48.02; Sat, 26 Nov 2022 10:48:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=DCYD0KPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229529AbiKZSXJ (ORCPT + 99 others); Sat, 26 Nov 2022 13:23:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229495AbiKZSXH (ORCPT ); Sat, 26 Nov 2022 13:23:07 -0500 X-Greylist: delayed 466 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sat, 26 Nov 2022 10:23:05 PST Received: from out-197.mta0.migadu.com (out-197.mta0.migadu.com [IPv6:2001:41d0:1004:224b::c5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8D9D1A05D for ; Sat, 26 Nov 2022 10:23:05 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1669486515; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=JDvTtFdWl1YXSMfasI4Saz9+J3V+bsk5xm78bG3AgR8=; b=DCYD0KPcyxtjNJVYy6HnI9aIp9UN1NOZmB15RRNNisl/+JIlP8nH+T8va/C39F3R2CskX+ JSvgWTHhm22MPGPI2B9fKoSlBT5WPofQE1mTwt+0rHXb5mJDPbFng957Bwzc/+u30VmA5O fTlC0571r0ej+/Nz6ILpPn6w+ZqN1T4= From: andrey.konovalov@linux.dev To: Marco Elver , David Gow Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm] kasan: fail non-kasan KUnit tests on KASAN reports Date: Sat, 26 Nov 2022 19:15:11 +0100 Message-Id: <655fd7e303b852809d3a8167d28091429f969c73.1669486407.git.andreyknvl@google.com> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750583688081172962?= X-GMAIL-MSGID: =?utf-8?q?1750585597436599655?= From: Andrey Konovalov 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 --- 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 */ +#include #include #include #include @@ -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. */