From patchwork Mon Aug 3 21:30:22 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: 6933311 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C42F2C05AC for ; Mon, 3 Aug 2015 21:37:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9938B20636 for ; Mon, 3 Aug 2015 21:37:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 90FA820634 for ; Mon, 3 Aug 2015 21:37:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755246AbbHCVhG (ORCPT ); Mon, 3 Aug 2015 17:37:06 -0400 Received: from out03.mta.xmission.com ([166.70.13.233]:55849 "EHLO out03.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754875AbbHCVhF (ORCPT ); Mon, 3 Aug 2015 17:37:05 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out03.mta.xmission.com with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.82) (envelope-from ) id 1ZMNPs-0007OC-AH; Mon, 03 Aug 2015 15:37:04 -0600 Received: from 97-119-22-40.omah.qwest.net ([97.119.22.40] helo=x220.int.ebiederm.org.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.82) (envelope-from ) id 1ZMNPq-0005vk-TN; Mon, 03 Aug 2015 15:37:04 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: Linux Containers Cc: , Al Viro , Andy Lutomirski , "Serge E. Hallyn" , Richard Weinberger , Andrey Vagin , Jann Horn , Willy Tarreau , Omar Sandoval , Miklos Szeredi , Linus Torvalds , "J. Bruce Fields" References: <871tncuaf6.fsf@x220.int.ebiederm.org> <87mw5xq7lt.fsf@x220.int.ebiederm.org> <87a8yqou41.fsf_-_@x220.int.ebiederm.org> <874moq9oyb.fsf_-_@x220.int.ebiederm.org> <871tfkawu9.fsf_-_@x220.int.ebiederm.org> Date: Mon, 03 Aug 2015 16:30:22 -0500 In-Reply-To: <871tfkawu9.fsf_-_@x220.int.ebiederm.org> (Eric W. Biederman's message of "Mon, 03 Aug 2015 16:25:18 -0500") Message-ID: <878u9s9i1d.fsf_-_@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 X-XM-AID: U2FsdGVkX18enW7B3BIRBjDETf/hFKRLbV3aqQ7ozu4= X-SA-Exim-Connect-IP: 97.119.22.40 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=-7.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-DCC: XMission; sa02 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Linux Containers X-Spam-Relay-Country: X-Spam-Timing: total 798 ms - load_scoreonly_sql: 0.07 (0.0%), signal_user_changed: 5.0 (0.6%), b_tie_ro: 3.7 (0.5%), parse: 1.39 (0.2%), extract_message_metadata: 26 (3.3%), get_uri_detail_list: 4.1 (0.5%), tests_pri_-1000: 11 (1.4%), tests_pri_-950: 2.2 (0.3%), tests_pri_-900: 1.82 (0.2%), tests_pri_-400: 43 (5.3%), check_bayes: 40 (5.1%), b_tokenize: 17 (2.1%), b_tok_get_all: 10 (1.2%), b_comp_prob: 5 (0.6%), b_tok_touch_all: 3.8 (0.5%), b_finish: 0.93 (0.1%), tests_pri_0: 687 (86.1%), tests_pri_500: 15 (1.8%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH review 5/6] 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 in02.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 In rare cases a directory can be renamed out from under a bind mount. In those cases without special handling it becomes possible to walk up the directory tree to the root dentry of the filesystem and down from the root dentry to every other file or directory on the filesystem. Like division by zero .. from an unconnected path can not be given a useful semantic as there is no predicting at which path component the code will realize it is unconnected. We certainly can not match the current behavior as the current behavior is a security hole. Therefore when encounting .. when following an unconnected path return -ENOENT. - Add a function path_connected to verify nd->path.dentry is reachable from nd->path.mnt.mnt_root. AKA to validate that rename did not do something nasty to the bind mount. To avoid races path_connected must be called after following a path component to it's next path component. Signed-off-by: "Eric W. Biederman" --- fs/namei.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index ae4e4c18b2ac..bccd3810ff60 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -560,6 +560,27 @@ static int __nd_alloc_stack(struct nameidata *nd) return 0; } +/** + * path_connected - Verify that a nd->path.dentry is below nd->path.mnt->mnt.mnt_root + * @nd: nameidate to verify + * + * Rename can sometimes move a file or directory outside of a bind + * mount, path_connected allows those cases to be detected. + */ +static bool path_connected(struct nameidata *nd) +{ + struct vfsmount *mnt = nd->path.mnt; + unsigned escape_count = read_mnt_escape_count(mnt); + + if (likely(escape_count == 0)) + return true; + + if (!is_subdir(nd->path.dentry, mnt->mnt_root)) + return false; + + return true; +} + static inline int nd_alloc_stack(struct nameidata *nd) { if (likely(nd->depth != EMBEDDED_LEVELS)) @@ -1294,6 +1315,8 @@ static int follow_dotdot_rcu(struct nameidata *nd) seq = read_seqcount_begin(&parent->d_seq); if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) return -ECHILD; + if (unlikely(!path_connected(nd))) + return -ENOENT; nd->path.dentry = parent; nd->seq = seq; break; @@ -1396,7 +1419,7 @@ static void follow_mount(struct path *path) } } -static void follow_dotdot(struct nameidata *nd) +static int follow_dotdot(struct nameidata *nd) { if (!nd->root.mnt) set_root(nd); @@ -1410,7 +1433,12 @@ static void follow_dotdot(struct nameidata *nd) } if (nd->path.dentry != nd->path.mnt->mnt_root) { /* rare case of legitimate dget_parent()... */ - nd->path.dentry = dget_parent(nd->path.dentry); + struct dentry *parent = dget_parent(nd->path.dentry); + if (unlikely(!path_connected(nd))) { + dput(parent); + return -ENOENT; + } + nd->path.dentry = parent; dput(old); break; } @@ -1419,6 +1447,7 @@ static void follow_dotdot(struct nameidata *nd) } follow_mount(&nd->path); nd->inode = nd->path.dentry->d_inode; + return 0; } /* @@ -1634,7 +1663,7 @@ static inline int handle_dots(struct nameidata *nd, int type) if (nd->flags & LOOKUP_RCU) { return follow_dotdot_rcu(nd); } else - follow_dotdot(nd); + return follow_dotdot(nd); } return 0; }