Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp60033ybm; Mon, 20 May 2019 11:51:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqwiWCbIt7JEX6CK8kdyMN4weJstaJ21asccAJnJ/xfw2rde0QkzXns8A7So8Zc88wFPaQOW X-Received: by 2002:a17:902:2907:: with SMTP id g7mr52504493plb.114.1558378308491; Mon, 20 May 2019 11:51:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558378308; cv=none; d=google.com; s=arc-20160816; b=S1oyQqzGIe8vHChPp3Dl72eitSwwKlmJEQx/pZTgsxp4eikvB4YlQ+A2mInQR7oJmX c0d7euuAfws4I60oNEdC6AFDnboKM4cVrbiEcG17heNrHO5XJtio35EMMo1Dps8zb8N6 6yo5gK6oZrMum/2Nmz3dvv6PBSsG1FEaFxSPLqJbt8bsRwBoiVD8DYSQhdz3dAHg5MGW a/iRWgHbBBSo5v3Chxmq4lwIGiMzyAI/y8Ur418CM+dwj48+n0GP/UWuUshon8bLL7z0 eROsBg8vOXTyogvlpUmYxDesa/6J2a4iBH4uxm/PQW5aBTTdbrYl8jmPcsEeSg42il+3 D4CA== 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=zZxtOFlyVMR7j681W7Kx6j/XSvXbHbeM817H6XJNyJo=; b=ks80MRE1iUl30Rmvm2LbdDmA7xB0SGtmFPUkYnn9uQqnBQgAlbIvGs2V/px3frwt2o MQyy+tvzFQfjpWa5b4seXfQ6gr81YkMp+EeqMuvehSUNJIU9qqc28MkHD4iEpj01dSQ6 lHvpj1qHucCCoICFmfOaO+x0K3UNtob6YpQmpJGqLzaLfWPfYXxS+yDx9mOjH9OIvbvJ ZgoXsH6S9CnpctdXtX3AebGWtwNeHhIT9CkEErKyIiH5C0R9YRyqqTA3QTWP0M/B2xte 1lh4RwjVCjxGo/wYw1Qvauteq48g/FQRq00VJFOLI+Bg0pd1iVPrVncLUXeCT47I5kOn YA4g== 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 cn12si19864892plb.349.2019.05.20.11.51.30; Mon, 20 May 2019 11:51:48 -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 S2387970AbfETNel (ORCPT + 99 others); Mon, 20 May 2019 09:34:41 -0400 Received: from mx1.mailbox.org ([80.241.60.212]:59666 "EHLO mx1.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728634AbfETNek (ORCPT ); Mon, 20 May 2019 09:34:40 -0400 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 7072950251; Mon, 20 May 2019 15:34:37 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id u25V7i0GyP7E; Mon, 20 May 2019 15:34:32 +0200 (CEST) From: Aleksa Sarai To: Al Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , David Howells , Shuah Khan , Shuah Khan 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-kselftest@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Subject: [PATCH RFC v8 04/10] namei: split out nd->dfd handling to dirfd_path_init Date: Mon, 20 May 2019 23:32:59 +1000 Message-Id: <20190520133305.11925-5-cyphar@cyphar.com> In-Reply-To: <20190520133305.11925-1-cyphar@cyphar.com> References: <20190520133305.11925-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 LOOKUP_BENEATH (and LOOKUP_IN_ROOT) 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 46c5302eea4a..dc323e25d4d2 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -2251,9 +2251,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) @@ -2287,52 +2337,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