[3/4] net/tap: fix file descriptor leak on error

Message ID 20181106193005.5383-4-stephen@networkplumber.org (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series Coverity issue fixes |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Stephen Hemminger Nov. 6, 2018, 7:30 p.m. UTC
  If netlink socket setup fails the file descriptor was leaked.

Coverity issue: 257040
Fixes: 7c25284e30c2 ("net/tap: add netlink back-end for flow API")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 drivers/net/tap/tap_netlink.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Wiles, Keith Nov. 7, 2018, 10:02 a.m. UTC | #1
> On Nov 6, 2018, at 7:30 PM, Stephen Hemminger <stephen@networkplumber.org> wrote:
> 
> If netlink socket setup fails the file descriptor was leaked.

Acked-by: Keith Wiles <keith.wiles@intel.com>
> 
> Coverity issue: 257040
> Fixes: 7c25284e30c2 ("net/tap: add netlink back-end for flow API")
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
> drivers/net/tap/tap_netlink.c | 3 +++
> 1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/net/tap/tap_netlink.c b/drivers/net/tap/tap_netlink.c
> index 6cb510092218..14bbbec754f6 100644
> --- a/drivers/net/tap/tap_netlink.c
> +++ b/drivers/net/tap/tap_netlink.c
> @@ -51,14 +51,17 @@ tap_nl_init(uint32_t nl_groups)
> 	}
> 	if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
> 		TAP_LOG(ERR, "Unable to set socket buffer send size");
> +		close(fd);
> 		return -1;
> 	}
> 	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
> 		TAP_LOG(ERR, "Unable to set socket buffer receive size");
> +		close(fd);
> 		return -1;
> 	}
> 	if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
> 		TAP_LOG(ERR, "Unable to bind to the netlink socket");
> +		close(fd);
> 		return -1;
> 	}
> 	return fd;
> -- 
> 2.17.1
> 

Regards,
Keith
  

Patch

diff --git a/drivers/net/tap/tap_netlink.c b/drivers/net/tap/tap_netlink.c
index 6cb510092218..14bbbec754f6 100644
--- a/drivers/net/tap/tap_netlink.c
+++ b/drivers/net/tap/tap_netlink.c
@@ -51,14 +51,17 @@  tap_nl_init(uint32_t nl_groups)
 	}
 	if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
 		TAP_LOG(ERR, "Unable to set socket buffer send size");
+		close(fd);
 		return -1;
 	}
 	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
 		TAP_LOG(ERR, "Unable to set socket buffer receive size");
+		close(fd);
 		return -1;
 	}
 	if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
 		TAP_LOG(ERR, "Unable to bind to the netlink socket");
+		close(fd);
 		return -1;
 	}
 	return fd;