]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
libext2fs/ismounted.c: check device id in advance to skip false device names
authorTheodore Ts'o <tytso@mit.edu>
Sun, 10 Nov 2019 17:11:49 +0000 (12:11 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Sun, 10 Nov 2019 17:11:49 +0000 (12:11 -0500)
If there is a trickster which tries to use device names as the mount
device for pseudo-file systems, the resulting /proc/mounts can confuse
ext2fs_check_mount_point().  (So far as I can tell, there's no good
reason to do this, but sysadmins do the darnest things.)

An example of this might be the following /proc/mounts excerpt:

/dev/sdb /mnt2 tmpfs rw,relatime 0 0
/dev/sdb /mnt ext4 rw,relatime 0 0

This is created via "mount -t tmpfs /dev/sdb /mnt2" followed via
"mount -t ext4 /dev/sdb /mnt".  (Normally, a sane mount of tmpfs would
use something like "mount -t tmpfs tmpfs /mnt2".)

Fix this by double checking the st_rdev of the claimed mountpoint and
match it with the dev_t of the device.  (Note that the GNU HURD
doesn't support st_rdev, so we can't solve this problem for the HURD.)

Reported-by: GuiYao <guiyao@huawei.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/ismounted.c

index 6cd497dc5e7eae07a1090e28a367bfb894a5b9ef..dc37cce4ce3086e29d22ede24cc8b638326c5113 100644 (file)
@@ -128,8 +128,19 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
        while ((mnt = getmntent (f)) != NULL) {
                if (mnt->mnt_fsname[0] != '/')
                        continue;
-               if (strcmp(file, mnt->mnt_fsname) == 0)
+               if (stat(mnt->mnt_dir, &st_buf) != 0)
+                       continue;
+               if (strcmp(file, mnt->mnt_fsname) == 0) {
+                       if (file_rdev && (file_rdev != st_buf.st_dev)) {
+#ifdef DEBUG
+                               printf("Bogus entry in %s!  "
+                                      "(%s does not exist)\n",
+                                      mtab_file, mnt->mnt_dir);
+#endif /* DEBUG */
+                               continue;
+                       }
                        break;
+               }
                if (stat(mnt->mnt_fsname, &st_buf) == 0) {
                        if (ext2fsP_is_disk_device(st_buf.st_mode)) {
 #ifndef __GNU__
@@ -168,32 +179,6 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
 #endif /* __GNU__ */
                goto errout;
        }
-#ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
-       /* Validate the entry in case /etc/mtab is out of date */
-       /*
-        * We need to be paranoid, because some broken distributions
-        * (read: Slackware) don't initialize /etc/mtab before checking
-        * all of the non-root filesystems on the disk.
-        */
-       if (stat(mnt->mnt_dir, &st_buf) < 0) {
-               retval = errno;
-               if (retval == ENOENT) {
-#ifdef DEBUG
-                       printf("Bogus entry in %s!  (%s does not exist)\n",
-                              mtab_file, mnt->mnt_dir);
-#endif /* DEBUG */
-                       retval = 0;
-               }
-               goto errout;
-       }
-       if (file_rdev && (st_buf.st_dev != file_rdev)) {
-#ifdef DEBUG
-               printf("Bogus entry in %s!  (%s not mounted on %s)\n",
-                      mtab_file, file, mnt->mnt_dir);
-#endif /* DEBUG */
-               goto errout;
-       }
-#endif /* __GNU__ */
        *mount_flags = EXT2_MF_MOUNTED;
 
 #ifdef MNTOPT_RO