Browse Source

libfstools: avoid false positives when matching devices and volumes

Revise matching code using strncmp() in order to avoid returning wrong
items, e.g. /dev/sda1 when /dev/sda was requested.

Ref: https://bugs.openwrt.org/index.php?do=details&task_id=2196
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Jo-Philipp Wich 5 years ago
parent
commit
9b36dc25dd
2 changed files with 4 additions and 4 deletions
  1. 3 3
      libfstools/find.c
  2. 1 1
      libfstools/ubi.c

+ 3 - 3
libfstools/find.c

@@ -23,6 +23,7 @@ int
 find_overlay_mount(char *overlay)
 {
 	FILE *fp = fopen("/proc/mounts", "r");
+	size_t len = strlen(overlay);
 	static char line[256];
 	int ret = -1;
 
@@ -30,7 +31,7 @@ find_overlay_mount(char *overlay)
 		return ret;
 
 	while (ret && fgets(line, sizeof(line), fp))
-		if (!strncmp(line, overlay, strlen(overlay)))
+		if (len < sizeof(line) && !strncmp(line, overlay, len) && line[len] == ' ')
 			ret = 0;
 
 	fclose(fp);
@@ -103,7 +104,6 @@ find_mount_point(char *block, int root_only)
 {
 	FILE *fp = fopen("/proc/self/mountinfo", "r");
 	static char line[256];
-	int len = strlen(block);
 	char *point = NULL, *pos, *tmp, *cpoint, *devname, *fstype;
 	struct stat s;
 	int rstat;
@@ -183,7 +183,7 @@ find_mount_point(char *block, int root_only)
 		devname = tmp;
 
 		/* if device name matches */
-		if (!strncmp(block, devname, len + 1)) {
+		if (!strcmp(block, devname)) {
 			if (root_only && fs_rootfs_only(fstype))
 				break;
 

+ 1 - 1
libfstools/ubi.c

@@ -147,7 +147,7 @@ static struct volume *ubi_volume_match(char *name, int ubi_num, int volid)
 		return NULL;
 	}
 
-	if (strncmp(name, volname, strlen(volname) + 1))
+	if (strcmp(name, volname))
 		return NULL;
 
 	p = calloc(1, sizeof(struct ubi_volume));