Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp626375img; Wed, 20 Mar 2019 07:39:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqyx5hyabzWiDAfTG92QXH2l1SlkvULpzm/d7TVOgLAym8vq6hOJcyFDxb6f/BG/qPFczlxF X-Received: by 2002:a17:902:2f:: with SMTP id 44mr8397412pla.139.1553092751768; Wed, 20 Mar 2019 07:39:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553092751; cv=none; d=google.com; s=arc-20160816; b=d4FKpktsiTutUZwssj/FYhJ0HkbbeiLvPM//lXai5Q5O20NEFQRsJSrxUNKZspH47V nVhOIhnMNJ1KaPi6noRMgeErDHZgMp2bJZniJPrzPeq3MEVeSOwW8wmmcVVug+DKkB6M WHLDAoyqYj5Xg6jx+6DUKBBd2eZpAuGCU6DR14wEEU9o6GIKrQUTFsryqoRkYDeBgsCe y7vi7hh3LaGTzcM6WE708gXpBAiT85/MqhwVSMR9X7XFJhRQUAV0Cj98DB3Plbo9b3hz /uwyDTvjMV49inXSmWUACRTd2tLDOHGGPw1fdxSJaMXhHeWxePwgz3yfXn8qeslEh/tv 4/hw== 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=BxZXjwcHhUoEN+1xLIkp0Nv4zXbGNY0YcKdZIcIr2Zw=; b=zsYbxsXd2y51ps4fh4tU/VPmufVqrM5jIoilPf8Mxr2CP4dyNlXFvlVggsuu8QFdMy p6lqsaRqFBsfy7wU5/8SBxDscremiLPtzZQ94Ab2c7qLxujCZzLK5RlD/VhxO8TU0332 RQDpkXRi5LzgQxqGnPUU7eCMJkbqD4il7s14ZCsxYPcTobGujR9Va3O8HO71JJDUic4n xxfYpFU1F0C+CeIS3U/Sfukaw3l/uIVE2BrJ+lfhuu923oXhEMFuWgj7NbaFccxozPB6 IZXEx2IYgby5NOcnZ2e+II+H3syJdlCWD11d48cesjhfQ1UiSjP7pdql4eWHRxPGnNRH 8dTg== 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 c22si1891741pgj.405.2019.03.20.07.38.56; Wed, 20 Mar 2019 07:39:11 -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 S1728059AbfCTOiL (ORCPT + 99 others); Wed, 20 Mar 2019 10:38:11 -0400 Received: from mx2.mailbox.org ([80.241.60.215]:49792 "EHLO mx2.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726611AbfCTOiH (ORCPT ); Wed, 20 Mar 2019 10:38:07 -0400 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 9A24FA33CB; Wed, 20 Mar 2019 15:38:03 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id hrjGHx_vFkXE; Wed, 20 Mar 2019 15:38:00 +0100 (CET) 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 , 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 RESEND v5 1/5] namei: split out nd->dfd handling to dirfd_path_init Date: Thu, 21 Mar 2019 01:37:13 +1100 Message-Id: <20190320143717.2523-2-cyphar@cyphar.com> In-Reply-To: <20190320143717.2523-1-cyphar@cyphar.com> References: <20190320143717.2523-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 a85deb55d0c9..4fdcb36f7c01 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -2168,9 +2168,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) @@ -2204,52 +2254,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