Blame SOURCES/0018-Replace-strcpy-with-strncpy-to-avoid-buffer-overflow.patch

72abff
From cfaaf34550c5ea92ee9b74d969a0c56c826cd020 Mon Sep 17 00:00:00 2001
72abff
From: Sebastien GODARD <sysstat@users.noreply.github.com>
72abff
Date: Thu, 11 Aug 2016 09:13:57 +0200
72abff
Subject: [PATCH] Replace strcpy() with strncpy() to avoid buffer overflows
72abff
72abff
Using strcpy() is not safe since destination buffer may overflow, for
72abff
example if the string being copied doesn't contain a terminator.
72abff
This patch replaces strcpy() with strncpy() to make sure no buffer
72abff
overflows happen.
72abff
72abff
Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
72abff
(cherry picked from commit 5aa69b67f03c00eded746c819eaa5b74a021ca1b)
72abff
---
72abff
 cifsiostat.c | 3 ++-
72abff
 ioconf.c     | 6 ++++--
72abff
 iostat.c     | 6 ++++--
72abff
 rd_stats.c   | 5 +++--
72abff
 sa_common.c  | 8 ++++----
72abff
 5 files changed, 17 insertions(+), 11 deletions(-)
72abff
72abff
diff --git a/cifsiostat.c b/cifsiostat.c
72abff
index 09b60b2..bca6576 100644
72abff
--- a/cifsiostat.c
72abff
+++ b/cifsiostat.c
72abff
@@ -345,7 +345,8 @@ void read_cifs_stat(int curr)
72abff
 			else {
72abff
 				start = 1;
72abff
 			}
72abff
-			strcpy(cifs_name, name_tmp);
72abff
+			strncpy(cifs_name, name_tmp, MAX_NAME_LEN);
72abff
+			cifs_name[MAX_NAME_LEN - 1] = '\0';
72abff
 		}
72abff
 		else {
72abff
 			if (!strncmp(line, "Reads:", 6)) {
72abff
diff --git a/ioconf.c b/ioconf.c
72abff
index 0011320..83008b1 100644
72abff
--- a/ioconf.c
72abff
+++ b/ioconf.c
72abff
@@ -282,7 +282,8 @@ int ioc_init(void)
72abff
 			 * exception info
72abff
 			 */
72abff
 			xblkp->ext_minor = iocp->ctrlno;
72abff
-			strcpy(xblkp->ext_name, blkp->name);
72abff
+			strncpy(xblkp->ext_name, blkp->name, IOC_NAMELEN + 1);
72abff
+			xblkp->ext_name[IOC_NAMELEN] = '\0';
72abff
 			xblkp->ext = 1;
72abff
 			continue;
72abff
 		}
72abff
@@ -393,7 +394,8 @@ char *ioc_name(unsigned int major, unsigned int minor)
72abff
 
72abff
 	/* Is this an extension record? */
72abff
 	if (p->blkp->ext && (p->blkp->ext_minor == minor)) {
72abff
-		strcpy(name, p->blkp->ext_name);
72abff
+		strncpy(name, p->blkp->ext_name, IOC_DEVLEN + 1);
72abff
+		name[IOC_DEVLEN] = '\0';
72abff
 		return (name);
72abff
 	}
72abff
 
72abff
diff --git a/iostat.c b/iostat.c
72abff
index 308a9af..e49daa0 100644
72abff
--- a/iostat.c
72abff
+++ b/iostat.c
72abff
@@ -366,7 +366,8 @@ void presave_device_list(void)
72abff
 
72abff
 		/* Now save devices and group names in the io_hdr_stats structures */
72abff
 		for (i = 0; (i < dlist_idx) && (i < iodev_nr); i++, shi++, sdli++) {
72abff
-			strcpy(shi->name, sdli->dev_name);
72abff
+			strncpy(shi->name, sdli->dev_name, MAX_NAME_LEN);
72abff
+			shi->name[MAX_NAME_LEN - 1] = '\0';
72abff
 			shi->used = TRUE;
72abff
 			if (shi->name[0] == ' ') {
72abff
 				/* Current device name is in fact the name of a group */
72abff
@@ -385,7 +386,8 @@ void presave_device_list(void)
72abff
 		 * included in that group.
72abff
 		 */
72abff
 		shi += iodev_nr - 1;
72abff
-		strcpy(shi->name, group_name);
72abff
+		strncpy(shi->name, group_name, MAX_NAME_LEN);
72abff
+		shi->name[MAX_NAME_LEN - 1] = '\0';
72abff
 		shi->used = TRUE;
72abff
 		shi->status = DISK_GROUP;
72abff
 	}
72abff
diff --git a/rd_stats.c b/rd_stats.c
72abff
index f288eb8..6aa8698 100644
72abff
--- a/rd_stats.c
72abff
+++ b/rd_stats.c
72abff
@@ -1915,7 +1915,7 @@ void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
72abff
 void read_filesystem(struct stats_filesystem *st_filesystem, int nbr)
72abff
 {
72abff
 	FILE *fp;
72abff
-	char line[512], fs_name[MAX_FS_LEN], mountp[256];
72abff
+	char line[512], fs_name[128], mountp[256];
72abff
 	int fs = 0;
72abff
 	struct stats_filesystem *st_filesystem_i;
72abff
 	struct statvfs buf;
72abff
@@ -1955,7 +1955,8 @@ void read_filesystem(struct stats_filesystem *st_filesystem, int nbr)
72abff
 			st_filesystem_i->f_bavail = buf.f_bavail * buf.f_frsize;
72abff
 			st_filesystem_i->f_files  = buf.f_files;
72abff
 			st_filesystem_i->f_ffree  = buf.f_ffree;
72abff
-			strcpy(st_filesystem_i->fs_name, fs_name);
72abff
+			strncpy(st_filesystem_i->fs_name, fs_name, MAX_FS_LEN);
72abff
+			st_filesystem_i->fs_name[MAX_FS_LEN - 1] = '\0';
72abff
 			strncpy(st_filesystem_i->mountp, mountp, MAX_FS_LEN);
72abff
 			st_filesystem_i->mountp[MAX_FS_LEN - 1] = '\0';
72abff
 		}
72abff
diff --git a/sa_common.c b/sa_common.c
72abff
index 2206e9f..df7d38d 100644
72abff
--- a/sa_common.c
72abff
+++ b/sa_common.c
72abff
@@ -549,7 +549,7 @@ unsigned int check_net_dev_reg(struct activity *a, int curr, int ref,
72abff
 					 * actually unregistered.
72abff
 					 */
72abff
 					memset(sndp, 0, STATS_NET_DEV_SIZE);
72abff
-					strcpy(sndp->interface, sndc->interface);
72abff
+					strncpy(sndp->interface, sndc->interface, MAX_IFACE_LEN - 1);
72abff
 				}
72abff
 			}
72abff
 			return index;
72abff
@@ -574,7 +574,7 @@ unsigned int check_net_dev_reg(struct activity *a, int curr, int ref,
72abff
 	sndp = (struct stats_net_dev *) a->buf[ref] + index;
72abff
 	/* Since the name is not the same, reset all the structure */
72abff
 	memset(sndp, 0, STATS_NET_DEV_SIZE);
72abff
-	strcpy(sndp->interface, sndc->interface);
72abff
+	strncpy(sndp->interface, sndc->interface, MAX_IFACE_LEN - 1);
72abff
 
72abff
 	return  index;
72abff
 }
72abff
@@ -625,7 +625,7 @@ unsigned int check_net_edev_reg(struct activity *a, int curr, int ref,
72abff
 				 * actually unregistered.
72abff
 				 */
72abff
 				memset(snedp, 0, STATS_NET_EDEV_SIZE);
72abff
-				strcpy(snedp->interface, snedc->interface);
72abff
+				strncpy(snedp->interface, snedc->interface, MAX_IFACE_LEN - 1);
72abff
 			}
72abff
 			return index;
72abff
 		}
72abff
@@ -649,7 +649,7 @@ unsigned int check_net_edev_reg(struct activity *a, int curr, int ref,
72abff
 	snedp = (struct stats_net_edev *) a->buf[ref] + index;
72abff
 	/* Since the name is not the same, reset all the structure */
72abff
 	memset(snedp, 0, STATS_NET_EDEV_SIZE);
72abff
-	strcpy(snedp->interface, snedc->interface);
72abff
+	strncpy(snedp->interface, snedc->interface, MAX_IFACE_LEN - 1);
72abff
 
72abff
 	return  index;
72abff
 }
72abff
-- 
72abff
2.14.3
72abff