diff options
author | Andi Kleen <ak@linux.intel.com> | 2016-12-15 09:49:40 -0800 |
---|---|---|
committer | Andi Kleen <ak@linux.intel.com> | 2016-12-15 09:50:54 -0800 |
commit | 52789974a9028f8fc3beaa40746fa88c3fc52308 (patch) | |
tree | 60e605220e4775694594e00b48208a03ddef7e4a | |
parent | 94d853b2ea818c5e31e61c089e87e5e19e7c5a17 (diff) | |
download | mcelog-52789974a9028f8fc3beaa40746fa88c3fc52308.tar.gz |
Fix malloc buffer overrun for unordered CPUs in sysfs
For some ordering of CPUs in sysfs the cache read code could overrun
its internal CPU array. Make sure the array is always correctly sized.
Signed-off-by: Andi Kleen <ak@linux.intel.com>
-rw-r--r-- | cache.c | 7 |
1 files changed, 5 insertions, 2 deletions
@@ -54,7 +54,9 @@ static void more_cpus(int cpu) int old = cachelen; if (!cachelen) cachelen = MIN_CPUS/2; - cachelen *= 2; + if (cachelen < cpu) + cachelen = cpu + 1; + cachelen = cachelen * 2; caches = xrealloc(caches, cachelen * sizeof(struct cache *)); memset(caches + old, 0, (cachelen - old) * sizeof(struct cache *)); } @@ -129,8 +131,9 @@ static int read_caches(void) numindex = st.st_nlink - 2; if (numindex < 0) numindex = MIN_INDEX; - if (cachelen <= cpu) + if (cpu >= cachelen) more_cpus(cpu); + assert(cpu < cachelen); caches[cpu] = xalloc(sizeof(struct cache) * (numindex+1)); for (i = 0; i < numindex; i++) { |