summaryrefslogtreecommitdiffstats
path: root/fs/namei.c
diff options
context:
space:
mode:
authorEric W. Biederman <ebiederman@twitter.com>2013-10-01 21:33:48 -0400
committerAl Viro <viro@zeniv.linux.org.uk>2014-10-09 02:38:56 -0400
commit8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe (patch)
treeb20ff83dba79142efd005327dee38289f5c5b5cc /fs/namei.c
parent80b5dce8c59b0de1ed6e403b8298e02dcb4db64b (diff)
vfs: Lazily remove mounts on unlinked files and directories.
With the introduction of mount namespaces and bind mounts it became possible to access files and directories that on some paths are mount points but are not mount points on other paths. It is very confusing when rm -rf somedir returns -EBUSY simply because somedir is mounted somewhere else. With the addition of user namespaces allowing unprivileged mounts this condition has gone from annoying to allowing a DOS attack on other users in the system. The possibility for mischief is removed by updating the vfs to support rename, unlink and rmdir on a dentry that is a mountpoint and by lazily unmounting mountpoints on deleted dentries. In particular this change allows rename, unlink and rmdir system calls on a dentry without a mountpoint in the current mount namespace to succeed, and it allows rename, unlink, and rmdir performed on a distributed filesystem to update the vfs cache even if when there is a mount in some namespace on the original dentry. There are two common patterns of maintaining mounts: Mounts on trusted paths with the parent directory of the mount point and all ancestory directories up to / owned by root and modifiable only by root (i.e. /media/xxx, /dev, /dev/pts, /proc, /sys, /sys/fs/cgroup/{cpu, cpuacct, ...}, /usr, /usr/local). Mounts on unprivileged directories maintained by fusermount. In the case of mounts in trusted directories owned by root and modifiable only by root the current parent directory permissions are sufficient to ensure a mount point on a trusted path is not removed or renamed by anyone other than root, even if there is a context where the there are no mount points to prevent this. In the case of mounts in directories owned by less privileged users races with users modifying the path of a mount point are already a danger. fusermount already uses a combination of chdir, /proc/<pid>/fd/NNN, and UMOUNT_NOFOLLOW to prevent these races. The removable of global rename, unlink, and rmdir protection really adds nothing new to consider only a widening of the attack window, and fusermount is already safe against unprivileged users modifying the directory simultaneously. In principle for perfect userspace programs returning -EBUSY for unlink, rmdir, and rename of dentires that have mounts in the local namespace is actually unnecessary. Unfortunately not all userspace programs are perfect so retaining -EBUSY for unlink, rmdir and rename of dentries that have mounts in the current mount namespace plays an important role of maintaining consistency with historical behavior and making imperfect userspace applications hard to exploit. v2: Remove spurious old_dentry. v3: Optimized shrink_submounts_and_drop Removed unsued afs label v4: Simplified the changes to check_submounts_and_drop Do not rename check_submounts_and_drop shrink_submounts_and_drop Document what why we need atomicity in check_submounts_and_drop Rely on the parent inode mutex to make d_revalidate and d_invalidate an atomic unit. v5: Refcount the mountpoint to detach in case of simultaneous renames. Reviewed-by: Miklos Szeredi <miklos@szeredi.hu> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs/namei.c')
-rw-r--r--fs/namei.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/fs/namei.c b/fs/namei.c
index a3a14b033b0d..2ba10904dba0 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -3567,8 +3567,6 @@ int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3567 error = -EBUSY; 3567 error = -EBUSY;
3568 if (is_local_mountpoint(dentry)) 3568 if (is_local_mountpoint(dentry))
3569 goto out; 3569 goto out;
3570 if (d_mountpoint(dentry))
3571 goto out;
3572 3570
3573 error = security_inode_rmdir(dir, dentry); 3571 error = security_inode_rmdir(dir, dentry);
3574 if (error) 3572 if (error)
@@ -3581,6 +3579,7 @@ int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3581 3579
3582 dentry->d_inode->i_flags |= S_DEAD; 3580 dentry->d_inode->i_flags |= S_DEAD;
3583 dont_mount(dentry); 3581 dont_mount(dentry);
3582 detach_mounts(dentry);
3584 3583
3585out: 3584out:
3586 mutex_unlock(&dentry->d_inode->i_mutex); 3585 mutex_unlock(&dentry->d_inode->i_mutex);
@@ -3683,7 +3682,7 @@ int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegate
3683 return -EPERM; 3682 return -EPERM;
3684 3683
3685 mutex_lock(&target->i_mutex); 3684 mutex_lock(&target->i_mutex);
3686 if (is_local_mountpoint(dentry) || d_mountpoint(dentry)) 3685 if (is_local_mountpoint(dentry))
3687 error = -EBUSY; 3686 error = -EBUSY;
3688 else { 3687 else {
3689 error = security_inode_unlink(dir, dentry); 3688 error = security_inode_unlink(dir, dentry);
@@ -3692,8 +3691,10 @@ int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegate
3692 if (error) 3691 if (error)
3693 goto out; 3692 goto out;
3694 error = dir->i_op->unlink(dir, dentry); 3693 error = dir->i_op->unlink(dir, dentry);
3695 if (!error) 3694 if (!error) {
3696 dont_mount(dentry); 3695 dont_mount(dentry);
3696 detach_mounts(dentry);
3697 }
3697 } 3698 }
3698 } 3699 }
3699out: 3700out:
@@ -4130,8 +4131,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4130 error = -EBUSY; 4131 error = -EBUSY;
4131 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) 4132 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4132 goto out; 4133 goto out;
4133 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4134 goto out;
4135 4134
4136 if (max_links && new_dir != old_dir) { 4135 if (max_links && new_dir != old_dir) {
4137 error = -EMLINK; 4136 error = -EMLINK;
@@ -4168,6 +4167,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4168 if (is_dir) 4167 if (is_dir)
4169 target->i_flags |= S_DEAD; 4168 target->i_flags |= S_DEAD;
4170 dont_mount(new_dentry); 4169 dont_mount(new_dentry);
4170 detach_mounts(new_dentry);
4171 } 4171 }
4172 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { 4172 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4173 if (!(flags & RENAME_EXCHANGE)) 4173 if (!(flags & RENAME_EXCHANGE))