libbbb: find_mount_point() too eager to stat mounted devices
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 29 Apr 2019 15:59:08 +0000 (17:59 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 29 Apr 2019 15:59:08 +0000 (17:59 +0200)
None of the below "devices" (first word on the line) are real.

sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
devtmpfs /dev devtmpfs rw,nosuid,size=7917900k,nr_inodes=1979475,mode=755 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
tmpfs /tmp tmpfs rw,relatime 0 0

function                                             old     new   delta
find_mount_point                                     297     302      +5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
libbb/find_mount_point.c

index 94bbf1d4ab2cf9ed510b02d4e022b8c4e1ad2f04..0e1be38204541c79c3bf49f9ebb8f331b31bbba5 100644 (file)
@@ -56,11 +56,22 @@ struct mntent* FAST_FUNC find_mount_point(const char *name, int subdir_too)
                        continue;
 
                /* Is device's dev_t == name's dev_t? */
-               if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == devno_of_name)
+               if (mountEntry->mnt_fsname[0] == '/'
+               /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+                * avoid stat'ing "sysfs", "proc", "none" and such,
+                * useless at best, can stat unrelated files at worst.
+                */
+                && stat(mountEntry->mnt_fsname, &s) == 0
+                && s.st_rdev == devno_of_name
+               ) {
                        break;
+               }
                /* Match the directory's mount point. */
-               if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == devno_of_name)
+               if (stat(mountEntry->mnt_dir, &s) == 0
+                && s.st_dev == devno_of_name
+               ) {
                        break;
+               }
        }
        endmntent(mtab_fp);