[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220210171903.66f35b6c@hermes.local>
Date: Thu, 10 Feb 2022 17:19:03 -0800
From: Stephen Hemminger <stephen@...workplumber.org>
To: Maxim Petrov <mmrmaximuzz@...il.com>
Cc: netdev@...r.kernel.org
Subject: Re: [PATCH iproute2] libnetlink: fix socket leak in
rtnl_open_byproto()
On Tue, 8 Feb 2022 20:20:45 +0300
Maxim Petrov <mmrmaximuzz@...il.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@...il.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
Powered by blists - more mailing lists