cgroup/rstat: validate cpu before css_rstat_cpu() access
Subsystems (6)
PATCH 1/1 cgroup/rstat: validate cpu before css_rstat_cpu() access
css_rstat_updated() is exposed as a BPF kfunc and accepts a caller-provided cpu argument. The function uses cpu for per-cpu rstat lookups without checking whether it refers to a valid possible CPU.
A BPF iter/cgroup program with CAP_BPF and CAP_PERFMON can pass an invalid cpu value. On an unfixed UBSCAN_BOUNDS test kernel, cpu == 0x7fffffff triggers:
UBSAN: array-index-out-of-bounds in kernel/cgroup/rstat.c:31:9
index 2147483647 is out of range for type 'long unsigned int [64]'
Call Trace:
css_rstat_updated
bpf_iter_run_prog
cgroup_iter_seq_show
bpf_seq_readReject invalid cpu values before css_rstat_cpu() access.
--- kernel/cgroup/rstat.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/cgroup/rstat.c +4 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only #include "cgroup-internal.h" +#include <linux/cpumask.h> #include <linux/sched/cputime.h> #include <linux/bpf.h>
@@ -90,6 +91,9 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) !IS_ENABLED(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS)) && in_nmi())return ; +if (unlikely (cpu <0 || cpu >= nr_cpu_ids || !cpu_possible (cpu))) +return ; + rstatc =css_rstat_cpu (css, cpu);/* * If already on list return. This check is racy and smp_mb() is needed
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The `css_rstat_updated` kfunc is vulnerable to a kernel panic if a BPF tracing program passes a…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The css_rstat_updated kfunc is vulnerable to a kernel panic if a BPF tracing program passes a css pointer where css->rstat_cpu is uninitialized (NULL). --
commit e8d546639085dcc3eee578c0dace07ba513cb482
Author: Qing Ming <[email protected]>
cgroup/rstat: validate cpu before css_rstat_cpu() access
This commit adds a bounds and validity check for the cpu parameter in css_rstat_updated() to prevent an out-of-bounds array access when called from a BPF program.
Quoted 3 lines
kernel/cgroup/rstat.c +0
-0
[ ... ]
kernel/cgroup/rstat.c __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) · line 97
Quoted 8 lines
kernel/cgroup/rstat.c +3
-0@@ -90,6 +91,9 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) !IS_ENABLED(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS)) && in_nmi())return ; +if (unlikely (cpu <0 || cpu >= nr_cpu_ids || !cpu_possible (cpu))) +return ; + rstatc =css_rstat_cpu (css, cpu);
Could this still lead to a panic if css->rstat_cpu is uninitialized?
The existing check at the top of this function:
if (!css_uses_rstat(css))
return;only verifies that css->ss->css_rstat_flush is defined. It doesn't appear to check if the per-cpu memory css->rstat_cpu has actually been allocated.
If a BPF program intercepts a css object during allocation (for example, by tracing init_and_link_css before css_rstat_init completes), or if an untrusted pointer is passed because the kfunc lacks KF_TRUSTED_ARGS, css->rstat_cpu could be NULL.
In that scenario, css_rstat_cpu() would compute per_cpu_ptr(NULL, cpu), which returns an offset from NULL rather than a valid memory address. The subsequent call to llist_on_list() would then dereference this invalid address.
Should there be an explicit check for !css->rstat_cpu to safely handle uninitialized rstat state from BPF callers?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks for taking a look.
I think this is separate from the bug fixed by this patch. The reproducer passes the self css obtained from the cgroup iterator, i.e. a live cgroup object. For cgroup::self, css_rstat_init(&cgrp->self) is called during cgroup creation before the cgroup is committed and made visible. For subsystem csses, css_create() calls css_rstat_init(css) before linking the css into the hierarchy/id and before online_css().
So BPF-visible live css objects which use rstat should already have css->rstat_cpu initialized. Also, on current kernels, kfunc pointer arguments are trusted by default; the absence of an explicit KF_TRUSTED_ARGS flag does not mean that an arbitrary css pointer can be passed to the kfunc.
The issue fixed here is the missing validation of the caller-provided cpu argument before css_rstat_cpu(css, cpu). If there is a concrete tracing path which can pass a partially initialized css to this kfunc, that would be a separate bug and should be handled separately.
Quoted 54 lines · 在 2026/05/15 20:52 CST [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Thecss_rstat_updatedkfunc is vulnerable to a kernel panic if a BPF tracing program passes acsspointer wherecss->rstat_cpuis uninitialized (NULL). --commit
e8d546639085dcc3eee578c0dace07ba513cb482
Author: Qing Ming <[email protected]>cgroup/rstat: validate cpu before
css_rstat_cpu()accessThis commit adds a bounds and validity check for the cpu parameter in
css_rstat_updated()to prevent an out-of-bounds array access when called from a BPF program.Quoted 3 lines
kernel/cgroup/rstat.c +0
-0[ ... ]
kernel/cgroup/rstat.c __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) · line 97
Quoted 8 lines
kernel/cgroup/rstat.c +3
-0@@ -90,6 +91,9 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) !IS_ENABLED(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS)) && in_nmi())return ; +if (unlikely (cpu <0 || cpu >= nr_cpu_ids || !cpu_possible (cpu))) +return ; + rstatc =css_rstat_cpu (css, cpu);Could this still lead to a panic if
css->rstat_cpuis uninitialized?The existing check at the top of this function:
if (!css_uses_rstat(css)) return;only verifies that
css->ss->css_rstat_flushis defined. It doesn't appear to check if the per-cpu memorycss->rstat_cpuhas actually been allocated.If a BPF program intercepts a css object during allocation (for example, by tracing
init_and_link_cssbeforecss_rstat_initcompletes), or if an untrusted pointer is passed because the kfunc lacksKF_TRUSTED_ARGS,css->rstat_cpucould be NULL.In that scenario,
css_rstat_cpu()would computeper_cpu_ptr(NULL, cpu), which returns an offset from NULL rather than a valid memory address. The subsequent call tollist_on_list()would then dereference this invalid address.Should there be an explicit check for !css->rstat_cpu to safely handle uninitialized rstat state from BPF callers?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Tejun Heo
maintainerkernel/cgroup/rstat.c __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) · line 96
Quoted 7 lines · Qing Ming
kernel/cgroup/rstat.c +3
-0@@ -90,6 +91,9 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) !IS_ENABLED(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS)) && in_nmi())return ; +if (unlikely (cpu <0 || cpu >= nr_cpu_ids || !cpu_possible (cpu))) +return ; +
Can you please add this validation to the BPF kfunc that's exposing it?
Thanks.
Signature · 1 line
tejun
Quoted 1 line · Tejun Heo
Can you please add this validation to the BPF kfunc that's exposing it?
Sure, will do. I'll split out an internal __css_rstat_updated() helper and keep the cpu validation in the css_rstat_updated() kfunc wrapper. Internal callers will be converted to the helper in v2.
PATCH v2 1/1 cgroup/rstat: validate cpu before css_rstat_cpu() access
In reply to Qing Mingcss_rstat_updated() is exposed as a BPF kfunc and accepts a caller-provided cpu argument. The function uses cpu for per-cpu rstat lookups without checking whether it refers to a valid possible CPU.
A BPF iter/cgroup program with CAP_BPF and CAP_PERFMON can pass an invalid cpu value. On an unfixed UBSCAN_BOUNDS test kernel, cpu == 0x7fffffff triggers:
UBSAN: array-index-out-of-bounds in kernel/cgroup/rstat.c:31:9
index 2147483647 is out of range for type 'long unsigned int [64]'
Call Trace:
css_rstat_updated
bpf_iter_run_prog
cgroup_iter_seq_show
bpf_seq_readAdd cpu validation to the BPF-facing css_rstat_updated() kfunc and move the common implementation to __css_rstat_updated() for in-kernel callers.
---
v2:
- Split css_rstat_updated() into a BPF-visible wrapper and an internal __css_rstat_updated() helper.
- Switch internal callers to __css_rstat_updated().
block/blk-cgroup.c | 2 +- include/linux/cgroup.h | 1 + kernel/cgroup/rstat.c | 30 ++++++++++++++++++++---------- mm/memcontrol.c | 6 +++--- 4 files changed, 25 insertions(+), 14 deletions(-)
block/blk-cgroup.c +1 -1
@@ -2241,7 +2241,7 @@ void blk_cgroup_bio_start(struct bio *bio) }u64_stats_update_end_irqrestore (&bis->sync, flags); -css_rstat_updated (&blkcg->css, cpu); +__css_rstat_updated (&blkcg->css, cpu);put_cpu (); }
include/linux/cgroup.h +1 -0
@@ -777,6 +777,7 @@ static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)/* * cgroup scalable recursive statistics. */ +void __css_rstat_updated (struct cgroup_subsys_state *css,int cpu);void css_rstat_updated (struct cgroup_subsys_state *css,int cpu);void css_rstat_flush (struct cgroup_subsys_state *css);
kernel/cgroup/rstat.c +20 -10
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only #include "cgroup-internal.h" +#include <linux/cpumask.h> #include <linux/sched/cputime.h> #include <linux/bpf.h>
@@ -53,7 +54,7 @@ static inline struct llist_head *ss_lhead_cpu(struct cgroup_subsys *ss, int cpu) }/** -* css_rstat_updated - keep track of updated rstat_cpu +* __css_rstat_updated - keep track of updated rstat_cpu * @css: target cgroup subsystem state * @cpu: cpu on which rstat_cpu was updated *
@@ -63,20 +64,17 @@ static inline struct llist_head *ss_lhead_cpu(struct cgroup_subsys *ss, int cpu)* * NOTE: if the user needs the guarantee that the updater either add itself in * the lockless list or the concurrent flusher flushes its updated stats, a -* memory barrier is needed before the call to css_rstat_updated() i.e. a +* memory barrier is needed before the call to __css_rstat_updated() i.e. a * barrier after updating the per-cpu stats and before calling -* css_rstat_updated(). +* __css_rstat_updated(). */ -__bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) +void __css_rstat_updated (struct cgroup_subsys_state *css,int cpu) {struct llist_head *lhead;struct css_rstat_cpu *rstatc;struct llist_node *self; -/* -* Since bpf programs can call this function, prevent access to -* uninitialized rstat pointers. -*/ +/* Prevent access to uninitialized rstat pointers. */ if (!css_uses_rstat (css))return ;
@@ -125,6 +123,18 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)llist_add (&rstatc->lnode, lhead); } +/* +* BPF-facing wrapper for __css_rstat_updated(). Validate the caller-provided +* CPU before passing it to the internal rstat updater. +*/ +__bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) +{ +if (unlikely (cpu <0 || cpu >= nr_cpu_ids || !cpu_possible (cpu))) +return ; + +__css_rstat_updated (css, cpu); +} +static void __css_process_update_tree (struct cgroup_subsys_state *css,int cpu) {/* put @css and all ancestors on the corresponding updated lists */
@@ -170,7 +180,7 @@ static void css_process_update_tree(struct cgroup_subsys *ss, int cpu)* flusher flush the stats updated by the updater who have * observed that they are already on the list. The * corresponding barrier pair for this one should be before -* css_rstat_updated() by the user. +* __css_rstat_updated() by the user. * * For now, there aren't any such user, so not adding the * barrier here but if such a use-case arise, please add
@@ -614,7 +624,7 @@ static void cgroup_base_stat_cputime_account_end(struct cgroup *cgrp, unsigned long flags) {u64_stats_update_end_irqrestore (&rstatbc->bsync, flags); -css_rstat_updated (&cgrp->self,smp_processor_id ()); +__css_rstat_updated (&cgrp->self,smp_processor_id ());put_cpu_ptr (rstatbc); }
mm/memcontrol.c +3 -3
@@ -679,7 +679,7 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, long val,if (!val)return ; -css_rstat_updated (&memcg->css, cpu); +__css_rstat_updated (&memcg->css, cpu); statc_pcpu = memcg->vmstats_percpu;for (; statc_pcpu; statc_pcpu = statc->parent_pcpu) { statc =this_cpu_ptr (statc_pcpu);
@@ -2796,7 +2796,7 @@ static inline void account_slab_nmi_safe(struct mem_cgroup *memcg,struct mem_cgroup_per_node *pn = memcg->nodeinfo[pgdat->node_id];/* preemption is disabled in_nmi(). */ -css_rstat_updated (&memcg->css,smp_processor_id ()); +__css_rstat_updated (&memcg->css,smp_processor_id ());if (idx ==NR_SLAB_RECLAIMABLE_B )atomic_add (nr, &pn->slab_reclaimable);else
@@ -3019,7 +3019,7 @@ static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val)mod_memcg_state (memcg,MEMCG_KMEM , val); }else {/* preemption is disabled in_nmi(). */ -css_rstat_updated (&memcg->css,smp_processor_id ()); +__css_rstat_updated (&memcg->css,smp_processor_id ());atomic_add (val, &memcg->kmem_stat); } }
Signature · 1 line
2.53.0
Hello,
Quoted 2 lines
Qing Ming (1):
cgroup/rstat: validate cpu beforecss_rstat_cpu()access
Applied to cgroup/for-7.1-fixes.
In hindsight, we should have added a separate kfunc wrapper from the start instead of tagging css_rstat_updated() with __bpf_kfunc directly, which would have avoided the rename. Oh well, it is what it is.
Thanks.
--
tejun
Shakeel Butt
maintainerQuoted 10 lines · Tejun Heo
Hello,
Quoted 2 lines
Qing Ming (1):
cgroup/rstat: validate cpu beforecss_rstat_cpu()accessApplied to cgroup/for-7.1-fixes.
In hindsight, we should have added a separate kfunc wrapper from the start instead of tagging
css_rstat_updated()with__bpf_kfuncdirectly, which would have avoided the rename. Oh well, it is what it is.
Is it frown upon to change the kfunc signature or remove __bpf_kfunc from a function? I am assuming we can but better not to, correct?
Tejun Heo
maintainerHello,
Quoted 6 lines · Shakeel Butt
Quoted 5 lines · Tejun Heo
Hello,
Quoted 2 lines
Qing Ming (1):
cgroup/rstat: validate cpu beforecss_rstat_cpu()access
Quoted 8 lines
Quoted 5 lines
Applied to cgroup/for-7.1-fixes.
In hindsight, we should have added a separate kfunc wrapper from the start instead of tagging
css_rstat_updated()with__bpf_kfuncdirectly, which would have avoided the rename. Oh well, it is what it is.Is it frown upon to change the kfunc signature or remove
__bpf_kfuncfrom a function? I am assuming we can but better not to, correct?
We can. It's just a bit gratuitous and causes unnecessary churn. Let's just leave it be for now. We can clean up later when e.g. more meaningful restructuring is necessary.
Thanks.
Signature · 1 line
tejun
This patch was applied to bpf/bpf.git (master) by Tejun Heo <[email protected]>:
Hello:
This patch was applied to bpf/bpf.git (master)
by Tejun Heo <[email protected]>:
Quoted 9 lines · On Sat, 16 May 2026 15:08:49 +0800 you wrote:
css_rstat_updated()is exposed as a BPF kfunc and accepts a caller-provided cpu argument. The function uses cpu for per-cpu rstat lookups without checking whether it refers to a valid possible CPU.A BPF iter/cgroup program with
CAP_BPFandCAP_PERFMONcan pass an invalid cpu value. On an unfixedUBSCAN_BOUNDStest kernel, cpu ==0x7ffffffftriggers:[...]
Here is the summary with links:
- [v2] cgroup/rstat: validate cpu before css_rstat_cpu() access
https://git.kernel.org/bpf/bpf/c/8817005efbdf
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html