Skip to content

Commit 0d039ea

Browse files
Al Virobrauner
authored andcommitted
fix a couple of races in MNT_TREE_BENEATH handling by do_move_mount()
Normally do_lock_mount(path, _) is locking a mountpoint pinned by *path and at the time when matching unlock_mount() unlocks that location it is still pinned by the same thing. Unfortunately, for 'beneath' case it's no longer that simple - the object being locked is not the one *path points to. It's the mountpoint of path->mnt. The thing is, without sufficient locking ->mnt_parent may change under us and none of the locks are held at that point. The rules are * mount_lock stabilizes m->mnt_parent for any mount m. * namespace_sem stabilizes m->mnt_parent, provided that m is mounted. * if either of the above holds and refcount of m is positive, we are guaranteed the same for refcount of m->mnt_parent. namespace_sem nests inside inode_lock(), so do_lock_mount() has to take inode_lock() before grabbing namespace_sem. It does recheck that path->mnt is still mounted in the same place after getting namespace_sem, and it does take care to pin the dentry. It is needed, since otherwise we might end up with racing mount --move (or umount) happening while we were getting locks; in that case dentry would no longer be a mountpoint and could've been evicted on memory pressure along with its inode - not something you want when grabbing lock on that inode. However, pinning a dentry is not enough - the matching mount is also pinned only by the fact that path->mnt is mounted on top it and at that point we are not holding any locks whatsoever, so the same kind of races could end up with all references to that mount gone just as we are about to enter inode_lock(). If that happens, we are left with filesystem being shut down while we are holding a dentry reference on it; results are not pretty. What we need to do is grab both dentry and mount at the same time; that makes inode_lock() safe *and* avoids the problem with fs getting shut down under us. After taking namespace_sem we verify that path->mnt is still mounted (which stabilizes its ->mnt_parent) and check that it's still mounted at the same place. From that point on to the matching namespace_unlock() we are guaranteed that mount/dentry pair we'd grabbed are also pinned by being the mountpoint of path->mnt, so we can quietly drop both the dentry reference (as the current code does) and mnt one - it's OK to do under namespace_sem, since we are not dropping the final refs. That solves the problem on do_lock_mount() side; unlock_mount() also has one, since dentry is guaranteed to stay pinned only until the namespace_unlock(). That's easy to fix - just have inode_unlock() done earlier, while it's still pinned by mp->m_dentry. Fixes: 6ac3928 "fs: allow to mount beneath top mount" # v6.5+ Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent d1f7256 commit 0d039ea

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

fs/namespace.c

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,56 +2826,62 @@ static struct mountpoint *do_lock_mount(struct path *path, bool beneath)
28262826
struct vfsmount *mnt = path->mnt;
28272827
struct dentry *dentry;
28282828
struct mountpoint *mp = ERR_PTR(-ENOENT);
2829+
struct path under = {};
28292830

28302831
for (;;) {
2831-
struct mount *m;
2832+
struct mount *m = real_mount(mnt);
28322833

28332834
if (beneath) {
2834-
m = real_mount(mnt);
2835+
path_put(&under);
28352836
read_seqlock_excl(&mount_lock);
2836-
dentry = dget(m->mnt_mountpoint);
2837+
under.mnt = mntget(&m->mnt_parent->mnt);
2838+
under.dentry = dget(m->mnt_mountpoint);
28372839
read_sequnlock_excl(&mount_lock);
2840+
dentry = under.dentry;
28382841
} else {
28392842
dentry = path->dentry;
28402843
}
28412844

28422845
inode_lock(dentry->d_inode);
2843-
if (unlikely(cant_mount(dentry))) {
2844-
inode_unlock(dentry->d_inode);
2845-
goto out;
2846-
}
2847-
28482846
namespace_lock();
28492847

2850-
if (beneath && (!is_mounted(mnt) || m->mnt_mountpoint != dentry)) {
2848+
if (unlikely(cant_mount(dentry) || !is_mounted(mnt)))
2849+
break; // not to be mounted on
2850+
2851+
if (beneath && unlikely(m->mnt_mountpoint != dentry ||
2852+
&m->mnt_parent->mnt != under.mnt)) {
28512853
namespace_unlock();
28522854
inode_unlock(dentry->d_inode);
2853-
goto out;
2855+
continue; // got moved
28542856
}
28552857

28562858
mnt = lookup_mnt(path);
2857-
if (likely(!mnt))
2859+
if (unlikely(mnt)) {
2860+
namespace_unlock();
2861+
inode_unlock(dentry->d_inode);
2862+
path_put(path);
2863+
path->mnt = mnt;
2864+
path->dentry = dget(mnt->mnt_root);
2865+
continue; // got overmounted
2866+
}
2867+
mp = get_mountpoint(dentry);
2868+
if (IS_ERR(mp))
28582869
break;
2859-
2860-
namespace_unlock();
2861-
inode_unlock(dentry->d_inode);
2862-
if (beneath)
2863-
dput(dentry);
2864-
path_put(path);
2865-
path->mnt = mnt;
2866-
path->dentry = dget(mnt->mnt_root);
2867-
}
2868-
2869-
mp = get_mountpoint(dentry);
2870-
if (IS_ERR(mp)) {
2871-
namespace_unlock();
2872-
inode_unlock(dentry->d_inode);
2870+
if (beneath) {
2871+
/*
2872+
* @under duplicates the references that will stay
2873+
* at least until namespace_unlock(), so the path_put()
2874+
* below is safe (and OK to do under namespace_lock -
2875+
* we are not dropping the final references here).
2876+
*/
2877+
path_put(&under);
2878+
}
2879+
return mp;
28732880
}
2874-
2875-
out:
2881+
namespace_unlock();
2882+
inode_unlock(dentry->d_inode);
28762883
if (beneath)
2877-
dput(dentry);
2878-
2884+
path_put(&under);
28792885
return mp;
28802886
}
28812887

@@ -2886,14 +2892,11 @@ static inline struct mountpoint *lock_mount(struct path *path)
28862892

28872893
static void unlock_mount(struct mountpoint *where)
28882894
{
2889-
struct dentry *dentry = where->m_dentry;
2890-
2895+
inode_unlock(where->m_dentry->d_inode);
28912896
read_seqlock_excl(&mount_lock);
28922897
put_mountpoint(where);
28932898
read_sequnlock_excl(&mount_lock);
2894-
28952899
namespace_unlock();
2896-
inode_unlock(dentry->d_inode);
28972900
}
28982901

28992902
static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)

0 commit comments

Comments
 (0)