Skip to contentSkip to conversation

lib: Convert test_user_copy to KUnit test

PATCH In review Vitor Massaru Iha 4 participants 12 messages Jul 21, 2020 – Aug 6, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 lib: Convert test_user_copy to KUnit test

This adds the conversion of the runtime tests of test_user_copy fuctions, from lib/test_user_copy.cto KUnit tests.

---
v2:
* splitted patch in 3:
- Allows to install and load modules in root filesystem;
- Provides an userspace memory context when tests are compiled as module;
- Convert test_user_copy to KUnit test;
* removed entry for CONFIG_TEST_USER_COPY;
* replaced pr_warn to KUNIT_EXPECT_FALSE_MSG in test macro to decrease the diff;
v3:
* rebased with last kunit branch
* Please apply this commit from kunit-fixes:
3f37d14b8a3152441f36b6bc74000996679f0998
And these from patchwork:
https://patchwork.kernel.org/patch/11676331/
https://patchwork.kernel.org/patch/11676335/

---
 lib/Kconfig.debug                           | 28 ++++++++------
 lib/Makefile                                |  2 +-
 lib/{test_user_copy.c => user_copy_kunit.c} | 42 +++++++++------------
 3 files changed, 35 insertions(+), 37 deletions(-)
 rename lib/{test_user_copy.c => user_copy_kunit.c} (91%)
lib/Kconfig.debug +16 -12
index 9ad9210d70a1..f699a3624ae7 100644
@@ -2078,18 +2078,6 @@ config TEST_VMALLOC
 
 	  If unsure, say N.
 
-config TEST_USER_COPY
-	tristate "Test user/kernel boundary protections"
-	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.
-
 config TEST_BPF
 	tristate "Test BPF filter functionality"
 	depends on m && NET
@@ -2154,6 +2142,22 @@ config SYSCTL_KUNIT_TEST
 
 	  If unsure, say N.
 
+config USER_COPY_KUNIT
+	tristate "KUnit Test for user/kernel boundary protections"
+	depends on KUNIT
+	depends on m
+	help
+	  This builds the "user_copy_kunit" 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.
+
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config LIST_KUNIT_TEST
 	tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/Makefile +1 -1
index b1c42c10073b..8c145f85accc 100644
@@ -78,7 +78,6 @@ obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
 obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
 obj-$(CONFIG_TEST_SORT) += test_sort.o
-obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
@@ -318,3 +317,4 @@ obj-$(CONFIG_OBJAGG) += objagg.o
 # KUnit tests
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
+obj-$(CONFIG_USER_COPY_KUNIT) += user_copy_kunit.o
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +18 -24

100 lines not on this page · raw message

base-commit: d43c7fb05765152d4d4a39a8ef957c4ea14d8847 --
2.26.2

#1 lore raw
Quoted 107 lines · Vitor Massaru Iha

This adds the conversion of the runtime tests of test_user_copy fuctions, from lib/test_user_copy.cto KUnit tests.

---
v2:
* splitted patch in 3:
- Allows to install and load modules in root filesystem;
- Provides an userspace memory context when tests are compiled as module;
- Convert test_user_copy to KUnit test;
* removed entry for CONFIG_TEST_USER_COPY;
* replaced pr_warn to KUNIT_EXPECT_FALSE_MSG in test macro to decrease the diff;
v3:
* rebased with last kunit branch
* Please apply this commit from kunit-fixes:
3f37d14b8a3152441f36b6bc74000996679f0998
And these from patchwork:
https://patchwork.kernel.org/patch/11676331/
https://patchwork.kernel.org/patch/11676335/

---
 lib/Kconfig.debug                           | 28 ++++++++------
 lib/Makefile                                |  2 +-
 lib/{test_user_copy.c => user_copy_kunit.c} | 42 +++++++++------------
 3 files changed, 35 insertions(+), 37 deletions(-)
 rename lib/{test_user_copy.c => user_copy_kunit.c} (91%)
lib/Kconfig.debug +16 -12
index 9ad9210d70a1..f699a3624ae7 100644
@@ -2078,18 +2078,6 @@ config TEST_VMALLOC
 
 	  If unsure, say N.
 
-config TEST_USER_COPY
-	tristate "Test user/kernel boundary protections"
-	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.
-
 config TEST_BPF
 	tristate "Test BPF filter functionality"
 	depends on m && NET
@@ -2154,6 +2142,22 @@ config SYSCTL_KUNIT_TEST
 
 	  If unsure, say N.
 
+config USER_COPY_KUNIT
+	tristate "KUnit Test for user/kernel boundary protections"
+	depends on KUNIT
+	depends on m
+	help
+	  This builds the "user_copy_kunit" 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.
+
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config LIST_KUNIT_TEST
 	tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/Makefile +1 -1
index b1c42c10073b..8c145f85accc 100644
@@ -78,7 +78,6 @@ obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
 obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
 obj-$(CONFIG_TEST_SORT) += test_sort.o
-obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
@@ -318,3 +317,4 @@ obj-$(CONFIG_OBJAGG) += objagg.o
 # KUnit tests
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
+obj-$(CONFIG_USER_COPY_KUNIT) += user_copy_kunit.o
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -0
similarity index 91%
rename from lib/test_user_copy.c
rename to lib/user_copy_kunit.c
index 5ff04d8fe971..a10ddd15b4cd 100644
@@ -16,6 +16,7 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include <linux/vmalloc.h>
+#include <kunit/test.h>
 
 /*
  * Several 32-bit architectures support 64-bit {get,put}_user() calls.

lib/user_copy_kunit.c line 39

Quoted 5 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -1
@@ -35,7 +36,7 @@
 ({									\
 	int cond = (condition);						\
 	if (cond)							\
-		pr_warn("[%d] " msg "\n", __LINE__, ##__VA_ARGS__);	\
+		KUNIT_EXPECT_FALSE_MSG(test, cond, msg, ##__VA_ARGS__);	\

I'm surprised any of this compiles with both a macro and arg named "test". :) Can you change the arg to something with more clarity? "context" or "kunit" seems better.

Quoted 31 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +3 -3
 	cond;								\
 })
 
@@ -44,7 +45,7 @@ static bool is_zeroed(void *from, size_t size)
 	return memchr_inv(from, 0x0, size) == NULL;
 }
 
-static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
+static int test_check_nonzero_user(struct kunit *test, char *kmem, char __user *umem, size_t size)
 {
 	int ret = 0;
 	size_t start, end, i, zero_start, zero_end;
@@ -102,7 +103,7 @@ static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
 	return ret;
 }
 
-static int test_copy_struct_from_user(char *kmem, char __user *umem,
+static int test_copy_struct_from_user(struct kunit *test, char *kmem, char __user *umem,
 				      size_t size)
 {
 	int ret = 0;
@@ -177,7 +178,7 @@ static int test_copy_struct_from_user(char *kmem, char __user *umem,
 	return ret;
 }
 
-static int __init test_user_copy_init(void)
+static void user_copy_test(struct kunit *test)
 {
 	int ret = 0;
 	char *kmem;

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 196

Quoted 6 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -2
@@ -192,16 +193,14 @@ static int __init test_user_copy_init(void)
 #endif
 
 	kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
-	if (!kmem)
-		return -ENOMEM;
+	KUNIT_EXPECT_FALSE_MSG(test, kmem == NULL, "kmalloc failed");

This would need to be an ASSERT, yes?

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 204

Quoted 10 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -2
 	user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
 			    PROT_READ | PROT_WRITE | PROT_EXEC,
 			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
 	if (user_addr >= (unsigned long)(TASK_SIZE)) {
-		pr_warn("Failed to allocate user memory\n");
 		kfree(kmem);
-		return -ENOMEM;
+		KUNIT_FAIL(test, "Failed to allocate user memory");
 	}

Why FAIL instead of ASSERT?

Quoted 15 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +2 -2
 	usermem = (char __user *)user_addr;
@@ -245,9 +244,9 @@ static int __init test_user_copy_init(void)
 #undef test_legit
 
 	/* Test usage of check_nonzero_user(). */
-	ret |= test_check_nonzero_user(kmem, usermem, 2 * PAGE_SIZE);
+	ret |= test_check_nonzero_user(test, kmem, usermem, 2 * PAGE_SIZE);
 	/* Test usage of copy_struct_from_user(). */
-	ret |= test_copy_struct_from_user(kmem, usermem, 2 * PAGE_SIZE);
+	ret |= test_copy_struct_from_user(test, kmem, usermem, 2 * PAGE_SIZE);
 
 	/*
 	 * Invalid usage: none of these copies should succeed.

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 318

Quoted 10 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +0 -7
@@ -309,23 +308,18 @@ static int __init test_user_copy_init(void)
 
 	vm_munmap(user_addr, PAGE_SIZE * 2);
 	kfree(kmem);
-
-	if (ret == 0) {
-		pr_info("tests passed.\n");
-		return 0;
-	}
-
-	return -EINVAL;

Does KUnit provide a end-of-test summary now?

Quoted 23 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +9 -7
 }
 
-module_init(test_user_copy_init);
-
-static void __exit test_user_copy_exit(void)
-{
-	pr_info("unloaded.\n");
-}
+static struct kunit_case user_copy_test_cases[] = {
+	KUNIT_CASE(user_copy_test),
+	{}
+};
 
-module_exit(test_user_copy_exit);
+static struct kunit_suite user_copy_test_suite = {
+	.name = "user_copy",
+	.test_cases = user_copy_test_cases,
+};
 
+kunit_test_suites(&user_copy_test_suite);
 MODULE_AUTHOR("Kees Cook <[email protected]>");
 MODULE_LICENSE("GPL");
Quoted 4 lines

base-commit: d43c7fb05765152d4d4a39a8ef957c4ea14d8847 --
2.26.2

Otherwise, yes, looking good.

Signature · 5 lines
Kees Cook
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#2 lore raw
Quoted 106 lines · Kees Cook
Quoted 104 lines · Vitor Massaru Iha

This adds the conversion of the runtime tests of test_user_copy fuctions, from lib/test_user_copy.cto KUnit tests.

---
v2:
* splitted patch in 3:
- Allows to install and load modules in root filesystem;
- Provides an userspace memory context when tests are compiled as module;
- Convert test_user_copy to KUnit test;
* removed entry for CONFIG_TEST_USER_COPY;
* replaced pr_warn to KUNIT_EXPECT_FALSE_MSG in test macro to decrease the diff;
v3:
* rebased with last kunit branch
* Please apply this commit from kunit-fixes:
3f37d14b8a3152441f36b6bc74000996679f0998
And these from patchwork:
https://patchwork.kernel.org/patch/11676331/
https://patchwork.kernel.org/patch/11676335/

---
 lib/Kconfig.debug                           | 28 ++++++++------
 lib/Makefile                                |  2 +-
 lib/{test_user_copy.c => user_copy_kunit.c} | 42 +++++++++------------
 3 files changed, 35 insertions(+), 37 deletions(-)
 rename lib/{test_user_copy.c => user_copy_kunit.c} (91%)
lib/Kconfig.debug +16 -12
index 9ad9210d70a1..f699a3624ae7 100644
@@ -2078,18 +2078,6 @@ config TEST_VMALLOC
 
        If unsure, say N.
 
-config TEST_USER_COPY
-     tristate "Test user/kernel boundary protections"
-     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.
-
 config TEST_BPF
      tristate "Test BPF filter functionality"
      depends on m && NET
@@ -2154,6 +2142,22 @@ config SYSCTL_KUNIT_TEST
 
        If unsure, say N.
 
+config USER_COPY_KUNIT
+     tristate "KUnit Test for user/kernel boundary protections"
+     depends on KUNIT
+     depends on m
+     help
+       This builds the "user_copy_kunit" 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.
+
+       For more information on KUnit and unit tests in general please refer
+       to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+       If unsure, say N.
+
 config LIST_KUNIT_TEST
      tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
      depends on KUNIT
lib/Makefile +1 -1
index b1c42c10073b..8c145f85accc 100644
@@ -78,7 +78,6 @@ obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
 obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
 obj-$(CONFIG_TEST_SORT) += test_sort.o
-obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
@@ -318,3 +317,4 @@ obj-$(CONFIG_OBJAGG) += objagg.o
 # KUnit tests
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
+obj-$(CONFIG_USER_COPY_KUNIT) += user_copy_kunit.o
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -0
similarity index 91%
rename from lib/test_user_copy.c
rename to lib/user_copy_kunit.c
index 5ff04d8fe971..a10ddd15b4cd 100644
@@ -16,6 +16,7 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include <linux/vmalloc.h>
+#include <kunit/test.h>
 
Quoted 12 lines

lib/user_copy_kunit.c line 39

Quoted 8 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -1
 /*
  * Several 32-bit architectures support 64-bit {get,put}_user() calls.
@@ -35,7 +36,7 @@
 ({                                                                   \
      int cond = (condition);                                         \
      if (cond)                                                       \
-             pr_warn("[%d] " msg "\n", __LINE__, ##__VA_ARGS__);     \
+             KUNIT_EXPECT_FALSE_MSG(test, cond, msg, ##__VA_ARGS__); \

I'm surprised any of this compiles with both a macro and arg named "test". :) Can you change the arg to something with more clarity? "context" or "kunit" seems better.

It will be out of the standard of the other tests in KUnit, but I agree that I should not use the same name "test" in the argument and in the name of the macro.
I'll replace it with "context" instead of "test" in arg.

Quoted 34 lines
Quoted 34 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +3 -3
      cond;                                                           \
 })
 
@@ -44,7 +45,7 @@ static bool is_zeroed(void *from, size_t size)
      return memchr_inv(from, 0x0, size) == NULL;
 }
 
-static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
+static int test_check_nonzero_user(struct kunit *test, char *kmem, char __user *umem, size_t size)
 {
      int ret = 0;
      size_t start, end, i, zero_start, zero_end;
@@ -102,7 +103,7 @@ static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
      return ret;
 }
 
-static int test_copy_struct_from_user(char *kmem, char __user *umem,
+static int test_copy_struct_from_user(struct kunit *test, char *kmem, char __user *umem,
                                    size_t size)
 {
      int ret = 0;
@@ -177,7 +178,7 @@ static int test_copy_struct_from_user(char *kmem, char __user *umem,
      return ret;
 }
 
-static int __init test_user_copy_init(void)
+static void user_copy_test(struct kunit *test)
 {
      int ret = 0;
      char *kmem;
@@ -192,16 +193,14 @@ static int __init test_user_copy_init(void)
 #endif
 
Quoted 6 lines

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 196

Quoted 4 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -2
      kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
-     if (!kmem)
-             return -ENOMEM;
+     KUNIT_EXPECT_FALSE_MSG(test, kmem == NULL, "kmalloc failed");

This would need to be an ASSERT, yes?

Yep, I'll fix it.

Quoted 12 lines

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 204

Quoted 11 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -2
      user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
                          PROT_READ | PROT_WRITE | PROT_EXEC,
                          MAP_ANONYMOUS | MAP_PRIVATE, 0);
      if (user_addr >= (unsigned long)(TASK_SIZE)) {
-             pr_warn("Failed to allocate user memory\n");
              kfree(kmem);
-             return -ENOMEM;
+             KUNIT_FAIL(test, "Failed to allocate user memory");
      }
Quoted 1 line

Why FAIL instead of ASSERT?

I did it this way so I wouldn't have to test twice if I had a memory allocation problem,
once in the "if" and once in the ASSERT, so the memory of the other kmalloc is freed
in case of memory allocation error in this memory allocation.

Quoted 17 lines
Quoted 17 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +2 -2
      usermem = (char __user *)user_addr;
@@ -245,9 +244,9 @@ static int __init test_user_copy_init(void)
 #undef test_legit
 
      /* Test usage of check_nonzero_user(). */
-     ret |= test_check_nonzero_user(kmem, usermem, 2 * PAGE_SIZE);
+     ret |= test_check_nonzero_user(test, kmem, usermem, 2 * PAGE_SIZE);
      /* Test usage of copy_struct_from_user(). */
-     ret |= test_copy_struct_from_user(kmem, usermem, 2 * PAGE_SIZE);
+     ret |= test_copy_struct_from_user(test, kmem, usermem, 2 * PAGE_SIZE);
 
      /*
       * Invalid usage: none of these copies should succeed.
@@ -309,23 +308,18 @@ static int __init test_user_copy_init(void)
 
Quoted 11 lines

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 318

Quoted 9 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +0 -7
      vm_munmap(user_addr, PAGE_SIZE * 2);
      kfree(kmem);
-
-     if (ret == 0) {
-             pr_info("tests passed.\n");
-             return 0;
-     }
-
-     return -EINVAL;

Does KUnit provide a end-of-test summary now?

When you talk about end-of-test summary, is it what is written in dmesg and not the kunit-tool?

Quoted 31 lines
Quoted 24 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +9 -7
 }
 
-module_init(test_user_copy_init);
-
-static void __exit test_user_copy_exit(void)
-{
-     pr_info("unloaded.\n");
-}
+static struct kunit_case user_copy_test_cases[] = {
+     KUNIT_CASE(user_copy_test),
+     {}
+};
 
-module_exit(test_user_copy_exit);
+static struct kunit_suite user_copy_test_suite = {
+     .name = "user_copy",
+     .test_cases = user_copy_test_cases,
+};
 
+kunit_test_suites(&user_copy_test_suite);
 MODULE_AUTHOR("Kees Cook <[email protected]>");
 MODULE_LICENSE("GPL");
Quoted 4 lines

base-commit: d43c7fb05765152d4d4a39a8ef957c4ea14d8847 --
2.26.2

Otherwise, yes, looking good.

Quoted 2 lines

--
Kees Cook

Thanks for the review!

#3 lore raw
Quoted 181 lines · Vitor Massaru Iha
Quoted 106 lines · Kees Cook
Quoted 104 lines · Vitor Massaru Iha

This adds the conversion of the runtime tests of test_user_copy fuctions, from lib/test_user_copy.cto KUnit tests.

---
v2:
* splitted patch in 3:
- Allows to install and load modules in root filesystem;
- Provides an userspace memory context when tests are compiled as module;
- Convert test_user_copy to KUnit test;
* removed entry for CONFIG_TEST_USER_COPY;
* replaced pr_warn to KUNIT_EXPECT_FALSE_MSG in test macro to decrease the diff;
v3:
* rebased with last kunit branch
* Please apply this commit from kunit-fixes:
3f37d14b8a3152441f36b6bc74000996679f0998
And these from patchwork:
https://patchwork.kernel.org/patch/11676331/
https://patchwork.kernel.org/patch/11676335/

---
 lib/Kconfig.debug                           | 28 ++++++++------
 lib/Makefile                                |  2 +-
 lib/{test_user_copy.c => user_copy_kunit.c} | 42 +++++++++------------
 3 files changed, 35 insertions(+), 37 deletions(-)
 rename lib/{test_user_copy.c => user_copy_kunit.c} (91%)
lib/Kconfig.debug +16 -12
index 9ad9210d70a1..f699a3624ae7 100644
@@ -2078,18 +2078,6 @@ config TEST_VMALLOC
 
        If unsure, say N.
 
-config TEST_USER_COPY
-     tristate "Test user/kernel boundary protections"
-     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.
-
 config TEST_BPF
      tristate "Test BPF filter functionality"
      depends on m && NET
@@ -2154,6 +2142,22 @@ config SYSCTL_KUNIT_TEST
 
        If unsure, say N.
 
+config USER_COPY_KUNIT
+     tristate "KUnit Test for user/kernel boundary protections"
+     depends on KUNIT
+     depends on m
+     help
+       This builds the "user_copy_kunit" 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.
+
+       For more information on KUnit and unit tests in general please refer
+       to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+       If unsure, say N.
+
 config LIST_KUNIT_TEST
      tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
      depends on KUNIT
lib/Makefile +1 -1
index b1c42c10073b..8c145f85accc 100644
@@ -78,7 +78,6 @@ obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
 obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
 obj-$(CONFIG_TEST_SORT) += test_sort.o
-obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
@@ -318,3 +317,4 @@ obj-$(CONFIG_OBJAGG) += objagg.o
 # KUnit tests
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
+obj-$(CONFIG_USER_COPY_KUNIT) += user_copy_kunit.o
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -0
similarity index 91%
rename from lib/test_user_copy.c
rename to lib/user_copy_kunit.c
index 5ff04d8fe971..a10ddd15b4cd 100644
@@ -16,6 +16,7 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include <linux/vmalloc.h>
+#include <kunit/test.h>
 
Quoted 12 lines

lib/user_copy_kunit.c line 39

Quoted 8 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -1
 /*
  * Several 32-bit architectures support 64-bit {get,put}_user() calls.
@@ -35,7 +36,7 @@
 ({                                                                   \
      int cond = (condition);                                         \
      if (cond)                                                       \
-             pr_warn("[%d] " msg "\n", __LINE__, ##__VA_ARGS__);     \
+             KUNIT_EXPECT_FALSE_MSG(test, cond, msg, ##__VA_ARGS__); \

I'm surprised any of this compiles with both a macro and arg named "test". :) Can you change the arg to something with more clarity? "context" or "kunit" seems better.

It will be out of the standard of the other tests in KUnit, but I agree that I should not use the same name "test" in the argument and in the name of the macro.
I'll replace it with "context" instead of "test" in arg.

Quoted 34 lines
Quoted 34 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +3 -3
      cond;                                                           \
 })
 
@@ -44,7 +45,7 @@ static bool is_zeroed(void *from, size_t size)
      return memchr_inv(from, 0x0, size) == NULL;
 }
 
-static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
+static int test_check_nonzero_user(struct kunit *test, char *kmem, char __user *umem, size_t size)
 {
      int ret = 0;
      size_t start, end, i, zero_start, zero_end;
@@ -102,7 +103,7 @@ static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
      return ret;
 }
 
-static int test_copy_struct_from_user(char *kmem, char __user *umem,
+static int test_copy_struct_from_user(struct kunit *test, char *kmem, char __user *umem,
                                    size_t size)
 {
      int ret = 0;
@@ -177,7 +178,7 @@ static int test_copy_struct_from_user(char *kmem, char __user *umem,
      return ret;
 }
 
-static int __init test_user_copy_init(void)
+static void user_copy_test(struct kunit *test)
 {
      int ret = 0;
      char *kmem;
@@ -192,16 +193,14 @@ static int __init test_user_copy_init(void)
 #endif
 
Quoted 6 lines

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 196

Quoted 4 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -2
      kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
-     if (!kmem)
-             return -ENOMEM;
+     KUNIT_EXPECT_FALSE_MSG(test, kmem == NULL, "kmalloc failed");

This would need to be an ASSERT, yes?

Yep, I'll fix it.

Quoted 12 lines
Quoted 11 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +1 -2
      user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
                          PROT_READ | PROT_WRITE | PROT_EXEC,
                          MAP_ANONYMOUS | MAP_PRIVATE, 0);
      if (user_addr >= (unsigned long)(TASK_SIZE)) {
-             pr_warn("Failed to allocate user memory\n");
              kfree(kmem);
-             return -ENOMEM;
+             KUNIT_FAIL(test, "Failed to allocate user memory");
      }
Quoted 7 lines
Quoted 1 line

Why FAIL instead of ASSERT?

I did it this way so I wouldn't have to test twice if I had a memory allocation problem,
once in the "if" and once in the ASSERT, so the memory of the other kmalloc is freed
in case of memory allocation error in this memory allocation.

Hm, In this case the test needs to stop, I'll fix it.

Quoted 68 lines
Quoted 18 lines
Quoted 18 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +2 -2
      usermem = (char __user *)user_addr;
@@ -245,9 +244,9 @@ static int __init test_user_copy_init(void)
 #undef test_legit
 
      /* Test usage of check_nonzero_user(). */
-     ret |= test_check_nonzero_user(kmem, usermem, 2 * PAGE_SIZE);
+     ret |= test_check_nonzero_user(test, kmem, usermem, 2 * PAGE_SIZE);
      /* Test usage of copy_struct_from_user(). */
-     ret |= test_copy_struct_from_user(kmem, usermem, 2 * PAGE_SIZE);
+     ret |= test_copy_struct_from_user(test, kmem, usermem, 2 * PAGE_SIZE);
 
      /*
       * Invalid usage: none of these copies should succeed.
@@ -309,23 +308,18 @@ static int __init test_user_copy_init(void)
 
Quoted 11 lines

lib/user_copy_kunit.c static int __init test_user_copy_init(void) · line 318

Quoted 9 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +0 -7
      vm_munmap(user_addr, PAGE_SIZE * 2);
      kfree(kmem);
-
-     if (ret == 0) {
-             pr_info("tests passed.\n");
-             return 0;
-     }
-
-     return -EINVAL;

Does KUnit provide a end-of-test summary now?

When you talk about end-of-test summary, is it what is written in dmesg and not the kunit-tool?

Quoted 31 lines
Quoted 24 lines
lib/user_copy_kunit.c renamed from lib/test_user_copy.c +9 -7
 }
 
-module_init(test_user_copy_init);
-
-static void __exit test_user_copy_exit(void)
-{
-     pr_info("unloaded.\n");
-}
+static struct kunit_case user_copy_test_cases[] = {
+     KUNIT_CASE(user_copy_test),
+     {}
+};
 
-module_exit(test_user_copy_exit);
+static struct kunit_suite user_copy_test_suite = {
+     .name = "user_copy",
+     .test_cases = user_copy_test_cases,
+};
 
+kunit_test_suites(&user_copy_test_suite);
 MODULE_AUTHOR("Kees Cook <[email protected]>");
 MODULE_LICENSE("GPL");
Quoted 4 lines

base-commit: d43c7fb05765152d4d4a39a8ef957c4ea14d8847 --
2.26.2

Otherwise, yes, looking good.

Quoted 2 lines

--
Kees Cook

Thanks for the review!

#4 lore raw
In reply to Vitor Massaru Iha
Quoted 2 lines · Vitor Massaru Iha

When you talk about end-of-test summary, is it what is written in dmesg and not the kunit-tool?

Right, if I build this as a module and do "modprobe user_copy_kunit", what will show up in dmesg?

Signature · 1 line
Kees Cook
#5 lore raw
Quoted 7 lines · Kees Cook
Quoted 2 lines · Vitor Massaru Iha

When you talk about end-of-test summary, is it what is written in dmesg and not the kunit-tool?

Right, if I build this as a module and do "modprobe user_copy_kunit", what will show up in dmesg?

No, It doesn't. I'll put the messages again.

#6 lore raw
Quoted 10 lines · Vitor Massaru Iha
Quoted 7 lines · Kees Cook
Quoted 2 lines · Vitor Massaru Iha

When you talk about end-of-test summary, is it what is written in dmesg and not the kunit-tool?

Right, if I build this as a module and do "modprobe user_copy_kunit", what will show up in dmesg?

No, It doesn't. I'll put the messages again.

Would it be possible to add that behavior to the core KUnit output? Then all module-based tests would include a summary line?

Signature · 1 line
Kees Cook
#7 lore raw
Quoted 8 lines · Kees Cook
Quoted 6 lines · Vitor Massaru Iha
Quoted 5 lines · Kees Cook
Quoted 2 lines · Vitor Massaru Iha

When you talk about end-of-test summary, is it what is written in dmesg and not the kunit-tool?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Right, if I build this as a module and do "modprobe user_copy_kunit", what will show up in dmesg?

No, It doesn't. I'll put the messages again.

Would it be possible to add that behavior to the core KUnit output? Then all module-based tests would include a summary line?

I will check what can be done.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#8 lore raw
In reply to Kees Cook
Quoted 8 lines · Kees Cook
Quoted 6 lines · Vitor Massaru Iha
Quoted 5 lines · Kees Cook
Quoted 2 lines · Vitor Massaru Iha

When you talk about end-of-test summary, is it what is written in dmesg and not the kunit-tool?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Right, if I build this as a module and do "modprobe user_copy_kunit", what will show up in dmesg?

No, It doesn't. I'll put the messages again.

Would it be possible to add that behavior to the core KUnit output? Then all module-based tests would include a summary line?

Nowadays with modprobe this is shown, is it necessary to add anything else?

root@(none):/# modprobe user_copy_kunit
TAP version 14
    # Subtest: user_copy
    1..1
random: fast init done
    ok 1 - user_copy_test
ok 1 - user_copy
root@(none):/#
Quoted 3 lines

--
Kees Cook

#9 lore raw
In reply to Vitor Massaru Iha

Hi,

Quoted 4 lines · Vitor Massaru Iha

This adds the conversion of the runtime tests of test_user_copy fuctions, from lib/test_user_copy.cto KUnit tests.

These tests are failing (at least) for arm-v7, loongarch, and mips qemu emulations; see below for failure logs.

Any idea if those might be architecture problems, problems with qemu, or problems with the test ?

Thanks,
Guenter

---
On arm:

[   13.097105]     # usercopy_test_valid: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.097105]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.097105]         user_addr == 4294967284 (0xfffffff4)
[   13.097105]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.097105] Failed to allocate user memory
[   13.098876]     # usercopy_test_valid: pass:0 fail:1 skip:0 total:1
[   13.099378]     not ok 1 usercopy_test_valid
[   13.101143]     # usercopy_test_invalid: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.101143]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.101143]         user_addr == 4294967284 (0xfffffff4)
[   13.101143]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.101143] Failed to allocate user memory
[   13.102726]     # usercopy_test_invalid: pass:0 fail:1 skip:0 total:1
[   13.103167]     not ok 2 usercopy_test_invalid
[   13.104744]     # usercopy_test_check_nonzero_user: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.104744]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.104744]         user_addr == 4294967284 (0xfffffff4)
[   13.104744]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.104744] Failed to allocate user memory
[   13.106485]     # usercopy_test_check_nonzero_user: pass:0 fail:1 skip:0 total:1
[   13.106935]     not ok 3 usercopy_test_check_nonzero_user
[   13.108812]     # usercopy_test_copy_struct_from_user: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.108812]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.108812]         user_addr == 4294967284 (0xfffffff4)
[   13.108812]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.108812] Failed to allocate user memory
[   13.110643]     # usercopy_test_copy_struct_from_user: pass:0 fail:1 skip:0 total:1
[   13.111096]     not ok 4 usercopy_test_copy_struct_from_user
[   13.111260] # usercopy: pass:0 fail:4 skip:0 total:4
[   13.111401] # Totals: pass:0 fail:4 skip:0 total:4
[   13.111533] not ok 40 usercopy

On loongarch:

[    5.202648] CPU 0 Unable to handle kernel paging request at virtual address 0000000000000008, era == 90000000040f4b60, ra == 900000000436e558
[    5.203061] Oops[#1]:
[    5.203288] CPU: 0 UID: 0 PID: 877 Comm: kunit_try_catch Tainted: G                 N 6.11.0-rc2+ #1
[    5.203469] Tainted: [N]=TEST
[    5.203523] Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015
[    5.203686] pc 90000000040f4b60 ra 900000000436e558 tp 9000000107dec000 sp 9000000107defc90
[    5.203801] a0 ffffffffffff4000 a1 90000001063554f8 a2 0000000000000000 a3 9000000107defb84
[    5.203913] a4 0000000000000001 a5 0000000000000000 a6 900000000696e030 a7 9000000004b617c0
[    5.204025] t0 9000000104ab8840 t1 0000800000000000 t2 0000000000208040 t3 9000000107dec000
[    5.204134] t4 0000000000000000 t5 9000000006f39f10 t6 0000000000000001 t7 0000000000006000
[    5.204245] t8 0000000045eeca10 u0 9000000004b61818 s9 90000001002e7e40 s0 0000000008000000
[    5.204355] s1 9000000104b58040 s2 0000000000000000 s3 fffffffffffff000 s4 9000000005f62d00
[    5.204465] s5 9000000104ab8840 s6 90000000061752e8 s7 0000000000000000 s8 9000000004144df8
[    5.204580]    ra: 900000000436e558 arch_pick_mmap_layout+0xa0/0x1fc
[    5.204928]   ERA: 90000000040f4b60 stack_top+0x58/0xa8
[    5.205009]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
[    5.205189]  PRMD: 00000004 (PPLV0 +PIE -PWE)
[    5.205286]  EUEN: 00000000 (-FPE -SXE -ASXE -BTE)
[    5.205398]  ECFG: 00071c1d (LIE=0,2-4,10-12 VS=7)
[    5.205629] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
[    5.205732]  BADV: 0000000000000008
[    5.205802]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
[    5.205897] Modules linked in:
[    5.205982] Process kunit_try_catch (pid: 877, threadinfo=9000000107dec000, task=9000000104ab8840)
[    5.206120] Stack : 9000000005f62d00 fffffffffffff000 900000000616f000 9000000107da5b40
[    5.206323]         9000000107defd58 9000000004a6cce4 0000000000000dc0 9000000104b58040
[    5.206444]         9000000100004ac0 0000000000000000 0000000000000000 0e03af9ea058218b
[    5.206562]         900000000616f000 90000001002d7b00 9000000107da5b40 9000000004a6c2c4
[    5.206681]         90000000061752e8 9000000104ab8840 9000000005f62d00 fffffffffffff000
[    5.206799]         900000000616f000 90000001002d7b00 9000000107da5b40 9000000004a6cb9c
[    5.206918]         9000000107cc8000 0000000000000000 0000000000000000 0000000000008000
[    5.207035]         0000000000000007 0000000000000022 0000000000000000 0e03af9ea058218b
[    5.207153]         900000000616f000 90000001079d2f00 90000001002d7b00 9000000004ba6a60
[    5.207271]         9000000104ab9170 90000000056c0788 00000000000000b0 0e03af9ea058218b
[    5.207389]         ...
[    5.207450] Call Trace:
[    5.207467] [<90000000040f4b60>] stack_top+0x58/0xa8
[    5.207588] [<900000000436e554>] arch_pick_mmap_layout+0x9c/0x1fc
[    5.207675] [<9000000004a6cce0>] kunit_vm_mmap_init+0x104/0x12c
[    5.207758] [<9000000004a6c2c0>] __kunit_add_resource+0x48/0xd0
[    5.207839] [<9000000004a6cb98>] kunit_vm_mmap+0x84/0xc8
[    5.207913] [<9000000004ba6a5c>] usercopy_test_init+0xb8/0x25c
[    5.207993] [<9000000004a6c064>] kunit_try_run_case+0x58/0x184
[    5.208073] [<9000000004a6e260>] kunit_generic_run_threadfn_adapter+0x20/0x48
[    5.208168] [<9000000004144914>] kthread+0x130/0x140
[    5.208238] [<90000000040f1c04>] ret_from_kernel_thread+0x8/0xa4
[    5.208323]
[    5.208370] Code: 15fffe84  26165190  2400158e <28c0220c> 14000070  03bffe10  0010c18c  15ffff90  0014c18c
[    5.208583]
[    5.208822] ---[ end trace 0000000000000000 ]---
[    5.209975]     # usercopy_test_valid: try faulted: last line seen lib/usercopy_kunit.c:304
[    5.210227]     # usercopy_test_valid: internal error occurred preventing test case from running: -4
[    5.210890]     # usercopy_test_valid: pass:0 fail:1 skip:0 total:1
[    5.210942]     not ok 1 usercopy_test_valid

On mips:

CPU 0 Unable to handle kernel paging request at virtual address 00000018, epc == 80105f14, ra == 80299028 Oops[#1]:

CPU: 0 UID: 0 PID: 783 Comm: kunit_try_catch Tainted: G                 N 6.11.0-rc2-00102-g8857973f206d #1
Tainted: [N]=TEST
Hardware name: mti,malta
$ 0   : 00000000 00000001 00000000 00000000
$ 4   : 00000000 854ccec0 ffffffff 00000002
$ 8   : 82c6c340 8155ba9c 42314149 00000001
$12   : ffffffff 00000012 00000000 00000012
$16   : 00000000 08000000 82c6c040 82c6c040
$20   : 821c7c28 854ccec0 82205c68 80164c7c
$24   : 00000000 00000000
$28   : 85b14000 85b17d20 00000001 80299028
Hi    : 00000000
Lo    : 00000004
epc   : 80105f14 mips_stack_top+0x20/0x78
ra    : 80299028 arch_pick_mmap_layout+0xa4/0x1a4
Status: 1000a403	KERNEL EXL IE
Cause : 00800008 (ExcCode 02)
BadVA : 00000018
PrId  : 00019300 (MIPS 24Kc)
Modules linked in:
Process kunit_try_catch (pid: 783, threadinfo=85b14000, task=854ccec0, tls=00000000)
Stack : 82205c68 80164c7c 00400cc0 00000004 813d989c 85b17dd4 85b2ac00 81250000
        82c6c040 8070f984 00000001 802f1560 00000400 00000dc0 ffffffff 8070fa14
        81b43a80 00000000 00000000 657ad470 00000001 85b2ac00 821c7c1c 821c7c1c
        81250000 8070efb8 81b43bc0 657ad470 821c7c1c 81250000 85b2ac00 81250000
        821c7c1c 81250000 821c7c28 854ccec0 82205c68 8070fa48 821c7c1c 821c7c1c
        ...
Call Trace:
[<80105f14>] mips_stack_top+0x20/0x78
[<80299028>] arch_pick_mmap_layout+0xa4/0x1a4
[<8070f984>] kunit_vm_mmap_init+0xe8/0x114
[<8070efb8>] __kunit_add_resource+0x4c/0xdc
[<8070fa48>] kunit_vm_mmap+0x98/0xf0
[<808181a4>] usercopy_test_init+0xc4/0x254
[<8070eca8>] kunit_try_run_case+0x74/0x234
[<807110f8>] kunit_generic_run_threadfn_adapter+0x28/0x50
[<801647fc>] kthread+0x12c/0x154
[<80103078>] ret_from_kernel_thread+0x14/0x1c
Code: 9084635d  0003182b  00031b80 <8c460018> 3402ffff  30840004  000213c0  00431023  8cc30004
---[ end trace 0000000000000000 ]---
    # usercopy_test_valid: try faulted: last line seen lib/usercopy_kunit.c:304
    # usercopy_test_valid: internal error occurred preventing test case from running: -4
    # usercopy_test_valid: pass:0 fail:1 skip:0 total:1
    not ok 1 usercopy_test_valid
#10 lore raw
Quoted 11 lines · Guenter Roeck

Hi,

Quoted 4 lines · Vitor Massaru Iha

This adds the conversion of the runtime tests of test_user_copy fuctions, from lib/test_user_copy.cto KUnit tests.

These tests are failing (at least) for arm-v7, loongarch, and mips qemu emulations; see below for failure logs.

Quoted 7 lines

Any idea if those might be architecture problems, problems with qemu, or problems with the test ?

Thanks,
Guenter

---

Several more failures. I missed those earlier because they don't result in backtraces.

sparc32:

    # usercopy_test_valid: EXPECTATION FAILED at lib/usercopy_kunit.c:209
    Expected val_u64 == 0x5a5b5c5d6a6b6c6d, but
        val_u64 == 1515936861 (0x5a5b5c5d)
        0x5a5b5c5d6a6b6c6d == 6510899242581322861 (0x5a5b5c5d6a6b6c6d)

legitimate get_user (u64) failed to do copy
# usercopy_test_valid: pass:0 fail:1 skip:0 total:1
not ok 1 usercopy_test_valid
# usercopy_test_invalid: pass:1 fail:0 skip:0 total:1
ok 2 usercopy_test_invalid
# usercopy_test_check_nonzero_user: pass:1 fail:0 skip:0 total:1 ok 3 usercopy_test_check_nonzero_user
# usercopy_test_copy_struct_from_user: pass:1 fail:0 skip:0 total:1 ok 4 usercopy_test_copy_struct_from_user
# usercopy: pass:3 fail:1 skip:0 total:4
# Totals: pass:3 fail:1 skip:0 total:4

nios2:

# module: usercopy_kunit
1..4
# usercopy_test_valid: pass:1 fail:0 skip:0 total:1
ok 1 usercopy_test_valid
# usercopy_test_invalid: EXPECTATION FAILED at lib/usercopy_kunit.c:279 Expected val_u8 == 0, but
val_u8 == 90 (0x5a)
zeroing failure for illegal get_user (u8)

    # usercopy_test_invalid: EXPECTATION FAILED at lib/usercopy_kunit.c:280
    Expected val_u16 == 0, but
        val_u16 == 23131 (0x5a5b)
zeroing failure for illegal get_user (u16)
    # usercopy_test_invalid: EXPECTATION FAILED at lib/usercopy_kunit.c:281
    Expected val_u32 == 0, but
        val_u32 == 1515936861 (0x5a5b5c5d)

zeroing failure for illegal get_user (u32)
# usercopy_test_invalid: pass:0 fail:1 skip:0 total:1 not ok 2 usercopy_test_invalid
# usercopy_test_check_nonzero_user: pass:1 fail:0 skip:0 total:1 ok 3 usercopy_test_check_nonzero_user
# usercopy_test_copy_struct_from_user: pass:1 fail:0 skip:0 total:1 ok 4 usercopy_test_copy_struct_from_user
# usercopy: pass:3 fail:1 skip:0 total:4
# Totals: pass:3 fail:1 skip:0 total:4
not ok 37 usercopy

microblaze:

# usercopy_test_valid: pass:1 fail:0 skip:0 total:1
ok 1 usercopy_test_valid
# usercopy_test_invalid: EXPECTATION FAILED at lib/usercopy_kunit.c:279 Expected val_u8 == 0, but
val_u8 == 90 (0x5a)
zeroing failure for illegal get_user (u8)

    # usercopy_test_invalid: EXPECTATION FAILED at lib/usercopy_kunit.c:280
    Expected val_u16 == 0, but
        val_u16 == 23131 (0x5a5b)
zeroing failure for illegal get_user (u16)
    # usercopy_test_invalid: EXPECTATION FAILED at lib/usercopy_kunit.c:281
    Expected val_u32 == 0, but
        val_u32 == 1515936861 (0x5a5b5c5d)

zeroing failure for illegal get_user (u32)
# usercopy_test_invalid: pass:0 fail:1 skip:0 total:1 not ok 2 usercopy_test_invalid
# usercopy_test_check_nonzero_user: pass:1 fail:0 skip:0 total:1 ok 3 usercopy_test_check_nonzero_user
# usercopy_test_copy_struct_from_user: pass:1 fail:0 skip:0 total:1 ok 4 usercopy_test_copy_struct_from_user
# usercopy: pass:3 fail:1 skip:0 total:4
# Totals: pass:3 fail:1 skip:0 total:4
not ok 39 usercopy

Quoted 140 lines

On arm:

[   13.097105]     # usercopy_test_valid: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.097105]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.097105]         user_addr == 4294967284 (0xfffffff4)
[   13.097105]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.097105] Failed to allocate user memory
[   13.098876]     # usercopy_test_valid: pass:0 fail:1 skip:0 total:1
[   13.099378]     not ok 1 usercopy_test_valid
[   13.101143]     # usercopy_test_invalid: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.101143]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.101143]         user_addr == 4294967284 (0xfffffff4)
[   13.101143]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.101143] Failed to allocate user memory
[   13.102726]     # usercopy_test_invalid: pass:0 fail:1 skip:0 total:1
[   13.103167]     not ok 2 usercopy_test_invalid
[   13.104744]     # usercopy_test_check_nonzero_user: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.104744]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.104744]         user_addr == 4294967284 (0xfffffff4)
[   13.104744]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.104744] Failed to allocate user memory
[   13.106485]     # usercopy_test_check_nonzero_user: pass:0 fail:1 skip:0 total:1
[   13.106935]     not ok 3 usercopy_test_check_nonzero_user
[   13.108812]     # usercopy_test_copy_struct_from_user: ASSERTION FAILED at lib/usercopy_kunit.c:311
[   13.108812]     Expected user_addr < (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))), but
[   13.108812]         user_addr == 4294967284 (0xfffffff4)
[   13.108812]         (unsigned long)((((0xC0000000UL))) - (((0x01000000UL)))) == 3204448256 (0xbf000000)
[   13.108812] Failed to allocate user memory
[   13.110643]     # usercopy_test_copy_struct_from_user: pass:0 fail:1 skip:0 total:1
[   13.111096]     not ok 4 usercopy_test_copy_struct_from_user
[   13.111260] # usercopy: pass:0 fail:4 skip:0 total:4
[   13.111401] # Totals: pass:0 fail:4 skip:0 total:4
[   13.111533] not ok 40 usercopy

On loongarch:

[    5.202648] CPU 0 Unable to handle kernel paging request at virtual address 0000000000000008, era == 90000000040f4b60, ra == 900000000436e558
[    5.203061] Oops[#1]:
[    5.203288] CPU: 0 UID: 0 PID: 877 Comm: kunit_try_catch Tainted: G                 N 6.11.0-rc2+ #1
[    5.203469] Tainted: [N]=TEST
[    5.203523] Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015
[    5.203686] pc 90000000040f4b60 ra 900000000436e558 tp 9000000107dec000 sp 9000000107defc90
[    5.203801] a0 ffffffffffff4000 a1 90000001063554f8 a2 0000000000000000 a3 9000000107defb84
[    5.203913] a4 0000000000000001 a5 0000000000000000 a6 900000000696e030 a7 9000000004b617c0
[    5.204025] t0 9000000104ab8840 t1 0000800000000000 t2 0000000000208040 t3 9000000107dec000
[    5.204134] t4 0000000000000000 t5 9000000006f39f10 t6 0000000000000001 t7 0000000000006000
[    5.204245] t8 0000000045eeca10 u0 9000000004b61818 s9 90000001002e7e40 s0 0000000008000000
[    5.204355] s1 9000000104b58040 s2 0000000000000000 s3 fffffffffffff000 s4 9000000005f62d00
[    5.204465] s5 9000000104ab8840 s6 90000000061752e8 s7 0000000000000000 s8 9000000004144df8
[    5.204580]    ra: 900000000436e558 arch_pick_mmap_layout+0xa0/0x1fc
[    5.204928]   ERA: 90000000040f4b60 stack_top+0x58/0xa8
[    5.205009]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
[    5.205189]  PRMD: 00000004 (PPLV0 +PIE -PWE)
[    5.205286]  EUEN: 00000000 (-FPE -SXE -ASXE -BTE)
[    5.205398]  ECFG: 00071c1d (LIE=0,2-4,10-12 VS=7)
[    5.205629] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
[    5.205732]  BADV: 0000000000000008
[    5.205802]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
[    5.205897] Modules linked in:
[    5.205982] Process kunit_try_catch (pid: 877, threadinfo=9000000107dec000, task=9000000104ab8840)
[    5.206120] Stack : 9000000005f62d00 fffffffffffff000 900000000616f000 9000000107da5b40
[    5.206323]         9000000107defd58 9000000004a6cce4 0000000000000dc0 9000000104b58040
[    5.206444]         9000000100004ac0 0000000000000000 0000000000000000 0e03af9ea058218b
[    5.206562]         900000000616f000 90000001002d7b00 9000000107da5b40 9000000004a6c2c4
[    5.206681]         90000000061752e8 9000000104ab8840 9000000005f62d00 fffffffffffff000
[    5.206799]         900000000616f000 90000001002d7b00 9000000107da5b40 9000000004a6cb9c
[    5.206918]         9000000107cc8000 0000000000000000 0000000000000000 0000000000008000
[    5.207035]         0000000000000007 0000000000000022 0000000000000000 0e03af9ea058218b
[    5.207153]         900000000616f000 90000001079d2f00 90000001002d7b00 9000000004ba6a60
[    5.207271]         9000000104ab9170 90000000056c0788 00000000000000b0 0e03af9ea058218b
[    5.207389]         ...
[    5.207450] Call Trace:
[    5.207467] [<90000000040f4b60>] stack_top+0x58/0xa8
[    5.207588] [<900000000436e554>] arch_pick_mmap_layout+0x9c/0x1fc
[    5.207675] [<9000000004a6cce0>] kunit_vm_mmap_init+0x104/0x12c
[    5.207758] [<9000000004a6c2c0>] __kunit_add_resource+0x48/0xd0
[    5.207839] [<9000000004a6cb98>] kunit_vm_mmap+0x84/0xc8
[    5.207913] [<9000000004ba6a5c>] usercopy_test_init+0xb8/0x25c
[    5.207993] [<9000000004a6c064>] kunit_try_run_case+0x58/0x184
[    5.208073] [<9000000004a6e260>] kunit_generic_run_threadfn_adapter+0x20/0x48
[    5.208168] [<9000000004144914>] kthread+0x130/0x140
[    5.208238] [<90000000040f1c04>] ret_from_kernel_thread+0x8/0xa4
[    5.208323]
[    5.208370] Code: 15fffe84  26165190  2400158e <28c0220c> 14000070  03bffe10  0010c18c  15ffff90  0014c18c
[    5.208583]
[    5.208822] ---[ end trace 0000000000000000 ]---
[    5.209975]     # usercopy_test_valid: try faulted: last line seen lib/usercopy_kunit.c:304
[    5.210227]     # usercopy_test_valid: internal error occurred preventing test case from running: -4
[    5.210890]     # usercopy_test_valid: pass:0 fail:1 skip:0 total:1
[    5.210942]     not ok 1 usercopy_test_valid

On mips:

CPU 0 Unable to handle kernel paging request at virtual address 00000018, epc == 80105f14, ra == 80299028 Oops[#1]:

CPU: 0 UID: 0 PID: 783 Comm: kunit_try_catch Tainted: G                 N 6.11.0-rc2-00102-g8857973f206d #1
Tainted: [N]=TEST
Hardware name: mti,malta
$ 0   : 00000000 00000001 00000000 00000000
$ 4   : 00000000 854ccec0 ffffffff 00000002
$ 8   : 82c6c340 8155ba9c 42314149 00000001
$12   : ffffffff 00000012 00000000 00000012
$16   : 00000000 08000000 82c6c040 82c6c040
$20   : 821c7c28 854ccec0 82205c68 80164c7c
$24   : 00000000 00000000
$28   : 85b14000 85b17d20 00000001 80299028
Hi    : 00000000
Lo    : 00000004
epc   : 80105f14 mips_stack_top+0x20/0x78
ra    : 80299028 arch_pick_mmap_layout+0xa4/0x1a4
Status: 1000a403	KERNEL EXL IE
Cause : 00800008 (ExcCode 02)
BadVA : 00000018
PrId  : 00019300 (MIPS 24Kc)
Modules linked in:
Process kunit_try_catch (pid: 783, threadinfo=85b14000, task=854ccec0, tls=00000000)
Stack : 82205c68 80164c7c 00400cc0 00000004 813d989c 85b17dd4 85b2ac00 81250000
        82c6c040 8070f984 00000001 802f1560 00000400 00000dc0 ffffffff 8070fa14
        81b43a80 00000000 00000000 657ad470 00000001 85b2ac00 821c7c1c 821c7c1c
        81250000 8070efb8 81b43bc0 657ad470 821c7c1c 81250000 85b2ac00 81250000
        821c7c1c 81250000 821c7c28 854ccec0 82205c68 8070fa48 821c7c1c 821c7c1c
        ...
Call Trace:
[<80105f14>] mips_stack_top+0x20/0x78
[<80299028>] arch_pick_mmap_layout+0xa4/0x1a4
[<8070f984>] kunit_vm_mmap_init+0xe8/0x114
[<8070efb8>] __kunit_add_resource+0x4c/0xdc
[<8070fa48>] kunit_vm_mmap+0x98/0xf0
[<808181a4>] usercopy_test_init+0xc4/0x254
[<8070eca8>] kunit_try_run_case+0x74/0x234
[<807110f8>] kunit_generic_run_threadfn_adapter+0x28/0x50
[<801647fc>] kthread+0x12c/0x154
[<80103078>] ret_from_kernel_thread+0x14/0x1c
Code: 9084635d  0003182b  00031b80 <8c460018> 3402ffff  30840004  000213c0  00431023  8cc30004
---[ end trace 0000000000000000 ]---
    # usercopy_test_valid: try faulted: last line seen lib/usercopy_kunit.c:304
    # usercopy_test_valid: internal error occurred preventing test case from running: -4
    # usercopy_test_valid: pass:0 fail:1 skip:0 total:1
    not ok 1 usercopy_test_valid
#11 lore raw
In reply to Guenter Roeck
Quoted 10 lines · Guenter Roeck

Hi,

Quoted 4 lines · Vitor Massaru Iha

This adds the conversion of the runtime tests of test_user_copy fuctions, from lib/test_user_copy.cto KUnit tests.

These tests are failing (at least) for arm-v7, loongarch, and mips qemu emulations; see below for failure logs.

Oh my. Good thing this got added to KUnit, then -- the core of the tests haven't actually changed. I will see what I can uncover.

Quoted 2 lines

Any idea if those might be architecture problems, problems with qemu, or problems with the test ?

The last two look like NULL derefs (??) and the first seems like maybe a test problem (the failure looks kind of like the CONFIG_MMU=n cases that were fixed earlier).

Signature · 1 line
Kees Cook