From 9b36dc25dd31197681dfcbe4e83879a9b885f451 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 27 Mar 2019 08:55:07 +0100 Subject: [PATCH] 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 --- libfstools/find.c | 6 +++--- libfstools/ubi.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libfstools/find.c b/libfstools/find.c index b383f57..cefdd23 100644 --- a/libfstools/find.c +++ b/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; diff --git a/libfstools/ubi.c b/libfstools/ubi.c index f9d6e0a..091ccf6 100644 --- a/libfstools/ubi.c +++ b/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)); -- 2.30.2