All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrey Grodzovsky" <andrey.grodzovsky@crowdstrike.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 2/5] libbpf: BPF programs dynamic loading and attaching
Date: Wed, 26 Aug 2026 18:33:05 +0000	[thread overview]
Message-ID: <20260826183305.EBADA1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260826181825.1826541-3-andrey.grodzovsky@crowdstrike.com>

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] `bpf_program__load_dynamically` overwrites `prog->fd` without closing the old one, leading to a file descriptor leak if the program is already loaded.
- [Medium] Public API `bpf_program__load_dynamically` returns a negative error code directly on failure instead of wrapping it in `libbpf_err()`, violating LIBBPF-001.
- [Medium] `bpf_program__load_dynamically` permanently corrupts the user-configured `log_level` state.
- [Medium] The internal static function `bpf_program__set_dynamicload` incorrectly uses the `libbpf_err()` wrapper.
- [Medium] The public API `bpf_program__load_dynamically` dereferences `prog` before verifying it is not NULL.
--

commit d4848d3e91d6da741dd4f244c1469a745b1d0a7e
Author: Slava Imameev <slava.imameev@crowdstrike.com>

libbpf: BPF programs dynamic loading and attaching

BPF programs designated as dynamically loaded can be loaded and
attached independently after the initial bpf_object loading and
attaching. This patch enables these programs to be reloaded and
reattached multiple times, reducing load times for utilities with
many conditionally needed programs.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 821d854d43a9f..c3fe1de432108 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
[ ... ]
> @@ -15220,12 +15239,73 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
>  	free(s);
>  }
>  
> +static int bpf_program__set_dynamicload(struct bpf_program *prog)
> +{
> +	struct bpf_object *obj;
> +	const char *attach_name;
> +
> +	obj = prog->obj;
> +	if (!obj)
> +		return libbpf_err(-EINVAL);

[Severity: Medium]
Is this internal static function improperly invoking the public API error
wrapper libbpf_err() instead of returning a negative error code? Because the
internal function wraps the error, errno is set prematurely, and the public
API returns an unwrapped error result.

[ ... ]
> +/*
> + * This function must be called after bpf_object__load_progs.
> + * Dynamically-loaded program data is initialized on object load.
> + * Post-load initialization is not supported.
> + */
> +int
> +bpf_program__load_dynamically(struct bpf_program *prog, int extra_log_level)
> +{
> +	int err;
> +	struct bpf_object *obj;
> +
> +	obj = prog->obj;

[Severity: Medium]
Can this cause a segmentation fault by dereferencing prog before verifying it
is not NULL? Expected behavior for libbpf public APIs is to handle NULL
arguments gracefully (as done in bpf_program__unload_dynamically).

> +	if (!obj || obj->state < OBJ_LOADED)
> +		return libbpf_err(-EINVAL);
> +
> +	if (prog_is_subprog(obj, prog) || prog->load_type != BPF_PROG_LOAD_TYPE_DYNAMIC)
> +		return libbpf_err(-EINVAL);
> +
> +	prog->log_level |= extra_log_level;
> +
> +	err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt,
> +				   obj->license, obj->kern_version, &prog->fd);

[Severity: High]
Could this lead to a file descriptor leak if the program is already loaded?
The call appears to unconditionally overwrite the prog->fd memory location
without closing the previous file descriptor.

> +	if (err) {
> +		pr_warn("prog '%s': failed to dynamically load: %d\n", prog->name, err);
> +		prog->log_level &= ~extra_log_level;
> +		return err;

[Severity: Medium]
Are we missing a libbpf_err() wrapper here? This return path directly exposes
a raw negative error to the caller, which may leave errno improperly set.

> +	}
> +
> +	prog->log_level &= ~extra_log_level;

[Severity: Medium]
Will this bitwise operation permanently corrupt the user-configured log
level? If the user previously set bits in prog->log_level that overlap with
extra_log_level, those original bits would be incorrectly cleared here
instead of being restored to their previous state.

> +	return 0;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260826181825.1826541-1-andrey.grodzovsky@crowdstrike.com?part=2

  reply	other threads:[~2026-08-26 18:33 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-26 18:18 [PATCH bpf-next v2 0/5] libbpf: BPF program dynamic loading Andrey Grodzovsky
2026-08-26 18:18 ` [PATCH bpf-next v2 1/5] libbpf: BPF program load type enum Andrey Grodzovsky
2026-08-26 19:25   ` bot+bpf-ci
2026-09-11 23:43   ` Andrii Nakryiko
2026-08-26 18:18 ` [PATCH bpf-next v2 2/5] libbpf: BPF programs dynamic loading and attaching Andrey Grodzovsky
2026-08-26 18:33   ` sashiko-bot [this message]
2026-08-26 19:38   ` bot+bpf-ci
2026-09-11 23:44   ` Andrii Nakryiko
2026-09-17  0:39     ` Andrey Grodzovsky
2026-09-17 21:51       ` Andrii Nakryiko
2026-09-18 18:29         ` Andrey Grodzovsky
2026-08-26 18:18 ` [PATCH bpf-next v2 3/5] libbpf: Convert bpf_program__set_autoload() to load-type enum Andrey Grodzovsky
2026-08-26 18:34   ` sashiko-bot
2026-08-26 19:25   ` bot+bpf-ci
2026-09-11 23:44   ` Andrii Nakryiko
2026-09-17  0:03     ` Andrey Grodzovsky
2026-08-26 18:18 ` [PATCH bpf-next v2 4/5] libbpf: Support declarative dynamic load via BTF decl_tag Andrey Grodzovsky
2026-08-26 18:33   ` sashiko-bot
2026-08-26 19:25   ` bot+bpf-ci
2026-08-26 18:18 ` [PATCH bpf-next v2 5/5] selftests/bpf: Cover BPF program dynamic loading Andrey Grodzovsky
2026-08-26 18:33   ` sashiko-bot
2026-08-26 19:25   ` bot+bpf-ci
2026-09-11 23:44   ` Andrii Nakryiko
2026-09-17  0:04     ` Andrey Grodzovsky
2026-09-11 23:43 ` [PATCH bpf-next v2 0/5] libbpf: " Andrii Nakryiko
2026-09-17  0:45   ` Andrey Grodzovsky
2026-09-18  0:49     ` Andrey Grodzovsky
2026-09-22 16:36       ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260826183305.EBADA1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=andrey.grodzovsky@crowdstrike.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.