All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Martin KaFai Lau" <martin.lau@linux.dev>
Cc: bpf@vger.kernel.org
Subject: Re: [RFC PATCH bpf-next 08/12] bpf: Add a few bpf_cgroup_array_* helper functions
Date: Tue, 19 May 2026 22:45:21 +0000	[thread overview]
Message-ID: <20260519224522.6B0941F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260519215841.2984970-9-martin.lau@linux.dev>

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.

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);
>  	}
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260519215841.2984970-1-martin.lau@linux.dev?part=8

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

Thread overview: 25+ 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 [this message]
2026-05-19 22:50     ` Martin KaFai Lau
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-26 17:54       ` Amery Hung
2026-05-26 21:37         ` Martin KaFai Lau
2026-05-26 22:23           ` Amery Hung
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=20260519224522.6B0941F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --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 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.