Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp4552615imj; Tue, 12 Feb 2019 19:10:07 -0800 (PST) X-Google-Smtp-Source: AHgI3IZzQokXPABjqmQfhXdGnxvxGkSYvLA1AfvFfxwGZkXDUP1EkgsL7rQGO5CJaFt4LjHzMJlR X-Received: by 2002:a63:2d43:: with SMTP id t64mr6668379pgt.155.1550027407486; Tue, 12 Feb 2019 19:10:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550027407; cv=none; d=google.com; s=arc-20160816; b=pXjTJ/JxwRjAIIE6wADwXAxff/TCHJc+g9onpeoD/aUADsA8R0JPGLrHisMD164ovl AVMAVzvyhC8W1WnUInkLho5SerOk8tipMUiKh+zAKZiW+cE+pv6jnH2DuPBQtI1fimIX yvcxld2jo9VWLqdpskieCDW0kMk/Q8TacpB2j2v4d608rpVcnpYijEnpsTKzAEnu1eI8 A8qpKWnmo6K98zgpB5yHHpkVhCoTV7eVud5JZXBNH/1w/X5jHIMK8LvSh5UGXM66Vd83 gkBsf/21SHCH1mBS3mdcSGY+OGDmRgohwzYgnlLuZzDVNbPPBRftolce6SD/YLW+s2Rp +4PA== 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=bJUhedYLmYIQsv7KcmupzalHtwu13Ny2GU1tjo7Ixjg=; b=xuk2OiNT5yZu04CSQcd5ldDG/wsUSRru5hoKKNs+dJ+rqxeJPm3oa7jtze8jh831UA Rc5t/bDaSJmeFaY1+u6nDQXzp2DKW/nbxfitkR28if8TKi+wtDjZXHFDgVzH0Tbz32O6 2OYURPV/TPrmMf5wEbWCj21SyOMDs7FhfNcbp6S4d9jU3fCacPE/xEzTUik6AxV76/+/ XclC2FppkfM4Ix1UU2AmWhoE0zW5m7pHf8B60Bn3O+dxWPixi5rzbOq+q13e5pNWMOmL Kk4qPb3mD8EOWV2b8mUsn7doR4GlsAUmy6izSlE42PgB3prLNC7tJEKumxxFjhIShCBc VyOg== 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 q2si13620995pgv.124.2019.02.12.19.09.52; Tue, 12 Feb 2019 19:10:07 -0800 (PST) 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 S2388009AbfBMDJd (ORCPT + 99 others); Tue, 12 Feb 2019 22:09:33 -0500 Received: from mx2.mailbox.org ([80.241.60.215]:32660 "EHLO mx2.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729595AbfBMDJa (ORCPT ); Tue, 12 Feb 2019 22:09:30 -0500 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 5BB06A1069; Wed, 13 Feb 2019 04:09:27 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter04.heinlein-hosting.de (spamfilter04.heinlein-hosting.de [80.241.56.122]) (amavisd-new, port 10030) with ESMTP id YhxrrQ6RS05X; Wed, 13 Feb 2019 04:09:22 +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 , 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 v5 1/5] namei: split out nd->dfd handling to dirfd_path_init Date: Wed, 13 Feb 2019 14:08:47 +1100 Message-Id: <20190213030851.1881-2-cyphar@cyphar.com> In-Reply-To: <20190213030851.1881-1-cyphar@cyphar.com> References: <20190213030851.1881-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.20.1