test modules
cover test modules
Hi,
This is a pair of test modules I'd like to see in the tree. Instead of putting these in lkdtm, where I've been adding various tests that trigger crashes, these don't make sense there since they need to be either distinctly separate, or their pass/fail state don't need to crash the machine.
The first is entirely a no-op module, designed to allow simple testing of the module loading and verification interface. It's useful to have a module that has no other uses or dependencies so it can be reliably used for just testing module loading and verification.
The second is a module that exercises the user memory access functions, in an effort to make sure that we can quickly catch any regressions in boundary checking (e.g. like what was recently fixed on ARM).
Thanks,
-Kees
PATCH 1/2 test: add minimal module for verification testing
When doing module loading verification tests (for example, with module singing, or LSM hooks), it is very handy to have a module that can be built on all systems under test, isn't auto-loaded at boot, and has no device or similar dependencies. This creates the "test_module.ko" module for that purpose, which only reports its load and unload to printk.
--- kernel/Makefile | 1 + kernel/test_module.c | 26 ++++++++++++++++++++++++++ lib/Kconfig.debug | 14 ++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 kernel/test_module.c
kernel/Makefile +1 -0
@@ -59,6 +59,7 @@ obj-$(CONFIG_IKCONFIG) += configs.o obj-$(CONFIG_RESOURCE_COUNTERS) += res_counter.o obj-$(CONFIG_SMP) += stop_machine.o obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o +obj-$(CONFIG_TEST_MODULE) += test_module.o obj-$(CONFIG_AUDIT) += audit.o auditfilter.o obj-$(CONFIG_AUDITSYSCALL) += auditsc.o obj-$(CONFIG_AUDIT_WATCH) += audit_watch.o
kernel/test_module.c new file +26 -0
@@ -0,0 +1,26 @@ +/* +* "hello world" kernel module +*/ + +#define pr_fmt (fmt) "test_module: " fmt + +#include <linux/module.h> + +static int __init test_module_init (void ) +{ +pr_info ("Hello, world\n" ); + +return 0 ; +} + +module_init (test_module_init); + +static void __exit test_module_exit(void) +{ +pr_info ("Goodbye\n" ); +} + +module_exit (test_module_exit); + +MODULE_AUTHOR ("Kees Cook <[email protected]>" ); +MODULE_LICENSE ("GPL" );
lib/Kconfig.debug +14 -0
@@ -1578,6 +1578,20 @@ config DMA_API_DEBUG This option causes a performance degredation. Use only if you want to debug device drivers. If unsure, say N. +config TEST_MODULE + tristate "Test module loading with 'hello world' module" + default n + depends on m + help + This builds the "test_module" module that emits "Hello, world" + on printk when loaded. It is designed to be used for basic + evaluation of the module loading subsystem (for example when + validating module verification). It lacks any extra dependencies, + and will not normally be loaded by the system unless explicitly + request by name. + + If unsure, say N. + source "samples/Kconfig" source "lib/Kconfig.kgdb"
Signature · 1 line
1.7.9.5
Andrew Morton
maintainerRe: [PATCH 1/2] test: add minimal module for verification testing
Quoted 9 lines · On Tue, 3 Dec 2013 13:27:33 -0800 Kees Cook <[email protected]> wrote:
When doing module loading verification tests (for example, with module singing, or LSM hooks), it is very handy to have a module that can be built on all systems under test, isn't auto-loaded at boot, and has no device or similar dependencies. This creates the "test_module.ko" module for that purpose, which only reports its load and unload to printk.
kernel/test_module.c line 7
Quoted 7 lines
kernel/test_module.c new file +7
-0@@ -0,0 +1,26 @@ +/* +* "hello world" kernel module +*/ + +#define pr_fmt (fmt) "test_module: " fmt + +#include <linux/module.h>
It really should have a whole bunch more includes, rather than relying on winning the nested include lottery.
kernel/test_module.c line 11
Quoted 3 lines
kernel/test_module.c new file +3
-0+static int __init test_module_init (void ) +{ +pr_info ("Hello, world\n" );
pr_info is maybe too low a facility level? Pretty much the whole point of the module is to print this message, so let's ensure that it is visible.
Quoted 23 lines
kernel/test_module.c new file +14
-0+return 0 ; +} + +module_init (test_module_init); + +static void __exit test_module_exit(void) +{ +pr_info ("Goodbye\n" ); +} + +module_exit (test_module_exit); + +MODULE_AUTHOR ("Kees Cook <[email protected]>" ); +MODULE_LICENSE ("GPL" );lib/Kconfig.debug +1
-0@@ -1578,6 +1578,20 @@ config DMA_API_DEBUG This option causes a performance degredation. Use only if you want to debug device drivers. If unsure, say N. +config TEST_MODULE
lib/Kconfig.debug config DMA_API_DEBUG · line 1591
Quoted 10 lines
lib/Kconfig.debug +10
-0+ tristate "Test module loading with 'hello world' module" + default n + depends on m + help + This builds the "test_module" module that emits "Hello, world" + on printk when loaded. It is designed to be used for basic + evaluation of the module loading subsystem (for example when + validating module verification). It lacks any extra dependencies, + and will not normally be loaded by the system unless explicitly + request by name.
Would be useful to have a comment along these lines in test_module.c, so readers know why it exists.
Re: [PATCH 1/2] test: add minimal module for verification testing
In reply to Kees CookQuoted 5 lines · Kees Cook
When doing module loading verification tests (for example, with module singing, or LSM hooks), it is very handy to have a module that can be built on all systems under test, isn't auto-loaded at boot, and has no device or similar dependencies. This creates the "test_module.ko" module for that purpose, which only reports its load and unload to printk.
Bikeshed time!
Quoted 1 line
+#define
pr_fmt(fmt)"test_module: " fmt
Perhaps:
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Cheers,
Rusty.
PATCH 2/2 test: check copy_to/from_user boundary validation
In reply to Kees CookTo help avoid an architecture failing to correctly check kernel/user boundaries when handling copy_to_user, copy_from_user, put_user, or get_user, perform some simple tests and fail to load if any of them behave unexpectedly.
Specifically, this is to make sure there is a way to notice if things like what was fixed in 8404663f81d212918ff85f493649a7991209fa04 ("ARM:
7527/1: uaccess: explicitly check __user pointer when !CPU_USE_DOMAINS") ever regresses again, for any architecture.
--- kernel/Makefile | 1 + kernel/test_user_copy.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 13 ++++++ 3 files changed, 117 insertions(+) create mode 100644 kernel/test_user_copy.c
kernel/Makefile +1 -0
@@ -60,6 +60,7 @@ obj-$(CONFIG_RESOURCE_COUNTERS) += res_counter.o obj-$(CONFIG_SMP) += stop_machine.o obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o obj-$(CONFIG_TEST_MODULE) += test_module.o +obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o obj-$(CONFIG_AUDIT) += audit.o auditfilter.o obj-$(CONFIG_AUDITSYSCALL) += auditsc.o obj-$(CONFIG_AUDIT_WATCH) += audit_watch.o
kernel/test_user_copy.c new file +103 -0
103 lines not on this page · raw message
lib/Kconfig.debug +13 -0
@@ -1592,6 +1592,19 @@ config TEST_MODULE If unsure, say N. +config TEST_USER_COPY + tristate "Test user/kernel boundary protections" + default n + depends on m + help + This builds the "test_user_copy" module that runs sanity checks + on the copy_to/from_user infrastructure, making sure basic + user/kernel boundary testing is working. If it fails to load, + a regression has been detected in the user/kernel memory boundary + protections. + + If unsure, say N. + source "samples/Kconfig" source "lib/Kconfig.kgdb"
Signature · 1 line
1.7.9.5
Andrew Morton
maintainerRe: [PATCH 2/2] test: check copy_to/from_user boundary validation
Quoted 9 lines · On Tue, 3 Dec 2013 13:27:34 -0800 Kees Cook <[email protected]> wrote:
To help avoid an architecture failing to correctly check kernel/user boundaries when handling
copy_to_user,copy_from_user,put_user, orget_user, perform some simple tests and fail to load if any of them behave unexpectedly.Specifically, this is to make sure there is a way to notice if things like what was fixed in
8404663f81d212918ff85f493649a7991209fa04("ARM:
7527/1: uaccess: explicitly check__userpointer when !CPU_USE_DOMAINS") ever regresses again, for any architecture.
I guess the challenge will be to get anyone to remember to run this.
Really, this could be viewed as a candidate for tools/testing/selftests. The tests in there are userspace tests, and your userspace test would consist of "modprobe test_user_copy". The advantage of this is that your test will get included whenever someone runs the selftest suite. This is better than having it stranded over in ./kernel/.
Quoted 4 lines
--- kernel/Makefile | 1 + kernel/test_user_copy.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 13 ++++++
We already have a whole pile of test modules - seven of them reside in lib/ and I think there's an RCU one somewhere. Can we bring order to all of this? Some form of integration under tools/testing would be one approach.
If you're disinclined to undertake such a project at this time, I'd suggest these two go into lib/ so they are known about if/when someone goes for the big cleanup.
Re: [PATCH 2/2] test: check copy_to/from_user boundary validation
Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Tue, 3 Dec 2013 13:27:34 -0800 Kees Cook <[email protected]> wrote:
To help avoid an architecture failing to correctly check kernel/user boundaries when handling
copy_to_user,copy_from_user,put_user, orget_user, perform some simple tests and fail to load if any of them behave unexpectedly.
Quoted 6 lines
Quoted 4 lines
Specifically, this is to make sure there is a way to notice if things like what was fixed in
8404663f81d212918ff85f493649a7991209fa04("ARM:
7527/1: uaccess: explicitly check__userpointer when !CPU_USE_DOMAINS") ever regresses again, for any architecture.I guess the challenge will be to get anyone to remember to run this.
FWIW, I'll be running it. :) I'm sure Fengguang Wu could add it too.
Quoted 6 lines
Really, this could be viewed as a candidate for
tools/testing/selftests. The tests in there are userspace tests, and your userspace test would consist of "modprobetest_user_copy". The advantage of this is that your test will get included whenever someone runs the selftest suite. This is better than having it stranded over in./kernel/.
Sure, I'd be happy to add logic to that area too.
Quoted 9 lines
Quoted 4 lines
--- kernel/Makefile | 1 + kernel/test_user_copy.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 13 ++++++We already have a whole pile of test modules - seven of them reside in
lib/and I think there's an RCU one somewhere. Can we bring order to all of this? Some form of integration undertools/testingwould be one approach.
I expect to be adding more of these, and I'd like to see these collected in a single place as well. There are things like test_nx.c in the x86 tree (which actually doesn't work any more, but that's a separate issue).
Quoted 3 lines
If you're disinclined to undertake such a project at this time, I'd suggest these two go into
lib/so they are known about if/when someone goes for the big cleanup.
lib/ sounds good. I'll move them there.
Thanks!
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Re: [PATCH 2/2] test: check copy_to/from_user boundary validation
In reply to Kees Cookkernel/test_user_copy.c line 30
Quoted 11 lines · Kees Cook
To help avoid an architecture failing to correctly check kernel/user boundaries when handling
copy_to_user,copy_from_user,put_user, orget_user, perform some simple tests and fail to load if any of them behave unexpectedly.kernel/test_user_copy.c +4
-0+#define failed (msg) { \ + pr_warn(msg); \ + ret = -EINVAL; \ +}
That's an ugly macro.
Maybe use something like:
#define test(func, msg) \
({ \
int ret = func; \
if (ret) \
pr_warn("%s\n", msg); \
ret; \
})kernel/test_user_copy.c line 42
Quoted 11 lines
kernel/test_user_copy.c +11
-0+static int __init test_user_copy_init (void ) +{ +int ret =0 ; +char *kmem; +char __user *usermem; +unsigned long user_addr; +unsigned long value =0x5A ; + + kmem =kmalloc (PAGE_SIZE *2 ,GFP_KERNEL ); +if (!kmem) { +pr_warn ("Failed to allocate kernel memory\n" );
Unnecessary as there's a generic alloc OOM
with a dump_stack()
[]
kernel/test_user_copy.c line 59
Quoted 3 lines
kernel/test_user_copy.c +3
-0+/* Legitimate usage: none of these should fail. */ +if (copy_from_user (kmem, usermem,PAGE_SIZE )) +failed ("legitimate copy_from_user failed" );
ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE), "legitimate copy_from_user failed");
kernel/test_user_copy.c line 61
Quoted 2 lines
kernel/test_user_copy.c +2
-0+if (copy_to_user (usermem, kmem,PAGE_SIZE )) +failed ("legitimate copy_to_user failed" );
ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE), "legitimate copy_to_user failed");
etc.