libfstools: avoid false positives when matching devices and volumes
authorJo-Philipp Wich <jo@mein.io>
Wed, 27 Mar 2019 07:55:07 +0000 (08:55 +0100)
committerJo-Philipp Wich <jo@mein.io>
Sun, 31 Mar 2019 14:09:40 +0000 (16:09 +0200)
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>
libfstools/find.c
libfstools/ubi.c

index b383f574bcd0819a57e258679f5370e0df0bd64d..cefdd23e26891bba28d038bcbcd1aed3c67bb337 100644 (file)
@@ -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;
 
index f9d6e0aaa718ef941862297cae84afaebf7440c8..091ccf6e5bbf0dc561853be7873290a6df3165d9 100644 (file)
@@ -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));