From 327d7882d7255c715b9296a191fbd0d28f28e917 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sun, 11 Feb 2018 10:37:59 +0100 Subject: [PATCH] sar/sadf: Use size_t type for size variable in read() syscall Use size_t type expected by read() system call to make sure size variable cannot be negative or overflow integer capacity. Signed-off-by: Sebastien GODARD --- sa.h | 2 +- sa_common.c | 15 ++++++++------- sa_conv.c | 8 +++++--- sar.c | 7 ++++--- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/sa.h b/sa.h index 1e7f4a1..0946b53 100644 --- a/sa.h +++ b/sa.h @@ -1289,7 +1289,7 @@ void remap_struct void replace_nonprintable_char (int, char *); int sa_fread - (int, void *, int, int); + (int, void *, size_t, int); int sa_get_record_timestamp_struct (unsigned int, struct record_header *, struct tm *, struct tm *); int sa_open_read_magic diff --git a/sa_common.c b/sa_common.c index 6ad1ee8..35df65c 100644 --- a/sa_common.c +++ b/sa_common.c @@ -1181,9 +1181,9 @@ void remap_struct(unsigned int gtypes_nr[], unsigned int ftypes_nr[], * 1 if EOF has been reached, 0 otherwise. *************************************************************************** */ -int sa_fread(int ifd, void *buffer, int size, int mode) +int sa_fread(int ifd, void *buffer, size_t size, int mode) { - int n; + ssize_t n; if ((n = read(ifd, buffer, size)) < 0) { fprintf(stderr, _("Error while reading system activity file: %s\n"), @@ -1229,7 +1229,7 @@ int sa_fread(int ifd, void *buffer, int size, int mode) int read_record_hdr(int ifd, void *buffer, struct record_header *record_hdr, struct file_header *file_hdr, int arch_64, int endian_mismatch) { - if (sa_fread(ifd, buffer, file_hdr->rec_size, SOFT_SIZE)) + if (sa_fread(ifd, buffer, (size_t) file_hdr->rec_size, SOFT_SIZE)) /* End of sa data file */ return 1; @@ -1427,7 +1427,7 @@ void read_file_stat_bunch(struct activity *act[], int curr, int ifd, int act_nr, for (j = 0; j < (nr_value * act[p]->nr2); j++) { sa_fread(ifd, (char *) act[p]->buf[curr] + j * act[p]->msize, - act[p]->fsize, HARD_SIZE); + (size_t) act[p]->fsize, HARD_SIZE); } } else if (nr_value > 0) { @@ -1435,7 +1435,8 @@ void read_file_stat_bunch(struct activity *act[], int curr, int ifd, int act_nr, * Note: If msize was smaller than fsize, * then it has been set to fsize in check_file_actlst(). */ - sa_fread(ifd, act[p]->buf[curr], act[p]->fsize * nr_value * act[p]->nr2, HARD_SIZE); + sa_fread(ifd, act[p]->buf[curr], + (size_t) act[p]->fsize * (size_t) nr_value * (size_t) act[p]->nr2, HARD_SIZE); } else { /* nr_value == 0: Nothing to read */ @@ -1603,7 +1604,7 @@ void check_file_actlst(int *ifd, char *dfile, struct activity *act[], SREALLOC(buffer, char, file_magic->header_size); /* Read sa data file standard header and allocate activity list */ - sa_fread(*ifd, buffer, file_magic->header_size, HARD_SIZE); + sa_fread(*ifd, buffer, (size_t) file_magic->header_size, HARD_SIZE); /* * Data file header size (file_magic->header_size) may be greater or * smaller than FILE_HEADER_SIZE. Remap the fields of the file header @@ -1645,7 +1646,7 @@ void check_file_actlst(int *ifd, char *dfile, struct activity *act[], for (i = 0; i < file_hdr->sa_act_nr; i++, fal++) { /* Read current file_activity structure from file */ - sa_fread(*ifd, buffer, file_hdr->act_size, HARD_SIZE); + sa_fread(*ifd, buffer, (size_t) file_hdr->act_size, HARD_SIZE); /* * Data file_activity size (file_hdr->act_size) may be greater or * smaller than FILE_ACTIVITY_SIZE. Remap the fields of the file's structure diff --git a/sa_conv.c b/sa_conv.c index 5cc2941..2901b6a 100644 --- a/sa_conv.c +++ b/sa_conv.c @@ -287,7 +287,7 @@ int upgrade_header_section(char dfile[], int fd, int stdfd, struct activity *act n = (previous_format == FORMAT_MAGIC_2171 ? FILE_HEADER_SIZE_2171 : hdr_size); SREALLOC(buffer, char, n); - sa_fread(fd, buffer, n, HARD_SIZE); + sa_fread(fd, buffer, (size_t) n, HARD_SIZE); /* Upgrade file_header structure */ upgrade_file_header(buffer, file_hdr, previous_format, @@ -1636,12 +1636,14 @@ int upgrade_common_record(int fd, int stdfd, struct activity *act[], struct file for (k = 0; k < act[p]->nr2; k++) { sa_fread(fd, (char *) act[p]->buf[0] + (j * act[p]->nr2 + k) * act[p]->msize, - ofal->size, HARD_SIZE); + (size_t) ofal->size, HARD_SIZE); } } } else if (act[p]->nr_ini > 0) { - sa_fread(fd, act[p]->buf[0], ofal->size * act[p]->nr_ini * act[p]->nr2, HARD_SIZE); + sa_fread(fd, act[p]->buf[0], + (size_t) ofal->size * (size_t) act[p]->nr_ini * (size_t) act[p]->nr2, + HARD_SIZE); } nr_struct = act[p]->nr_ini; diff --git a/sar.c b/sar.c index b05a36d..f7ee3a4 100644 --- a/sar.c +++ b/sar.c @@ -569,9 +569,9 @@ void write_stats_startup(int curr) * 1 if end of file has been reached, 0 otherwise. *************************************************************************** */ -int sa_read(void *buffer, int size) +int sa_read(void *buffer, size_t size) { - int n; + ssize_t n; while (size) { @@ -689,7 +689,8 @@ void read_sadc_stat_bunch(int curr) (size_t) act[p]->fsize * (size_t) act[p]->nr_ini * (size_t) act[p]->nr2); } } - if (sa_read(act[p]->buf[curr], act[p]->fsize * act[p]->nr[curr] * act[p]->nr2)) { + if (sa_read(act[p]->buf[curr], + (size_t) act[p]->fsize * (size_t) act[p]->nr[curr] * (size_t) act[p]->nr2)) { print_read_error(END_OF_DATA_UNEXPECTED); } } -- 2.50.0