From patchwork Fri Apr 3 01:56:21 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: 6153231 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 E89199F2EC for ; Fri, 3 Apr 2015 02:01:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DD31120379 for ; Fri, 3 Apr 2015 02:01:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B44AF2035E for ; Fri, 3 Apr 2015 02:01:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753367AbbDCCBo (ORCPT ); Thu, 2 Apr 2015 22:01:44 -0400 Received: from out02.mta.xmission.com ([166.70.13.232]:56096 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751308AbbDCCBm (ORCPT ); Thu, 2 Apr 2015 22:01:42 -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 1YdqvW-0002Br-5B; Thu, 02 Apr 2015 20:01:42 -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 1Ydquv-0003Lp-6Q; Thu, 02 Apr 2015 20:01:05 -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:21 -0500 Message-Id: <1428026183-14879-17-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: U2FsdGVkX19pG8gpSF3kVCL5hT2oajejSowkuk1JQOA= 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-Level: 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; sa05 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Linux Containers X-Spam-Relay-Country: X-Spam-Timing: total 391 ms - load_scoreonly_sql: 0.10 (0.0%), signal_user_changed: 6 (1.4%), b_tie_ro: 3.9 (1.0%), parse: 1.69 (0.4%), extract_message_metadata: 15 (4.0%), get_uri_detail_list: 4.8 (1.2%), tests_pri_-1000: 5 (1.3%), tests_pri_-950: 1.11 (0.3%), tests_pri_-900: 0.91 (0.2%), tests_pri_-400: 32 (8.1%), check_bayes: 30 (7.7%), b_tokenize: 10 (2.6%), b_tok_get_all: 10 (2.5%), b_comp_prob: 3.2 (0.8%), b_tok_touch_all: 4.0 (1.0%), b_finish: 1.05 (0.3%), tests_pri_0: 317 (81.0%), tests_pri_500: 9 (2.2%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH review 17/19] vfs: Test for and handle paths that are unreachable from their mnt_root 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 - Add a mount flag MNT_VIOLATED to mark loopback mounts that have had a dentry moved into a directory that does not descend from the mount root dentry. - Add a function path_connected to verify a path.dentry is reachable from path.mnt.mnt_root. AKA rename did not do something nasty to the bind mount. - Disable ".." when a path is not connected during lookup. (Maybe we want to stop ".." at this path instead?) Following .. is not disabled after a transition to / and is never disabled when / is the directory we start with. Because we already limit .. no higher than / - In prepend_path and it's callers in the d_path family for a path that is not connected don't attempt to find parent directories. Cc: stable@vger.kernel.org Signed-off-by: "Eric W. Biederman" --- fs/dcache.c | 3 +++ fs/internal.h | 1 + fs/namei.c | 30 ++++++++++++++++++++++++++++++ include/linux/mount.h | 1 + include/linux/namei.h | 2 ++ 5 files changed, 37 insertions(+) diff --git a/fs/dcache.c b/fs/dcache.c index c71e3732e53b..e07eb03f6de6 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -2871,6 +2871,9 @@ static int prepend_path(const struct path *path, char *bptr; int blen; + if (!path_connected(path)) + root = path; + rcu_read_lock(); restart_mnt: read_seqbegin_or_lock(&mount_lock, &m_seq); diff --git a/fs/internal.h b/fs/internal.h index 01dce1d1476b..046767f0042e 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -51,6 +51,7 @@ extern void __init chrdev_init(void); extern int user_path_mountpoint_at(int, const char __user *, unsigned int, struct path *); extern int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *, unsigned int, struct path *); +extern bool path_connected(const struct path *); /* * namespace.c diff --git a/fs/namei.c b/fs/namei.c index c83145af4bfc..ec09c90089a9 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -493,6 +493,22 @@ void path_put(const struct path *path) } EXPORT_SYMBOL(path_put); +/** + * path_connected - Verify that a path->dentry is below path->mnt->mnt.mnt_root + * @path: path to verify + * + * Rename can sometimes move a file or directory outside of bind mount + * don't honor paths where this has happened. + */ +bool path_connected(const struct path *path) +{ + struct vfsmount *mnt = path->mnt; + if (!(mnt->mnt_flags & MNT_VIOLATED)) + return true; + + return is_subdir(path->dentry, mnt->mnt_root); +} + struct nameidata { struct path path; struct qstr last; @@ -712,6 +728,7 @@ void nd_jump_link(struct nameidata *nd, struct path *path) nd->path = *path; nd->inode = nd->path.dentry->d_inode; nd->flags |= LOOKUP_JUMPED; + nd->flags &= ~LOOKUP_NODOTDOT; } void nd_set_link(struct nameidata *nd, char *path) @@ -897,6 +914,7 @@ follow_link(struct path *link, struct nameidata *nd, void **p) nd->path = nd->root; path_get(&nd->root); nd->flags |= LOOKUP_JUMPED; + nd->flags &= ~LOOKUP_NODOTDOT; } nd->inode = nd->path.dentry->d_inode; error = link_path_walk(s, nd); @@ -1161,6 +1179,7 @@ static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, path->mnt = &mounted->mnt; path->dentry = mounted->mnt.mnt_root; nd->flags |= LOOKUP_JUMPED; + nd->flags &= ~LOOKUP_NODOTDOT; nd->seq = read_seqcount_begin(&path->dentry->d_seq); /* * Update the inode too. We don't need to re-check the @@ -1176,6 +1195,10 @@ static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, static int follow_dotdot_rcu(struct nameidata *nd) { struct inode *inode = nd->inode; + + if (nd->flags & LOOKUP_NODOTDOT) + return 0; + if (!nd->root.mnt) set_root_rcu(nd); @@ -1293,6 +1316,9 @@ static void follow_mount(struct path *path) static void follow_dotdot(struct nameidata *nd) { + if (nd->flags & LOOKUP_NODOTDOT) + return; + if (!nd->root.mnt) set_root(nd); @@ -1909,6 +1935,8 @@ static int path_init(int dfd, const char *name, unsigned int flags, } else { get_fs_pwd(current->fs, &nd->path); } + if (unlikely(!path_connected(&nd->path))) + nd->flags |= LOOKUP_NODOTDOT; } else { /* Caller must check execute permissions on the starting path component */ struct fd f = fdget_raw(dfd); @@ -1936,6 +1964,8 @@ static int path_init(int dfd, const char *name, unsigned int flags, path_get(&nd->path); fdput(f); } + if (unlikely(!path_connected(&nd->path))) + nd->flags |= LOOKUP_NODOTDOT; } nd->inode = nd->path.dentry->d_inode; diff --git a/include/linux/mount.h b/include/linux/mount.h index 564beeec5d83..778d7cf65b9a 100644 --- a/include/linux/mount.h +++ b/include/linux/mount.h @@ -62,6 +62,7 @@ struct mnt_namespace; #define MNT_SYNC_UMOUNT 0x2000000 #define MNT_MARKED 0x4000000 #define MNT_UMOUNT 0x8000000 +#define MNT_VIOLATED 0x10000000 struct vfsmount { struct dentry *mnt_root; /* root of the mounted tree */ diff --git a/include/linux/namei.h b/include/linux/namei.h index c8990779f0c3..55c8aaec7b03 100644 --- a/include/linux/namei.h +++ b/include/linux/namei.h @@ -45,6 +45,8 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND}; #define LOOKUP_ROOT 0x2000 #define LOOKUP_EMPTY 0x4000 +#define LOOKUP_NODOTDOT 0x10000 + extern int user_path_at(int, const char __user *, unsigned, struct path *); extern int user_path_at_empty(int, const char __user *, unsigned, struct path *, int *empty);