From: Florian Westphal <fw@strlen.de>
To: heminhong <heminhong@kylinos.cn>
Cc: stephen@networkplumber.org, netdev@vger.kernel.org
Subject: Re: [PATCH v2] iproute2: prevent memory leak
Date: Wed, 15 Nov 2023 04:32:48 +0100 [thread overview]
Message-ID: <20231115033248.GC21242@breakpoint.cc> (raw)
In-Reply-To: <20231115023703.15417-1-heminhong@kylinos.cn>
heminhong <heminhong@kylinos.cn> wrote:
> When the return value of rtnl_talk() is less than 0, 'answer' does not
> need to release. When the return value of rtnl_talk() is greater than
> or equal to 0, 'answer' will be allocated, if subsequent processing fails,
> the memory should be free, otherwise it will cause memory leak.
I don't understand this patch. Care to elaborate where a memory leak is?
rtnl_talk -> __rtnl_talk -> __rtnl_talk_iov
998 static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
999 size_t iovlen, struct nlmsghdr **answer,
1000 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
[..]
1102 if (answer)
1103 *answer = (struct nlmsghdr *)buf;
1104 else
1105 free(buf);
1106 return 0;
1107 }
1108
1109 if (answer) {
1110 *answer = (struct nlmsghdr *)buf;
1111 return 0;
1112 }
I see no other spots where 'answer' is set, i.e. assignment ONLY on
'return 0'.
> Signed-off-by: heminhong <heminhong@kylinos.cn>
> ---
> ip/link_gre.c | 2 ++
> ip/link_gre6.c | 2 ++
> ip/link_ip6tnl.c | 2 ++
> ip/link_iptnl.c | 2 ++
> ip/link_vti.c | 2 ++
> ip/link_vti6.c | 2 ++
> 6 files changed, 12 insertions(+)
>
> diff --git a/ip/link_gre.c b/ip/link_gre.c
> index 74a5b5e9..b86ec22d 100644
> --- a/ip/link_gre.c
> +++ b/ip/link_gre.c
> @@ -111,6 +111,8 @@ static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
>
> if (rtnl_talk(&rth, &req.n, &answer) < 0) {
> get_failed:
> + if (answer)
> + free(answer);
This if() is not needed. free(NULL) is fine. But in this case,
'answer' can contain stack-garbage, as this variable isn't initialised
to NULL.
Moreover, the placement would need to be ABOVE the label, not below.
But, see above, I don't see a 'answer' related memleak.
next prev parent reply other threads:[~2023-11-15 3:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-14 9:24 [PATCH] iproute2: prevent memory leak heminhong
2023-11-15 0:36 ` Stephen Hemminger
2023-11-15 2:37 ` [PATCH v2] " heminhong
2023-11-15 3:32 ` Florian Westphal [this message]
2023-11-15 3:33 ` Stephen Hemminger
2023-11-15 7:56 ` [PATCH v3] " heminhong
2023-11-15 10:23 ` Petr Machata
2023-11-16 3:13 ` [PATCH v4] " heminhong
2023-11-16 12:04 ` Andrea Claudi
2023-11-16 23:05 ` Stephen Hemminger
2023-11-17 0:45 ` Andrea Claudi
2023-11-17 3:31 ` Stephen Hemminger
2023-11-17 17:20 ` patchwork-bot+netdevbpf
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=20231115033248.GC21242@breakpoint.cc \
--to=fw@strlen.de \
--cc=heminhong@kylinos.cn \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.