Received: by 2002:ac0:cd04:0:0:0:0:0 with SMTP id w4csp482086imn; Fri, 1 Jul 2022 21:27:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vUFK34ByaWgHTIkNj3uknOtpB/F9//DiO+rlVfoLMz8gWDIZQe031YcHwfKtRvuas4bHh3 X-Received: by 2002:a17:907:8689:b0:726:c8d3:8d97 with SMTP id qa9-20020a170907868900b00726c8d38d97mr16519003ejc.578.1656736027665; Fri, 01 Jul 2022 21:27:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656736027; cv=none; d=google.com; s=arc-20160816; b=reQo1v+04QiPL10Gat7vAeJMidb7k4w8QCg4dAs5LE5ArTRmD1e7ettdMdWsiGT4T/ TVdR9hY4hvPrgmnyie+KOaWroMu8VcrQMbK2VIOoi7hs0InK8/cY5diEkb0J2vDQLnZX ziNhyQ/XmJhaP6nVk9K7c46h7zOWipPxeEtUG1aA4sB/2IqLRen6U4FAUAH+2UOdhWGm 57TP5Ps7yztmD0Oc9dNWuy81ZM/eNyNQmBa65ENuZmCNV+N1GJSoCtLGWz6wUxo72a1N IjokOO1AQ3/Qxw5FTW8/6FIRSH/cJVWlswNaCe/MsdoZCV1eA5BYhQJeeaKsqXBhp4R8 PJHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=HBYnSHzQWg2Lp2cioqz+iG/q+4XSrXc7FbjoZdcHpbA=; b=chEwrofmxSckq2uQPNk8WlYYG2CXUg9CZTwsHK8hmy6kNWUsCKsEyyVJSkPzwjuSlS 0T489S/zo4ONJcvgmiozMTmjCgpKeAyKgGmsIS8cDgqHY3a4wqAhcyLDfrMrmY3jWHkJ JE7eOzdSmagktNj34kfWkMXfvOwHQTGqs3qNsuB1xaN9Q4d1Lgv+drszr6K7pOhr559/ yTLy7js1tf9ILNWhsf1wwaNwtHujFsceFAKSwYdQ6zwfTrWTrlkLpfLe0O+mC3TRm1FG +XATprgZrlx8Defakr68wHiVpagvxLzg5n3TIYHUT8Fv0WJf7GBp+xECDLuuv3RivA5D K2EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lxI8R+2B; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw28-20020a1709066a1c00b006e88579e94fsi5204752ejc.798.2022.07.01.21.26.40; Fri, 01 Jul 2022 21:27:07 -0700 (PDT) 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=@google.com header.s=20210112 header.b=lxI8R+2B; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232263AbiGBEKu (ORCPT + 99 others); Sat, 2 Jul 2022 00:10:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232144AbiGBEKi (ORCPT ); Sat, 2 Jul 2022 00:10:38 -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 75EFB34BAF for ; Fri, 1 Jul 2022 21:10:19 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-31c782f7cadso8061327b3.18 for ; Fri, 01 Jul 2022 21:10:19 -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=HBYnSHzQWg2Lp2cioqz+iG/q+4XSrXc7FbjoZdcHpbA=; b=lxI8R+2BiFyIZIQT3Z8E6VUUX7SJWJwlS4m8uYrDpLk9tJq1hJfYZ/wKsRPQ7un8xP BGC68H6zMkzJxgQEO3lAogc4mt1HjWX0CL0tpdOx+ihRO8huvjSujzsHIJDwr0qFUA+R HH0TCAR3RB5ZVNcDHtskNIwPUBSOmp/viRstPTFEgyxsS+clwidWKThmtmPKLi0+3lzh tcVE0aSlTOG60UOz8sUQtA5g3WgC/7svYkE/JA9yy5UrVvgWWwcwGc80vG+3PfHt9O+O L+PopdPOgg43U82wOfInv41nEGulTRdZOcZGJmmvghaGL9GT1q912vvmXvPuq5FlMN7v jsPw== 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=HBYnSHzQWg2Lp2cioqz+iG/q+4XSrXc7FbjoZdcHpbA=; b=GUJw66O+EK3Ai4qclFzHlLWP3OW9P2Ur251uTq2sY0qLCCVYxyo61GIJstluRu9ujz 2s4cQVGLWw2q8jEGtckeB+YaUARHjW8iurD1E0P0zZlIh56HfQL8Y4z7y5sSrcy4EvnE uG2nBFZsl4OXgxZzeUI4xKLHtHZOvEkO2vDfgPmqwSD5nbJ4d8/ANZoBz/Xkz6zftV+Y tMU5YkHze4E3eodfZQX0RlrC6qyYZ4mIdrIxrymD69sKqSzchqX6BP/wobE+op90UoGB hoX0fNxLoYt5Gs9qW3D2yz7EeWFGAnAOMppt/u7ADO2RHID3QVSFD33FOxjQ6+BYELf6 osgA== X-Gm-Message-State: AJIora+rOrJZz3wnDxZ5grnqYAZhm3Y047gxNRiOKUwoKWxBUXRVaFva Ut8iCZ1aV6J6iCBZEStJQKKavSylPJOZYA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:74a:0:b0:31c:c28:a620 with SMTP id 71-20020a81074a000000b0031c0c28a620mr21591367ywh.137.1656735018635; Fri, 01 Jul 2022 21:10:18 -0700 (PDT) Date: Sat, 2 Jul 2022 12:09:59 +0800 In-Reply-To: <20220702040959.3232874-1-davidgow@google.com> Message-Id: <20220702040959.3232874-4-davidgow@google.com> Mime-Version: 1.0 References: <20220702040959.3232874-1-davidgow@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v5 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 Make any kselftest test module (using the kselftest_module framework) taint the kernel with TAINT_TEST on module load. Also mark the module as a test module using MODULE_INFO(test, "Y") so that other tools can tell this is a test module. We can't rely solely on this, though, as these test modules are also often built-in. Finally, update the kselftest documentation to mention that the kernel should be tainted, and how to do so manually (as below). 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. Reviewed-by: Luis Chamberlain Signed-off-by: David Gow --- Documentation/dev-tools/kselftest.rst | 9 +++++++++ tools/testing/selftests/kselftest_module.h | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/Documentation/dev-tools/kselftest.rst b/Documentation/dev-tools/kselftest.rst index a833ecf12fbc..1096a9833550 100644 --- a/Documentation/dev-tools/kselftest.rst +++ b/Documentation/dev-tools/kselftest.rst @@ -250,6 +250,14 @@ assist writing kernel modules that are for use with kselftest: - ``tools/testing/selftests/kselftest_module.h`` - ``tools/testing/selftests/kselftest/module.sh`` +Note that test modules should taint the kernel with TAINT_TEST. This will +happen automatically for modules which are in the ``tools/testing/`` +directory, or for modules which use the ``kselftest_module.h`` header above. +Otherwise, you'll need to add ``MODULE_INFO(test, "Y")`` to your module +source. selftests which do not load modules typically should not taint the +kernel, but in cases where a non-test module is loaded, TEST_TAINT can be +applied from userspace by writing to ``/proc/sys/kernel/tainted``. + How to use ---------- @@ -308,6 +316,7 @@ A bare bones test module might look like this: KSTM_MODULE_LOADERS(test_foo); MODULE_AUTHOR("John Developer "); MODULE_LICENSE("GPL"); + MODULE_INFO(test, "Y"); Example test script ------------------- diff --git a/tools/testing/selftests/kselftest_module.h b/tools/testing/selftests/kselftest_module.h index e2ea41de3f35..63cd7487373f 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_TEST, LOCKDEP_STILL_OK); \ selftest(); \ return kstm_report(total_tests, failed_tests, skipped_tests); \ } \ @@ -51,4 +53,6 @@ static void __exit __module##_exit(void) \ module_init(__module##_init); \ module_exit(__module##_exit) +MODULE_INFO(test, "Y"); + #endif /* __KSELFTEST_MODULE_H */ -- 2.37.0.rc0.161.g10f37bed90-goog