From: NeilBrown Date: Mon, 6 Jul 2015 03:21:33 +0000 (+1000) Subject: sysfs: reject reads that use the whole buffer. X-Git-Tag: mdadm-3.3.3~31 X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fmdadm.git;a=commitdiff_plain;h=5418499ae48851399c280ca1986741762bd039f9 sysfs: reject reads that use the whole buffer. If a read fills the whole buffer, then we possibly missed something of the end, and we definitely shouldn't put a '\0' beyond the end, so just return an error. This should never happen anyway. Signed-off-by: NeilBrown --- diff --git a/sysfs.c b/sysfs.c index 18f3df9a..72684702 100644 --- a/sysfs.c +++ b/sysfs.c @@ -490,7 +490,7 @@ int sysfs_fd_get_ll(int fd, unsigned long long *val) lseek(fd, 0, 0); n = read(fd, buf, sizeof(buf)); - if (n <= 0) + if (n <= 0 || n == sizeof(buf)) return -2; buf[n] = 0; *val = strtoull(buf, &ep, 0); @@ -526,7 +526,7 @@ int sysfs_fd_get_two(int fd, unsigned long long *v1, unsigned long long *v2) lseek(fd, 0, 0); n = read(fd, buf, sizeof(buf)); - if (n <= 0) + if (n <= 0 || n == sizeof(buf)) return -2; buf[n] = 0; *v1 = strtoull(buf, &ep, 0); @@ -562,7 +562,7 @@ int sysfs_fd_get_str(int fd, char *val, int size) lseek(fd, 0, 0); n = read(fd, val, size); - if (n <= 0) + if (n <= 0 || n == size) return -1; val[n] = 0; return n; @@ -715,7 +715,7 @@ int sysfs_disk_to_sg(int fd) struct stat st; char path[256]; char sg_path[256]; - char sg_major_minor[8]; + char sg_major_minor[10]; char *c; DIR *dir; struct dirent *de; @@ -750,7 +750,7 @@ int sysfs_disk_to_sg(int fd) rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); close(fd); - if (rv < 0) + if (rv < 0 || rv == sizeof(sg_major_minor)) return -1; else sg_major_minor[rv - 1] = '\0';