--- /home/linux/views/linux-2.6.12-rc4/fs/namespace.c 2005-05-06 23:22:29.000000000 -0700 +++ linux-2.6.12-rc4/fs/namespace.c 2005-05-22 13:22:46.000000000 -0700 @@ -616,11 +616,14 @@ out_unlock: } /* - * do loopback mount. + * do loopback mount. The loopback mount can be done from any namespace + * to any other namespace including the current namespace, as long as + * the task acquired rights to manipulate them. */ static int do_loopback(struct nameidata *nd, char *old_name, int recurse) { struct nameidata old_nd; + struct namespace *mntpt_ns, *old_ns; struct vfsmount *mnt = NULL; int err = mount_is_safe(nd); if (err) @@ -631,15 +634,54 @@ static int do_loopback(struct nameidata if (err) return err; - down_write(¤t->namespace->sem); err = -EINVAL; - if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) { - err = -ENOMEM; - if (recurse) - mnt = copy_tree(old_nd.mnt, old_nd.dentry); - else - mnt = clone_mnt(old_nd.mnt, old_nd.dentry); + spin_lock(&vfsmount_lock); + + /* + * Allow all kinds of binds and rbinds except + * recursive bind to a detached mount + */ + mntpt_ns = nd->mnt->mnt_namespace; + old_ns = old_nd.mnt->mnt_namespace; + if (!mntpt_ns || !mntpt_ns->root || + (recurse && (!old_ns || !old_ns->root))) { + spin_unlock(&vfsmount_lock); + goto out; } + get_namespace(mntpt_ns); + if (recurse && old_ns != mntpt_ns) + get_namespace(old_ns); + + spin_unlock(&vfsmount_lock); + + /* + * make sure we don't race with some other thread manipulating the + * namespaces. + */ + if (recurse && (old_ns < mntpt_ns)) + down_write(&old_ns->sem); + down_write(&mntpt_ns->sem); + if (recurse && (old_ns > mntpt_ns)) + down_write(&old_ns->sem); + + + /* + * well... mounts might have detached while we acquired + * the semaphores. Revalidate that the destination mount + * is still attached. + */ + if (!nd->mnt->mnt_namespace) + goto error_out; + + /* + * and if the source mount is detached, than just do + * a bind, instead of a rbind + */ + err = -ENOMEM; + if (recurse && old_nd.mnt->mnt_namespace) + mnt = copy_tree(old_nd.mnt, old_nd.dentry); + else + mnt = clone_mnt(old_nd.mnt, old_nd.dentry); if (mnt) { /* stop bind mounts from expiring */ @@ -656,7 +698,15 @@ static int do_loopback(struct nameidata mntput(mnt); } - up_write(¤t->namespace->sem); +error_out: + up_write(&mntpt_ns->sem); + if (recurse && (old_ns != mntpt_ns)) { + up_write(&old_ns->sem); + put_namespace(old_ns); + } + put_namespace(mntpt_ns); + +out: path_release(&old_nd); return err; }