Skip to content

eBPF-based security platform

upatch: check dirfd() and take libbpf's errors from what it returns

Daniel Borkmann75d0730d7374
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 258cade8..987190e9 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2783,6 +2783,11 @@ static int wipe_dir_flat(const char *dir)
if (!d)
return -errno;
dfd = dirfd(d);
+ if (dfd < 0) {
+ ret = -errno;
+ closedir(d);
+ return ret;
+ }
while ((e = readdir(d))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
continue;
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index bb7c143d..09a61c88 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -728,19 +728,19 @@ static int open_map_pin(const char *upatch, const char *map_name,
{
char path[PATH_MAX];
__u32 info_len = sizeof(*info_out);
- int fd;
+ int fd, ret;
if (upatch_pin_path(path, sizeof(path), upatch, "maps", map_name, NULL))
return -ENAMETOOLONG;
+ /* libbpf returns the error it failed with. */
fd = bpf_obj_get(path);
if (fd < 0)
- return -errno;
+ return fd;
memset(info_out, 0, sizeof(*info_out));
- if (bpf_map_get_info_by_fd(fd, info_out, &info_len)) {
- int e = -errno;
-
+ ret = bpf_map_get_info_by_fd(fd, info_out, &info_len);
+ if (ret) {
close(fd);
- return e;
+ return ret;
}
*fd_out = fd;
return 0;