BPF List
 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 v4 2/7] libbpf: BPF programs manual loading and attaching
Date: Mon, 21 Sep 2026 23:03:33 +0000	[thread overview]
Message-ID: <20260921230334.13F781F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260921223937.3203093-3-andrey.grodzovsky@crowdstrike.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: bpf_program__load() allows loading from failed object, causing Use-After-Close
--

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

libbpf: BPF programs manual loading and attaching

BPF programs designated as manually loaded can be loaded and attached
independently after the initial bpf_object loading and attaching. This
patch adds bpf_program__load() to support loading these manual-strategy
programs independently.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a3085847cb97f..939f0d6378e38 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c

[ ... ]

> @@ -9233,7 +9286,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
>  			err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
>  	}
>  
> -	bpf_object_post_load_cleanup(obj);
> +	bpf_object_post_load_cleanup(obj, false);
>  	obj->state = OBJ_LOADED; /* doesn't matter if successfully or not */
>  
>  	if (err) {

[Severity: High]
Could this state assignment allow dangerous program loads if
bpf_object_load() fails?

When bpf_object_load() fails after bpf_object_prepare(), it falls through to
error handling which calls bpf_object_unload() and closes the map FDs.
However, the state is still unconditionally set to OBJ_LOADED.

[ ... ]

> @@ -15386,3 +15501,34 @@ enum bpf_prog_load_strategy bpf_program__load_strategy(const struct bpf_program
>  {
>  	return prog->load_strategy;
>  }
> +
> +/*
> + * This function must be called after bpf_object__prepare (or
> + * bpf_object__load, which calls bpf_object__prepare internally).
> + * Manually-loaded program data is initialized on object prepare.
> + * Post-prepare initialization is not supported.
> + */
> +int
> +bpf_program__load(struct bpf_program *prog)
> +{
> +	int err;
> +	struct bpf_object *obj = prog->obj;
> +
> +	if (obj->state < OBJ_PREPARED)
> +		return libbpf_err(-EINVAL);

[Severity: High]
Is this state check sufficient to prevent a use-after-close if the underlying
object failed to load?

If an application ignores a prior bpf_object_load() error and calls
bpf_program__load(), this check will pass because the failed object is now in
the OBJ_LOADED state (which is >= OBJ_PREPARED).

Because the failure triggered bpf_object_unload(), the map FDs were closed,
allowing bpf_program__load() to proceed with stale map FDs.

If a map FD is reused, the kernel might attach the BPF program to an
unrelated map, causing data corruption or security boundary violations.
For programs with BTF, the kernel rejects the load with -EFAULT because
func_info is freed but func_info_cnt remains > 0. For programs without BTF,
the kernel proceeds using the stale FDs.

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

  reply	other threads:[~2026-09-21 23:03 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21 22:39 [PATCH bpf-next v4 0/7] libbpf: BPF program manual loading Andrey Grodzovsky
2026-09-21 22:39 ` [PATCH bpf-next v4 1/7] libbpf: BPF program load strategy enum Andrey Grodzovsky
2026-09-21 23:21   ` bot+bpf-ci
2026-09-21 22:39 ` [PATCH bpf-next v4 2/7] libbpf: BPF programs manual loading and attaching Andrey Grodzovsky
2026-09-21 23:03   ` sashiko-bot [this message]
2026-09-22 23:51     ` Andrey Grodzovsky
2026-09-21 22:39 ` [PATCH bpf-next v4 3/7] libbpf: Support declarative manual load via SEC("!...") prefix Andrey Grodzovsky
2026-09-21 23:12   ` sashiko-bot
2026-09-21 23:21   ` bot+bpf-ci
2026-09-21 22:39 ` [PATCH bpf-next v4 4/7] libbpf: Reject gen_loader for objects with already-manual programs Andrey Grodzovsky
2026-09-21 22:39 ` [PATCH bpf-next v4 5/7] libbpf: Version bpf_program__set_autoattach() ABI change Andrey Grodzovsky
2026-09-21 23:31   ` sashiko-bot
2026-09-21 22:39 ` [PATCH bpf-next v4 6/7] selftests/bpf: Cover BPF program load strategy transitions Andrey Grodzovsky
2026-09-21 23:37   ` sashiko-bot
2026-09-21 22:39 ` [PATCH bpf-next v4 7/7] selftests/bpf: Cover BPF program manual loading Andrey Grodzovsky
2026-09-22  1:37 ` [PATCH bpf-next v4 0/7] libbpf: " Alexei Starovoitov
2026-09-22 14:34   ` Andrey Grodzovsky

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=20260921230334.13F781F000FF@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox