summaryrefslogtreecommitdiffstats
path: root/tools/lib
diff options
context:
space:
mode:
authorToke Høiland-Jørgensen <toke@redhat.com>2019-11-02 12:09:37 +0100
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2019-12-31 16:44:33 +0100
commit8f2570572e0e52026d09bbbe18e81bf2985ca92d (patch)
treee4b8d3ca1ee26a6cac98b7c746611a3707798078 /tools/lib
parent8679dce8c662e1c72230332812a283b23d34a9a7 (diff)
downloadlinux-stable-8f2570572e0e52026d09bbbe18e81bf2985ca92d.tar.gz
linux-stable-8f2570572e0e52026d09bbbe18e81bf2985ca92d.tar.bz2
linux-stable-8f2570572e0e52026d09bbbe18e81bf2985ca92d.zip
libbpf: Fix error handling in bpf_map__reuse_fd()
[ Upstream commit d1b4574a4b86565325ef2e545eda8dfc9aa07c60 ] bpf_map__reuse_fd() was calling close() in the error path before returning an error value based on errno. However, close can change errno, so that can lead to potentially misleading error messages. Instead, explicitly store errno in the err variable before each goto. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/157269297769.394725.12634985106772698611.stgit@toke.dk Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'tools/lib')
-rw-r--r--tools/lib/bpf/libbpf.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index e0276520171b..a267cd0c0ce2 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1897,16 +1897,22 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
return -errno;
new_fd = open("/", O_RDONLY | O_CLOEXEC);
- if (new_fd < 0)
+ if (new_fd < 0) {
+ err = -errno;
goto err_free_new_name;
+ }
new_fd = dup3(fd, new_fd, O_CLOEXEC);
- if (new_fd < 0)
+ if (new_fd < 0) {
+ err = -errno;
goto err_close_new_fd;
+ }
err = zclose(map->fd);
- if (err)
+ if (err) {
+ err = -errno;
goto err_close_new_fd;
+ }
free(map->name);
map->fd = new_fd;
@@ -1925,7 +1931,7 @@ err_close_new_fd:
close(new_fd);
err_free_new_name:
free(new_name);
- return -errno;
+ return err;
}
int bpf_map__resize(struct bpf_map *map, __u32 max_entries)