Skip to contentSkip to conversation

lib: bpf_legacy: some misc fixes

PATCH New Andrea Claudi 1 participant 3 messages Apr 19, 2021

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 1/2 lib: bpf_legacy: treat 0 as a valid file descriptor

In reply to Andrea Claudi

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")
---
 lib/bpf_legacy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf_legacy.c +1 -1
index 8a03b9c2..7ff10e4f 100644
@@ -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)
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/2 lib: bpf_legacy: fix missing socket close when connect() fails

In reply to Andrea Claudi

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")
---
 lib/bpf_legacy.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
lib/bpf_legacy.c +13 -8
index 7ff10e4f..7ec9ce9d 100644
@@ -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;
 }
 
Signature · 1 line
2.30.2
#0 lore raw

cover lib: bpf_legacy: some misc fixes

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(-)
Signature · 1 line
2.30.2