All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2 0/2] lib: bpf_legacy: some misc fixes
@ 2021-04-19 13:49 Andrea Claudi
  2021-04-19 13:49 ` [PATCH iproute2 1/2] lib: bpf_legacy: treat 0 as a valid file descriptor Andrea Claudi
  2021-04-19 13:49 ` [PATCH iproute2 2/2] lib: bpf_legacy: fix missing socket close when connect() fails Andrea Claudi
  0 siblings, 2 replies; 3+ messages in thread
From: Andrea Claudi @ 2021-04-19 13:49 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

This patch set fixes two issues on bpf_legacy library code.
- 1/2 fixes a covscan warning about a possible missing close() treating
  0 as a valid file descriptor, as almost elsewhere in iproute2 code.
- 2/2 fixes a missing close() on a socket in some error paths in two
  functions.

Andrea Claudi (2):
  lib: bpf_legacy: treat 0 as a valid file descriptor
  lib: bpf_legacy: fix missing socket close when connect() fails

 lib/bpf_legacy.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH iproute2 1/2] lib: bpf_legacy: treat 0 as a valid file descriptor
  2021-04-19 13:49 [PATCH iproute2 0/2] lib: bpf_legacy: some misc fixes Andrea Claudi
@ 2021-04-19 13:49 ` Andrea Claudi
  2021-04-19 13:49 ` [PATCH iproute2 2/2] lib: bpf_legacy: fix missing socket close when connect() fails Andrea Claudi
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Claudi @ 2021-04-19 13:49 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

As stated in the man page(), open returns a non-negative integer as a
file descriptor. Hence, when checking for its return value to be ok, we
should include 0 as a valid value.

This fixes a covscan warning about a missing close() in this function.

Fixes: ecb05c0f997d ("bpf: improve error reporting around tail calls")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
 lib/bpf_legacy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c
index 8a03b9c2..7ff10e4f 100644
--- a/lib/bpf_legacy.c
+++ b/lib/bpf_legacy.c
@@ -2832,7 +2832,7 @@ static void bpf_get_cfg(struct bpf_elf_ctx *ctx)
 	int fd;
 
 	fd = open(path_jit, O_RDONLY);
-	if (fd > 0) {
+	if (fd >= 0) {
 		char tmp[16] = {};
 
 		if (read(fd, tmp, sizeof(tmp)) > 0)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH iproute2 2/2] lib: bpf_legacy: fix missing socket close when connect() fails
  2021-04-19 13:49 [PATCH iproute2 0/2] lib: bpf_legacy: some misc fixes Andrea Claudi
  2021-04-19 13:49 ` [PATCH iproute2 1/2] lib: bpf_legacy: treat 0 as a valid file descriptor Andrea Claudi
@ 2021-04-19 13:49 ` Andrea Claudi
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Claudi @ 2021-04-19 13:49 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

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 --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;
 }
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-19 13:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-19 13:49 [PATCH iproute2 0/2] lib: bpf_legacy: some misc fixes Andrea Claudi
2021-04-19 13:49 ` [PATCH iproute2 1/2] lib: bpf_legacy: treat 0 as a valid file descriptor Andrea Claudi
2021-04-19 13:49 ` [PATCH iproute2 2/2] lib: bpf_legacy: fix missing socket close when connect() fails Andrea Claudi

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.