summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorShakeel Butt <shakeel.butt@linux.dev>2026-11-12 10:38:03 -0800
committerAlexei Starovoitov <ast@kernel.org>2026-11-14 19:03:59 +0000
commit4b4eb7f44ee05754339d07e9df649809f45d14c5 (patch)
tree3d593f8835e428e8592028db3dde7616f541563d /kernel
parent9594dc51561247f97f153fda413349e5f8dd52d3 (diff)
bpf, cgroup: Don't lose errors in effective BPF_LSM_CGROUP queries
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> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://patch.msgid.link/20261112183804.1171535-1-shakeel.butt@linux.dev Closes: https://lore.kernel.org/bpf/sashiko-outbox-211618@kernel.org/
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/cgroup.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index ae082ac86130..f195d5fcae6c 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -464,7 +464,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,
@@ -1567,7 +1567,8 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
cgroup_bpf_held());
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;