Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

David Carlier and committed by
Tejun Heo
cabd76bb 0b82cc33

+9 -4
+9 -4
tools/sched_ext/scx_flatcg.c
··· 102 102 103 103 static void fcg_read_stats(struct scx_flatcg *skel, __u64 *stats) 104 104 { 105 - __u64 cnts[FCG_NR_STATS][skel->rodata->nr_cpus]; 105 + __u64 *cnts; 106 106 __u32 idx; 107 107 108 + cnts = calloc(skel->rodata->nr_cpus, sizeof(__u64)); 109 + if (!cnts) 110 + return; 111 + 108 112 memset(stats, 0, sizeof(stats[0]) * FCG_NR_STATS); 109 - memset(cnts, 0, sizeof(cnts)); 110 113 111 114 for (idx = 0; idx < FCG_NR_STATS; idx++) { 112 115 int ret, cpu; 113 116 114 117 ret = bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats), 115 - &idx, cnts[idx]); 118 + &idx, cnts); 116 119 if (ret < 0) 117 120 continue; 118 121 for (cpu = 0; cpu < skel->rodata->nr_cpus; cpu++) 119 - stats[idx] += cnts[idx][cpu]; 122 + stats[idx] += cnts[cpu]; 120 123 } 124 + 125 + free(cnts); 121 126 } 122 127 123 128 int main(int argc, char **argv)