BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Anastasios Papagiannis" <tasos.papagiannnis@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v5 2/7] exec: Clear bprm->mm before dropping its reference
Date: Mon, 07 Sep 2026 17:07:25 +0000	[thread overview]
Message-ID: <20260907170725.9EC261F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260907165220.52431-3-tasos.papagiannnis@gmail.com>

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

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch addresses this exact class of BPF Use-After-Free (UAF) vulnerability for `bprm->mm` while missing that `bprm->file` suffers from the same flaw. The BPF verifier explicitly marks `bprm->file` as a fully trusted pointer via `BTF_TYPE_SAFE_TRUSTED(struct linux_binprm) { struct file *file; };`. In `free_bprm()`, the file's reference is dropped via `do_close_execat(bprm->file)`, but the pointer is not cleared. Immediately afterward, `bprm_drop_loader(bprm)` is called, which serves as a BPF fentry tracepoint. A BPF program attaching to this hook will receive a dangling `bprm->file` that the verifier incorrectly considers `PTR_TRUSTED` and completely safe to pass to kfuncs, bypassing BPF safety guarantees.
--

commit a611d673e7752074063d4d9476d61d335949964b
Author: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>

exec: Clear bprm->mm before dropping its reference

The patch clears bprm->mm to NULL before dropping its reference via mmput()
to ensure safe access from BPF kfuncs.

> diff --git a/fs/exec.c b/fs/exec.c
> index 745f6eb5279e6..4ddd403fd91c7 100644
> --- a/fs/exec.c
> +++ b/fs/exec.c
> @@ -1456,9 +1456,12 @@ void bprm_drop_loader(struct linux_binprm *bprm)
>  
>  static void free_bprm(struct linux_binprm *bprm)
>  {
> -	if (bprm->mm) {
> +	struct mm_struct *mm = bprm->mm;
> +
> +	if (mm) {
>  		acct_arg_size(bprm, 0);
> -		mmput(bprm->mm);
> +		bprm->mm = NULL;
> +		mmput(mm);
>  	}
>  	if (bprm->user_ns)
>  		put_user_ns(bprm->user_ns);

[Severity: High]
This isn't a bug introduced by this patch, but while it addresses a BPF
Use-After-Free vulnerability for bprm->mm, it appears bprm->file in the same
function suffers from the same flaw.

Further down in free_bprm(), the reference to bprm->file is dropped via
do_close_execat(), but the pointer itself is not cleared:

    do_close_execat(bprm->file);
    bprm_drop_loader(bprm);

Immediately afterward, bprm_drop_loader() is called. This acts as a BPF
fentry attach point where a BPF program can access the dangling bprm->file.

The BPF verifier explicitly marks bprm->file as a fully trusted pointer via
BTF_TYPE_SAFE_TRUSTED(struct linux_binprm) { struct file *file; };.

Because bprm->file is left dangling, a BPF fentry program attached to
bprm_drop_loader() will receive a pointer to a released file that the verifier
incorrectly considers PTR_TRUSTED and safe to pass to kfuncs, bypassing BPF
safety guarantees.

Should we apply the same pattern here, storing bprm->file in a local variable,
setting bprm->file to NULL, and then calling do_close_execat() on the local
variable to prevent BPF programs from accessing a zero-refcount object?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260907165220.52431-1-tasos.papagiannnis@gmail.com?part=2

  reply	other threads:[~2026-09-07 17:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-07 16:52 [PATCH bpf-next v5 0/7] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
2026-09-07 16:52 ` [PATCH bpf-next v5 1/7] mm: Add copy_remote_mm_str() Anastasios Papagiannis
2026-09-07 20:06   ` David Hildenbrand (Arm)
2026-09-08 13:16   ` Lorenzo Stoakes (ARM)
2026-09-07 16:52 ` [PATCH bpf-next v5 2/7] exec: Clear bprm->mm before dropping its reference Anastasios Papagiannis
2026-09-07 17:07   ` sashiko-bot [this message]
2026-09-07 16:52 ` [PATCH bpf-next v5 3/7] bpf: Add user memory access kfuncs for mm_struct Anastasios Papagiannis
2026-09-08 11:56   ` Matt Bobrowski
2026-09-07 16:52 ` [PATCH bpf-next v5 4/7] bpf: Allow reads through trusted-or-null BTF pointers Anastasios Papagiannis
2026-09-08 10:24   ` Kumar Kartikeya Dwivedi
2026-09-08 11:47     ` Anastasios Papagiannis
2026-09-08 13:19       ` Anastasios Papagiannis
2026-09-07 16:52 ` [PATCH bpf-next v5 5/7] selftests/bpf: Cover trusted-or-null BTF pointer reads Anastasios Papagiannis
2026-09-07 16:52 ` [PATCH bpf-next v5 6/7] bpf: Mark linux_binprm->mm as trusted-or-null Anastasios Papagiannis
2026-09-08 10:12   ` Matt Bobrowski
2026-09-07 16:52 ` [PATCH bpf-next v5 7/7] selftests/bpf: Test mm_struct user memory kfuncs with linux_binprm Anastasios Papagiannis

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=20260907170725.9EC261F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tasos.papagiannnis@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox