All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sun Jian" <sun.jian.kdev@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf 1/2] bpf: Avoid eliding lookup NULLness for inner map templates
Date: Wed, 24 Jun 2026 07:37:38 +0000	[thread overview]
Message-ID: <20260624073738.86F361F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260624072031.735846-1-sun.jian.kdev@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The patch relies on the `BPF_F_INNER_MAP` flag to prevent unsafe NULL-check elision for inner map templates. However, this fix is fundamentally incomplete because `BPF_MAP_TYPE_PERCPU_ARRAY` explicitly rejects the `BPF_F_INNER_MAP` flag during creation. This leaves per-CPU arrays vulnerable to out-of-bounds memory accesses and NULL pointer dereferences.
--

commit 3ab30507af620d9da556d3b2d80a1f172474785b
Author: Sun Jian <sun.jian.kdev@gmail.com>

bpf: Avoid eliding lookup NULLness for inner map templates

This commit prevents the verifier from eliding NULLness checks for array maps
used as inner map templates. It achieves this by requiring that the
BPF_F_INNER_MAP flag is not set on the map before clearing the PTR_MAYBE_NULL
flag from the register.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 69d75515ed3f7..a32704bb5e058 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -10577,6 +10577,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
>  
>  		if (func_id == BPF_FUNC_map_lookup_elem &&
>  		    can_elide_value_nullness(meta.map.ptr->map_type) &&
> +		    !(meta.map.ptr->map_flags & BPF_F_INNER_MAP) &&

[Severity: Critical]
This isn't a bug introduced by this patch, but does this check completely
prevent NULL-check elision for all inner map templates?

Looking at can_elide_value_nullness(), it allows both BPF_MAP_TYPE_ARRAY
and BPF_MAP_TYPE_PERCPU_ARRAY.

However, in array_map_alloc_check(), the BPF_F_INNER_MAP flag is
explicitly rejected for per-CPU arrays:

kernel/bpf/arraymap.c:array_map_alloc_check() {
    if (attr->map_type != BPF_MAP_TYPE_ARRAY &&
        attr->map_flags & (BPF_F_MMAPABLE | BPF_F_INNER_MAP))
        return -EINVAL;
    ...
}

Since BPF_MAP_TYPE_PERCPU_ARRAY can never have the BPF_F_INNER_MAP flag
set, won't this allow the verifier to still incorrectly elide NULLness
checks for per-CPU arrays used as inner map templates?

If a per-CPU array with a large max_entries is used as an inner map
template, and a concrete map with a smaller max_entries is inserted at
runtime (which is possible because percpu_array_map_ops.map_meta_equal uses
bpf_map_meta_equal() which ignores max_entries checks), couldn't this
lead to an out-of-bounds access or NULL pointer dereference?

Additionally, since percpu_array_map_gen_lookup() also relies on
BPF_F_INNER_MAP to skip JIT inlining, will it inline the lookup with the
template's excessively large bounds hardcoded, resulting in out-of-bounds
heap reads on the concrete inner map?

>  		    meta.const_map_key >= 0 &&
>  		    meta.const_map_key < meta.map.ptr->max_entries)
>  			ret_flag &= ~PTR_MAYBE_NULL;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260624072031.735846-1-sun.jian.kdev@gmail.com?part=1

  parent reply	other threads:[~2026-06-24  7:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-24  7:20 [PATCH bpf 1/2] bpf: Avoid eliding lookup NULLness for inner map templates Sun Jian
2026-06-24  7:20 ` [PATCH bpf 2/2] selftests/bpf: Add inner map template lookup NULLness test Sun Jian
2026-06-24  7:37 ` sashiko-bot [this message]
2026-06-24  7:41   ` [PATCH bpf 1/2] bpf: Avoid eliding lookup NULLness for inner map templates sun jian

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=20260624073738.86F361F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sun.jian.kdev@gmail.com \
    /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.