diff mbox series

[iproute2,2/2] lib: bpf_legacy: fix missing socket close when connect() fails

Message ID 0c07eb9675f0f9f4a48a9c99e01a6518c5192d23.1618839527.git.aclaudi@redhat.com (mailing list archive)
State Accepted
Delegated to: Stephen Hemminger
Headers show
Series lib: bpf_legacy: some misc fixes | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Andrea Claudi April 19, 2021, 1:49 p.m. UTC
In functions bpf_{send,recv}_map_fds(), when connect fails after a
socket is successfully opened, we return with error missing a close on
the socket.

Fix this closing the socket if opened and using a single return point
for both the functions.

Fixes: 6256f8c9e45f ("tc, bpf: finalize eBPF support for cls and act front-end")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
 lib/bpf_legacy.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c
index 7ff10e4f..7ec9ce9d 100644
--- a/lib/bpf_legacy.c
+++ b/lib/bpf_legacy.c
@@ -3092,13 +3092,13 @@  int bpf_send_map_fds(const char *path, const char *obj)
 		.st  = &ctx->stat,
 		.obj = obj,
 	};
-	int fd, ret;
+	int fd, ret = -1;
 
 	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
 	if (fd < 0) {
 		fprintf(stderr, "Cannot open socket: %s\n",
 			strerror(errno));
-		return -1;
+		goto out;
 	}
 
 	strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
@@ -3107,7 +3107,7 @@  int bpf_send_map_fds(const char *path, const char *obj)
 	if (ret < 0) {
 		fprintf(stderr, "Cannot connect to %s: %s\n",
 			path, strerror(errno));
-		return -1;
+		goto out;
 	}
 
 	ret = bpf_map_set_send(fd, &addr, sizeof(addr), &bpf_aux,
@@ -3117,7 +3117,9 @@  int bpf_send_map_fds(const char *path, const char *obj)
 			path, strerror(errno));
 
 	bpf_maps_teardown(ctx);
-	close(fd);
+out:
+	if (fd >= 0)
+		close(fd);
 	return ret;
 }
 
@@ -3125,13 +3127,13 @@  int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
 		     unsigned int entries)
 {
 	struct sockaddr_un addr = { .sun_family = AF_UNIX };
-	int fd, ret;
+	int fd, ret = -1;
 
 	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
 	if (fd < 0) {
 		fprintf(stderr, "Cannot open socket: %s\n",
 			strerror(errno));
-		return -1;
+		goto out;
 	}
 
 	strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
@@ -3140,7 +3142,7 @@  int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
 	if (ret < 0) {
 		fprintf(stderr, "Cannot bind to socket: %s\n",
 			strerror(errno));
-		return -1;
+		goto out;
 	}
 
 	ret = bpf_map_set_recv(fd, fds, aux, entries);
@@ -3149,7 +3151,10 @@  int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
 			path, strerror(errno));
 
 	unlink(addr.sun_path);
-	close(fd);
+
+out:
+	if (fd >= 0)
+		close(fd);
 	return ret;
 }