Skip to contentSkip to conversation

kunit: executor: Fix a memory leak on failure in kunit_filter_tests

PATCH Superseded David Gow 4 participants 4 messages Jul 12, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

David Gow

author
#0 lore raw

PATCH 1/1 kunit: executor: Fix a memory leak on failure in kunit_filter_tests

It's possible that memory allocation for the copy will fail, but for the copy of the suite to succeed. In this case, the copy could be leaked.

Properly free 'copy' in the error case for the allocation of 'filtered' failing.

Note that there may also have been a similar issue in kunit_filter_subsuites, before it was removed in "kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites".

This was reported by clang-analyzer via the kernel test robot, here:
https://lore.kernel.org/all/[email protected]/

Fixes a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")
---
 lib/kunit/executor.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/kunit/executor.c +3 -1
index 6c489d6c5e5d..5e223327196a 100644
@@ -74,8 +74,10 @@ kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)
 		return ERR_PTR(-ENOMEM);
 
 	filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
-	if (!filtered)
+	if (!filtered) {
+		kfree(copy);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	n = 0;
 	kunit_suite_for_each_test_case(suite, test_case) {
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
#1 lore raw

Hi David,

Quoted 1 line · David Gow

It's possible that memory allocation for the copy will fail, but for the

I think it is "for the _filtered_ will fail". Maybe a typo?

Thanks,
Yujie

Quoted 35 lines

copy of the suite to succeed. In this case, the copy could be leaked.

Properly free 'copy' in the error case for the allocation of 'filtered' failing.

Note that there may also have been a similar issue in kunit_filter_subsuites, before it was removed in "kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites".

This was reported by clang-analyzer via the kernel test robot, here:
https://lore.kernel.org/all/[email protected]/

Fixes a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")
---
  lib/kunit/executor.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
lib/kunit/executor.c +3 -1
index 6c489d6c5e5d..5e223327196a 100644
@@ -74,8 +74,10 @@ kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)
  		return ERR_PTR(-ENOMEM);
 
  	filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
-	if (!filtered)
+	if (!filtered) {
+		kfree(copy);
  		return ERR_PTR(-ENOMEM);
+	}
 
  	n = 0;
  	kunit_suite_for_each_test_case(suite, test_case) {
Reviewed-by on v1 1/1
#2 lore raw
In reply to David Gow
Quoted 11 lines · David Gow

It's possible that memory allocation for the copy will fail, but for the copy of the suite to succeed. In this case, the copy could be leaked.

Properly free 'copy' in the error case for the allocation of 'filtered' failing.

Note that there may also have been a similar issue in kunit_filter_subsuites, before it was removed in "kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites".

Quoted 6 lines

This was reported by clang-analyzer via the kernel test robot, here:
https://lore.kernel.org/all/[email protected]/

Fixes a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")

Thanks for taking care of this.

Reviewed-by on v1 1/1
#3 lore raw
In reply to David Gow
Quoted 11 lines · David Gow

It's possible that memory allocation for the copy will fail, but for the copy of the suite to succeed. In this case, the copy could be leaked.

Properly free 'copy' in the error case for the allocation of 'filtered' failing.

Note that there may also have been a similar issue in kunit_filter_subsuites, before it was removed in "kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites".

Quoted 6 lines

This was reported by clang-analyzer via the kernel test robot, here:
https://lore.kernel.org/all/[email protected]/

Fixes a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")