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: tduszyn...@marvell.com Signed-off-by: Tomasz Duszynski <tduszyn...@marvell.com> --- lib/pmu/pmu.c | 2 +- lib/pmu/pmu_arm64.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pmu/pmu.c b/lib/pmu/pmu.c index 742829cf9d..8960faa7d0 100644 --- a/lib/pmu/pmu.c +++ b/lib/pmu/pmu.c @@ -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); diff --git a/lib/pmu/pmu_arm64.c b/lib/pmu/pmu_arm64.c index 2c40b5f702..f3a817b42f 100644 --- a/lib/pmu/pmu_arm64.c +++ b/lib/pmu/pmu_arm64.c @@ -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); -- 2.34.1