Skip to contentSkip to conversation

lib: Add test module for CONFIG_DEBUG_VIRTUAL

PATCH Applied Florian Fainelli 3 participants 6 messages Aug 8, 2017 – Aug 19, 2017 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: Add test module for CONFIG_DEBUG_VIRTUAL

Add a test module that allows testing that CONFIG_DEBUG_VIRTUAL works correctly, at least that it can catch invalid calls to virt_to_phys() against the non-linear kernel virtual address map.

---
 lib/Kconfig.debug        | 11 +++++++++++
 lib/Makefile             |  1 +
 lib/test_debug_virtual.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 lib/test_debug_virtual.c
lib/Kconfig.debug +11 -0
index 98fe715522e8..d16bbc12429f 100644
@@ -1902,6 +1902,17 @@ config TEST_KMOD
 
 	  If unsure, say N.
 
+config TEST_DEBUG_VIRTUAL
+	tristate "Test CONFIG_DEBUG_VIRTUAL feature"
+	depends on DEBUG_VIRTUAL
+	help
+	  Test the kernel's ability to detect incorrect calls to
+	  virt_to_phys() done against the non-linear part of the
+	  kernel's virtual address map.
+
+	  If unsure, say N.
+
+
 source "samples/Kconfig"
 
 source "lib/Kconfig.kgdb"
lib/Makefile +1 -0
index 40c18372b301..469ce5e24e4f 100644
@@ -62,6 +62,7 @@ obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
 obj-$(CONFIG_TEST_UUID) += test_uuid.o
 obj-$(CONFIG_TEST_PARMAN) += test_parman.o
 obj-$(CONFIG_TEST_KMOD) += test_kmod.o
+obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
 
 ifeq ($(CONFIG_DEBUG_KOBJECT),y)
 CFLAGS_kobject.o += -DDEBUG
lib/test_debug_virtual.c new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw
Quoted 78 lines · Florian Fainelli

Add a test module that allows testing that CONFIG_DEBUG_VIRTUAL works correctly, at least that it can catch invalid calls to virt_to_phys() against the non-linear kernel virtual address map.

---
 lib/Kconfig.debug        | 11 +++++++++++
 lib/Makefile             |  1 +
 lib/test_debug_virtual.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 lib/test_debug_virtual.c
lib/Kconfig.debug +11 -0
index 98fe715522e8..d16bbc12429f 100644
@@ -1902,6 +1902,17 @@ config TEST_KMOD
 
 	  If unsure, say N.
 
+config TEST_DEBUG_VIRTUAL
+	tristate "Test CONFIG_DEBUG_VIRTUAL feature"
+	depends on DEBUG_VIRTUAL
+	help
+	  Test the kernel's ability to detect incorrect calls to
+	  virt_to_phys() done against the non-linear part of the
+	  kernel's virtual address map.
+
+	  If unsure, say N.
+
+
 source "samples/Kconfig"
 
 source "lib/Kconfig.kgdb"
lib/Makefile +1 -0
index 40c18372b301..469ce5e24e4f 100644
@@ -62,6 +62,7 @@ obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
 obj-$(CONFIG_TEST_UUID) += test_uuid.o
 obj-$(CONFIG_TEST_PARMAN) += test_parman.o
 obj-$(CONFIG_TEST_KMOD) += test_kmod.o
+obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
 
 ifeq ($(CONFIG_DEBUG_KOBJECT),y)
 CFLAGS_kobject.o += -DDEBUG
lib/test_debug_virtual.c new file +26 -0
new file mode 100644
index 000000000000..b9cdeecc19dc
@@ -0,0 +1,49 @@
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/export.h>
+#include <linux/mm.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/sizes.h>
+
+#include <asm/page.h>
+#ifdef CONFIG_MIPS
+#include <asm/bootinfo.h>
+#endif
+
+struct foo {
+	unsigned int bar;
+};
+
+struct foo *foo;
+
+static int __init test_debug_virtual_init(void)
+{
+	phys_addr_t pa;
+	void *va;
+
+	va = (void *)VMALLOC_START;
+	pa = virt_to_phys(va);

lib/test_debug_virtual.c line 36

Quoted 10 lines
lib/test_debug_virtual.c new file +10 -0
+
+	pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);
+
+	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
+	if (!foo)
+		return -ENOMEM;
+
+	pa = virt_to_phys(foo);
+	va = foo;
+	pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);

Should there be a tests here of some sort? When should this fail, why? There is no docs on this self test, could one be added?

Luis

Quoted 17 lines
lib/test_debug_virtual.c new file +13 -0
+
+	return 0;
+}
+module_init(test_debug_virtual_init);
+
+static void __exit test_debug_virtual_exit(void)
+{
+	kfree(foo);
+}
+module_exit(test_debug_virtual_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Test module for CONFIG_DEBUG_VIRTUAL");
Signature · 1 line
2.9.3
Signature · 2 lines
Luis Rodriguez, SUSE LINUX GmbH
Maxfeldstrasse 5; D-90409 Nuernberg
#2 lore raw
Quoted 19 lines · Luis R. Rodriguez
Quoted 18 lines · Florian Fainelli

Add a test module that allows testing that CONFIG_DEBUG_VIRTUAL works correctly, at least that it can catch invalid calls to virt_to_phys() against the non-linear kernel virtual address map.

---

lib/test_debug_virtual.c new file +11 -0
+static int __init test_debug_virtual_init(void)
+{
+	phys_addr_t pa;
+	void *va;
+
+	va = (void *)VMALLOC_START;
+	pa = virt_to_phys(va);
+
+	pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);
+
+	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
Quoted 8 lines

lib/test_debug_virtual.c line 36

Quoted 6 lines
lib/test_debug_virtual.c new file +6 -0
+	if (!foo)
+		return -ENOMEM;
+
+	pa = virt_to_phys(foo);
+	va = foo;
+	pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);

Should there be a tests here of some sort? When should this fail, why?

There is no test per-se, the kernel will produce warning with CONFIG_DEBUG_VIRTUAL telling you that what you are doing is wrong.

Quoted 1 line

There is no docs on this self test, could one be added?

I suppose I could add one even though that just means pointing out the code that produces the warning?

Signature · 1 line
Florian
#3 lore raw
Quoted 29 lines · Florian Fainelli
Quoted 19 lines · Luis R. Rodriguez
Quoted 18 lines · Florian Fainelli

Add a test module that allows testing that CONFIG_DEBUG_VIRTUAL works correctly, at least that it can catch invalid calls to virt_to_phys() against the non-linear kernel virtual address map.

---

lib/test_debug_virtual.c new file +11 -0
+static int __init test_debug_virtual_init(void)
+{
+	phys_addr_t pa;
+	void *va;
+
+	va = (void *)VMALLOC_START;
+	pa = virt_to_phys(va);
+
+	pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);
+
+	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
Quoted 8 lines

lib/test_debug_virtual.c line 36

Quoted 6 lines
lib/test_debug_virtual.c new file +6 -0
+	if (!foo)
+		return -ENOMEM;
+
+	pa = virt_to_phys(foo);
+	va = foo;
+	pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);

Should there be a tests here of some sort? When should this fail, why?

Quoted 7 lines

There is no test per-se, the kernel will produce warning with CONFIG_DEBUG_VIRTUAL telling you that what you are doing is wrong.

Quoted 1 line

There is no docs on this self test, could one be added?

I suppose I could add one even though that just means pointing out the code that produces the warning?

A /* note */ indicating what you just said above would suffice then but typically tests return back to userspace an error, so another option would be to see if one could get a return value that an error happened and return that back to the module init. Grepping just for warning for an error seems error prone.

Luis

#4 lore raw
Quoted 35 lines · Luis R. Rodriguez
Quoted 34 lines · Florian Fainelli
Quoted 19 lines · Luis R. Rodriguez
Quoted 18 lines · Florian Fainelli

Add a test module that allows testing that CONFIG_DEBUG_VIRTUAL works correctly, at least that it can catch invalid calls to virt_to_phys() against the non-linear kernel virtual address map.

---

lib/test_debug_virtual.c new file +11 -0
+static int __init test_debug_virtual_init(void)
+{
+  phys_addr_t pa;
+  void *va;
+
+  va = (void *)VMALLOC_START;
+  pa = virt_to_phys(va);
+
+  pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);
+
+  foo = kzalloc(sizeof(*foo), GFP_KERNEL);
Quoted 8 lines

lib/test_debug_virtual.c line 36

Quoted 6 lines
lib/test_debug_virtual.c new file +6 -0
+  if (!foo)
+          return -ENOMEM;
+
+  pa = virt_to_phys(foo);
+  va = foo;
+  pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);

Should there be a tests here of some sort? When should this fail, why?

There is no test per-se, the kernel will produce warning with CONFIG_DEBUG_VIRTUAL telling you that what you are doing is wrong.

Quoted 1 line

There is no docs on this self test, could one be added?

Quoted 8 lines
Quoted 2 lines

I suppose I could add one even though that just means pointing out the code that produces the warning?

A /* note */ indicating what you just said above would suffice then but typically tests return back to userspace an error, so another option would be to see if one could get a return value that an error happened and return that back to the module init. Grepping just for warning for an error seems error prone.

If the test depends on the kernel's response (i.e. WARN, BUG, panic) that cannot be detected in the test itself, it may be better suited for lkdtm (drivers/misc/lkdtm*) which is almost entirely comprised of tests like that.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw
Quoted 45 lines · Kees Cook
Quoted 35 lines · Luis R. Rodriguez
Quoted 34 lines · Florian Fainelli
Quoted 19 lines · Luis R. Rodriguez
Quoted 18 lines · Florian Fainelli

Add a test module that allows testing that CONFIG_DEBUG_VIRTUAL works correctly, at least that it can catch invalid calls to virt_to_phys() against the non-linear kernel virtual address map.

---

lib/test_debug_virtual.c new file +11 -0
+static int __init test_debug_virtual_init(void)
+{
+  phys_addr_t pa;
+  void *va;
+
+  va = (void *)VMALLOC_START;
+  pa = virt_to_phys(va);
+
+  pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);
+
+  foo = kzalloc(sizeof(*foo), GFP_KERNEL);
Quoted 8 lines

lib/test_debug_virtual.c line 36

Quoted 6 lines
lib/test_debug_virtual.c new file +6 -0
+  if (!foo)
+          return -ENOMEM;
+
+  pa = virt_to_phys(foo);
+  va = foo;
+  pr_info("PA: %pa for VA: 0x%lx\n", &pa, (unsigned long)va);

Should there be a tests here of some sort? When should this fail, why?

There is no test per-se, the kernel will produce warning with CONFIG_DEBUG_VIRTUAL telling you that what you are doing is wrong.

Quoted 1 line

There is no docs on this self test, could one be added?

Quoted 8 lines
Quoted 2 lines

I suppose I could add one even though that just means pointing out the code that produces the warning?

A /* note */ indicating what you just said above would suffice then but typically tests return back to userspace an error, so another option would be to see if one could get a return value that an error happened and return that back to the module init. Grepping just for warning for an error seems error prone.

Quoted 4 lines

If the test depends on the kernel's response (i.e. WARN, BUG, panic) that cannot be detected in the test itself, it may be better suited for lkdtm (drivers/misc/lkdtm*) which is almost entirely comprised of tests like that.

OK, do you have any specific coding styles, naming guidelines or anything else that you would like to see for these lkdtm* modules?

On second thought it might be possible to produce both the warning and check the virtual address against PAGE_OFFSET and if it is outside, return an error during module_init(), I will experiment with that a little bit.

Signature · 1 line
Florian