linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
  • * [PATCH v3 1/3] panic: Taint kernel if tests are run
           [not found] <20220429043913.626647-1-davidgow@google.com>
           [not found] ` <20220430030019.803481-1-davidgow@google.com>
    @ 2022-05-13  8:32 ` David Gow
      2022-05-13 15:35   ` Luis Chamberlain
      2022-05-17 20:45   ` Brendan Higgins
      2022-05-13  8:32 ` [PATCH v3 2/3] kunit: Taint the kernel when KUnit " David Gow
      2022-05-13  8:32 ` [PATCH v3 3/3] selftest: Taint kernel when test module loaded David Gow
      3 siblings, 2 replies; 23+ messages in thread
    From: David Gow @ 2022-05-13  8:32 UTC (permalink / raw)
      To: Brendan Higgins, Andy Shevchenko, Jonathan Corbet, Andrew Morton,
    	Kees Cook, Shuah Khan, Greg KH, Luis Chamberlain
      Cc: David Gow, Guilherme G . Piccoli, Sebastian Reichel, John Ogness,
    	Joe Fradley, Daniel Latypov, kunit-dev, linux-kselftest,
    	linux-doc, linux-kernel, Jani Nikula, Lucas De Marchi,
    	Aaron Tomlin, linux-fsdevel, linux-block
    
    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.)
    
    Signed-off-by: David Gow <davidgow@google.com>
    ---
    
    Updated this to handle the most common case of selftest modules, in
    addition to KUnit tests. There's room for other tests or test frameworks
    to use this as well, either with a call to add_taint() from within the
    kernel, or by writing to /proc/sys/kernel/tainted.
    
    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 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..546f3071940d 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 (such as a KUnit 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 f5844908a089..2f5f2a9ecaf7 100644
    --- a/include/linux/panic.h
    +++ b/include/linux/panic.h
    @@ -74,7 +74,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 eb4dfb932c85..1cf707e3bacd 100644
    --- a/kernel/panic.c
    +++ b/kernel/panic.c
    @@ -404,6 +404,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 },
     };
     
     /**
    -- 
    2.36.0.550.gb090851708-goog
    
    
    ^ permalink raw reply related	[flat|nested] 23+ messages in thread
  • * [PATCH v3 2/3] kunit: Taint the kernel when KUnit tests are run
           [not found] <20220429043913.626647-1-davidgow@google.com>
           [not found] ` <20220430030019.803481-1-davidgow@google.com>
      2022-05-13  8:32 ` [PATCH v3 1/3] panic: Taint kernel if tests are run David Gow
    @ 2022-05-13  8:32 ` David Gow
      2022-05-13 15:36   ` Luis Chamberlain
                         ` (2 more replies)
      2022-05-13  8:32 ` [PATCH v3 3/3] selftest: Taint kernel when test module loaded David Gow
      3 siblings, 3 replies; 23+ messages in thread
    From: David Gow @ 2022-05-13  8:32 UTC (permalink / raw)
      To: Brendan Higgins, Andy Shevchenko, Jonathan Corbet, Andrew Morton,
    	Kees Cook, Shuah Khan, Greg KH, Luis Chamberlain
      Cc: David Gow, Guilherme G . Piccoli, Sebastian Reichel, John Ogness,
    	Joe Fradley, Daniel Latypov, kunit-dev, linux-kselftest,
    	linux-doc, linux-kernel, Jani Nikula, Lucas De Marchi,
    	Aaron Tomlin, linux-fsdevel, linux-block
    
    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.
    
    Signed-off-by: David Gow <davidgow@google.com>
    ---
     lib/kunit/test.c | 4 ++++
     1 file changed, 4 insertions(+)
    
    diff --git a/lib/kunit/test.c b/lib/kunit/test.c
    index 0f66c13d126e..2b808117bd4a 100644
    --- a/lib/kunit/test.c
    +++ b/lib/kunit/test.c
    @@ -11,6 +11,7 @@
     #include <kunit/test-bug.h>
     #include <linux/kernel.h>
     #include <linux/moduleparam.h>
    +#include <linux/panic.h>
     #include <linux/sched/debug.h>
     #include <linux/sched.h>
     
    @@ -498,6 +499,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);
    +
     	kunit_print_subtest_start(suite);
     
     	kunit_suite_for_each_test_case(suite, test_case) {
    -- 
    2.36.0.550.gb090851708-goog
    
    
    ^ permalink raw reply related	[flat|nested] 23+ messages in thread
  • * [PATCH v3 3/3] selftest: Taint kernel when test module loaded
           [not found] <20220429043913.626647-1-davidgow@google.com>
                       ` (2 preceding siblings ...)
      2022-05-13  8:32 ` [PATCH v3 2/3] kunit: Taint the kernel when KUnit " David Gow
    @ 2022-05-13  8:32 ` David Gow
      2022-05-13 15:38   ` Luis Chamberlain
      3 siblings, 1 reply; 23+ messages in thread
    From: David Gow @ 2022-05-13  8:32 UTC (permalink / raw)
      To: Brendan Higgins, Andy Shevchenko, Jonathan Corbet, Andrew Morton,
    	Kees Cook, Shuah Khan, Greg KH, Luis Chamberlain
      Cc: David Gow, Guilherme G . Piccoli, Sebastian Reichel, John Ogness,
    	Joe Fradley, Daniel Latypov, kunit-dev, linux-kselftest,
    	linux-doc, linux-kernel, Jani Nikula, Lucas De Marchi,
    	Aaron Tomlin, linux-fsdevel, linux-block
    
    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. These modules will have to be manually modified if they should
    taint the kernel this way.
    
    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 <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 <linux/module.h>
    +#include <linux/panic.h>
     
     /*
      * 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);	\
     }							\
    -- 
    2.36.0.550.gb090851708-goog
    
    
    ^ permalink raw reply related	[flat|nested] 23+ messages in thread

  • end of thread, other threads:[~2022-05-17 20:59 UTC | newest]
    
    Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
    -- links below jump to the message on this page --
         [not found] <20220429043913.626647-1-davidgow@google.com>
         [not found] ` <20220430030019.803481-1-davidgow@google.com>
         [not found]   ` <Ym7P7mCoMiQq99EM@bombadil.infradead.org>
    2022-05-01 18:24     ` [PATCH v2] kunit: Taint kernel if any tests run Luis Chamberlain
    2022-05-03  6:49       ` David Gow
    2022-05-04 14:51         ` Luis Chamberlain
    2022-05-04 16:25           ` Daniel Latypov
    2022-05-04 18:46             ` Luis Chamberlain
    2022-05-04 19:19               ` Daniel Latypov
    2022-05-04 21:12                 ` Luis Chamberlain
    2022-05-05  5:57                   ` Luis Chamberlain
    2022-05-06  7:01                     ` David Gow
    2022-05-09 20:43                       ` Luis Chamberlain
    2022-05-13  8:32 ` [PATCH v3 1/3] panic: Taint kernel if tests are run David Gow
    2022-05-13 15:35   ` Luis Chamberlain
    2022-05-17 20:45   ` Brendan Higgins
    2022-05-13  8:32 ` [PATCH v3 2/3] kunit: Taint the kernel when KUnit " David Gow
    2022-05-13 15:36   ` Luis Chamberlain
    2022-05-13 19:08   ` Daniel Latypov
    2022-05-14  3:04     ` David Gow
    2022-05-14 19:25       ` Daniel Latypov
    2022-05-17 20:58         ` Brendan Higgins
    2022-05-17 20:58   ` Brendan Higgins
    2022-05-13  8:32 ` [PATCH v3 3/3] selftest: Taint kernel when test module loaded David Gow
    2022-05-13 15:38   ` Luis Chamberlain
    2022-05-14  8:34     ` David Gow
    

    This is a public inbox, see mirroring instructions
    for how to clone and mirror all data and code used for this inbox;
    as well as URLs for NNTP newsgroup(s).