From: Stephen Hemminger <stephen@networkplumber.org>
To: Maxim Petrov <mmrmaximuzz@gmail.com>
Cc: netdev@vger.kernel.org
Subject: Re: [PATCH iproute2] libnetlink: fix socket leak in rtnl_open_byproto()
Date: Thu, 10 Feb 2022 17:19:03 -0800 [thread overview]
Message-ID: <20220210171903.66f35b6c@hermes.local> (raw)
In-Reply-To: <d376d06b-d1e3-c462-3a60-cc2e8ed7a147@gmail.com>
On Tue, 8 Feb 2022 20:20:45 +0300
Maxim Petrov <mmrmaximuzz@gmail.com> wrote:
> rtnl_open_byproto() does not close the opened socket in case of errors, and the
> socket is returned to the caller in the `fd` field of the struct. However, none
> of the callers care about the socket, so close it in the function immediately to
> avoid any potential resource leaks.
>
> Signed-off-by: Maxim Petrov <mmrmaximuzz@gmail.com>
Can do the same thing without introducing a goto
diff --git a/lib/libnetlink.c b/lib/libnetlink.c
index 7e977a6762f8..0ed6d68b5c08 100644
--- a/lib/libnetlink.c
+++ b/lib/libnetlink.c
@@ -226,29 +226,26 @@ int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
memset(&rth->local, 0, sizeof(rth->local));
rth->local.nl_family = AF_NETLINK;
rth->local.nl_groups = subscriptions;
+ addr_len = sizeof(rth->local);
if (bind(rth->fd, (struct sockaddr *)&rth->local,
sizeof(rth->local)) < 0) {
perror("Cannot bind netlink socket");
- return -1;
- }
- addr_len = sizeof(rth->local);
- if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
+ } else if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
&addr_len) < 0) {
perror("Cannot getsockname");
- return -1;
- }
- if (addr_len != sizeof(rth->local)) {
+ } else if (addr_len != sizeof(rth->local)) {
fprintf(stderr, "Wrong address length %d\n", addr_len);
- return -1;
- }
- if (rth->local.nl_family != AF_NETLINK) {
+ } else if (rth->local.nl_family != AF_NETLINK) {
fprintf(stderr, "Wrong address family %d\n",
rth->local.nl_family);
- return -1;
+ } else {
+ rth->seq = time(NULL);
+ return 0;
}
- rth->seq = time(NULL);
- return 0;
+
+ rtnl_close(rth);
+ return -1;
}
int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
--
2.34.1
next prev parent reply other threads:[~2022-02-11 1:19 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-08 17:20 [PATCH iproute2] libnetlink: fix socket leak in rtnl_open_byproto() Maxim Petrov
2022-02-11 1:19 ` Stephen Hemminger [this message]
2022-02-11 18:30 ` Maxim Petrov
2022-02-11 18:35 ` Stephen Hemminger
2022-02-11 19:40 ` Stephen Hemminger
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=20220210171903.66f35b6c@hermes.local \
--to=stephen@networkplumber.org \
--cc=mmrmaximuzz@gmail.com \
--cc=netdev@vger.kernel.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.