From f409aa5c6363144c9711226319614f3b248d9828 Mon Sep 17 00:00:00 2001 From: Julia Kartseva Date: Fri, 7 Jan 2022 15:02:57 -0800 Subject: [PATCH 1/3] bpf: check if lsm link ptr is libbpf error BPF_RAW_TRACEPOINT_OPEN is expected to work only on x86 and x86_64, since BPF trampoline is implemented only on these architectures. Attach probing by bpf_program__attach_lsm already happens in `bpf_lsm_supported`. The resulting pointer can store libbpf error and that is the case for unsupported architectures. Add libbpf error check to `bpf_lsm_supported` so execution does not reach the point where unit startup fails. --- src/core/bpf-lsm.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/bpf-lsm.c b/src/core/bpf-lsm.c index e0333963c5..4ca082a005 100644 --- a/src/core/bpf-lsm.c +++ b/src/core/bpf-lsm.c @@ -45,10 +45,11 @@ static bool bpf_can_link_lsm_program(struct bpf_program *prog) { assert(prog); link = sym_bpf_program__attach_lsm(prog); - if (!link) - return -ENOMEM; - return 1; + /* If bpf_program__attach_lsm fails the resulting value stores libbpf error code instead of memory + * pointer. That is the case when the helper is called on architectures where BPF trampoline (hence + * BPF_LSM_MAC attach type) is not supported. */ + return sym_libbpf_get_error(link) == 0; } static int prepare_restrict_fs_bpf(struct restrict_fs_bpf **ret_obj) { From d179b8d3683a38e1fa591b0079851ce2aff7d7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 9 Jan 2022 19:53:22 +0100 Subject: [PATCH 2/3] bpf: adjust comment about not calling sym_bpf_link__destroy --- src/shared/bpf-link.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/shared/bpf-link.c b/src/shared/bpf-link.c index 85eab09032..fea49b2ecb 100644 --- a/src/shared/bpf-link.c +++ b/src/shared/bpf-link.c @@ -32,12 +32,12 @@ int bpf_serialize_link(FILE *f, FDSet *fds, const char *key, struct bpf_link *li } struct bpf_link *bpf_link_free(struct bpf_link *link) { - - /* Avoid a useless dlopen() if link == NULL */ - if (!link) - return NULL; - - (void) sym_bpf_link__destroy(link); + /* If libbpf wasn't dlopen()ed, sym_bpf_link__destroy might be unresolved (NULL), so let's not try to + * call it if link is NULL. link might also be a non-null "error pointer", but such a value can only + * originate from a call to libbpf, but that means that libbpf is available, and we can let + * bpf_link__destroy() handle it. */ + if (link) + (void) sym_bpf_link__destroy(link); return NULL; } From ccfc534deed2f3873c967851497af10d8a1ee01c Mon Sep 17 00:00:00 2001 From: Julia Kartseva Date: Sun, 9 Jan 2022 21:35:35 -0800 Subject: [PATCH 3/3] bpf: fix bpf_can_link_lsm_program condition Since bpf_can_link_lsm_program return value is boolean, the expression `r < 0` is always false. --- src/core/bpf-lsm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/bpf-lsm.c b/src/core/bpf-lsm.c index 4ca082a005..83f3199349 100644 --- a/src/core/bpf-lsm.c +++ b/src/core/bpf-lsm.c @@ -167,9 +167,9 @@ int lsm_bpf_supported(void) { if (r < 0) return supported = 0; - r = bpf_can_link_lsm_program(obj->progs.restrict_filesystems); - if (r < 0) { - log_warning_errno(r, "Failed to link BPF program. Assuming BPF is not available: %m"); + if (!bpf_can_link_lsm_program(obj->progs.restrict_filesystems)) { + log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "Failed to link BPF program. Assuming BPF is not available"); return supported = 0; }