Skip to content

Commit 5f31c54

Browse files
author
Al Viro
committed
path_overmount(): avoid false negatives
Holding namespace_sem is enough to make sure that result remains valid. It is *not* enough to avoid false negatives from __lookup_mnt(). Mounts can be unhashed outside of namespace_sem (stuck children getting detached on final mntput() of lazy-umounted mount) and having an unrelated mount removed from the hash chain while we traverse it may end up with false negative from __lookup_mnt(). We need to sample and recheck the seqlock component of mount_lock... Bug predates the introduction of path_overmount() - it had come from the code in finish_automount() that got abstracted into that helper. Reviewed-by: Christian Brauner <brauner@kernel.org> Fixes: 26df603 ("fix automount/automount race properly") Fixes: 6ac3928 ("fs: allow to mount beneath top mount") Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 1f282cd commit 5f31c54

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

fs/namespace.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,18 +3478,25 @@ static int do_set_group(struct path *from_path, struct path *to_path)
34783478
* Check if path is overmounted, i.e., if there's a mount on top of
34793479
* @path->mnt with @path->dentry as mountpoint.
34803480
*
3481-
* Context: This function expects namespace_lock() to be held.
3481+
* Context: namespace_sem must be held at least shared.
3482+
* MUST NOT be called under lock_mount_hash() (there one should just
3483+
* call __lookup_mnt() and check if it returns NULL).
34823484
* Return: If path is overmounted true is returned, false if not.
34833485
*/
34843486
static inline bool path_overmounted(const struct path *path)
34853487
{
3488+
unsigned seq = read_seqbegin(&mount_lock);
3489+
bool no_child;
3490+
34863491
rcu_read_lock();
3487-
if (unlikely(__lookup_mnt(path->mnt, path->dentry))) {
3488-
rcu_read_unlock();
3489-
return true;
3490-
}
3492+
no_child = !__lookup_mnt(path->mnt, path->dentry);
34913493
rcu_read_unlock();
3492-
return false;
3494+
if (need_seqretry(&mount_lock, seq)) {
3495+
read_seqlock_excl(&mount_lock);
3496+
no_child = !__lookup_mnt(path->mnt, path->dentry);
3497+
read_sequnlock_excl(&mount_lock);
3498+
}
3499+
return unlikely(!no_child);
34933500
}
34943501

34953502
/**

0 commit comments

Comments
 (0)