diff mbox series

kunit: fix reference count leak in kfree_at_end

Message ID 1631165296-81082-1-git-send-email-xiyuyang19@fudan.edu.cn
State Accepted
Commit f62314b1ced25c58b86e044fc951cd6a1ea234cf
Headers show
Series kunit: fix reference count leak in kfree_at_end | expand

Commit Message

Xiyu Yang Sept. 9, 2021, 5:28 a.m. UTC
The reference counting issue happens in the normal path of
kfree_at_end(). When kunit_alloc_and_get_resource() is invoked, the
function forgets to handle the returned resource object, whose refcount
increased inside, causing a refcount leak.

Fix this issue by calling kunit_put_resource() at the end of function.

Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
 lib/kunit/executor_test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

David Gow Sept. 9, 2021, 6:54 a.m. UTC | #1
On Thu, Sep 9, 2021 at 1:28 PM 'Xiyu Yang' via KUnit Development
<kunit-dev@googlegroups.com> wrote:
>
> The reference counting issue happens in the normal path of
> kfree_at_end(). When kunit_alloc_and_get_resource() is invoked, the
> function forgets to handle the returned resource object, whose refcount
> increased inside, causing a refcount leak.
>
> Fix this issue by calling kunit_put_resource() at the end of function.
>
> Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
> ---

Thanks for looking into this.

+ Daniel -- any thoughts?

>  lib/kunit/executor_test.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c
> index cdbe54b16501..3af30abad826 100644
> --- a/lib/kunit/executor_test.c
> +++ b/lib/kunit/executor_test.c
> @@ -113,11 +113,13 @@ static void kfree_res_free(struct kunit_resource *res)
>   */
>  static void kfree_at_end(struct kunit *test, const void *to_free)
>  {
> +       struct kunit_resource *res;
>         /* kfree() handles NULL already, but avoid allocating a no-op cleanup. */
>         if (IS_ERR_OR_NULL(to_free))
>                 return;
> -       kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
> +       res = kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
>                                      (void *)to_free);
> +       kunit_put_resource(res);

Why not just change this to kunit_alloc_resource()?

>  }
>
>  static struct kunit_suite *alloc_fake_suite(struct kunit *test,
> --
> 2.7.4
>
> --
> 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+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/1631165296-81082-1-git-send-email-xiyuyang19%40fudan.edu.cn.
diff mbox series

Patch

diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c
index cdbe54b16501..3af30abad826 100644
--- a/lib/kunit/executor_test.c
+++ b/lib/kunit/executor_test.c
@@ -113,11 +113,13 @@  static void kfree_res_free(struct kunit_resource *res)
  */
 static void kfree_at_end(struct kunit *test, const void *to_free)
 {
+	struct kunit_resource *res;
 	/* kfree() handles NULL already, but avoid allocating a no-op cleanup. */
 	if (IS_ERR_OR_NULL(to_free))
 		return;
-	kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
+	res = kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
 				     (void *)to_free);
+	kunit_put_resource(res);
 }
 
 static struct kunit_suite *alloc_fake_suite(struct kunit *test,