Skip to contentSkip to conversation

cgroup: avoid null de-ref in css_rstat_exit()

PATCH Applied JP Kobryn 2 participants 2 messages Aug 7, 2025 – Aug 9, 2025 cgroup

lore
Share & tools
lore

JP Kobryn

author
#0 lore raw

PATCH 1/1 cgroup: avoid null de-ref in css_rstat_exit()

css_rstat_exit() may be called asynchronously in scenarios where preceding calls to css_rstat_init() have not completed. One such example is this sequence below:

css_create(...)

{
	...
	init_and_link_css(css, ...);

	err = percpu_ref_init(...);
	if (err)
		goto err_free_css;
	err = cgroup_idr_alloc(...);
	if (err)
		goto err_free_css;
	err = css_rstat_init(css, ...);
	if (err)
		goto err_free_css;
	...
err_free_css:
	INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
	queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
	return ERR_PTR(err);
}

If any of the three goto jumps are taken, async cleanup will begin and css_rstat_exit() will be invoked on an uninitialized css->rstat_cpu.

Avoid accessing the unitialized field by returning early in css_rstat_exit() if this is the case.

Fixes 5da3bfa029d68 ("cgroup: use separate rstat trees for each subsystem")
Reported-by [email protected]
---
 kernel/cgroup/rstat.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/cgroup/rstat.c +3 -0
index cbeaa499a96a..408e52d5f7a4 100644
@@ -488,6 +488,9 @@ void css_rstat_exit(struct cgroup_subsys_state *css)
 	if (!css_uses_rstat(css))
 		return;
 
+	if (!css->rstat_cpu)
+		return;
+
 	css_rstat_flush(css);
 
 	/* sanity check */
Signature · 1 line
2.47.3
maintainer Applied
#1 lore raw
Quoted 28 lines · JP Kobryn

css_rstat_exit() may be called asynchronously in scenarios where preceding calls to css_rstat_init() have not completed. One such example is this sequence below:

css_create(...)

{
	...
	init_and_link_css(css, ...);

	err = percpu_ref_init(...);
	if (err)
		goto err_free_css;
	err = cgroup_idr_alloc(...);
	if (err)
		goto err_free_css;
	err = css_rstat_init(css, ...);
	if (err)
		goto err_free_css;
	...
err_free_css:
	INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
	queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
	return ERR_PTR(err);
}

If any of the three goto jumps are taken, async cleanup will begin and css_rstat_exit() will be invoked on an uninitialized css->rstat_cpu.

Quoted 8 lines

Avoid accessing the unitialized field by returning early in css_rstat_exit() if this is the case.

Fixes 5da3bfa029d68 ("cgroup: use separate rstat trees for each subsystem")
Reported-by [email protected]

Applied to cgroup/for-6.17-fixes.

Thanks.

Signature · 1 line
tejun