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 d4ce234491..686cc24e83 100644
--- a/lib/pmu/pmu.c
+++ b/lib/pmu/pmu.c
@@ -130,7 +130,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 3f4f5fa297..c211d5495b 100644
--- a/lib/pmu/pmu_arm64.c
+++ b/lib/pmu/pmu_arm64.c
@@ -26,12 +26,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

Reply via email to