public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kamil Duljas <kamil.duljas@gmail.com>
To: Jakub Kicinski <kuba@kernel.org>
Cc: "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>, Jiri Pirko <jiri@resnulli.us>,
	Johannes Berg <johannes@sipsolutions.net>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Kamil Duljas <kamil.duljas@gmail.com>
Subject: [PATCH] genetlink: Prevent memory leak when krealloc fail
Date: Sat, 18 Nov 2023 12:33:57 +0100	[thread overview]
Message-ID: <20231118113357.1999-1-kamil.duljas@gmail.com> (raw)

genl_allocate_reserve_groups() allocs new memory in while loop
but if krealloc fail, the memory allocated by kzalloc is not freed.
It seems allocated memory is unnecessary when the function
returns -ENOMEM

Signed-off-by: Kamil Duljas <kamil.duljas@gmail.com>
---
 net/netlink/genetlink.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
index 92ef5ed2e7b0..82273d6eaea3 100644
--- a/net/netlink/genetlink.c
+++ b/net/netlink/genetlink.c
@@ -437,8 +437,10 @@ static int genl_allocate_reserve_groups(int n_groups, int *first_id)
 			} else {
 				new_groups = krealloc(mc_groups, nlen,
 						      GFP_KERNEL);
-				if (!new_groups)
+				if (!new_groups) {
+					kfree(mc_groups);
 					return -ENOMEM;
+				}
 				mc_groups = new_groups;
 				for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
 					mc_groups[mc_groups_longs + i] = 0;
-- 
2.42.0.windows.2


             reply	other threads:[~2023-11-18 11:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-18 11:33 Kamil Duljas [this message]
2023-11-18 12:02 ` [PATCH] genetlink: Prevent memory leak when krealloc fail Florian Westphal
2023-11-18 13:27   ` Kamil Duljas
2023-11-18 13:42     ` Florian Westphal

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=20231118113357.1999-1-kamil.duljas@gmail.com \
    --to=kamil.duljas@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jiri@resnulli.us \
    --cc=johannes@sipsolutions.net \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox