Skip to content

Commit cabd76b

Browse files
devnexenhtejun
authored andcommitted
tools/sched_ext: scx_flatcg: fix potential stack overflow from VLA in fcg_read_stats
fcg_read_stats() had a VLA allocating 21 * nr_cpus * 8 bytes on the stack, risking stack overflow on large CPU counts (nr_cpus can be up to 512). Fix by using a single heap allocation with the correct size, reusing it across all stat indices, and freeing it at the end. Signed-off-by: David Carlier <devnexen@gmail.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 0b82cc3 commit cabd76b

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

tools/sched_ext/scx_flatcg.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,27 @@ static float read_cpu_util(__u64 *last_sum, __u64 *last_idle)
102102

103103
static void fcg_read_stats(struct scx_flatcg *skel, __u64 *stats)
104104
{
105-
__u64 cnts[FCG_NR_STATS][skel->rodata->nr_cpus];
105+
__u64 *cnts;
106106
__u32 idx;
107107

108+
cnts = calloc(skel->rodata->nr_cpus, sizeof(__u64));
109+
if (!cnts)
110+
return;
111+
108112
memset(stats, 0, sizeof(stats[0]) * FCG_NR_STATS);
109-
memset(cnts, 0, sizeof(cnts));
110113

111114
for (idx = 0; idx < FCG_NR_STATS; idx++) {
112115
int ret, cpu;
113116

114117
ret = bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats),
115-
&idx, cnts[idx]);
118+
&idx, cnts);
116119
if (ret < 0)
117120
continue;
118121
for (cpu = 0; cpu < skel->rodata->nr_cpus; cpu++)
119-
stats[idx] += cnts[idx][cpu];
122+
stats[idx] += cnts[cpu];
120123
}
124+
125+
free(cnts);
121126
}
122127

123128
int main(int argc, char **argv)

0 commit comments

Comments
 (0)