From: Andrea Claudi Date: Mon, 19 Apr 2021 13:49:57 +0000 (+0200) Subject: lib: bpf_legacy: fix missing socket close when connect() fails X-Git-Tag: v5.12.0~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e1ad689545a0a2a798869cb95de7dbe4b138bdae;p=thirdparty%2Fiproute2.git lib: bpf_legacy: fix missing socket close when connect() fails 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 Signed-off-by: Stephen Hemminger --- diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c index 7ff10e4f2..7ec9ce9d6 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; }