From patchwork Fri Apr 3 01:56:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Eric W. Biederman" X-Patchwork-Id: 6153261 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 95A6B9F2EC for ; Fri, 3 Apr 2015 02:01:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7ACC420379 for ; Fri, 3 Apr 2015 02:01:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 539932035E for ; Fri, 3 Apr 2015 02:01:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753412AbbDCCBu (ORCPT ); Thu, 2 Apr 2015 22:01:50 -0400 Received: from out02.mta.xmission.com ([166.70.13.232]:56133 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753372AbbDCCBo (ORCPT ); Thu, 2 Apr 2015 22:01:44 -0400 Received: from in01.mta.xmission.com ([166.70.13.51]) by out02.mta.xmission.com with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.82) (envelope-from ) id 1YdqvX-0002Cd-J0; Thu, 02 Apr 2015 20:01:43 -0600 Received: from 70-59-163-10.omah.qwest.net ([70.59.163.10] helo=x220.int.ebiederm.org) by in01.mta.xmission.com with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1Ydqv0-0003Lp-FX; Thu, 02 Apr 2015 20:01:11 -0600 From: "Eric W. Biederman" To: Linux Containers Cc: , "Serge E. Hallyn" , Andy Lutomirski , Richard Weinberger , Andrey Vagin , Al Viro , Jann Horn , Willy Tarreau , Omar Sandoval Date: Thu, 2 Apr 2015 20:56:23 -0500 Message-Id: <1428026183-14879-19-git-send-email-ebiederm@xmission.com> X-Mailer: git-send-email 2.2.1 In-Reply-To: <87a8yqou41.fsf_-_@x220.int.ebiederm.org> References: <87a8yqou41.fsf_-_@x220.int.ebiederm.org> X-XM-AID: U2FsdGVkX1+K4xcvLNLBLz5ekJ5MdkLJ+DB55RZdedQ= X-SA-Exim-Connect-IP: 70.59.163.10 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-DCC: XMission; sa04 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: *;Linux Containers X-Spam-Relay-Country: X-Spam-Timing: total 440 ms - load_scoreonly_sql: 0.03 (0.0%), signal_user_changed: 2.9 (0.7%), b_tie_ro: 2.1 (0.5%), parse: 1.02 (0.2%), extract_message_metadata: 17 (3.9%), get_uri_detail_list: 4.5 (1.0%), tests_pri_-1000: 6 (1.4%), tests_pri_-950: 1.36 (0.3%), tests_pri_-900: 1.10 (0.2%), tests_pri_-400: 35 (8.0%), check_bayes: 34 (7.8%), b_tokenize: 14 (3.1%), b_tok_get_all: 13 (2.9%), b_comp_prob: 3.0 (0.7%), b_tok_touch_all: 2.8 (0.6%), b_finish: 0.61 (0.1%), tests_pri_0: 368 (83.5%), tests_pri_500: 3.9 (0.9%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH review 19/19] vfs: Do not allow escaping from bind mounts. X-SA-Exim-Version: 4.2.1 (built Wed, 24 Sep 2014 11:00:52 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Rename can move a file or directory outside of a bind mount. This has allowed programs with paths below the renamed directory to traverse up their directory tree to the real root of the filesystem instead of just the root of their bind mount. In the presence of such renames limit applications to what the bind mount intended to reveal by marking mounts that have had dentries renamed out of them with MNT_VIOLATED, marking mounts that can no longer walk up to their parent mounts with MNT_UMOUNT_PENDING and then lazily unmounting such mounts. All moves go through __d_move so __d_move has been modified to mark all mounts whose dentries have been moved outside of them. Once the root dentry of a violated mount has been found a new function mnt_set_violated is called to: - mark all mounts that have that dentry as their root as violated - to mark all children of violated mounts that can no longer reach their parents. - to schedule for unmounting all children of violated mounts that can no longer reach their parents. The children that can't reach their parents are only scheduled for unmounting because the sleeping namespace_sem can not be taken inside of __d_move which can not sleep. This change adds a field to struct mount mnt_pending_umount that is used to thread the list of pending unmounts through struct mount. As there are small but unavioable races between scheduling an unmount and the possibility of userspace calling umount_tree, umount_tree has been modified to remove all mounts that are being unmounted from the pending_umount list. This closes a hole where it was possible in some circumstances to follow .. past the root of a bind mount. Cc: stable@vger.kernel.org Signed-off-by: "Eric W. Biederman" --- fs/dcache.c | 29 ++++++++++++++++++++++++++++ fs/internal.h | 1 + fs/mount.h | 1 + fs/namespace.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) diff --git a/fs/dcache.c b/fs/dcache.c index cae4a42c1846..e04e2a23ad00 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -2535,6 +2535,26 @@ static void dentry_unlock_for_move(struct dentry *dentry, struct dentry *target) spin_unlock(&dentry->d_lock); } +static void mark_violated_mounts(struct dentry *dentry, struct dentry *target) +{ + /* Mark all mountroots that are ancestors of dentry + * that do not share a common ancestor with target + * + * This function assumes both dentries are part of a DAG. + */ + struct dentry *p; + + for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) { + if (!d_mountroot(p)) + continue; + + if (d_ancestor(p, target)) + break; + + mnt_set_violated(p, dentry); + } +} + /* * When switching names, the actual string doesn't strictly have to * be preserved in the target - because we're dropping the target @@ -2569,6 +2589,15 @@ static void __d_move(struct dentry *dentry, struct dentry *target, BUG_ON(d_ancestor(dentry, target)); BUG_ON(d_ancestor(target, dentry)); + /* If we are not splicing a dentry, mark mounts which may have + * paths that are no longer able to follow d_parent up to + * mnt_root after this move. + */ + if (!IS_ROOT(dentry) && !IS_ROOT(target)) { + mark_violated_mounts(dentry, target); + mark_violated_mounts(target, dentry); + } + dentry_lock_for_move(dentry, target); write_seqcount_begin(&dentry->d_seq); diff --git a/fs/internal.h b/fs/internal.h index 046767f0042e..2f04050ab32f 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -71,6 +71,7 @@ extern int __mnt_want_write_file(struct file *); extern void __mnt_drop_write(struct vfsmount *); extern void __mnt_drop_write_file(struct file *); +extern void mnt_set_violated(struct dentry *root, struct dentry *moving); /* * fs_struct.c */ diff --git a/fs/mount.h b/fs/mount.h index a8be3033e022..0697b23fe417 100644 --- a/fs/mount.h +++ b/fs/mount.h @@ -41,6 +41,7 @@ struct mount { union { struct rcu_head mnt_rcu; struct llist_node mnt_llist; + struct hlist_node mnt_pending_umount; }; #ifdef CONFIG_SMP struct mnt_pcp __percpu *mnt_pcp; diff --git a/fs/namespace.c b/fs/namespace.c index 5b1b666439ac..c38d299ff26f 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -245,6 +245,7 @@ static struct mount *alloc_vfsmnt(const char *name) mnt->mnt_writers = 0; #endif + INIT_HLIST_NODE(&mnt->mnt_pending_umount); INIT_HLIST_NODE(&mnt->mnt_hash); INIT_LIST_HEAD(&mnt->mnt_child); INIT_LIST_HEAD(&mnt->mnt_mounts); @@ -1485,6 +1486,7 @@ static void umount_tree(struct mount *mnt, enum umount_tree_flags how) while (!list_empty(&tmp_list)) { bool disconnect; p = list_first_entry(&tmp_list, struct mount, mnt_list); + hlist_del_init(&mnt->mnt_pending_umount); list_del_init(&p->mnt_expire); list_del_init(&p->mnt_list); __touch_mnt_namespace(p->mnt_ns); @@ -1646,6 +1648,65 @@ out_unlock: namespace_unlock(); } +static HLIST_HEAD(pending_umount); +static void umount_pending_umounts(struct work_struct *unused) +{ + HLIST_HEAD(head); + + namespace_lock(); + lock_mount_hash(); + + hlist_move_list(&pending_umount, &head); + + while (!hlist_empty(&head)) { + struct mount *mnt = + hlist_entry(head.first, struct mount, mnt_pending_umount); + umount_tree(mnt, UMOUNT_CONNECTED); + } + + unlock_mount_hash(); + namespace_unlock(); +} + +static DECLARE_WORK(pending_umount_work, umount_pending_umounts); + +void mnt_set_violated(struct dentry *root, struct dentry *moving) +{ + struct mountroot *mr; + struct mount *mnt; + + lock_mount_hash(); + mr = lookup_mountroot(root); + if (!mr) + goto out; + + hlist_for_each_entry(mnt, &mr->r_list, mnt_mr_list) { + struct mount *child; + /* Be wary of this mount */ + mnt->mnt.mnt_flags |= MNT_VIOLATED; + + list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { + /* Ignore children that will continue to be connected */ + if ((child->mnt_mountpoint != moving) && + !d_ancestor(moving, child->mnt_mountpoint)) + continue; + + /* Deal with mounts loosing the connection to + * their parents + */ + if (!(child->mnt.mnt_flags & MNT_UMOUNT)) { + child->mnt.mnt_flags |= MNT_UNREACHABLE_PARENT; + hlist_add_head(&child->mnt_pending_umount, &pending_umount); + schedule_work(&pending_umount_work); + } else { + umount_mnt(child); + } + } + } +out: + unlock_mount_hash(); +} + /* * Is the caller allowed to modify his namespace? */