[v7,8/8] lib/pmu: fix out-of-bound access
Checks
Commit Message
Make sure that out-of-bound access does not happen by saving one byte in
buffer for NUL terminator.
Fixes: a8926a65ad1d ("pmu: support Arm")
Fixes: 960c43184c4d ("pmu: introduce library for reading PMU events")
Cc: tduszynski@marvell.com
Signed-off-by: Tomasz Duszynski <tduszynski@marvell.com>
---
lib/pmu/pmu.c | 2 +-
lib/pmu/pmu_arm64.c | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
@@ -126,7 +126,7 @@ get_event_config(const char *name, uint64_t config[3])
if (fp == NULL)
return -errno;
- ret = fread(buf, 1, sizeof(buf), fp);
+ ret = fread(buf, 1, sizeof(buf) - 1, fp);
if (ret == 0) {
fclose(fp);
@@ -24,12 +24,13 @@ read_attr_int(const char *path, int *val)
if (fd == -1)
return -errno;
- ret = read(fd, buf, sizeof(buf));
+ ret = read(fd, buf, sizeof(buf) - 1);
if (ret == -1) {
close(fd);
return -errno;
}
+ buf[ret] = '\0';
*val = strtol(buf, NULL, 10);
close(fd);