Skip to content

Commit 55a24d9

Browse files
devnexenhtejun
authored andcommitted
tools/sched_ext: scx_central: fix CPU_SET and skeleton leak on early exit
Use CPU_SET_S() instead of CPU_SET() on the dynamically allocated cpuset to avoid a potential out-of-bounds write when nr_cpu_ids exceeds CPU_SETSIZE. Also destroy the skeleton before returning on invalid central CPU ID to prevent a resource leak. Signed-off-by: David Carlier <devnexen@gmail.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 0767684 commit 55a24d9

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

tools/sched_ext/scx_central.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ int main(int argc, char **argv)
7474
u32 central_cpu = strtoul(optarg, NULL, 0);
7575
if (central_cpu >= skel->rodata->nr_cpu_ids) {
7676
fprintf(stderr, "invalid central CPU id value, %u given (%u max)\n", central_cpu, skel->rodata->nr_cpu_ids);
77+
scx_central__destroy(skel);
7778
return -1;
7879
}
7980
skel->rodata->central_cpu = (s32)central_cpu;
@@ -109,7 +110,7 @@ int main(int argc, char **argv)
109110
SCX_BUG_ON(!cpuset, "Failed to allocate cpuset");
110111
cpuset_size = CPU_ALLOC_SIZE(skel->rodata->nr_cpu_ids);
111112
CPU_ZERO_S(cpuset_size, cpuset);
112-
CPU_SET(skel->rodata->central_cpu, cpuset);
113+
CPU_SET_S(skel->rodata->central_cpu, cpuset_size, cpuset);
113114
SCX_BUG_ON(sched_setaffinity(0, cpuset_size, cpuset),
114115
"Failed to affinitize to central CPU %d (max %d)",
115116
skel->rodata->central_cpu, skel->rodata->nr_cpu_ids - 1);

0 commit comments

Comments
 (0)