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: 6256f8c9e4 ("tc, bpf: finalize eBPF support for cls and act front-end")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Andrea Claudi 2021-04-19 15:49:57 +02:00 committed by Stephen Hemminger
parent 92af24c907
commit e1ad689545

View File

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