* [PATCH] bpftool: Fix memory leak in dump_link_nlmsg on realloc failure
@ 2025-06-19 3:10 chenyuan
2025-06-19 6:57 ` [PATH v2] bpftool: Fix memory leak in dump_xx_nlmsg " chenyuan
0 siblings, 1 reply; 7+ messages in thread
From: chenyuan @ 2025-06-19 3:10 UTC (permalink / raw)
To: ast, andrii; +Cc: bpf, linux-kernel, chenyuan_fl, chenyuan
From: chenyuan <chenyuan@kylinos.cn>
In function dump_link_nlmsg(), when realloc() fails to allocate memory,
the original pointer to the buffer is overwritten with NULL. This causes
a memory leak because the previously allocated buffer becomes unreachable
without being freed.
Fix: 7900efc19214 ("tools/bpf: bpftool: improve output format for bpftool net")
Signed-off-by: chenyuan <chenyuan@kylinos.cn>
---
tools/bpf/bpftool/net.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index 64f958f437b0..e00637b85e56 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -366,17 +366,18 @@ static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
{
struct bpf_netdev_t *netinfo = cookie;
struct ifinfomsg *ifinfo = msg;
+ struct ip_devname_ifindex *tmp;
if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
return 0;
if (netinfo->used_len == netinfo->array_len) {
- netinfo->devices = realloc(netinfo->devices,
- (netinfo->array_len + 16) *
+ tmp = realloc(netinfo->devices, (netinfo->array_len + 16) *
sizeof(struct ip_devname_ifindex));
- if (!netinfo->devices)
+ if (!tmp)
return -ENOMEM;
+ netinfo->devices = tmp;
netinfo->array_len += 16;
}
netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATH v2] bpftool: Fix memory leak in dump_xx_nlmsg on realloc failure
2025-06-19 3:10 [PATCH] bpftool: Fix memory leak in dump_link_nlmsg on realloc failure chenyuan
@ 2025-06-19 6:57 ` chenyuan
2025-06-19 8:45 ` Quentin Monnet
2025-06-19 15:55 ` Alexei Starovoitov
0 siblings, 2 replies; 7+ messages in thread
From: chenyuan @ 2025-06-19 6:57 UTC (permalink / raw)
To: ast, andrii; +Cc: bpf, linux-kernel, chenyuan_fl, chenyuan
From: chenyuan <chenyuan@kylinos.cn>
In function dump_xx_nlmsg(), when realloc() fails to allocate memory,
the original pointer to the buffer is overwritten with NULL. This causes
a memory leak because the previously allocated buffer becomes unreachable
without being freed.
Fix: 7900efc19214 ("tools/bpf: bpftool: improve output format for bpftool net")
Signed-off-by: chenyuan <chenyuan@kylinos.cn>
---
tools/bpf/bpftool/net.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index 64f958f437b0..1abedfe6f33f 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -366,17 +366,18 @@ static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
{
struct bpf_netdev_t *netinfo = cookie;
struct ifinfomsg *ifinfo = msg;
+ struct ip_devname_ifindex *tmp;
if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
return 0;
if (netinfo->used_len == netinfo->array_len) {
- netinfo->devices = realloc(netinfo->devices,
- (netinfo->array_len + 16) *
+ tmp = realloc(netinfo->devices, (netinfo->array_len + 16) *
sizeof(struct ip_devname_ifindex));
- if (!netinfo->devices)
+ if (!tmp)
return -ENOMEM;
+ netinfo->devices = tmp;
netinfo->array_len += 16;
}
netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
@@ -395,6 +396,7 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
{
struct bpf_tcinfo_t *tcinfo = cookie;
struct tcmsg *info = msg;
+ struct tc_kind_handle *tmp;
if (tcinfo->is_qdisc) {
/* skip clsact qdisc */
@@ -406,11 +408,12 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
}
if (tcinfo->used_len == tcinfo->array_len) {
- tcinfo->handle_array = realloc(tcinfo->handle_array,
+ tmp = realloc(tcinfo->handle_array,
(tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
- if (!tcinfo->handle_array)
+ if (!tmp)
return -ENOMEM;
+ tcinfo->handle_array = tmp;
tcinfo->array_len += 16;
}
tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATH v2] bpftool: Fix memory leak in dump_xx_nlmsg on realloc failure
2025-06-19 6:57 ` [PATH v2] bpftool: Fix memory leak in dump_xx_nlmsg " chenyuan
@ 2025-06-19 8:45 ` Quentin Monnet
2025-06-19 15:55 ` Alexei Starovoitov
1 sibling, 0 replies; 7+ messages in thread
From: Quentin Monnet @ 2025-06-19 8:45 UTC (permalink / raw)
To: chenyuan, ast, andrii; +Cc: bpf, linux-kernel, chenyuan
2025-06-19 14:57 UTC+0800 ~ chenyuan <chenyuan_fl@163.com>
> From: chenyuan <chenyuan@kylinos.cn>
>
> In function dump_xx_nlmsg(), when realloc() fails to allocate memory,
> the original pointer to the buffer is overwritten with NULL. This causes
> a memory leak because the previously allocated buffer becomes unreachable
> without being freed.
>
> Fix: 7900efc19214 ("tools/bpf: bpftool: improve output format for bpftool net")
Fixes: 7900efc19214 ("tools/bpf: bpftool: improve output format for bpftool net")
(Not "Fix:")
> Signed-off-by: chenyuan <chenyuan@kylinos.cn>
> ---
> tools/bpf/bpftool/net.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> index 64f958f437b0..1abedfe6f33f 100644
> --- a/tools/bpf/bpftool/net.c
> +++ b/tools/bpf/bpftool/net.c
> @@ -366,17 +366,18 @@ static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
> {
> struct bpf_netdev_t *netinfo = cookie;
> struct ifinfomsg *ifinfo = msg;
> + struct ip_devname_ifindex *tmp;
>
> if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
> return 0;
>
> if (netinfo->used_len == netinfo->array_len) {
> - netinfo->devices = realloc(netinfo->devices,
> - (netinfo->array_len + 16) *
> + tmp = realloc(netinfo->devices, (netinfo->array_len + 16) *
> sizeof(struct ip_devname_ifindex));
Nit: Can you please break the line after the comma rather than after the '*' sign,
like in dump_class_qdisc_nlmsg()?
Otherwise looks good, thanks for this! You can add my tag for v3:
Reviewed-by: Quentin Monnet <qmo@kernel.org>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATH v2] bpftool: Fix memory leak in dump_xx_nlmsg on realloc failure
2025-06-19 6:57 ` [PATH v2] bpftool: Fix memory leak in dump_xx_nlmsg " chenyuan
2025-06-19 8:45 ` Quentin Monnet
@ 2025-06-19 15:55 ` Alexei Starovoitov
2025-06-20 1:21 ` [PATCH v3] " Yuan Chen
1 sibling, 1 reply; 7+ messages in thread
From: Alexei Starovoitov @ 2025-06-19 15:55 UTC (permalink / raw)
To: chenyuan; +Cc: Alexei Starovoitov, Andrii Nakryiko, bpf, LKML, chenyuan
On Wed, Jun 18, 2025 at 11:57 PM chenyuan <chenyuan_fl@163.com> wrote:
>
> From: chenyuan <chenyuan@kylinos.cn>
>
> In function dump_xx_nlmsg(), when realloc() fails to allocate memory,
> the original pointer to the buffer is overwritten with NULL. This causes
> a memory leak because the previously allocated buffer becomes unreachable
> without being freed.
>
> Fix: 7900efc19214 ("tools/bpf: bpftool: improve output format for bpftool net")
> Signed-off-by: chenyuan <chenyuan@kylinos.cn>
SOB and Author field should have full name as "First Last".
pw-bot: cr
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3] bpftool: Fix memory leak in dump_xx_nlmsg on realloc failure
2025-06-19 15:55 ` Alexei Starovoitov
@ 2025-06-20 1:21 ` Yuan Chen
2025-06-20 9:00 ` Quentin Monnet
2025-06-20 18:40 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 7+ messages in thread
From: Yuan Chen @ 2025-06-20 1:21 UTC (permalink / raw)
To: ast, qmo, alexei.starovoitov; +Cc: bpf, linux-kernel, chenyuan_fl, Yuan Chen
From: Yuan Chen <chenyuan@kylinos.cn>
In function dump_xx_nlmsg(), when realloc() fails to allocate memory,
the original pointer to the buffer is overwritten with NULL. This causes
a memory leak because the previously allocated buffer becomes unreachable
without being freed.
Fixes: 7900efc19214 ("tools/bpf: bpftool: improve output format for bpftool net")
Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
---
tools/bpf/bpftool/net.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index 64f958f437b0..cfc6f944f7c3 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -366,17 +366,18 @@ static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
{
struct bpf_netdev_t *netinfo = cookie;
struct ifinfomsg *ifinfo = msg;
+ struct ip_devname_ifindex *tmp;
if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
return 0;
if (netinfo->used_len == netinfo->array_len) {
- netinfo->devices = realloc(netinfo->devices,
- (netinfo->array_len + 16) *
- sizeof(struct ip_devname_ifindex));
- if (!netinfo->devices)
+ tmp = realloc(netinfo->devices,
+ (netinfo->array_len + 16) * sizeof(struct ip_devname_ifindex));
+ if (!tmp)
return -ENOMEM;
+ netinfo->devices = tmp;
netinfo->array_len += 16;
}
netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
@@ -395,6 +396,7 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
{
struct bpf_tcinfo_t *tcinfo = cookie;
struct tcmsg *info = msg;
+ struct tc_kind_handle *tmp;
if (tcinfo->is_qdisc) {
/* skip clsact qdisc */
@@ -406,11 +408,12 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
}
if (tcinfo->used_len == tcinfo->array_len) {
- tcinfo->handle_array = realloc(tcinfo->handle_array,
+ tmp = realloc(tcinfo->handle_array,
(tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
- if (!tcinfo->handle_array)
+ if (!tmp)
return -ENOMEM;
+ tcinfo->handle_array = tmp;
tcinfo->array_len += 16;
}
tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v3] bpftool: Fix memory leak in dump_xx_nlmsg on realloc failure
2025-06-20 1:21 ` [PATCH v3] " Yuan Chen
@ 2025-06-20 9:00 ` Quentin Monnet
2025-06-20 18:40 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 7+ messages in thread
From: Quentin Monnet @ 2025-06-20 9:00 UTC (permalink / raw)
To: Yuan Chen, ast, alexei.starovoitov; +Cc: bpf, linux-kernel, Yuan Chen
2025-06-20 09:21 UTC+0800 ~ Yuan Chen <chenyuan_fl@163.com>
> From: Yuan Chen <chenyuan@kylinos.cn>
>
> In function dump_xx_nlmsg(), when realloc() fails to allocate memory,
> the original pointer to the buffer is overwritten with NULL. This causes
> a memory leak because the previously allocated buffer becomes unreachable
> without being freed.
>
> Fixes: 7900efc19214 ("tools/bpf: bpftool: improve output format for bpftool net")
> Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
Reviewed-by: Quentin Monnet <qmo@kernel.org>
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3] bpftool: Fix memory leak in dump_xx_nlmsg on realloc failure
2025-06-20 1:21 ` [PATCH v3] " Yuan Chen
2025-06-20 9:00 ` Quentin Monnet
@ 2025-06-20 18:40 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-06-20 18:40 UTC (permalink / raw)
To: Yuan Chen; +Cc: ast, qmo, alexei.starovoitov, bpf, linux-kernel, chenyuan
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Fri, 20 Jun 2025 09:21:33 +0800 you wrote:
> From: Yuan Chen <chenyuan@kylinos.cn>
>
> In function dump_xx_nlmsg(), when realloc() fails to allocate memory,
> the original pointer to the buffer is overwritten with NULL. This causes
> a memory leak because the previously allocated buffer becomes unreachable
> without being freed.
>
> [...]
Here is the summary with links:
- [v3] bpftool: Fix memory leak in dump_xx_nlmsg on realloc failure
https://git.kernel.org/bpf/bpf-next/c/99fe8af069a9
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-06-20 18:39 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-19 3:10 [PATCH] bpftool: Fix memory leak in dump_link_nlmsg on realloc failure chenyuan
2025-06-19 6:57 ` [PATH v2] bpftool: Fix memory leak in dump_xx_nlmsg " chenyuan
2025-06-19 8:45 ` Quentin Monnet
2025-06-19 15:55 ` Alexei Starovoitov
2025-06-20 1:21 ` [PATCH v3] " Yuan Chen
2025-06-20 9:00 ` Quentin Monnet
2025-06-20 18:40 ` patchwork-bot+netdevbpf
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).