2021-11-30 04:04:51

by zhanchengbin

[permalink] [raw]
Subject: [PATCH] resize2fs : resize2fs failed due to the same name of tmpfs

If there is a tmpfs with the same name as the disk, and mount before the
disk,example:
/dev/sdd /root/tmp tmpfs rw,seclabel,relatime 0 0
/dev/sdd /root/mnt ext4 rw,seclabel,relatime 0 0

Create a hard link /dev/sdd-ln for the disk and resize2fs it.The items in
/proc/mounts are traversed, When you get to tmpfs,file!=mnt->mnt_fsname,
Therefore, the stat(mnt->mnt_fsname, &st_buf) branch is used,However, the
values of file_rdev and st_buf.st_rdev are the same.As a result, the system
mistakenly considers that disk is mounted to /root/tmp.As a result,resize2fs
fails.

mkdir /root/tmp
mkdir /root/mnt
mkfs.ext4 -F -b 1024 -E "resize=10000000" /dev/sdd 32768
mount -t tmpfs /dev/sdd /root/tmp
mount /dev/sdd /root/mnt
ln /dev/sdd /dev/sdd-ln
resize2fs /dev/sdd-ln 6G

Signed-off-by: zhanchengbin <[email protected]>
Signed-off-by: [email protected]
---
lib/ext2fs/ismounted.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/ext2fs/ismounted.c b/lib/ext2fs/ismounted.c
index aee7d726..463a82a6 100644
--- a/lib/ext2fs/ismounted.c
+++ b/lib/ext2fs/ismounted.c
@@ -98,6 +98,7 @@ static errcode_t check_mntent_file(const char
*mtab_file, const char *file,
{
struct mntent *mnt;
struct stat st_buf;
+ struct stat dir_st_buf;
errcode_t retval = 0;
dev_t file_dev=0, file_rdev=0;
ino_t file_ino=0;
@@ -144,8 +145,12 @@ static errcode_t check_mntent_file(const char
*mtab_file, const char *file,
if (stat(mnt->mnt_fsname, &st_buf) == 0) {
if (ext2fsP_is_disk_device(st_buf.st_mode)) {
#ifndef __GNU__
- if (file_rdev && (file_rdev == st_buf.st_rdev))
- break;
+ if (file_rdev && (file_rdev == st_buf.st_rdev)) {
+ if (stat(mnt->mnt_dir, &dir_st_buf) != 0)
+ continue;
+ if (file_rdev == dir_st_buf.st_dev)
+ break;
+ }
if (check_loop_mounted(mnt->mnt_fsname,
st_buf.st_rdev, file_dev,
file_ino) == 1)
--
2.23.0


2021-12-01 05:05:51

by zhanchengbin

[permalink] [raw]
Subject: Re: [PATCH] resize2fs : resize2fs failed due to the same name of tmpfs

If there is a tmpfs with the same name as the disk, and mount before the
disk,example:
/dev/sdd /root/tmp tmpfs rw,seclabel,relatime 0 0
/dev/sdd /root/mnt ext4 rw,seclabel,relatime 0 0

Create a hard link /dev/sdd-ln for the disk and resize2fs it.The items in
/proc/mounts are traversed, When you get to tmpfs,file!=mnt->mnt_fsname,
Therefore, the stat(mnt->mnt_fsname, &st_buf) branch is used,However, the
values of file_rdev and st_buf.st_rdev are the same.As a result, the system
mistakenly considers that disk is mounted to /root/tmp.As a result,resize2fs
fails.

mkdir /root/tmp
mkdir /root/mnt
mkfs.ext4 -F -b 1024 -E "resize=10000000" /dev/sdd 32768
mount -t tmpfs /dev/sdd /root/tmp
mount /dev/sdd /root/mnt
ln /dev/sdd /dev/sdd-ln
resize2fs /dev/sdd-ln 6G

Signed-off-by: zhanchengbin <[email protected]>
Signed-off-by: guiyao <[email protected]>
---
lib/ext2fs/ismounted.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/ext2fs/ismounted.c b/lib/ext2fs/ismounted.c
index aee7d726..463a82a6 100644
--- a/lib/ext2fs/ismounted.c
+++ b/lib/ext2fs/ismounted.c
@@ -98,6 +98,7 @@ static errcode_t check_mntent_file(const char
*mtab_file, const char *file,
{
struct mntent *mnt;
struct stat st_buf;
+ struct stat dir_st_buf;
errcode_t retval = 0;
dev_t file_dev=0, file_rdev=0;
ino_t file_ino=0;
@@ -144,8 +145,12 @@ static errcode_t check_mntent_file(const char
*mtab_file, const char *file,
if (stat(mnt->mnt_fsname, &st_buf) == 0) {
if (ext2fsP_is_disk_device(st_buf.st_mode)) {
#ifndef __GNU__
- if (file_rdev && (file_rdev == st_buf.st_rdev))
- break;
+ if (file_rdev && (file_rdev == st_buf.st_rdev)) {
+ if (stat(mnt->mnt_dir, &dir_st_buf) != 0)
+ continue;
+ if (file_rdev == dir_st_buf.st_dev)
+ break;
+ }
if (check_loop_mounted(mnt->mnt_fsname,
st_buf.st_rdev, file_dev,
file_ino) == 1)
--
2.23.0

在 2021/11/30 12:04, zhanchengbin 写道:
> If there is a tmpfs with the same name as the disk, and mount before the
> disk,example:
>     /dev/sdd /root/tmp tmpfs rw,seclabel,relatime 0 0
>     /dev/sdd /root/mnt ext4 rw,seclabel,relatime 0 0
>
> Create a hard link /dev/sdd-ln for the disk and resize2fs it.The items in
> /proc/mounts are traversed, When you get to tmpfs,file!=mnt->mnt_fsname,
> Therefore, the stat(mnt->mnt_fsname, &st_buf) branch is used,However, the
> values of file_rdev and st_buf.st_rdev are the same.As a result, the system
> mistakenly considers that disk is mounted to /root/tmp.As a
> result,resize2fs
> fails.
>
> mkdir /root/tmp
> mkdir /root/mnt
> mkfs.ext4 -F -b 1024 -E "resize=10000000" /dev/sdd 32768
> mount -t tmpfs /dev/sdd /root/tmp
> mount /dev/sdd /root/mnt
> ln /dev/sdd /dev/sdd-ln
> resize2fs /dev/sdd-ln 6G
>
> Signed-off-by: zhanchengbin <[email protected]>
> Signed-off-by: [email protected]
> ---
>  lib/ext2fs/ismounted.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/lib/ext2fs/ismounted.c b/lib/ext2fs/ismounted.c
> index aee7d726..463a82a6 100644
> --- a/lib/ext2fs/ismounted.c
> +++ b/lib/ext2fs/ismounted.c
> @@ -98,6 +98,7 @@ static errcode_t check_mntent_file(const char
> *mtab_file, const char *file,
>  {
>      struct mntent     *mnt;
>      struct stat    st_buf;
> +    struct stat    dir_st_buf;
>      errcode_t    retval = 0;
>      dev_t        file_dev=0, file_rdev=0;
>      ino_t        file_ino=0;
> @@ -144,8 +145,12 @@ static errcode_t check_mntent_file(const char
> *mtab_file, const char *file,
>          if (stat(mnt->mnt_fsname, &st_buf) == 0) {
>              if (ext2fsP_is_disk_device(st_buf.st_mode)) {
>  #ifndef __GNU__
> -                if (file_rdev && (file_rdev == st_buf.st_rdev))
> -                    break;
> +                if (file_rdev && (file_rdev == st_buf.st_rdev)) {
> +                    if (stat(mnt->mnt_dir, &dir_st_buf) != 0)
> +                        continue;
> +                    if (file_rdev == dir_st_buf.st_dev)
> +                        break;
> +                }
>                  if (check_loop_mounted(mnt->mnt_fsname,
>                          st_buf.st_rdev, file_dev,
>                          file_ino) == 1)

2021-12-21 20:57:55

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH] resize2fs : resize2fs failed due to the same name of tmpfs

On Tue, Nov 30, 2021 at 12:04:48PM +0800, zhanchengbin wrote:
> If there is a tmpfs with the same name as the disk, and mount before the
> disk,example:
> /dev/sdd /root/tmp tmpfs rw,seclabel,relatime 0 0
> /dev/sdd /root/mnt ext4 rw,seclabel,relatime 0 0

This should already be fixed e2fsprogs 1.45.5+ via this commit:

commit ea4d53b7b9079fd6e2cc34cf569a993a183bfbd2
Author: Theodore Ts'o <[email protected]>
Date: Sun Nov 10 12:11:49 2019 -0500

libext2fs/ismounted.c: check device id in advance to skip false device names

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 <[email protected]>
Signed-off-by: Theodore Ts'o <[email protected]>

I've tested via tst_ismounted and I can't replicate the issue you've described.

% cd /build/e2fsprogs-maint/lib/ext2fs
% make tst_ismounted
% sudo ./tst_ismounted /dev/dm-7
Bogus entry in /proc/mounts! (/dev/dm-7 is not mounted on /root/tmp)
Device /dev/dm-7 reports flags 11
/dev/dm-7 is apparently in use.
/dev/dm-7 is mounted.
/dev/dm-7 is mounted on /root/mnt.

Cheers,

- Ted

2021-12-31 08:15:08

by zhanchengbin

[permalink] [raw]
Subject: Re: [PATCH] resize2fs : resize2fs failed due to the same name of tmpfs

To clarify, commit ea4d53b7b9079fd6e2cc34cf569a993a183bfbd2 does
solve the problem for mounting a disk with the same name as file
system "tmpfs". However, after mounting it, "ln" command (creating
a hard link) and "resize2fs" command still report an error.

example:
dev_name="/dev/sdc" (ps: a disk in you self)
mkdir /root/tmp
mkdir /root/mnt
mkfs.ext4 -F -b 1024 -E "resize=10000000" "${dev_name}" 32768
mount -t tmpfs "${dev_name}" /root/tmp
mount "${dev_name}" /root/mnt
ln "${dev_name}" "${dev_name}"-ln
resize2fs "${dev_name}"-ln 6G

You can see the disk is mounted on /root/tmp, but it should be
mounted on /root/mnt actually. I will modify the commit message
and submit the v2 patch to solve this problem.

在 2021/12/22 4:57, Theodore Ts'o 写道:
> On Tue, Nov 30, 2021 at 12:04:48PM +0800, zhanchengbin wrote:
>> If there is a tmpfs with the same name as the disk, and mount before the
>> disk,example:
>> /dev/sdd /root/tmp tmpfs rw,seclabel,relatime 0 0
>> /dev/sdd /root/mnt ext4 rw,seclabel,relatime 0 0
>
> This should already be fixed e2fsprogs 1.45.5+ via this commit:
>
> commit ea4d53b7b9079fd6e2cc34cf569a993a183bfbd2
> Author: Theodore Ts'o <[email protected]>
> Date: Sun Nov 10 12:11:49 2019 -0500
>
> libext2fs/ismounted.c: check device id in advance to skip false device names
>
> 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 <[email protected]>
> Signed-off-by: Theodore Ts'o <[email protected]>
>
> I've tested via tst_ismounted and I can't replicate the issue you've described.
>
> % cd /build/e2fsprogs-maint/lib/ext2fs
> % make tst_ismounted
> % sudo ./tst_ismounted /dev/dm-7
> Bogus entry in /proc/mounts! (/dev/dm-7 is not mounted on /root/tmp)
> Device /dev/dm-7 reports flags 11
> /dev/dm-7 is apparently in use.
> /dev/dm-7 is mounted.
> /dev/dm-7 is mounted on /root/mnt.
>
> Cheers,
>
> - Ted
> .
>