eBPF-based security platform
treewide: report the errno of the failure, not of what ran after it
Daniel Borkmann661a43779baa
diff --git a/builtin-upatch.c b/builtin-upatch.cindex 49d7ff9d..f1a5e914 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1083,9 +1083,10 @@ static int prepare_uprobe_targets(struct prog_spec *specs,return -E2BIG;}fd = open(proc_path, O_RDONLY | O_CLOEXEC);+ ret = fd < 0 ? -errno : 0;close(path_fd);- if (fd < 0)- return -errno;+ if (ret)+ return ret;if (fstat(fd, &opened_stat) != 0) {ret = -errno;close(fd);diff --git a/testing/helper/policy-probe.c b/testing/helper/policy-probe.cindex 29638417..0b890aa7 100644--- a/testing/helper/policy-probe.c+++ b/testing/helper/policy-probe.c@@ -98,7 +98,7 @@ static int check_mappings(const char *trusted, const char *outside){long length = sysconf(_SC_PAGESIZE);void *mapping;- int fd;+ int fd, err;errno = 0;mapping = mmap(NULL, length, PROT_READ | PROT_EXEC,@@ -119,8 +119,9 @@ static int check_mappings(const char *trusted, const char *outside)return 1;errno = 0;mapping = mmap(NULL, length, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);+ err = errno;close(fd);- if (mapping != MAP_FAILED || errno != EACCES)+ if (mapping != MAP_FAILED || err != EACCES)return 1;fd = open(trusted, O_RDONLY);if (fd < 0)diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 0c0398c8..6eff06f1 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -681,10 +681,14 @@ static int build_resolve_source(const struct bpm_build_spec *spec,if (!source[0] || source[0] == '/' || strstr(source, "../") ||!strcmp(source, ".."))return -EINVAL;- if (strsprintf(path, sizeof(path), "%s/%s", spec->scope, source) < 0 ||- stat(path, st) || !S_ISREG(st->st_mode) || !build_within(spec->scope, path) ||- !realpath(path, out))- return errno ? -errno : -EINVAL;+ if (strsprintf(path, sizeof(path), "%s/%s", spec->scope, source) < 0)+ return -ENAMETOOLONG;+ if (stat(path, st))+ return -errno;+ if (!S_ISREG(st->st_mode) || !build_within(spec->scope, path))+ return -EINVAL;+ if (!realpath(path, out))+ return -errno;if (strlen(out) >= cap)return -ENAMETOOLONG;return 0;diff --git a/util/image_config.c b/util/image_config.cindex 8b6d3c31..da40f885 100644--- a/util/image_config.c+++ b/util/image_config.c@@ -102,8 +102,14 @@ static int validate_tree(int root_fd, dev_t device, __u32 depth,close(fd);return ret;}- errno = 0;- while ((entry = readdir(dir)) != NULL) {+ for (;;) {+ errno = 0;+ entry = readdir(dir);+ if (!entry) {+ if (errno)+ ret = -errno;+ break;+ }if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))continue;if (++*entries > 65536) {@@ -133,10 +139,7 @@ static int validate_tree(int root_fd, dev_t device, __u32 depth,if (ret)break;}- errno = 0;}- if (!entry && errno && !ret)- ret = -errno;closedir(dir);return ret;}diff --git a/util/image_keys.c b/util/image_keys.cindex 59b4da4e..f25c329c 100644--- a/util/image_keys.c+++ b/util/image_keys.c@@ -26,7 +26,7 @@ static int revocations(int root_fd, bool apply, bool boot){struct dirent *entry;struct stat root, st;- int keys_fd, dir_fd, ret = 0;+ int keys_fd, dir_fd, err, ret = 0;DIR *dir;if (fstat(root_fd, &root))@@ -37,9 +37,10 @@ static int revocations(int root_fd, bool apply, bool boot)return errno == ENOENT ? 0 : -errno;dir_fd = openat(keys_fd, "revoked",O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);+ err = dir_fd < 0 ? errno : 0;close(keys_fd);if (dir_fd < 0)- return errno == ENOENT ? 0 : -errno;+ return err == ENOENT ? 0 : -err;dir = fdopendir(dir_fd);if (!dir) {ret = -errno;diff --git a/util/image_runtime.c b/util/image_runtime.cindex b9866598..03fea00d 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -186,9 +186,15 @@ static int device_observed(dev_t device, const __u8 root[32], __u32 kind)block = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);if (block < 0)return -errno;- if (fstat(block, &st) || !S_ISBLK(st.st_mode) || st.st_rdev != device ||- ioctl(block, BLKGETDISKSEQ, &diskseq)) {- ret = errno ? -errno : -EPERM;+ if (fstat(block, &st))+ ret = -errno;+ else if (!S_ISBLK(st.st_mode) || st.st_rdev != device)+ ret = -EPERM;+ else if (ioctl(block, BLKGETDISKSEQ, &diskseq))+ ret = -errno;+ else+ ret = 0;+ if (ret) {close(block);return ret;}diff --git a/util/kmsg.c b/util/kmsg.cindex 81b5c3a2..887272ba 100644--- a/util/kmsg.c+++ b/util/kmsg.c@@ -147,8 +147,10 @@ int kmsg_tail(struct kmsg_record *out, size_t cap)*/if (clock_gettime(CLOCK_REALTIME, &rt_at_open) != 0 ||clock_gettime(CLOCK_MONOTONIC, &mt_at_open) != 0) {+ int err = errno;+close(fd);- return -errno;+ return -err;}for (;;) {diff --git a/util/upatch_resolve.c b/util/upatch_resolve.cindex f287a8a8..7fb71e96 100644--- a/util/upatch_resolve.c+++ b/util/upatch_resolve.c@@ -108,7 +108,7 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,ret = bpf_link_get_info_by_fd(link_fd, &linfo, &len);if (ret != 0 || linfo.prog_id == 0) {close(link_fd);- return ret ? -errno : -EINVAL;+ return ret ? ret : -EINVAL;}if (linfo.type == BPF_LINK_TYPE_UPROBE_MULTI) {struct bpf_link_info detail = {};@@ -131,7 +131,6 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,ret = bpf_link_get_info_by_fd(link_fd, &detail, &len);close(link_fd);if (ret != 0) {- ret = -errno;xfree(path, path_size);return ret;}@@ -162,7 +161,7 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,ret = bpf_prog_get_info_by_fd(prog_fd, &info, &len);close(prog_fd);if (ret != 0)- return -errno;+ return ret;if (vbtf && info.attach_btf_id &&info.attach_btf_id < btf__type_cnt(vbtf)) {