From: Kees Cook <keescook@chromium.org>
To: Vitor Massaru Iha <vitor@massaru.org>
Cc: linux-kselftest@vger.kernel.org, brendanhiggins@google.com,
linux-kernel@vger.kernel.org, davidgow@google.com,
linux-kernel-mentees@lists.linuxfoundation.org,
kunit-dev@googlegroups.com
Subject: Re: [Linux-kernel-mentees] [PATCH v3] lib: Convert test_user_copy to KUnit test
Date: Tue, 21 Jul 2020 12:09:30 -0700 [thread overview]
Message-ID: <202007211207.5BAA9D8D@keescook> (raw)
In-Reply-To: <20200721174654.72132-1-vitor@massaru.org>
On Tue, Jul 21, 2020 at 02:46:54PM -0300, Vitor Massaru Iha wrote:
> This adds the conversion of the runtime tests of test_user_copy fuctions,
> from `lib/test_user_copy.c`to KUnit tests.
>
> Signed-off-by: Vitor Massaru Iha <vitor@massaru.org>
> ---
> 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%)
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 9ad9210d70a1..f699a3624ae7 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -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
> diff --git a/lib/Makefile b/lib/Makefile
> index b1c42c10073b..8c145f85accc 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -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
> diff --git a/lib/test_user_copy.c b/lib/user_copy_kunit.c
> similarity index 91%
> rename from lib/test_user_copy.c
> rename to lib/user_copy_kunit.c
> index 5ff04d8fe971..a10ddd15b4cd 100644
> --- a/lib/test_user_copy.c
> +++ b/lib/user_copy_kunit.c
> @@ -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.
> @@ -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.
> 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
>
> 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?
>
> 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?
>
> 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)
>
> 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?
> }
>
> -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 <keescook@chromium.org>");
> MODULE_LICENSE("GPL");
>
> base-commit: d43c7fb05765152d4d4a39a8ef957c4ea14d8847
> --
> 2.26.2
>
Otherwise, yes, looking good.
--
Kees Cook
_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
WARNING: multiple messages have this Message-ID (diff)
From: Kees Cook <keescook@chromium.org>
To: Vitor Massaru Iha <vitor@massaru.org>
Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org, brendanhiggins@google.com,
davidgow@google.com, skhan@linuxfoundation.org,
linux-kernel-mentees@lists.linuxfoundation.org
Subject: Re: [PATCH v3] lib: Convert test_user_copy to KUnit test
Date: Tue, 21 Jul 2020 12:09:30 -0700 [thread overview]
Message-ID: <202007211207.5BAA9D8D@keescook> (raw)
In-Reply-To: <20200721174654.72132-1-vitor@massaru.org>
On Tue, Jul 21, 2020 at 02:46:54PM -0300, Vitor Massaru Iha wrote:
> This adds the conversion of the runtime tests of test_user_copy fuctions,
> from `lib/test_user_copy.c`to KUnit tests.
>
> Signed-off-by: Vitor Massaru Iha <vitor@massaru.org>
> ---
> 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%)
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 9ad9210d70a1..f699a3624ae7 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -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
> diff --git a/lib/Makefile b/lib/Makefile
> index b1c42c10073b..8c145f85accc 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -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
> diff --git a/lib/test_user_copy.c b/lib/user_copy_kunit.c
> similarity index 91%
> rename from lib/test_user_copy.c
> rename to lib/user_copy_kunit.c
> index 5ff04d8fe971..a10ddd15b4cd 100644
> --- a/lib/test_user_copy.c
> +++ b/lib/user_copy_kunit.c
> @@ -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.
> @@ -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.
> 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
>
> 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?
>
> 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?
>
> 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)
>
> 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?
> }
>
> -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 <keescook@chromium.org>");
> MODULE_LICENSE("GPL");
>
> base-commit: d43c7fb05765152d4d4a39a8ef957c4ea14d8847
> --
> 2.26.2
>
Otherwise, yes, looking good.
--
Kees Cook
next prev parent reply other threads:[~2020-07-21 19:09 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-21 17:46 [Linux-kernel-mentees] [PATCH v3] lib: Convert test_user_copy to KUnit test Vitor Massaru Iha
2020-07-21 17:46 ` Vitor Massaru Iha
2020-07-21 19:09 ` Kees Cook [this message]
2020-07-21 19:09 ` Kees Cook
2020-07-21 22:19 ` [Linux-kernel-mentees] " Vitor Massaru Iha
2020-07-21 22:19 ` Vitor Massaru Iha
2020-07-21 23:11 ` [Linux-kernel-mentees] " Vitor Massaru Iha
2020-07-21 23:11 ` Vitor Massaru Iha
2020-07-22 2:12 ` [Linux-kernel-mentees] " Kees Cook
2020-07-22 2:12 ` Kees Cook
2020-07-22 18:29 ` [Linux-kernel-mentees] " Vitor Massaru Iha
2020-07-22 18:29 ` Vitor Massaru Iha
2020-07-22 19:45 ` [Linux-kernel-mentees] " Kees Cook
2020-07-22 19:45 ` Kees Cook
2020-07-22 20:21 ` [Linux-kernel-mentees] " Vitor Massaru Iha
2020-07-22 20:21 ` Vitor Massaru Iha
2020-07-22 22:28 ` [Linux-kernel-mentees] " Vitor Massaru Iha
2020-07-22 22:28 ` Vitor Massaru Iha
2024-08-06 15:48 ` Guenter Roeck
2024-08-06 16:00 ` Guenter Roeck
2024-08-06 19:34 ` Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202007211207.5BAA9D8D@keescook \
--to=keescook@chromium.org \
--cc=brendanhiggins@google.com \
--cc=davidgow@google.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=vitor@massaru.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.