From patchwork Fri Jul 1 08:47:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 12902975 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 823B7C433EF for ; Fri, 1 Jul 2022 08:47:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236244AbiGAIr5 (ORCPT ); Fri, 1 Jul 2022 04:47:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232240AbiGAIr4 (ORCPT ); Fri, 1 Jul 2022 04:47:56 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AD65735B4 for ; Fri, 1 Jul 2022 01:47:51 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id n131-20020a255989000000b0066cef4b4a3aso1478844ybb.8 for ; Fri, 01 Jul 2022 01:47:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=NuZanDiX6ZABSMbfniI1TGr/DxYkHBykbUx4x4ghkJs=; b=CZLCtNgssNVr6nSXDn6fB604VBq+KkeWkf1tsogW0rqBqDsWGig719MPVJL6gYAvgY 9RLRb9A15yblqEPywZl4Lgr6YSf9RXOrCrx05owdb7fROgwlMHPgEHweT+hfEokeoi8o LNHODCorIbIyKm8aLbO68fc481xtKztaTjZzYQOWZdYOt+CAO+QwI1YDcZSKV3SuqVOA FJ1LmVHx3rC43z4OWzucakfkXECe5G+RwAjDe0BEzlgwIeQv+q77AUOJhIdqm3JQLg0q MP46dyYMh7yz1bpDLglsTAKkhXu3eoWqjeliptD0SJta9R4nLcM52BDKD2JG16NnEN4l O7Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=NuZanDiX6ZABSMbfniI1TGr/DxYkHBykbUx4x4ghkJs=; b=3Fh2842XdFlRP5Agq7GEmIkOstHOUfcrmbcR1jrQtbJlqDwubp/Zj/S1087E0xbfEi FO39rj76dq+Naqkd7zW/NsJmhqf4UEVK7UOr+/BxCv1UgjYDY8mN96/+1Kz7BUeg8FOg PhIHYjCzDI9RJnqKKkF7AdUEqRup0as+DcZaiEMXRuDWnlX+e+JzvKvlMPqWg3V+z8Bp ZVTvnyH6US8mDxxkopE0tir4CVTnX3ReWLgvPRKK/VxiqmTEPbDaoHTOJ8WIso4JnlNL Qh+N7NVYvV89k76YOyZoEdUGy6cw6jCTrFXSN0sC4D3yJTy+XlLVAYI84Nzt04Tgtmgj 5K1A== X-Gm-Message-State: AJIora/y2WnxTlxZk+VkNrPqeZyV3y0IyiH7FZBVaWGDiflYs5wMfS9R AtaAS5AZegDiAdapFZdmQ3XrJEr4tm1LhA== X-Google-Smtp-Source: AGRyM1ueeVhwlJQ7zRKjLy1gM9/U9w/+eRK9KccBKyOEVw0DgaknmIifOXTfkG/tj2NIxQjOyOX+A2D1Xy7eSg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:d292:0:b0:66c:8adb:ce55 with SMTP id j140-20020a25d292000000b0066c8adbce55mr14348011ybg.131.1656665270212; Fri, 01 Jul 2022 01:47:50 -0700 (PDT) Date: Fri, 1 Jul 2022 16:47:41 +0800 Message-Id: <20220701084744.3002019-1-davidgow@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 1/4] panic: Taint kernel if tests are run From: David Gow To: Brendan Higgins , Andy Shevchenko , Jonathan Corbet , Andrew Morton , Kees Cook , Shuah Khan , Greg KH , Luis Chamberlain , Masahiro Yamada Cc: David Gow , "Guilherme G . Piccoli" , Sebastian Reichel , John Ogness , Joe Fradley , Daniel Latypov , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Jani Nikula , Lucas De Marchi , Aaron Tomlin , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, Michal Marek , Nick Desaulniers , linux-kbuild@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Most in-kernel tests (such as KUnit tests) are not supposed to run on production systems: they may do deliberately illegal things to trigger errors, and have security implications (for example, KUnit assertions will often deliberately leak kernel addresses). Add a new taint type, TAINT_TEST to signal that a test has been run. This will be printed as 'N' (originally for kuNit, as every other sensible letter was taken.) This should discourage people from running these tests on production systems, and to make it easier to tell if tests have been run accidentally (by loading the wrong configuration, etc.) Acked-by: Luis Chamberlain Reviewed-by: Brendan Higgins Signed-off-by: David Gow --- Finally getting back to this, with the addition of a MODULE_INFO() to mark a module as a test module. This is automatically set for modules in the "tools/testing" directory by modpost (see patch #2). The 'N' character for the taint is even less useful now that it's no longer short for kuNit, but all the letters in TEST are taken. :-( Changes since v3: https://lore.kernel.org/lkml/20220513083212.3537869-1-davidgow@google.com/ - Remove the mention of KUnit from the documentation. - Add Luis and Brendan's Acked/Reviewed-by tags. Changes since v2: https://lore.kernel.org/linux-kselftest/20220430030019.803481-1-davidgow@google.com/ - Rename TAINT_KUNIT -> TAINT_TEST. - Split into separate patches for adding the taint, and triggering it. - Taint on a kselftest_module being loaded (patch 3/3) Changes since v1: https://lore.kernel.org/linux-kselftest/20220429043913.626647-1-davidgow@google.com/ - Make the taint per-module, to handle the case when tests are in (longer lasting) modules. (Thanks Greg KH). Note that this still has checkpatch.pl warnings around bracket placement, which are intentional as part of matching the surrounding code. --- Documentation/admin-guide/tainted-kernels.rst | 1 + include/linux/panic.h | 3 ++- kernel/panic.c | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/tainted-kernels.rst b/Documentation/admin-guide/tainted-kernels.rst index ceeed7b0798d..7d80e8c307d1 100644 --- a/Documentation/admin-guide/tainted-kernels.rst +++ b/Documentation/admin-guide/tainted-kernels.rst @@ -100,6 +100,7 @@ Bit Log Number Reason that got the kernel tainted 15 _/K 32768 kernel has been live patched 16 _/X 65536 auxiliary taint, defined for and used by distros 17 _/T 131072 kernel was built with the struct randomization plugin + 18 _/N 262144 an in-kernel test has been run === === ====== ======================================================== Note: The character ``_`` is representing a blank in this table to make reading diff --git a/include/linux/panic.h b/include/linux/panic.h index e71161da69c4..c7759b3f2045 100644 --- a/include/linux/panic.h +++ b/include/linux/panic.h @@ -68,7 +68,8 @@ static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout) #define TAINT_LIVEPATCH 15 #define TAINT_AUX 16 #define TAINT_RANDSTRUCT 17 -#define TAINT_FLAGS_COUNT 18 +#define TAINT_TEST 18 +#define TAINT_FLAGS_COUNT 19 #define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) struct taint_flag { diff --git a/kernel/panic.c b/kernel/panic.c index a3c758dba15a..6b3369e21026 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -428,6 +428,7 @@ const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { [ TAINT_LIVEPATCH ] = { 'K', ' ', true }, [ TAINT_AUX ] = { 'X', ' ', true }, [ TAINT_RANDSTRUCT ] = { 'T', ' ', true }, + [ TAINT_TEST ] = { 'N', ' ', true }, }; /** From patchwork Fri Jul 1 08:47:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 12902976 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0F1BCCA483 for ; Fri, 1 Jul 2022 08:47:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234092AbiGAIr6 (ORCPT ); Fri, 1 Jul 2022 04:47:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235991AbiGAIr4 (ORCPT ); Fri, 1 Jul 2022 04:47:56 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33F66735BF for ; Fri, 1 Jul 2022 01:47:55 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id n131-20020a255989000000b0066cef4b4a3aso1478943ybb.8 for ; Fri, 01 Jul 2022 01:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ml8Z1qiNNiXT/1PzWyvvt8aN8dvJcZjDGqTV68m+tGo=; b=NAQpECpRDHIM/hlGNxwkg9Hv5tgmFumtJ6WiXYFdt9aQ4roUlytgKojb2oeQb8TjrB HtvQBKcDWTPW0CjksH/AhxabVHDzZRMvRckEaD1IChNoWcA+AfXWvMzo5dmBGoZ7rZ6N ISBu70K79ljdgzwwSF+syxkbcCxHMVeZ1DW4SyFLagkIxCY3sc/IbqWgF642UX89HEVY HCM2kY5y7QtM4/HpY7j5bkgVJKqqeel36d+kTOEv9wuWYxInCQtPmh0pNG58a4QwgHSS qD6mJgVR0l2XfXKAlRjasF9Q9XreGRXEuMYw9+sZwGQuRQ2K3W511ibsd9lsVTfQPphz 1zfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ml8Z1qiNNiXT/1PzWyvvt8aN8dvJcZjDGqTV68m+tGo=; b=TO2Pbea8a+iSP2PlKTI9kIvCNRL7PMddvooCs6Wo9BBiLWlcicX6pANGGE5UpttjWq 1qx7Jm5SXs01VQPQY8tjI6O9jcffW2dovMRjOO9FkCuJ5SNdZH+ZSE89ouyYhxM/bcV5 y3ftQe0goJ/h0BEemTLVPQk22MbJ0VcDy7fSIDIOivE4ayuv1KWG1OTQLdZR5esCje9d 2MYIlAhge6vmOwZqK9wZM4bCH8scVWeHUx/Tkt3aU1C/Fxijt7fVzew1nOULPR3mrizn TmzOhlPKdhBg9mj9PoI4sFKly+rea5qyb1LHeeWI8zm6LesnE+U4zh1ZD3oHD9S0XAc7 4qaQ== X-Gm-Message-State: AJIora8gU2cWDUh/z/aoJRYKz4uumkxGJe6wsZ0YFElTLp7Tu/2dhbAA JrAh59avY/waHFuUhfzHX9nhuzm1eAZSSw== X-Google-Smtp-Source: AGRyM1v7ew3j0R7c9yqC49uzuTOOzGkqAmVFXXxCn9zfU56NTTgCwP9X9gSqBlJ1BgffmPx/w2LuWEXg7FpvCA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:2443:0:b0:2eb:4ffe:fab2 with SMTP id k64-20020a812443000000b002eb4ffefab2mr14909347ywk.330.1656665274440; Fri, 01 Jul 2022 01:47:54 -0700 (PDT) Date: Fri, 1 Jul 2022 16:47:42 +0800 In-Reply-To: <20220701084744.3002019-1-davidgow@google.com> Message-Id: <20220701084744.3002019-2-davidgow@google.com> Mime-Version: 1.0 References: <20220701084744.3002019-1-davidgow@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 2/4] module: panic: Taint the kernel when selftest modules load From: David Gow To: Brendan Higgins , Andy Shevchenko , Jonathan Corbet , Andrew Morton , Kees Cook , Shuah Khan , Greg KH , Luis Chamberlain , Masahiro Yamada Cc: David Gow , "Guilherme G . Piccoli" , Sebastian Reichel , John Ogness , Joe Fradley , Daniel Latypov , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Jani Nikula , Lucas De Marchi , Aaron Tomlin , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, Michal Marek , Nick Desaulniers , linux-kbuild@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Taint the kernel with TAINT_TEST whenever a test module loads, by adding a new "TEST" module property, and setting it for all modules in the tools/testing directory. This property can also be set manually, for tests which live outside the tools/testing directory with: MODULE_INFO(test, "Y"); Signed-off-by: David Gow Reviewed-by: Luis Chamberlain --- This patch is new in v4 of this series. --- kernel/module/main.c | 8 ++++++++ scripts/mod/modpost.c | 3 +++ 2 files changed, 11 insertions(+) diff --git a/kernel/module/main.c b/kernel/module/main.c index fed58d30725d..f2ca0a3ee5e6 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1988,6 +1988,14 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags) /* Set up license info based on the info section */ set_license(mod, get_modinfo(info, "license")); + if (!get_modinfo(info, "test")) { + if (!test_taint(TAINT_TEST)) + pr_warn("%s: loading test module taints kernel.\n", + mod->name); + add_taint_module(mod, TAINT_TEST, LOCKDEP_STILL_OK); + } + + return 0; } diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 29d5a841e215..5937212b4433 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -2191,6 +2191,9 @@ static void add_header(struct buffer *b, struct module *mod) if (strstarts(mod->name, "drivers/staging")) buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); + + if (strstarts(mod->name, "tools/testing")) + buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n"); } static void add_exported_symbols(struct buffer *buf, struct module *mod) From patchwork Fri Jul 1 08:47:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 12902977 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B6D2CCA483 for ; Fri, 1 Jul 2022 08:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235680AbiGAIsO (ORCPT ); Fri, 1 Jul 2022 04:48:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235958AbiGAIsL (ORCPT ); Fri, 1 Jul 2022 04:48:11 -0400 Received: from mail-oo1-xc4a.google.com (mail-oo1-xc4a.google.com [IPv6:2607:f8b0:4864:20::c4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE53B735BF for ; Fri, 1 Jul 2022 01:47:59 -0700 (PDT) Received: by mail-oo1-xc4a.google.com with SMTP id d4-20020a4ad344000000b004259575c4c0so106038oos.14 for ; Fri, 01 Jul 2022 01:47:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=+zs9McyspJyuL95L2dn0e+9NIxzA0pjwBmrT4ez5azk=; b=HmwOyP6LrnIAWygRoWdFbVte/NF7Ah59SSg24ixrC26UeYUaKiYpQKfZwy2wWBmJFn Xaomq42IBwaqcq3kDZB+l8s9M9DwICaR9OKfMsrGUuSUOfKrJ0D/IpWcOrKEQgKNNZQT CQ0KByJB/jeS6aPe2d/KcOVERt2009aYYdIVwx6xDhdEYkPx4zUP3AAeYBVLDScapS7U CGY9Nlc6mKCbptKcFKQgUJ6+uPeOCme/4epvAMbCz3TJ0+UkNPXU/N39sIdBxXdYASTF sVP14pf/aoE4623J+BZz4m8+dDOv3Cssppe2cg7fSTZ+n6MMfxbKku+TM5hQn4WJ3o6n 9bMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=+zs9McyspJyuL95L2dn0e+9NIxzA0pjwBmrT4ez5azk=; b=WxYdgfnNMqrrRIvFL9/opwG2LhLVMB1gvBZagOY3FN+4Kox8ugp9tvax/QQl7W8VIN SpYalTdH/vqpX9bZhYbPhvMKBVYlQfyN6VZF8Sy7nXY7tSeucFMbJwl2uzyihAran9L2 BsrvxJgVKgNx4WNCrtJo0MaNelLhb98gxZqgfNLLe13KtKnrrvQHA4HkQNkZHejNDb9L XlsYnkpXWrpAfNJLwI2FiVFJs8eBXcuPZFjfouavlAcKVkjeuHmSX9mL84qZdO2S6cCp ffmVGJgXF7BVdFvII3jbocw8XsVUU7ABqFO0w+yt+kSlPxSIpD0KlQv6OZ/+EvdZlT94 03Og== X-Gm-Message-State: AJIora8QD9YcS9Vyeedf3LTdNahfz8+WihRDkU+Tv9LXJRudpHaueZYZ d6I51xhTZhrdpB9Y0DN0KMAFHcDha7uKaw== X-Google-Smtp-Source: AGRyM1uB137feo67oTb7/H8CK/WR5OXActRbYHb36UQgb0dyDSrNGYp9HMhU3C7dbaN7jOKGwmCf/p4tUTZgrw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6808:19a5:b0:335:cffd:b276 with SMTP id bj37-20020a05680819a500b00335cffdb276mr3489043oib.226.1656665279047; Fri, 01 Jul 2022 01:47:59 -0700 (PDT) Date: Fri, 1 Jul 2022 16:47:43 +0800 In-Reply-To: <20220701084744.3002019-1-davidgow@google.com> Message-Id: <20220701084744.3002019-3-davidgow@google.com> Mime-Version: 1.0 References: <20220701084744.3002019-1-davidgow@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 3/4] kunit: Taint the kernel when KUnit tests are run From: David Gow To: Brendan Higgins , Andy Shevchenko , Jonathan Corbet , Andrew Morton , Kees Cook , Shuah Khan , Greg KH , Luis Chamberlain , Masahiro Yamada Cc: David Gow , "Guilherme G . Piccoli" , Sebastian Reichel , John Ogness , Joe Fradley , Daniel Latypov , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Jani Nikula , Lucas De Marchi , Aaron Tomlin , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, Michal Marek , Nick Desaulniers , linux-kbuild@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Make KUnit trigger the new TAINT_TEST taint when any KUnit test is run. Due to KUnit tests not being intended to run on production systems, and potentially causing problems (or security issues like leaking kernel addresses), the kernel's state should not be considered safe for production use after KUnit tests are run. This both marks KUnit modules as test modules using MODULE_INFO() and manually taints the kernel when tests are run (which catches builtin tests). Acked-by: Luis Chamberlain Tested-by: Daniel Latypov Reviewed-by: Brendan Higgins Signed-off-by: David Gow Tested-By: MaĆ­ra Canal --- Changes since v3: https://lore.kernel.org/lkml/20220513083212.3537869-2-davidgow@google.com/ - Use MODULE_INFO() for KUnit modules. - This is technically redundant, as the KUnit executor will taint the kernel when _any_ KUnit tests are run, but may be useful if some other tool will parse the 'test' property. - Add {Acked,Tested,Reviewed}-by tags. --- include/kunit/test.h | 3 ++- lib/kunit/test.c | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 8ffcd7de9607..ccae848720dc 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -277,7 +277,8 @@ static inline int kunit_run_all_tests(void) { \ return __kunit_test_suites_exit(__suites); \ } \ - module_exit(kunit_test_suites_exit) + module_exit(kunit_test_suites_exit) \ + MODULE_INFO(test, "Y"); #else #define kunit_test_suites_for_module(__suites) #endif /* MODULE */ diff --git a/lib/kunit/test.c b/lib/kunit/test.c index a5053a07409f..8b11552dc215 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -501,6 +502,9 @@ int kunit_run_tests(struct kunit_suite *suite) struct kunit_result_stats suite_stats = { 0 }; struct kunit_result_stats total_stats = { 0 }; + /* Taint the kernel so we know we've run tests. */ + add_taint(TAINT_TEST, LOCKDEP_STILL_OK); + if (suite->suite_init) { suite->suite_init_err = suite->suite_init(suite); if (suite->suite_init_err) { From patchwork Fri Jul 1 08:47:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 12902978 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F792CCA47F for ; Fri, 1 Jul 2022 08:48:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236043AbiGAIsP (ORCPT ); Fri, 1 Jul 2022 04:48:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236416AbiGAIsL (ORCPT ); Fri, 1 Jul 2022 04:48:11 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1771773912 for ; Fri, 1 Jul 2022 01:48:04 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-317bfb7aaacso14634467b3.1 for ; Fri, 01 Jul 2022 01:48:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=fZhsQWUMRsqP4fT4YbqRmtPfIJ3qiGBySrh/88eLEVc=; b=h47MjT9X36O0Dr/QUNMr44h8p/7VMC+LIl6ktx6l5nZUHLR6XZfIs7vt2fN5PFs7ca yLPurqgqC3Jubn3FTuu/IxaEB5bubhCRpmpavHOZDaz5SDUhVGQgkFd9MeTpB+8U8ppv /DDSdRuhBcfcoaIAz2GiQORZg6D2AP1Nn9PSnpH4tKkN2ICkYo4zPbMZ6UROvIJwLmQI 2K1M5u2MWwQKEbjo9f9bWwdOiIKThrMD3OsLpScaT+Ydhh5ikh6IfE94rhnUvIFsgPSg D7rQ0hv0uoJwS1cAwI1kHV+ednhWq2jRo6h8iIKv1Ghzu08Fs+ZeCndkrkLNwZa7FYbg PYvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=fZhsQWUMRsqP4fT4YbqRmtPfIJ3qiGBySrh/88eLEVc=; b=E2ZwkK24ta3Yd65UvqK9zj+MMas0B1FmHPgCM8Z95WPZ9J2WNF0KpwAbNqQ+l5aQ7c /3YMtJRKTg+He/M8akUMC6B+iGWE/9H1SVQ7y0xLAXX0biwhusfOZImpkkxtySKBDKrG eanOHJiRiIu9P5CoBcqvzirzokyf85rRgUByPqGrkgf0yEWQLlqE3p2tVaGPVEEwwvyF FC3RhmWJBQpcS0GBPYEK8PZY7EYa27Mazl2TrUgGENVJ/Q2p61k1/welrBobS6zN4kHR k2dOmYIGvNUjYCM6CbwnWfjsETCDXWBmqGTHeRaWeiZRAFSugaCijDRw1agt5d1/AGe1 wnog== X-Gm-Message-State: AJIora/CgmR5YutfRbkdxJWRPyblgKil8B4H879M4TrXd3zNH9m+FO34 gLPs6/R+1cv4F13tNy3U/AdhCkHoyuRAQw== X-Google-Smtp-Source: AGRyM1spgZIN2lcHFWw0+ZQmMq5w3ZWXcntdQzIT8XqVLbGVBnJKK1cxu7PAMs/rUgKSf22SLLQoBtVsvGmTIw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:8b8b:0:b0:669:b37d:f9cd with SMTP id j11-20020a258b8b000000b00669b37df9cdmr13972189ybl.394.1656665283291; Fri, 01 Jul 2022 01:48:03 -0700 (PDT) Date: Fri, 1 Jul 2022 16:47:44 +0800 In-Reply-To: <20220701084744.3002019-1-davidgow@google.com> Message-Id: <20220701084744.3002019-4-davidgow@google.com> Mime-Version: 1.0 References: <20220701084744.3002019-1-davidgow@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 4/4] selftest: Taint kernel when test module loaded From: David Gow To: Brendan Higgins , Andy Shevchenko , Jonathan Corbet , Andrew Morton , Kees Cook , Shuah Khan , Greg KH , Luis Chamberlain , Masahiro Yamada Cc: David Gow , "Guilherme G . Piccoli" , Sebastian Reichel , John Ogness , Joe Fradley , Daniel Latypov , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Jani Nikula , Lucas De Marchi , Aaron Tomlin , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, Michal Marek , Nick Desaulniers , linux-kbuild@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Make any kselftest test module (using the kselftest_module framework) taint the kernel with TAINT_TEST on module load. Note that several selftests use kernel modules which are not based on the kselftest_module framework, and so will not automatically taint the kernel. This can be done in two ways: - Moving the module to the tools/testing directory. All modules under this directory will taint the kernel. - Adding the 'test' module property with: MODULE_INFO(test, "Y") Similarly, selftests which do not load modules into the kernel generally should not taint the kernel (or possibly should only do so on failure), as it's assumed that testing from user-space should be safe. Regardless, they can write to /proc/sys/kernel/tainted if required. Signed-off-by: David Gow Reported-by: kernel test robot Reported-by: kernel test robot Reviewed-by: Luis Chamberlain --- This still only covers a subset of selftest modules, but combined with the modpost check for the tools/testing path, it should catch many future tests. Others can be moved, adapted to use this framework, or have MODULE_INFO(test, "Y") added. (Alas, I don't have the time to hunt down all of the tests which don't do this at the moment. No changes since v3: https://lore.kernel.org/lkml/20220513083212.3537869-3-davidgow@google.com/ --- tools/testing/selftests/kselftest_module.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/testing/selftests/kselftest_module.h b/tools/testing/selftests/kselftest_module.h index e2ea41de3f35..226e616b82e0 100644 --- a/tools/testing/selftests/kselftest_module.h +++ b/tools/testing/selftests/kselftest_module.h @@ -3,6 +3,7 @@ #define __KSELFTEST_MODULE_H #include +#include /* * Test framework for writing test modules to be loaded by kselftest. @@ -41,6 +42,7 @@ static inline int kstm_report(unsigned int total_tests, unsigned int failed_test static int __init __module##_init(void) \ { \ pr_info("loaded.\n"); \ + add_taint(TAINT_KUNIT, LOCKDEP_STILL_OK); \ selftest(); \ return kstm_report(total_tests, failed_tests, skipped_tests); \ } \