Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3961221yba; Tue, 7 May 2019 09:45:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZf4MBmMPPJT7WMmn/lSeTsu757rHUB6CzjOm9vvA+slihqjYWYoZJ+8WHqx67S2UI5idO X-Received: by 2002:a17:902:7207:: with SMTP id ba7mr39423087plb.329.1557247523074; Tue, 07 May 2019 09:45:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557247523; cv=none; d=google.com; s=arc-20160816; b=mBLWbumMYQl75Y7Ou6v+QfAeDQsXIjTIcHTeD1kWN/Luc7BYhqcBFrAbsh9VjEq0DI IlvwFzth7Lvl+zPrLQXvlERbHGu6nIyLjayfufFNZCN3tOUoTUIh5wlT9mw5ZcE86vKm 8jMA4b+q+Q7lB943YGE1zlA876vfWZfoOO2lyzC24G/eHrCWOyuqw+gUxGXdAuBmRIIt Rlx8rqCX8gI7z0xq04conqKTwnndnFHIQVLPHC2gB9ggxKr1kpHG51ylRF1U+Rx0/Ev5 /tpnpiW2wH7wmPXWiNmRnPNzWKT3EEwdjCZRSUxGvHxSx4R8tprrWHzrISgy2QXj43Sf HQbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Uutpa5iYjDdUUs9w2ilK5NY9VVFMchccLrP7mpx+hOE=; b=HnuCle5Mh+NeINiy3GR0xE7lQBy1Ez4Oo+x0Gt2fu29akf7az05EjOrVRRY5HZuIZI 1cjqZMyieim/biUsOueiIzwXuXu6f6O2eL9JuD4wtwIzp/3uOVyuCA9usPWIvHjqI9AR zASfGZb0oJI+PNO6ppDYdJBHTPe2AmewYmY2jsgccyC94RhRSVBiiX6vN4DYh8q0qH22 1u/IufRRVWzckfWE0J6rgYi6R4YbUJLod1EKAugf3C7vGfsJQWFcl2/o/5mMNlp4nbKE Syi6LbpqdZkQ8qD4BMGDqP0uLgHqgxhuhTU1wWuCMWTt9P1risjIV8YFjge6GDwk+XpO 0+Iw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b21si21679421pff.35.2019.05.07.09.45.06; Tue, 07 May 2019 09:45:23 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727183AbfEGQoE (ORCPT + 99 others); Tue, 7 May 2019 12:44:04 -0400 Received: from mx2.mailbox.org ([80.241.60.215]:34230 "EHLO mx2.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726476AbfEGQoE (ORCPT ); Tue, 7 May 2019 12:44:04 -0400 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 3563CA1163; Tue, 7 May 2019 18:44:00 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id UnBhOpM0Ktiv; Tue, 7 May 2019 18:43:49 +0200 (CEST) From: Aleksa Sarai To: Al Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , David Howells Cc: Aleksa Sarai , Eric Biederman , Andy Lutomirski , Andrew Morton , Alexei Starovoitov , Kees Cook , Jann Horn , Christian Brauner , Tycho Andersen , David Drysdale , Chanho Min , Oleg Nesterov , Aleksa Sarai , Linus Torvalds , containers@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Subject: [PATCH v7 1/5] namei: split out nd->dfd handling to dirfd_path_init Date: Wed, 8 May 2019 02:43:13 +1000 Message-Id: <20190507164317.13562-2-cyphar@cyphar.com> In-Reply-To: <20190507164317.13562-1-cyphar@cyphar.com> References: <20190507164317.13562-1-cyphar@cyphar.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Previously, path_init's handling of *at(dfd, ...) was only done once, but with O_BENEATH (and O_THISROOT) we have to parse the initial nd->path at different times (before or after absolute path handling) depending on whether we have been asked to scope resolution within a root. Signed-off-by: Aleksa Sarai --- fs/namei.c | 103 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 44 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 5ebd64b21970..2a91b72aa5e9 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -2166,9 +2166,59 @@ static int link_path_walk(const char *name, struct nameidata *nd) } } +/* + * Configure nd->path based on the nd->dfd. This is only used as part of + * path_init(). + */ +static inline int dirfd_path_init(struct nameidata *nd) +{ + if (nd->dfd == AT_FDCWD) { + if (nd->flags & LOOKUP_RCU) { + struct fs_struct *fs = current->fs; + unsigned seq; + + do { + seq = read_seqcount_begin(&fs->seq); + nd->path = fs->pwd; + nd->inode = nd->path.dentry->d_inode; + nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); + } while (read_seqcount_retry(&fs->seq, seq)); + } else { + get_fs_pwd(current->fs, &nd->path); + nd->inode = nd->path.dentry->d_inode; + } + } else { + /* Caller must check execute permissions on the starting path component */ + struct fd f = fdget_raw(nd->dfd); + struct dentry *dentry; + + if (!f.file) + return -EBADF; + + dentry = f.file->f_path.dentry; + + if (*nd->name->name && unlikely(!d_can_lookup(dentry))) { + fdput(f); + return -ENOTDIR; + } + + nd->path = f.file->f_path; + if (nd->flags & LOOKUP_RCU) { + nd->inode = nd->path.dentry->d_inode; + nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); + } else { + path_get(&nd->path); + nd->inode = nd->path.dentry->d_inode; + } + fdput(f); + } + return 0; +} + /* must be paired with terminate_walk() */ static const char *path_init(struct nameidata *nd, unsigned flags) { + int error; const char *s = nd->name->name; if (!*s) @@ -2202,52 +2252,17 @@ static const char *path_init(struct nameidata *nd, unsigned flags) nd->m_seq = read_seqbegin(&mount_lock); if (*s == '/') { - set_root(nd); - if (likely(!nd_jump_root(nd))) - return s; - return ERR_PTR(-ECHILD); - } else if (nd->dfd == AT_FDCWD) { - if (flags & LOOKUP_RCU) { - struct fs_struct *fs = current->fs; - unsigned seq; - - do { - seq = read_seqcount_begin(&fs->seq); - nd->path = fs->pwd; - nd->inode = nd->path.dentry->d_inode; - nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); - } while (read_seqcount_retry(&fs->seq, seq)); - } else { - get_fs_pwd(current->fs, &nd->path); - nd->inode = nd->path.dentry->d_inode; - } - return s; - } else { - /* Caller must check execute permissions on the starting path component */ - struct fd f = fdget_raw(nd->dfd); - struct dentry *dentry; - - if (!f.file) - return ERR_PTR(-EBADF); - - dentry = f.file->f_path.dentry; - - if (*s && unlikely(!d_can_lookup(dentry))) { - fdput(f); - return ERR_PTR(-ENOTDIR); - } - - nd->path = f.file->f_path; - if (flags & LOOKUP_RCU) { - nd->inode = nd->path.dentry->d_inode; - nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); - } else { - path_get(&nd->path); - nd->inode = nd->path.dentry->d_inode; - } - fdput(f); + if (likely(!nd->root.mnt)) + set_root(nd); + error = nd_jump_root(nd); + if (unlikely(error)) + s = ERR_PTR(error); return s; } + error = dirfd_path_init(nd); + if (unlikely(error)) + return ERR_PTR(error); + return s; } static const char *trailing_symlink(struct nameidata *nd) -- 2.21.0