BPF List
 help / color / mirror / Atom feed
From: Martin KaFai Lau <martin.lau@linux.dev>
To: sashiko-reviews@lists.linux.dev
Cc: bpf@vger.kernel.org, Yonghong Song <yonghong.song@linux.dev>
Subject: Re: [RFC PATCH bpf-next 08/12] bpf: Add a few bpf_cgroup_array_* helper functions
Date: Tue, 19 May 2026 15:50:30 -0700	[thread overview]
Message-ID: <b239e40b-8065-4ebd-a275-d4e4528e187b@linux.dev> (raw)
In-Reply-To: <20260519224522.6B0941F000E9@smtp.kernel.org>

On 5/19/26 3:45 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> 
> New issues:
> - [High] bpf_cgroup_array_copy_to_user() incorrectly returns -ENOSPC when the requested count is zero.
> 
> Pre-existing issues:
> - [Critical] purge_effective_progs() calculates linear indices that do not account for BPF_F_PREORDER physical reordering, leading to Use-After-Free.
> --
> 
> commit 23e774d89cf8101cef3b2e8789a63f7fb930be02
> Author: Martin KaFai Lau <martin.lau@kernel.org>
> 
> bpf: Add a few bpf_cgroup_array_* helper functions
> 
> This patch adds bpf_cgroup_array_*() helper functions to prepare for
> supporting struct_ops maps in the cgroup bpf array. These helpers currently
> skip the dummy_bpf_prog, which is the same as the existing behavior.
> The callers are changed to use these new helpers as a no-op change.
> 
>> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
>> index b5769f5401e64..a033aa479ab6e 100644
>> --- a/kernel/bpf/cgroup.c
>> +++ b/kernel/bpf/cgroup.c
> 
> [ ... ]
> 
>> +static int bpf_cgroup_array_copy_to_user(struct bpf_prog_array *array,
>> +					 __u32 __user *prog_ids, int cnt,
>> +					 enum cgroup_bpf_attach_type atype)
>> +{
>> +	struct bpf_prog_array_item *item;
>> +	int i = 0;
>> +	u32 id;
>> +
>> +	for (item = array->items; item->prog && i < cnt; item++) {
>> +		if (item->prog == bpf_cgroup_array_dummy(atype))
>> +			continue;
>> +		id = item->prog->aux->id;
>> +		if (copy_to_user(prog_ids + i, &id, sizeof(id)))
>> +			return -EFAULT;
>> +		i++;
>> +	}
>> +	return item->prog ? -ENOSPC : 0;
>> +}
> 
> [Severity: High]
> If cnt is 0 (which happens if the attach type only has dummy programs,
> causing bpf_cgroup_array_length() to return 0), the loop condition i < cnt
> will fail immediately. At this point, item->prog still points to a dummy
> program (which is not NULL).
> 
> Does this incorrectly return -ENOSPC instead of 0?
> 
> If this occurs during __cgroup_bpf_query(), it looks like the entire
> syscall could fail with -ENOSPC even when the user provided a large enough
> buffer.
> 
> [ ... ]
> 
>> @@ -1124,7 +1185,7 @@ static void purge_effective_progs(struct cgroup *cgrp, struct bpf_prog *prog,
>>   				lockdep_is_held(&cgroup_mutex));
>>   
>>   		/* Remove the program from the array */
>> -		WARN_ONCE(bpf_prog_array_delete_safe_at(progs, pos),
>> +		WARN_ONCE(bpf_cgroup_array_delete_safe_at(progs, pos, atype),
> 
> [Severity: Critical]
> This is a pre-existing issue, but does pos correctly reflect the program's
> physical index in the effective array?
> 
> When building the effective program array, compute_effective_progs() places
> programs with BPF_F_PREORDER at the beginning of the array. However,
> purge_effective_progs() calculates pos by sequentially iterating through
> the hierarchy's hlists, which does not account for this reordering.

It is a question I have also raised in the commit message of patch 9 also.
> 
> If update_effective_progs() fails (for example, due to OOM) during detach,
> purge_effective_progs() is called as a fallback.
> 
> With the incorrect pos, could bpf_cgroup_array_delete_safe_at() overwrite
> the wrong program with a dummy, leaving the intended program in the
> effective array?
> 
> Since __cgroup_bpf_detach() subsequently calls bpf_prog_put() on the
> detached program, the effective array would be left holding a dangling
> pointer to a freed bpf_prog, which could result in a use-after-free when
> the cgroup hook runs.
> 
>>   			  "Failed to purge a prog from array at index %d", pos);
>>   	}
>>   }
> 


  reply	other threads:[~2026-05-19 22:50 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-19 21:58 [RFC PATCH bpf-next 00/12] bpf: A common way to attach struct_ops to a cgroup Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 01/12] bpf: Remove __rcu tagging in st_link->map Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 02/12] bpf: Make struct_ops tasks_rcu grace period optional Martin KaFai Lau
2026-05-19 22:54   ` sashiko-bot
2026-05-20  0:25     ` Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 03/12] bpf: Add bpf_struct_ops accessor helpers Martin KaFai Lau
2026-05-19 22:25   ` sashiko-bot
2026-05-19 21:58 ` [RFC PATCH bpf-next 04/12] bpf: Remove unnecessary prog_list_prog() check Martin KaFai Lau
2026-05-19 22:49   ` sashiko-bot
2026-05-19 21:58 ` [RFC PATCH bpf-next 05/12] bpf: Replace prog_list_prog() check with direct pl->prog and pl->link check Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 06/12] bpf: Add prog_list_init_item(), prog_list_replace_item(), and prog_list_id() Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 07/12] bpf: Move LSM trampoline unlink into bpf_cgroup_link_auto_detach() Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 08/12] bpf: Add a few bpf_cgroup_array_* helper functions Martin KaFai Lau
2026-05-19 22:45   ` sashiko-bot
2026-05-19 22:50     ` Martin KaFai Lau [this message]
2026-05-19 21:58 ` [RFC PATCH bpf-next 09/12] bpf: Add infrastructure to support attaching struct_ops to cgroups Martin KaFai Lau
2026-05-19 22:50   ` sashiko-bot
2026-05-19 23:56     ` Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 10/12] bpf: tcp: Support selected sock_ops callbacks as struct_ops Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 11/12] libbpf: Support attaching struct_ops to a cgroup Martin KaFai Lau
2026-05-19 21:58 ` [RFC PATCH bpf-next 12/12] selftests/bpf: Test " Martin KaFai Lau
2026-05-19 23:03   ` 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=b239e40b-8065-4ebd-a275-d4e4528e187b@linux.dev \
    --to=martin.lau@linux.dev \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yonghong.song@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