From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <chenyuan0y@gmail.com>
Cc: <andriy.shevchenko@linux.intel.com>,
<anjali.k.kulkarni@oracle.com>, <davem@davemloft.net>,
<dhowells@redhat.com>, <edumazet@google.com>, <horms@kernel.org>,
<kuba@kernel.org>, <kuniyu@amazon.com>, <netdev@vger.kernel.org>,
<pabeni@redhat.com>, <pctammela@mojatatu.com>,
<zzjas98@gmail.com>
Subject: Re: [net/netlink] Question about potential memleak in netlink_proto_init()
Date: Thu, 14 Mar 2024 17:54:58 -0700 [thread overview]
Message-ID: <20240315005458.10355-1-kuniyu@amazon.com> (raw)
In-Reply-To: <ZfOalln/myRNOkH6@cy-server>
From: Chenyuan Yang <chenyuan0y@gmail.com>
Date: Thu, 14 Mar 2024 19:47:18 -0500
> Dear Netlink Developers,
>
> We are curious whether the function `netlink_proto_init()` might have a memory leak.
>
> The function is https://elixir.bootlin.com/linux/v6.8/source/net/netlink/af_netlink.c#L2908
> and the relevant code is
> ```
> static int __init netlink_proto_init(void)
> {
> int i;
> ...
>
> for (i = 0; i < MAX_LINKS; i++) {
> if (rhashtable_init(&nl_table[i].hash,
> &netlink_rhashtable_params) < 0) {
> while (--i > 0)
> rhashtable_destroy(&nl_table[i].hash);
> kfree(nl_table);
> goto panic;
If rhashtable_init() fails, the kernel panic occurs, so there's
no real memleak issue.
> }
> }
> ...
> }
> ```
>
> In the for loop, when `rhashtable_init()` fails, the function will free
> the allocated memory for `nl_table[i].hash` by checking `while (--i > 0)`.
> However, the first element (`i=1`) of `nl_table` is not freed since `i` is
> decremented before the check.
>
> Based on our understanding, a possible fix would be
> ```
> - while (--i > 0)
> + while (--i >= 0)
> ```
Change itself looks good, no need for cleanup in the first place though.
next prev parent reply other threads:[~2024-03-15 0:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-15 0:47 [net/netlink] Question about potential memleak in netlink_proto_init() Chenyuan Yang
2024-03-15 0:53 ` Florian Westphal
2024-03-15 0:54 ` Kuniyuki Iwashima [this message]
2024-03-15 14:36 ` Andy Shevchenko
2024-03-16 15:11 ` Chenyuan Yang
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=20240315005458.10355-1-kuniyu@amazon.com \
--to=kuniyu@amazon.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=anjali.k.kulkarni@oracle.com \
--cc=chenyuan0y@gmail.com \
--cc=davem@davemloft.net \
--cc=dhowells@redhat.com \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pctammela@mojatatu.com \
--cc=zzjas98@gmail.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;
as well as URLs for NNTP newsgroup(s).