From: sashiko-bot@kernel.org
To: "Avinash Pal" <avinashpal441@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH] bpf: verifier: fix NULL deref in map_kptr_match_type() for scalar regs
Date: Mon, 20 Apr 2026 04:05:54 +0000 [thread overview]
Message-ID: <20260420040554.9BA41C19425@smtp.kernel.org> (raw)
In-Reply-To: <20260419040426.9672-1-avinashpal441@gmail.com>
Sashiko AI review found 2 potential issue(s):
- [High] Type confusion and non-deterministic behavior by accessing union member `reg->btf` before validating register type.
- [Low] Compiler warning due to mixed declarations and executable code.
--
commit e1d023d433c4be96ed8eba9f6b8df2ec2929d841
Author: Avinash Pal <avinashpal441@gmail.com>
bpf: verifier: fix NULL deref in map_kptr_match_type() for scalar regs
This commit attempts to fix a NULL pointer dereference in
map_kptr_match_type() by adding an early return if reg->btf is NULL.
It targets a crash that occurs when storing a scalar value into a kptr slot.
Link: https://bugzilla.kernel.org/show_bug.cgi?id=221372
> For scalar registers reg->btf is NULL. map_kptr_match_type() passes
> this directly to btf_is_kernel(), which performs a container_of()-based
> offset access and triggers a general protection fault:
Since base_type(reg->type) != PTR_TO_BTF_ID evaluates to true for scalars,
wouldn't the code jump to bad_type before ever reaching btf_is_kernel()?
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 69d75515ed3f7..15b8f71074f93 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -4545,6 +4545,18 @@ static int map_kptr_match_type(struct bpf_verifier_env *env,
> struct btf_field *kptr_field,
> struct bpf_reg_state *reg, u32 regno)
> {
> + /*
> + * If the source register has no BTF type (e.g. it is a scalar),
> + * it cannot possibly match a kptr slot. Reject early to avoid
> + * passing a NULL reg->btf to btf_is_kernel(), which would cause
> + * a NULL pointer dereference inside that function.
> + */
> + if (!reg->btf) {
> + verbose(env, "R%d is not a pointer, cannot store into kptr slot\n",
> + regno);
> + return -EACCES;
> + }
> +
Does placing this executable code before the variable declarations trigger a
-Wdeclaration-after-statement compiler warning?
> const char *targ_name = btf_type_name(kptr_field->kptr.btf, kptr_field->kptr.btf_id);
> int perm_flags;
> const char *reg_name = "";
In struct bpf_reg_state, btf is part of a union:
struct bpf_reg_state {
...
union {
int range;
...
struct {
struct btf *btf;
u32 btf_id;
};
...
};
...
};
By checking !reg->btf at the top of the function, does this read an inactive
union member before validating the register type?
For instance, if the register is a PTR_TO_PACKET with range == 0, could
reg->btf incorrectly evaluate to NULL here and return -EACCES instead of safely
falling through to the bad_type check below?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260419040426.9672-1-avinashpal441@gmail.com?part=1
next prev parent reply other threads:[~2026-04-20 4:05 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-19 4:04 [PATCH] bpf: verifier: fix NULL deref in map_kptr_match_type() for scalar regs Avinash Pal
2026-04-19 4:32 ` bot+bpf-ci
2026-04-19 5:01 ` sun jian
2026-04-20 4:05 ` sashiko-bot [this message]
-- strict thread matches above, loose matches on Subject: below --
2026-04-19 3:59 Avinash Pal
2026-04-20 4:00 ` sashiko-bot
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=20260420040554.9BA41C19425@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=avinashpal441@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=sashiko@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.