Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 2/4] perf header: Use write lock when translating BPF prog info pointers
Date: Sun, 02 Aug 2026 14:59:55 +0000	[thread overview]
Message-ID: <20260802145956.481701F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260802142712.154726-3-acme@kernel.org>

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

Pre-existing issues:
- [High] The patch changes `down_read` to `down_write` to protect in-place pointer mutations, but the fix is ineffective because primary readers access the data locklessly.
- [Medium] `bpil_offs_to_addr()` permanently corrupts the in-memory `info_linear->arrays` mask when restoring a valid object containing empty trailing arrays.
--

--- Patch [2]: [PATCH 2/4] perf header: Use write lock when translating BPF prog info pointers ---
commit b51042073da80b7c616b5e20db21243852bbccec
Author: Arnaldo Carvalho de Melo <acme@redhat.com>

perf header: Use write lock when translating BPF prog info pointers

write_bpf_prog_info() temporarily mutates pointers into file offsets,
causing concurrent readers to dereference invalid addresses. This patch
upgrades the synchronization from a read lock to a write lock during the
translation window to exclude concurrent readers.

> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index 091d8f7f6bd2c..70ba9570e3179 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -1120,7 +1120,8 @@ static int write_bpf_prog_info(struct feat_fd *ff  __maybe_unused,
>  	struct rb_node *next;
>  	int ret = 0;
>  
> -	down_read(&env->bpf_progs.lock);
> +	/* write lock: bpil_addr_to_offs() temporarily mutates info_linear */
> +	down_write(&env->bpf_progs.lock);

[Severity: High]
This is a pre-existing issue, but does upgrading to a write lock actually
prevent concurrent accesses from seeing the mutated pointers?

Looking at perf_env__find_bpf_prog_info(), it acquires the read lock but
drops it before returning the node pointer:

tools/perf/util/env.c:perf_env__find_bpf_prog_info() {
    ...
    up_read(&env->bpf_progs.lock);
    return node;
}

This means callers like machine__process_bpf_event_load() will dereference
the pointer locklessly:

tools/perf/util/bpf-event.c:machine__process_bpf_event_load() {
    ...
    u64 *addrs = (u64 *)(uintptr_t)(info_linear->info.jited_ksyms);
    ...
}

Because the primary readers are accessing the data outside the critical
section, could they still observe a file offset instead of a valid pointer
even with down_write() here?

>  
>  	ret = do_write(ff, &env->bpf_progs.infos_cnt,
>  		       sizeof(env->bpf_progs.infos_cnt));
> @@ -1150,7 +1151,7 @@ static int write_bpf_prog_info(struct feat_fd *ff  __maybe_unused,
>  			goto out;

[Severity: Medium]
This isn't a bug introduced by this patch, but could calling
bpil_offs_to_addr() just before this return permanently corrupt the state
for empty trailing arrays?

If a BPF program contains an empty array (count = 0) at the end of the
serialized struct, its offset is exactly data_len. When this function calls
bpil_offs_to_addr(node->info_linear) to reverse the offsets, it hits this
bounds check:

tools/perf/util/bpf-utils.c:bpil_offs_to_addr() {
    ...
    if (offs >= info_linear->data_len || ...)
        ...
        info_linear->arrays &= ~(1UL << i);
    ...
}

Since offs >= data_len evaluates to true, will this falsely mark an empty
array as absent in the arrays bitmask, corrupting the live in-memory object?

>  	}
>  out:
> -	up_read(&env->bpf_progs.lock);
> +	up_write(&env->bpf_progs.lock);
>  	return ret;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260802142712.154726-1-acme@kernel.org?part=2

  reply	other threads:[~2026-08-02 14:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-02 14:27 [PATCHES 0/4] perf bpf hardening Arnaldo Carvalho de Melo
2026-08-02 14:27 ` [PATCH 1/4] perf libbfd: Validate BPF prog info arrays before pointer cast Arnaldo Carvalho de Melo
2026-08-02 14:54   ` sashiko-bot
2026-08-02 14:27 ` [PATCH 2/4] perf header: Use write lock when translating BPF prog info pointers Arnaldo Carvalho de Melo
2026-08-02 14:59   ` sashiko-bot [this message]
2026-08-02 14:27 ` [PATCH 3/4] perf bpf: Add PROG_TAGS to required arrays in __bpf_event__print_bpf_prog_info() Arnaldo Carvalho de Melo
2026-08-02 14:56   ` sashiko-bot
2026-08-02 14:27 ` [PATCH 4/4] perf libbfd: Fix memory leaks and NULL fclose in BPF disassembly Arnaldo Carvalho de Melo

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=20260802145956.481701F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acme@kernel.org \
    --cc=linux-perf-users@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