All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeel.butt@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Sashiko <sashiko-bot@kernel.org>,
	Meta kernel team <kernel-team@meta.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH bpf-next] bpf, cgroup: Don't lose errors in effective BPF_LSM_CGROUP queries
Date: Thu, 12 Nov 2026 10:38:03 -0800	[thread overview]
Message-ID: <20261112183804.1171535-1-shakeel.butt@linux.dev> (raw)

An effective BPF_LSM_CGROUP query walks one attach type per LSM hook.
__cgroup_bpf_query() sets ret to -ENOSPC before the loop when prog_cnt
is too small, but the effective branch then overwrites ret with each
type's copy result:

- If the buffer runs out exactly at the end of a type, the last copy
  returns 0 and the query returns 0 instead of -ENOSPC.
- If one type's copy faults and a later one succeeds, the query returns
  0 instead of -EFAULT.

Return -EFAULT right away when a copy faults, as the non-effective
branch does, and leave ret alone otherwise.  The copy helper then no
longer needs to return -ENOSPC.  That also removes a false -ENOSPC it
gave when a dummy entry followed the last copied program.

Reproduced in a VM with two BPF_LSM_CGROUP programs on different hooks.
An effective query with prog_cnt=1 returned 0, and so did one whose
first id slot was not writable.  With this patch they fail with -ENOSPC
and -EFAULT.  Queries of other attach types already did.

Fixes: b79c9fc9551b ("bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/bpf/sashiko-outbox-211618@kernel.org/
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
This is independent of the "bpf, cgroup: Don't wait for cgroup_mutex to
read or free cgroup bpf state" series [1], which changes nearby lines.
The two apply cleanly in either order.

[1] https://lore.kernel.org/bpf/20261112172721.440275-1-shakeel.butt@linux.dev/

 kernel/bpf/cgroup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 57a785241d42..2bffa9f659cd 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -424,7 +424,7 @@ static int bpf_cgroup_array_copy_to_user(struct bpf_prog_array *array,
 			return -EFAULT;
 		i++;
 	}
-	return item->prog ? -ENOSPC : 0;
+	return 0;
 }
 
 static int bpf_cgroup_array_delete_safe_at(struct bpf_prog_array *array,
@@ -1505,7 +1505,8 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
 			effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
 							      lockdep_is_held(&cgroup_mutex));
 			cnt = min_t(int, bpf_cgroup_array_length(effective, atype), total_cnt);
-			ret = bpf_cgroup_array_copy_to_user(effective, prog_ids, cnt, atype);
+			if (bpf_cgroup_array_copy_to_user(effective, prog_ids, cnt, atype))
+				return -EFAULT;
 		} else {
 			struct hlist_head *progs;
 			struct bpf_prog_list *pl;

base-commit: 2e14a351662dc5a3b7421d210049c59edd0425d2
-- 
2.53.0-Meta


                 reply	other threads:[~2026-11-12 18:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261112183804.1171535-1-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=sashiko-bot@kernel.org \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --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 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.