[PATCH 0/4] kunit: Fix some bugs in kunit_filter_suites()

6 views
Skip to first unread message

Jinjie Ruan

unread,
Aug 31, 2023, 3:17:12 AM8/31/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
The state handle in kunit_module_notify() is not correct when
the mod->state switch from MODULE_STATE_COMING to MODULE_STATE_GOING.

And it's necessary to check NULL for kzalloc() in
kunit_parse_glob_filter().

The order in which memory is released in err path in kunit_filter_suites()
is also problematic.

And there is a possible memory leak in kunit_filter_suites().

This patchset fix the above issues.

Jinjie Ruan (4):
kunit: Fix wild-memory-access bug in kunit_free_suite_set()
kunit: Fix possible null-ptr-deref in kunit_parse_glob_filter()
kunit: Fix possible memory leak in kunit_filter_suites()
kunit: Fix the wrong error path in kunit_filter_suites()

lib/kunit/executor.c | 39 +++++++++++++++++++++++++++------------
lib/kunit/test.c | 3 ++-
2 files changed, 29 insertions(+), 13 deletions(-)

--
2.34.1

Jinjie Ruan

unread,
Aug 31, 2023, 3:17:13 AM8/31/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
Inject fault while probing kunit-example-test.ko, if kstrdup()
fails in mod_sysfs_setup() in load_module(), the mod->state will
switch from MODULE_STATE_COMING to MODULE_STATE_GOING instead of
from MODULE_STATE_LIVE to MODULE_STATE_GOING, so only
kunit_module_exit() will be called without kunit_module_init(), and
the mod->kunit_suites is no set correctly and the free in
kunit_free_suite_set() will cause below wild-memory-access bug.

The mod->state state machine when load_module() succeeds:

MODULE_STATE_UNFORMED ---> MODULE_STATE_COMING ---> MODULE_STATE_LIVE
^ |
| | delete_module
+---------------- MODULE_STATE_GOING <---------+

The mod->state state machine when load_module() fails at
mod_sysfs_setup():

MODULE_STATE_UNFORMED ---> MODULE_STATE_COMING ---> MODULE_STATE_GOING
^ |
| |
+-----------------------------------------------+

Call kunit_module_init() at MODULE_STATE_COMING state to fix the issue
because MODULE_STATE_LIVE is transformed from it.

Unable to handle kernel paging request at virtual address ffffff341e942a88
KASAN: maybe wild-memory-access in range [0x0003f9a0f4a15440-0x0003f9a0f4a15447]
Mem abort info:
ESR = 0x0000000096000004
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x04: level 0 translation fault
Data abort info:
ISV = 0, ISS = 0x00000004, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
swapper pgtable: 4k pages, 48-bit VAs, pgdp=00000000441ea000
[ffffff341e942a88] pgd=0000000000000000, p4d=0000000000000000
Internal error: Oops: 0000000096000004 [#1] PREEMPT SMP
Modules linked in: kunit_example_test(-) cfg80211 rfkill 8021q garp mrp stp llc ipv6 [last unloaded: kunit_example_test]
CPU: 3 PID: 2035 Comm: modprobe Tainted: G W N 6.5.0-next-20230828+ #136
Hardware name: linux,dummy-virt (DT)
pstate: a0000005 (NzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : kfree+0x2c/0x70
lr : kunit_free_suite_set+0xcc/0x13c
sp : ffff8000829b75b0
x29: ffff8000829b75b0 x28: ffff8000829b7b90 x27: 0000000000000000
x26: dfff800000000000 x25: ffffcd07c82a7280 x24: ffffcd07a50ab300
x23: ffffcd07a50ab2e8 x22: 1ffff00010536ec0 x21: dfff800000000000
x20: ffffcd07a50ab2f0 x19: ffffcd07a50ab2f0 x18: 0000000000000000
x17: 0000000000000000 x16: 0000000000000000 x15: ffffcd07c24b6764
x14: ffffcd07c24b63c0 x13: ffffcd07c4cebb94 x12: ffff700010536ec7
x11: 1ffff00010536ec6 x10: ffff700010536ec6 x9 : dfff800000000000
x8 : 00008fffefac913a x7 : 0000000041b58ab3 x6 : 0000000000000000
x5 : 1ffff00010536ec5 x4 : ffff8000829b7628 x3 : dfff800000000000
x2 : ffffff341e942a80 x1 : ffffcd07a50aa000 x0 : fffffc0000000000
Call trace:
kfree+0x2c/0x70
kunit_free_suite_set+0xcc/0x13c
kunit_module_notify+0xd8/0x360
blocking_notifier_call_chain+0xc4/0x128
load_module+0x382c/0x44a4
init_module_from_file+0xd4/0x128
idempotent_init_module+0x2c8/0x524
__arm64_sys_finit_module+0xac/0x100
invoke_syscall+0x6c/0x258
el0_svc_common.constprop.0+0x160/0x22c
do_el0_svc+0x44/0x5c
el0_svc+0x38/0x78
el0t_64_sync_handler+0x13c/0x158
el0t_64_sync+0x190/0x194
Code: aa0003e1 b25657e0 d34cfc42 8b021802 (f9400440)
---[ end trace 0000000000000000 ]---
Kernel panic - not syncing: Oops: Fatal exception
SMP: stopping secondary CPUs
Kernel Offset: 0x4d0742200000 from 0xffff800080000000
PHYS_OFFSET: 0xffffee43c0000000
CPU features: 0x88000203,3c020000,1000421b
Memory Limit: none
Rebooting in 1 seconds..

Fixes: 3d6e44623841 ("kunit: unify module and builtin suite definitions")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
---
lib/kunit/test.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 49698a168437..421f13981412 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -784,12 +784,13 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val,

switch (val) {
case MODULE_STATE_LIVE:
- kunit_module_init(mod);
break;
case MODULE_STATE_GOING:
kunit_module_exit(mod);
break;
case MODULE_STATE_COMING:
+ kunit_module_init(mod);
+ break;
case MODULE_STATE_UNFORMED:
break;
}
--
2.34.1

Jinjie Ruan

unread,
Aug 31, 2023, 3:17:15 AM8/31/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
Inject fault while probing kunit-example-test.ko, if kzalloc fails
in kunit_parse_glob_filter(), strcpy() or strncpy() to NULL will
cause below null-ptr-deref bug. So check NULL for kzalloc() and
return int instead of void for kunit_parse_glob_filter().

Unable to handle kernel paging request at virtual address dfff800000000000
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
Mem abort info:
ESR = 0x0000000096000005
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x05: level 1 translation fault
Data abort info:
ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
[dfff800000000000] address between user and kernel address ranges
Internal error: Oops: 0000000096000005 [#1] PREEMPT SMP
Modules linked in: kunit_example_test cfg80211 rfkill 8021q garp mrp stp llc ipv6 [last unloaded: kunit_example_test]
CPU: 4 PID: 6047 Comm: modprobe Tainted: G W N 6.5.0-next-20230829+ #141
Hardware name: linux,dummy-virt (DT)
pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : strncpy+0x58/0xc0
lr : kunit_filter_suites+0x15c/0xa84
sp : ffff800082a17420
x29: ffff800082a17420 x28: 0000000000000000 x27: 0000000000000004
x26: 0000000000000000 x25: ffffa847e40a5320 x24: 0000000000000001
x23: 0000000000000000 x22: 0000000000000001 x21: dfff800000000000
x20: 000000000000002a x19: 0000000000000000 x18: 00000000750b3b54
x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
x14: 0000000000000000 x13: 34393178302f3039 x12: ffff7508fcea4ec1
x11: 1ffff508fcea4ec0 x10: ffff7508fcea4ec0 x9 : dfff800000000000
x8 : ffff6051b1a7f86a x7 : ffff800082a17270 x6 : 0000000000000002
x5 : 0000000000000098 x4 : ffff028d9817b250 x3 : 0000000000000000
x2 : 0000000000000000 x1 : ffffa847e40a5320 x0 : 0000000000000000
Call trace:
strncpy+0x58/0xc0
kunit_filter_suites+0x15c/0xa84
kunit_module_notify+0x1b0/0x3ac
blocking_notifier_call_chain+0xc4/0x128
do_init_module+0x250/0x594
load_module+0x37b0/0x44b4
init_module_from_file+0xd4/0x128
idempotent_init_module+0x2c8/0x524
__arm64_sys_finit_module+0xac/0x100
invoke_syscall+0x6c/0x258
el0_svc_common.constprop.0+0x160/0x22c
do_el0_svc+0x44/0x5c
el0_svc+0x38/0x78
el0t_64_sync_handler+0x13c/0x158
el0t_64_sync+0x190/0x194
Code: 5400028a d343fe63 12000a62 39400034 (38f56863)
---[ end trace 0000000000000000 ]---
Kernel panic - not syncing: Oops: Fatal exception
SMP: stopping secondary CPUs
Kernel Offset: 0x284761400000 from 0xffff800080000000
PHYS_OFFSET: 0xfffffd7380000000
CPU features: 0x88000203,3c020000,1000421b
Memory Limit: none
Rebooting in 1 seconds..

Fixes: a127b154a8f2 ("kunit: tool: allow filtering test cases via glob")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
---
lib/kunit/executor.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 5181aa2e760b..25ce8d6e5fe7 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -65,7 +65,7 @@ struct kunit_glob_filter {
};

/* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */
-static void kunit_parse_glob_filter(struct kunit_glob_filter *parsed,
+static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed,
const char *filter_glob)
{
const int len = strlen(filter_glob);
@@ -73,16 +73,28 @@ static void kunit_parse_glob_filter(struct kunit_glob_filter *parsed,

if (!period) {
parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
+ if (!parsed->suite_glob)
+ return -ENOMEM;
+
parsed->test_glob = NULL;
strcpy(parsed->suite_glob, filter_glob);
- return;
+ return 0;
}

parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
+ if (!parsed->suite_glob)
+ return -ENOMEM;
+
parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
+ if (!parsed->test_glob) {
+ kfree(parsed->suite_glob);
+ return -ENOMEM;
+ }

strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
+
+ return 0;
}

/* Create a copy of suite with only tests that match test_glob. */
@@ -152,8 +164,13 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
}
copy_start = copy;

- if (filter_glob)
- kunit_parse_glob_filter(&parsed_glob, filter_glob);
+ if (filter_glob) {
+ *err = kunit_parse_glob_filter(&parsed_glob, filter_glob);
+ if (*err) {
+ kfree(copy);
+ return filtered;
+ }
+ }

/* Parse attribute filters */
if (filters) {
--
2.34.1

Jinjie Ruan

unread,
Aug 31, 2023, 3:17:18 AM8/31/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
If both filter_glob and filters are not NULL, and kunit_parse_glob_filter()
succeed, but kcalloc parsed_filters fails, the suite_glob and test_glob of
parsed kzalloc in kunit_parse_glob_filter() will be leaked.

Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
---
lib/kunit/executor.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 25ce8d6e5fe7..7654c09c1ab1 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -176,10 +176,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
if (filters) {
filter_count = kunit_get_filter_count(filters);
parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
- if (!parsed_filters) {
- kfree(copy);
- return filtered;
- }
+ if (!parsed_filters)
+ goto err;
for (j = 0; j < filter_count; j++)
parsed_filters[j] = kunit_next_attr_filter(&filters, err);
if (*err)
--
2.34.1

Jinjie Ruan

unread,
Aug 31, 2023, 3:17:18 AM8/31/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
Take the last kfree(parsed_filters) and add it to be the first. Take
the first kfree(copy) and add it to be the last. The Best practice is to
return these errors reversely.

Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
Fixes: abbf73816b6f ("kunit: fix possible memory leak in kunit_filter_suites()")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
---
lib/kunit/executor.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 7654c09c1ab1..da9444d22711 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -229,16 +229,16 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
filtered.end = copy;

err:
- if (*err)
- kfree(copy);
+ if (filter_count)
+ kfree(parsed_filters);

if (filter_glob) {
kfree(parsed_glob.suite_glob);
kfree(parsed_glob.test_glob);
}

- if (filter_count)
- kfree(parsed_filters);
+ if (*err)
+ kfree(copy);

return filtered;
}
--
2.34.1

Rae Moar

unread,
Aug 31, 2023, 4:29:26 PM8/31/23
to Jinjie Ruan, brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
Hello!

This looks good to me. It seems to be working well.

Reviewed-by: Rae Moar <rm...@google.com>

Thanks!
-Rae

> ---
> lib/kunit/test.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> index 49698a168437..421f13981412 100644
> --- a/lib/kunit/test.c
> +++ b/lib/kunit/test.c
> @@ -784,12 +784,13 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
>
> switch (val) {
> case MODULE_STATE_LIVE:
> - kunit_module_init(mod);
> break;
> case MODULE_STATE_GOING:
> kunit_module_exit(mod);
> break;
> case MODULE_STATE_COMING:
> + kunit_module_init(mod);
> + break;
> case MODULE_STATE_UNFORMED:
> break;
> }
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-2-ruanjinjie%40huawei.com.

Rae Moar

unread,
Aug 31, 2023, 4:34:19 PM8/31/23
to Jinjie Ruan, brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Thu, Aug 31, 2023 at 3:17 AM 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
Hello!

This also looks good to me. Thanks for catching these!

Reviewed-by: Rae Moar <rm...@google.com>

Thanks!
-Rae

> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-3-ruanjinjie%40huawei.com.

Rae Moar

unread,
Aug 31, 2023, 5:01:31 PM8/31/23
to Jinjie Ruan, brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Thu, Aug 31, 2023 at 3:17 AM 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> If both filter_glob and filters are not NULL, and kunit_parse_glob_filter()
> succeed, but kcalloc parsed_filters fails, the suite_glob and test_glob of
> parsed kzalloc in kunit_parse_glob_filter() will be leaked.
>
> Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
> Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>

Hello!

Thanks for catching this! I just had two concerns listed below.

-Rae

> ---
> lib/kunit/executor.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
> index 25ce8d6e5fe7..7654c09c1ab1 100644
> --- a/lib/kunit/executor.c
> +++ b/lib/kunit/executor.c
> @@ -176,10 +176,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
> if (filters) {
> filter_count = kunit_get_filter_count(filters);
> parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
> - if (!parsed_filters) {
> - kfree(copy);
> - return filtered;
> - }
> + if (!parsed_filters)
> + goto err;

When this goes to the "err" label this will likely enter the if
(filter_count) statement and attempt to free parsed_filters. Since
parsed_filters is NULL in this case, it could be a cleaner practice to
set a new label, maybe "filter_err", to skip over that if statement.
Although, I realize this may require the order of the patches to
switch.

Additionally, the value of *err should definitely be set here before
"goto err". The kunit_run_all_tests() function checks this err value
in order to produce the correct error message and to prevent any
attempt to run tests. I would suggest: *err = -ENOMEM;

> for (j = 0; j < filter_count; j++)
> parsed_filters[j] = kunit_next_attr_filter(&filters, err);
> if (*err)
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-4-ruanjinjie%40huawei.com.

Rae Moar

unread,
Aug 31, 2023, 5:03:55 PM8/31/23
to Jinjie Ruan, brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Thu, Aug 31, 2023 at 3:17 AM 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> Take the last kfree(parsed_filters) and add it to be the first. Take
> the first kfree(copy) and add it to be the last. The Best practice is to
> return these errors reversely.
>
> Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
> Fixes: abbf73816b6f ("kunit: fix possible memory leak in kunit_filter_suites()")
> Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>

Hello!

This seems fine to me. See my comments from the last patch regarding a
potential change in the order of the patches.

Otherwise, this works for me.

Reviewed-by: Rae Moar <rm...@google.com>

Thanks!
-Rae

> ---
> lib/kunit/executor.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
> index 7654c09c1ab1..da9444d22711 100644
> --- a/lib/kunit/executor.c
> +++ b/lib/kunit/executor.c
> @@ -229,16 +229,16 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
> filtered.end = copy;
>
> err:
> - if (*err)
> - kfree(copy);
> + if (filter_count)
> + kfree(parsed_filters);
>
> if (filter_glob) {
> kfree(parsed_glob.suite_glob);
> kfree(parsed_glob.test_glob);
> }
>
> - if (filter_count)
> - kfree(parsed_filters);
> + if (*err)
> + kfree(copy);
>
> return filtered;
> }
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-5-ruanjinjie%40huawei.com.

David Gow

unread,
Sep 1, 2023, 1:18:14 AM9/1/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
Nice catch, thanks. I'm not seeing the actual panic here (with a quick
hack to fail module loading), but the patch looks sound.

I suspect we may want to split the module loading up into separate
'init' and 'run' phases, tied to MODULE_STATE_COMING and
MODULE_STATE_LIVE respectively, at some point (perhaps when the
planned support for triggering tests from userspace is done), but this
is a good fix in the meantime,

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David


> lib/kunit/test.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> index 49698a168437..421f13981412 100644
> --- a/lib/kunit/test.c
> +++ b/lib/kunit/test.c
> @@ -784,12 +784,13 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
>
> switch (val) {
> case MODULE_STATE_LIVE:
> - kunit_module_init(mod);
> break;
> case MODULE_STATE_GOING:
> kunit_module_exit(mod);
> break;
> case MODULE_STATE_COMING:
> + kunit_module_init(mod);
> + break;
> case MODULE_STATE_UNFORMED:
> break;
> }
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-2-ruanjinjie%40huawei.com.

David Gow

unread,
Sep 1, 2023, 1:18:18 AM9/1/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Thu, 31 Aug 2023 at 15:17, 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
Looks sensible to me! Thanks for catching it.

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David


> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-3-ruanjinjie%40huawei.com.

David Gow

unread,
Sep 1, 2023, 1:18:22 AM9/1/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Thu, 31 Aug 2023 at 15:17, 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> If both filter_glob and filters are not NULL, and kunit_parse_glob_filter()
> succeed, but kcalloc parsed_filters fails, the suite_glob and test_glob of
> parsed kzalloc in kunit_parse_glob_filter() will be leaked.
>
> Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
> Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
> ---

As Rae points out, I don't think this is correct. If filter_count is
non-zero, but parsed_filters is NULL due to an allocation failure,
we'll still end up trying to kfree() a NULL pointer.

That's why this didn't goto err; before: we explicitly _don't_ want to
free parsed_filters here. We could set filter_count = 0 before goto
err, but I think either wrapping the kfree() call in if
(parsed_filters), or

Indeed, this may be the case for most of the cleanup here: we're
checking if we intended to allocate something (e.g., the filter_count
is nonzero, the filter_glob is set), rather than whether we
successfully allocated something, so can kfree(NULL) on failure. The
issues with ordering (that you fix in the next patch) could help if we
had several labels to jump to after each allocation, or just checking
that what we're freeing was non-NULL (and initialising them to NULL if
needed).

Thanks,
-- David


> lib/kunit/executor.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
> index 25ce8d6e5fe7..7654c09c1ab1 100644
> --- a/lib/kunit/executor.c
> +++ b/lib/kunit/executor.c
> @@ -176,10 +176,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
> if (filters) {
> filter_count = kunit_get_filter_count(filters);
> parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
> - if (!parsed_filters) {
> - kfree(copy);
> - return filtered;
> - }
> + if (!parsed_filters)
> + goto err;
> for (j = 0; j < filter_count; j++)
> parsed_filters[j] = kunit_next_attr_filter(&filters, err);
> if (*err)
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-4-ruanjinjie%40huawei.com.

David Gow

unread,
Sep 1, 2023, 1:18:27 AM9/1/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Thu, 31 Aug 2023 at 15:17, 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> Take the last kfree(parsed_filters) and add it to be the first. Take
> the first kfree(copy) and add it to be the last. The Best practice is to
> return these errors reversely.
>
> Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
> Fixes: abbf73816b6f ("kunit: fix possible memory leak in kunit_filter_suites()")
> Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
> ---

Agreed, these should be freed in reverse order.

Would it make sense to initialise 'copy' to NULL, and free it if (copy
!= NULL), rather than if (*err)? As mentioned in the previous patch, I
think that'd be more correct.

We could also have several labels which target only the things which
actually have been allocated so far.

Thoughts?
-- David

> lib/kunit/executor.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
> index 7654c09c1ab1..da9444d22711 100644
> --- a/lib/kunit/executor.c
> +++ b/lib/kunit/executor.c
> @@ -229,16 +229,16 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
> filtered.end = copy;
>
> err:
> - if (*err)
> - kfree(copy);
> + if (filter_count)
> + kfree(parsed_filters);
>
> if (filter_glob) {
> kfree(parsed_glob.suite_glob);
> kfree(parsed_glob.test_glob);
> }

I think this might also be potentially broken. If
parsed_glob.{suite,test}_glob are not successfully allocated,
filter_glob will still be set, and we'll kfree() something invalid.
Should we also init parsed_glob.* to NULL, and free them if non-NULL,
rather than relying on the presence of filter_glob?



>
> - if (filter_count)
> - kfree(parsed_filters);
> + if (*err)
> + kfree(copy);
>
> return filtered;
> }
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230831071655.2907683-5-ruanjinjie%40huawei.com.

Ruan Jinjie

unread,
Sep 3, 2023, 2:37:14 AM9/3/23
to David Gow, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com


On 2023/9/1 13:18, David Gow wrote:
> On Thu, 31 Aug 2023 at 15:17, 'Jinjie Ruan' via KUnit Development
> <kuni...@googlegroups.com> wrote:
>>
>> Take the last kfree(parsed_filters) and add it to be the first. Take
>> the first kfree(copy) and add it to be the last. The Best practice is to
>> return these errors reversely.
>>
>> Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
>> Fixes: abbf73816b6f ("kunit: fix possible memory leak in kunit_filter_suites()")
>> Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
>> ---
>
> Agreed, these should be freed in reverse order.
>
> Would it make sense to initialise 'copy' to NULL, and free it if (copy
> != NULL), rather than if (*err)? As mentioned in the previous patch, I
> think that'd be more correct.

There is a problem because the normal return also go through the all err
paths but 'copy' is not NULL and should not be freed.

>
> We could also have several labels which target only the things which
> actually have been allocated so far.

That's a good idea!

>
> Thoughts?
> -- David
>
>> lib/kunit/executor.c | 8 ++++----
>> 1 file changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
>> index 7654c09c1ab1..da9444d22711 100644
>> --- a/lib/kunit/executor.c
>> +++ b/lib/kunit/executor.c
>> @@ -229,16 +229,16 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
>> filtered.end = copy;
>>
>> err:
>> - if (*err)
>> - kfree(copy);
>> + if (filter_count)
>> + kfree(parsed_filters);
>>
>> if (filter_glob) {
>> kfree(parsed_glob.suite_glob);
>> kfree(parsed_glob.test_glob);
>> }
>
> I think this might also be potentially broken. If
> parsed_glob.{suite,test}_glob are not successfully allocated,
> filter_glob will still be set, and we'll kfree() something invalid.
> Should we also init parsed_glob.* to NULL, and free them if non-NULL,
> rather than relying on the presence of filter_glob?

if not successsfully allocated, it will be NULL and kfree NULL is ok.

Jinjie Ruan

unread,
Sep 3, 2023, 3:10:55 AM9/3/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
The state handle in kunit_module_notify() is not correct when
the mod->state switch from MODULE_STATE_COMING to MODULE_STATE_GOING.

And it's necessary to check NULL for kzalloc() in
kunit_parse_glob_filter().

The order in which memory is released in err path in kunit_filter_suites()
is also problematic.

And there is a possible memory leak in kunit_filter_suites().

This patchset fix the above issues.

Changes in v2:
- Adjust the 4th patch to be the second.
- Add goto labels in kunit_filter_suites() and adapt to it.
- Fix the issue in the third patch.
- Update the commit message and title.

Jinjie Ruan (4):
kunit: Fix wild-memory-access bug in kunit_free_suite_set()
kunit: Fix the wrong err path and add goto labels in
kunit_filter_suites()
kunit: Fix possible null-ptr-deref in kunit_parse_glob_filter()
kunit: Fix possible memory leak in kunit_filter_suites()

lib/kunit/executor.c | 48 ++++++++++++++++++++++++++++++--------------
lib/kunit/test.c | 3 ++-
2 files changed, 35 insertions(+), 16 deletions(-)

--
2.34.1

Jinjie Ruan

unread,
Sep 3, 2023, 3:11:00 AM9/3/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, Ruan Jinjie
Take the last kfree(parsed_filters) and add it to be the first. Take
the first kfree(copy) and add it to be the last. The Best practice is to
return these errors reversely.

And as David suggested, add several labels which target only the things
which actually have been allocated so far.

Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
Fixes: abbf73816b6f ("kunit: fix possible memory leak in kunit_filter_suites()")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
Reviewed-by: Rae Moar <rm...@google.com>
Suggested-by: David Gow <davi...@google.com>
---
v2:
- Add err path labels.
- Update the commit message and title.
---
lib/kunit/executor.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 5181aa2e760b..0eda42b0c9bb 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -166,7 +166,7 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
for (j = 0; j < filter_count; j++)
parsed_filters[j] = kunit_next_attr_filter(&filters, err);
if (*err)
- goto err;
+ goto free_parsed_filters;
}

for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
@@ -178,7 +178,7 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
parsed_glob.test_glob);
if (IS_ERR(filtered_suite)) {
*err = PTR_ERR(filtered_suite);
- goto err;
+ goto free_parsed_filters;
}
}
if (filter_count > 0 && parsed_filters != NULL) {
@@ -195,10 +195,11 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
filtered_suite = new_filtered_suite;

if (*err)
- goto err;
+ goto free_parsed_filters;
+
if (IS_ERR(filtered_suite)) {
*err = PTR_ERR(filtered_suite);
- goto err;
+ goto free_parsed_filters;
}
if (!filtered_suite)
break;
@@ -213,17 +214,19 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
filtered.start = copy_start;
filtered.end = copy;

-err:
- if (*err)
- kfree(copy);
+free_parsed_filters:
+ if (filter_count)
+ kfree(parsed_filters);

+free_parsed_glob:
if (filter_glob) {
kfree(parsed_glob.suite_glob);
kfree(parsed_glob.test_glob);
}

- if (filter_count)
- kfree(parsed_filters);
+free_copy:

Jinjie Ruan

unread,
Sep 3, 2023, 3:11:00 AM9/3/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
Inject fault while probing kunit-example-test.ko, if kstrdup()
fails in mod_sysfs_setup() in load_module(), the mod->state will
switch from MODULE_STATE_COMING to MODULE_STATE_GOING instead of
from MODULE_STATE_LIVE to MODULE_STATE_GOING, so only
kunit_module_exit() will be called without kunit_module_init(), and
the mod->kunit_suites is no set correctly and the free in
kunit_free_suite_set() will cause below wild-memory-access bug.

The mod->state state machine when load_module() succeeds:

MODULE_STATE_UNFORMED ---> MODULE_STATE_COMING ---> MODULE_STATE_LIVE
^ |
| | delete_module
+---------------- MODULE_STATE_GOING <---------+

The mod->state state machine when load_module() fails at
mod_sysfs_setup():

MODULE_STATE_UNFORMED ---> MODULE_STATE_COMING ---> MODULE_STATE_GOING
^ |
| |
+-----------------------------------------------+

Call kunit_module_init() at MODULE_STATE_COMING state to fix the issue
because MODULE_STATE_LIVE is transformed from it.

Unable to handle kernel paging request at virtual address ffffff341e942a88
KASAN: maybe wild-memory-access in range [0x0003f9a0f4a15440-0x0003f9a0f4a15447]
Mem abort info:
ESR = 0x0000000096000004
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x04: level 0 translation fault
Data abort info:
ISV = 0, ISS = 0x00000004, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
swapper pgtable: 4k pages, 48-bit VAs, pgdp=00000000441ea000
[ffffff341e942a88] pgd=0000000000000000, p4d=0000000000000000
Internal error: Oops: 0000000096000004 [#1] PREEMPT SMP
Modules linked in: kunit_example_test(-) cfg80211 rfkill 8021q garp mrp stp llc ipv6 [last unloaded: kunit_example_test]
CPU: 3 PID: 2035 Comm: modprobe Tainted: G W N 6.5.0-next-20230828+ #136
Hardware name: linux,dummy-virt (DT)
pstate: a0000005 (NzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : kfree+0x2c/0x70
lr : kunit_free_suite_set+0xcc/0x13c
sp : ffff8000829b75b0
x29: ffff8000829b75b0 x28: ffff8000829b7b90 x27: 0000000000000000
x26: dfff800000000000 x25: ffffcd07c82a7280 x24: ffffcd07a50ab300
x23: ffffcd07a50ab2e8 x22: 1ffff00010536ec0 x21: dfff800000000000
x20: ffffcd07a50ab2f0 x19: ffffcd07a50ab2f0 x18: 0000000000000000
x17: 0000000000000000 x16: 0000000000000000 x15: ffffcd07c24b6764
x14: ffffcd07c24b63c0 x13: ffffcd07c4cebb94 x12: ffff700010536ec7
x11: 1ffff00010536ec6 x10: ffff700010536ec6 x9 : dfff800000000000
x8 : 00008fffefac913a x7 : 0000000041b58ab3 x6 : 0000000000000000
x5 : 1ffff00010536ec5 x4 : ffff8000829b7628 x3 : dfff800000000000
x2 : ffffff341e942a80 x1 : ffffcd07a50aa000 x0 : fffffc0000000000
Call trace:
kfree+0x2c/0x70
kunit_free_suite_set+0xcc/0x13c
kunit_module_notify+0xd8/0x360
blocking_notifier_call_chain+0xc4/0x128
load_module+0x382c/0x44a4
init_module_from_file+0xd4/0x128
idempotent_init_module+0x2c8/0x524
__arm64_sys_finit_module+0xac/0x100
invoke_syscall+0x6c/0x258
el0_svc_common.constprop.0+0x160/0x22c
do_el0_svc+0x44/0x5c
el0_svc+0x38/0x78
el0t_64_sync_handler+0x13c/0x158
el0t_64_sync+0x190/0x194
Code: aa0003e1 b25657e0 d34cfc42 8b021802 (f9400440)
---[ end trace 0000000000000000 ]---
Kernel panic - not syncing: Oops: Fatal exception
SMP: stopping secondary CPUs
Kernel Offset: 0x4d0742200000 from 0xffff800080000000
PHYS_OFFSET: 0xffffee43c0000000
CPU features: 0x88000203,3c020000,1000421b
Memory Limit: none
Rebooting in 1 seconds..

Fixes: 3d6e44623841 ("kunit: unify module and builtin suite definitions")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
Reviewed-by: Rae Moar <rm...@google.com>
Reviewed-by: David Gow <davi...@google.com>
---
v2:
- Add Reviewed-by.
- Adjust the 4th patch to be the second.
---

Jinjie Ruan

unread,
Sep 3, 2023, 3:11:05 AM9/3/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
Inject fault while probing kunit-example-test.ko, if kzalloc fails
in kunit_parse_glob_filter(), strcpy() or strncpy() to NULL will
cause below null-ptr-deref bug. So check NULL for kzalloc() and
return int instead of void for kunit_parse_glob_filter().

Unable to handle kernel paging request at virtual address dfff800000000000
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
Mem abort info:
ESR = 0x0000000096000005
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x05: level 1 translation fault
Data abort info:
ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
[dfff800000000000] address between user and kernel address ranges
Internal error: Oops: 0000000096000005 [#1] PREEMPT SMP
Modules linked in: kunit_example_test cfg80211 rfkill 8021q garp mrp stp llc ipv6 [last unloaded: kunit_example_test]
CPU: 4 PID: 6047 Comm: modprobe Tainted: G W N 6.5.0-next-20230829+ #141
Hardware name: linux,dummy-virt (DT)
pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : strncpy+0x58/0xc0
lr : kunit_filter_suites+0x15c/0xa84
sp : ffff800082a17420
x29: ffff800082a17420 x28: 0000000000000000 x27: 0000000000000004
x26: 0000000000000000 x25: ffffa847e40a5320 x24: 0000000000000001
x23: 0000000000000000 x22: 0000000000000001 x21: dfff800000000000
x20: 000000000000002a x19: 0000000000000000 x18: 00000000750b3b54
x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
x14: 0000000000000000 x13: 34393178302f3039 x12: ffff7508fcea4ec1
x11: 1ffff508fcea4ec0 x10: ffff7508fcea4ec0 x9 : dfff800000000000
x8 : ffff6051b1a7f86a x7 : ffff800082a17270 x6 : 0000000000000002
x5 : 0000000000000098 x4 : ffff028d9817b250 x3 : 0000000000000000
x2 : 0000000000000000 x1 : ffffa847e40a5320 x0 : 0000000000000000
Call trace:
strncpy+0x58/0xc0
kunit_filter_suites+0x15c/0xa84
kunit_module_notify+0x1b0/0x3ac
blocking_notifier_call_chain+0xc4/0x128
do_init_module+0x250/0x594
load_module+0x37b0/0x44b4
init_module_from_file+0xd4/0x128
idempotent_init_module+0x2c8/0x524
__arm64_sys_finit_module+0xac/0x100
invoke_syscall+0x6c/0x258
el0_svc_common.constprop.0+0x160/0x22c
do_el0_svc+0x44/0x5c
el0_svc+0x38/0x78
el0t_64_sync_handler+0x13c/0x158
el0t_64_sync+0x190/0x194
Code: 5400028a d343fe63 12000a62 39400034 (38f56863)
---[ end trace 0000000000000000 ]---
Kernel panic - not syncing: Oops: Fatal exception
SMP: stopping secondary CPUs
Kernel Offset: 0x284761400000 from 0xffff800080000000
PHYS_OFFSET: 0xfffffd7380000000
CPU features: 0x88000203,3c020000,1000421b
Memory Limit: none
Rebooting in 1 seconds..

Fixes: a127b154a8f2 ("kunit: tool: allow filtering test cases via glob")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
Reviewed-by: Rae Moar <rm...@google.com>
Reviewed-by: David Gow <davi...@google.com>
---
v2:
- goto the new add identical purpose free_copy label.
---
lib/kunit/executor.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 0eda42b0c9bb..28f144de748b 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -152,8 +164,11 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
}
copy_start = copy;

- if (filter_glob)
- kunit_parse_glob_filter(&parsed_glob, filter_glob);
+ if (filter_glob) {
+ *err = kunit_parse_glob_filter(&parsed_glob, filter_glob);
+ if (*err)
+ goto free_copy;

Jinjie Ruan

unread,
Sep 3, 2023, 3:11:07 AM9/3/23
to brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ruanj...@huawei.com
If both filter_glob and filters are not NULL, and kunit_parse_glob_filter()
succeed, but kcalloc parsed_filters fails, the suite_glob and test_glob of
parsed kzalloc in kunit_parse_glob_filter() will be leaked.

As Rae suggested, assign -ENOMEM to *err to correctly free copy and goto
free_parsed_glob to free the suite/test_glob of parsed.

Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
Suggested-by: Rae Moar <rm...@google.com>
---
v2:
- Add *err = -ENOMEM before goto to correctly free copy.
- Goto the new add identical purpose free_parsed_glob label.
- Update the commit message.
---
lib/kunit/executor.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 28f144de748b..a6348489d45f 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -175,8 +175,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
filter_count = kunit_get_filter_count(filters);
parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
if (!parsed_filters) {
- kfree(copy);
- return filtered;
+ *err = -ENOMEM;
+ goto free_parsed_glob;
}
for (j = 0; j < filter_count; j++)
parsed_filters[j] = kunit_next_attr_filter(&filters, err);
--
2.34.1

kernel test robot

unread,
Sep 3, 2023, 5:43:59 AM9/3/23
to Jinjie Ruan, brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, oe-kbu...@lists.linux.dev
Hi Jinjie,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on next-20230831]
[cannot apply to v6.5]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Jinjie-Ruan/kunit-Fix-wild-memory-access-bug-in-kunit_free_suite_set/20230903-151137
base: linus/master
patch link: https://lore.kernel.org/r/20230903071028.1518913-3-ruanjinjie%40huawei.com
patch subject: [PATCH v2 2/4] kunit: Fix the wrong err path and add goto labels in kunit_filter_suites()
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20230903/202309031733...@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230903/202309031733...@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <l...@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202309031733...@intel.com/

All warnings (new ones prefixed by >>):

lib/kunit/executor.c: In function 'kunit_filter_suites':
>> lib/kunit/executor.c:227:1: warning: label 'free_copy' defined but not used [-Wunused-label]
227 | free_copy:
| ^~~~~~~~~
>> lib/kunit/executor.c:221:1: warning: label 'free_parsed_glob' defined but not used [-Wunused-label]
221 | free_parsed_glob:
| ^~~~~~~~~~~~~~~~


vim +/free_copy +227 lib/kunit/executor.c

132
133 struct kunit_suite_set
134 kunit_filter_suites(const struct kunit_suite_set *suite_set,
135 const char *filter_glob,
136 char *filters,
137 char *filter_action,
138 int *err)
139 {
140 int i, j, k;
141 int filter_count = 0;
142 struct kunit_suite **copy, **copy_start, *filtered_suite, *new_filtered_suite;
143 struct kunit_suite_set filtered = {NULL, NULL};
144 struct kunit_glob_filter parsed_glob;
145 struct kunit_attr_filter *parsed_filters = NULL;
146
147 const size_t max = suite_set->end - suite_set->start;
148
149 copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
150 if (!copy) { /* won't be able to run anything, return an empty set */
151 return filtered;
152 }
153 copy_start = copy;
154
155 if (filter_glob)
156 kunit_parse_glob_filter(&parsed_glob, filter_glob);
157
158 /* Parse attribute filters */
159 if (filters) {
160 filter_count = kunit_get_filter_count(filters);
161 parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
162 if (!parsed_filters) {
163 kfree(copy);
164 return filtered;
165 }
166 for (j = 0; j < filter_count; j++)
167 parsed_filters[j] = kunit_next_attr_filter(&filters, err);
168 if (*err)
169 goto free_parsed_filters;
170 }
171
172 for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
173 filtered_suite = suite_set->start[i];
174 if (filter_glob) {
175 if (!glob_match(parsed_glob.suite_glob, filtered_suite->name))
176 continue;
177 filtered_suite = kunit_filter_glob_tests(filtered_suite,
178 parsed_glob.test_glob);
179 if (IS_ERR(filtered_suite)) {
180 *err = PTR_ERR(filtered_suite);
181 goto free_parsed_filters;
182 }
183 }
184 if (filter_count > 0 && parsed_filters != NULL) {
185 for (k = 0; k < filter_count; k++) {
186 new_filtered_suite = kunit_filter_attr_tests(filtered_suite,
187 parsed_filters[k], filter_action, err);
188
189 /* Free previous copy of suite */
190 if (k > 0 || filter_glob) {
191 kfree(filtered_suite->test_cases);
192 kfree(filtered_suite);
193 }
194
195 filtered_suite = new_filtered_suite;
196
197 if (*err)
198 goto free_parsed_filters;
199
200 if (IS_ERR(filtered_suite)) {
201 *err = PTR_ERR(filtered_suite);
202 goto free_parsed_filters;
203 }
204 if (!filtered_suite)
205 break;
206 }
207 }
208
209 if (!filtered_suite)
210 continue;
211
212 *copy++ = filtered_suite;
213 }
214 filtered.start = copy_start;
215 filtered.end = copy;
216
217 free_parsed_filters:
218 if (filter_count)
219 kfree(parsed_filters);
220
> 221 free_parsed_glob:
222 if (filter_glob) {
223 kfree(parsed_glob.suite_glob);
224 kfree(parsed_glob.test_glob);
225 }
226
> 227 free_copy:
228 if (*err)
229 kfree(copy);
230
231 return filtered;
232 }
233

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

kernel test robot

unread,
Sep 3, 2023, 5:44:00 AM9/3/23
to Jinjie Ruan, brendan...@linux.dev, davi...@google.com, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com, ll...@lists.linux.dev, oe-kbu...@lists.linux.dev
Hi Jinjie,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on next-20230831]
[cannot apply to v6.5]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Jinjie-Ruan/kunit-Fix-wild-memory-access-bug-in-kunit_free_suite_set/20230903-151137
base: linus/master
patch link: https://lore.kernel.org/r/20230903071028.1518913-3-ruanjinjie%40huawei.com
patch subject: [PATCH v2 2/4] kunit: Fix the wrong err path and add goto labels in kunit_filter_suites()
config: x86_64-rhel-8.3-rust (https://download.01.org/0day-ci/archive/20230903/202309031713...@intel.com/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230903/202309031713...@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <l...@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202309031713...@intel.com/

All warnings (new ones prefixed by >>):

>> lib/kunit/executor.c:221:1: warning: unused label 'free_parsed_glob' [-Wunused-label]
free_parsed_glob:
^~~~~~~~~~~~~~~~~
>> lib/kunit/executor.c:227:1: warning: unused label 'free_copy' [-Wunused-label]
free_copy:
^~~~~~~~~~
2 warnings generated.


vim +/free_parsed_glob +221 lib/kunit/executor.c

David Gow

unread,
Sep 5, 2023, 3:12:52 AM9/5/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
Still looks good, thanks.

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David


David Gow

unread,
Sep 5, 2023, 3:12:56 AM9/5/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Sun, 3 Sept 2023 at 15:11, 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> Take the last kfree(parsed_filters) and add it to be the first. Take
> the first kfree(copy) and add it to be the last. The Best practice is to
> return these errors reversely.
>
> And as David suggested, add several labels which target only the things
> which actually have been allocated so far.
>
> Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
> Fixes: abbf73816b6f ("kunit: fix possible memory leak in kunit_filter_suites()")
> Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
> Reviewed-by: Rae Moar <rm...@google.com>
> Suggested-by: David Gow <davi...@google.com>
> ---
> v2:
> - Add err path labels.
> - Update the commit message and title.
> ---

This looks good to me. The kernel test robot does complain about
unused labels, so it might make sense to wait to introduce the labels
in the later patches (or merge patches 2,3,4 together), but personally
I'm okay with it as-is, as these should be merged in one go.

Reviewed-by: David Gow <davi...@google.com>


-- David
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230903071028.1518913-3-ruanjinjie%40huawei.com.

David Gow

unread,
Sep 5, 2023, 3:13:41 AM9/5/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Sun, 3 Sept 2023 at 15:11, 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
Looks good to me.

This is still,
Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David


> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230903071028.1518913-4-ruanjinjie%40huawei.com.

David Gow

unread,
Sep 5, 2023, 3:13:47 AM9/5/23
to Jinjie Ruan, brendan...@linux.dev, sk...@linuxfoundation.org, j...@codeconstruct.com.au, dlat...@google.com, rm...@google.com, linux-k...@vger.kernel.org, kuni...@googlegroups.com
On Sun, 3 Sept 2023 at 15:11, 'Jinjie Ruan' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> If both filter_glob and filters are not NULL, and kunit_parse_glob_filter()
> succeed, but kcalloc parsed_filters fails, the suite_glob and test_glob of
> parsed kzalloc in kunit_parse_glob_filter() will be leaked.
>
> As Rae suggested, assign -ENOMEM to *err to correctly free copy and goto
> free_parsed_glob to free the suite/test_glob of parsed.
>
> Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
> Signed-off-by: Jinjie Ruan <ruanj...@huawei.com>
> Suggested-by: Rae Moar <rm...@google.com>
> ---
> v2:
> - Add *err = -ENOMEM before goto to correctly free copy.
> - Goto the new add identical purpose free_parsed_glob label.
> - Update the commit message.
> ---

Looks good to me.

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David


> lib/kunit/executor.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
> index 28f144de748b..a6348489d45f 100644
> --- a/lib/kunit/executor.c
> +++ b/lib/kunit/executor.c
> @@ -175,8 +175,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
> filter_count = kunit_get_filter_count(filters);
> parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
> if (!parsed_filters) {
> - kfree(copy);
> - return filtered;
> + *err = -ENOMEM;
> + goto free_parsed_glob;
> }
> for (j = 0; j < filter_count; j++)
> parsed_filters[j] = kunit_next_attr_filter(&filters, err);
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230903071028.1518913-5-ruanjinjie%40huawei.com.
Reply all
Reply to author
Forward
0 new messages