libbpf/backport-libbpf-Fix-the-case-of-running-as-non-root-with-capa.patch
JofDiamonds c614223903 backport patches from upstream
(cherry picked from commit 972dd0611492ea98918768c82579eea0b65ec697)
2023-03-06 19:09:16 +08:00

88 lines
3.0 KiB
Diff

From 9da0dcb62149ab0a6c5711813d77a844ec6f393b Mon Sep 17 00:00:00 2001
From: Jon Doron <jond@wiz.io>
Date: Sun, 25 Sep 2022 10:04:31 +0300
Subject: [PATCH] libbpf: Fix the case of running as non-root with capabilities
When running rootless with special capabilities like:
FOWNER / DAC_OVERRIDE / DAC_READ_SEARCH
The "access" API will not make the proper check if there is really
access to a file or not.
>From the access man page:
"
The check is done using the calling process's real UID and GID, rather
than the effective IDs as is done when actually attempting an operation
(e.g., open(2)) on the file. Similarly, for the root user, the check
uses the set of permitted capabilities rather than the set of effective
capabilities; ***and for non-root users, the check uses an empty set of
capabilities.***
"
What that means is that for non-root user the access API will not do the
proper validation if the process really has permission to a file or not.
To resolve this this patch replaces all the access API calls with
faccessat with AT_EACCESS flag.
Signed-off-by: Jon Doron <jond@wiz.io>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220925070431.1313680-1-arilou@gmail.com
---
src/btf.c | 2 +-
src/libbpf.c | 4 ++--
src/usdt.c | 2 +-
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/btf.c b/src/btf.c
index 3d6c30d..a542787 100644
--- a/src/btf.c
+++ b/src/btf.c
@@ -4694,7 +4694,7 @@ struct btf *btf__load_vmlinux_btf(void)
for (i = 0; i < ARRAY_SIZE(locations); i++) {
snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
- if (access(path, R_OK))
+ if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS))
continue;
if (locations[i].raw_btf)
diff --git a/src/libbpf.c b/src/libbpf.c
index 632c92d..fcaad31 100644
--- a/src/libbpf.c
+++ b/src/libbpf.c
@@ -823,7 +823,7 @@ __u32 get_kernel_version(void)
__u32 major, minor, patch;
struct utsname info;
- if (access(ubuntu_kver_file, R_OK) == 0) {
+ if (faccessat(AT_FDCWD, ubuntu_kver_file, R_OK, AT_EACCESS) == 0) {
FILE *f;
f = fopen(ubuntu_kver_file, "r");
@@ -11261,7 +11261,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
continue;
snprintf(result, result_sz, "%.*s/%s", seg_len, s, file);
/* ensure it is an executable file/link */
- if (access(result, R_OK | X_OK) < 0)
+ if (faccessat(AT_FDCWD, result, R_OK | X_OK, AT_EACCESS) < 0)
continue;
pr_debug("resolved '%s' to '%s'\n", file, result);
return 0;
diff --git a/src/usdt.c b/src/usdt.c
index f1c9339..058b91a 100644
--- a/src/usdt.c
+++ b/src/usdt.c
@@ -282,7 +282,7 @@ struct usdt_manager *usdt_manager_new(struct bpf_object *obj)
* If this is not supported, USDTs with semaphores will not be supported.
* Added in: a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
*/
- man->has_sema_refcnt = access(ref_ctr_sysfs_path, F_OK) == 0;
+ man->has_sema_refcnt = faccessat(AT_FDCWD, ref_ctr_sysfs_path, F_OK, AT_EACCESS) == 0;
return man;
}
--
2.33.0