Re: [PATCH bpf-next v1 11/13] tools/libbpf: Add bpf_program__attach_lsm

From: Andrii Nakryiko
Date: Tue Dec 24 2019 - 01:45:23 EST


On Fri, Dec 20, 2019 at 7:42 AM KP Singh <kpsingh@xxxxxxxxxxxx> wrote:
>
> From: KP Singh <kpsingh@xxxxxxxxxx>
>
> Add functionality in libbpf to attach eBPF program to LSM hooks.
>
> Signed-off-by: KP Singh <kpsingh@xxxxxxxxxx>
> ---
> tools/lib/bpf/libbpf.c | 127 +++++++++++++++++++++++++++++++++++++--
> tools/lib/bpf/libbpf.h | 2 +
> tools/lib/bpf/libbpf.map | 1 +
> 3 files changed, 126 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index b0b27d8e5a37..ab2b23b4f21f 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -5122,8 +5122,8 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
> return -ESRCH;
> }
>
> -static inline int __btf__typdef_with_prefix(struct btf *btf, const char *name,
> - const char *prefix)
> +static inline int __btf__type_with_prefix(struct btf *btf, const char *name,
> + const char *prefix)

Please do this rename in a patch that introduced this function, there
is no need to split such changes between two patches. See also my
request to rename and generalize it a bit.

> {
>
> size_t prefix_len = strlen(prefix);
> @@ -5149,9 +5149,9 @@ int libbpf_find_vmlinux_btf_id(const char *name,
> }
>

[...]

>
> +
> +static int bpf_link__destroy_lsm(struct bpf_link *link)
> +{
> + struct bpf_link_lsm *ll = container_of(link, struct bpf_link_lsm, link);

struct bpf_link link being a first field is a requirement for
bpf_link, so you don't need container_of, just cast link to your type.

> + char errmsg[STRERR_BUFSIZE];
> + int ret;
> +
> + ret = bpf_prog_detach2(ll->prog_fd, ll->hook_fd, BPF_LSM_MAC);
> + if (ret < 0) {
> + ret = -errno;
> + pr_warn("failed to detach from hook: %s\n",
> + libbpf_strerror_r(ret, errmsg, sizeof(errmsg)));
> + return ret;
> + }
> + close(ll->hook_fd);
> + return 0;
> +}
> +
> +static const char *__lsm_hook_name(const char *title)
> +{
> +
> + int i;
> +
> + if (!title)
> + return ERR_PTR(-EINVAL);
> +
> + for (i = 0; i < ARRAY_SIZE(section_names); i++) {

section_names have been renamed to section_defs a while ago, please rebase

> + if (section_names[i].prog_type != BPF_PROG_TYPE_LSM)
> + continue;
> +

[...]