Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4139738imu; Mon, 12 Nov 2018 06:28:18 -0800 (PST) X-Google-Smtp-Source: AJdET5fOKJD6pCRpbWuztDHqOShBkkT8yLXBrOtCzXGcsq80Ict98gSDB5jgbOVrOaH+AmtX3n76 X-Received: by 2002:a63:314c:: with SMTP id x73mr1013769pgx.323.1542032898922; Mon, 12 Nov 2018 06:28:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542032898; cv=none; d=google.com; s=arc-20160816; b=VEtH/syLtljhbRseHaayIYdo3JxDzbTYbtOWBsVEzu0jatk0X8k3cSfjkXIbZsg+w9 HzsON2wTzSFgnhV93CSTFlpB5hT3UAfG5yuLCglSmcKoAlPlF5LE3NP7fk8NnssyfqID 6Ds9YJo4bGF7G2pjbyEEWw/lRrQLoSDNePKiH7IENduVlAkJwGPDht08eXxc+F23E020 gDvlmlCinOzQEQTHNbooLkEuPPQO8yAX/nwdTgtllsC/WY1iwg4dYwSO2N4WMmC1mGtQ uN1j4Lj8YxHnOV6C9CUtN7DiVcHStVYgmGozzHmxWA9VD7PGez+7lrErTx/LLNwdRQEb Q4PQ== 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=eDRP170dSKxv9VNIh84TnbD5+UWFpoubVyouC3bKn5Q=; b=VXdLzme8/V0YoI3O8QT6JNbKQPTtBI4YGuwfbzZ0tcioSisO3nOAW8hZm88WSG6MnR 34/K/X5RSoO3GVVkFAlOs0K3DDT0M6IdSqUO2S26C194I/oVynN3KHmHPUykJ/holbcu 7mQP8vnH0ORJ+xobrrf9j9KTd4s4ez2METt1F8/+CTcO6brwwLiX17X5SriDBh7bfnx6 6Nxqp19XT5osTZuefeLQTMHfdlSUbJuukUIgrnf5cgJKw4MIEOyJZzthW2EmSyx6RWM3 InPiLEe47xCriiiBpiCcf23y9R1ksSmchh6czQLyJkcUKnmTPlO9rEhxtSvyzu2qWSP6 cJ1Q== 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 f15-v6si11466947pff.131.2018.11.12.06.28.01; Mon, 12 Nov 2018 06:28:18 -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 S1729827AbeKMAUw (ORCPT + 99 others); Mon, 12 Nov 2018 19:20:52 -0500 Received: from mx2.mailbox.org ([80.241.60.215]:56286 "EHLO mx2.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729794AbeKMAUv (ORCPT ); Mon, 12 Nov 2018 19:20:51 -0500 Received: from smtp1.mailbox.org (unknown [IPv6:2001:67c:2050:105:465:1:1: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 88F20A120F; Mon, 12 Nov 2018 15:27:18 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id bT_JQH23xqxN; Mon, 12 Nov 2018 15:27:14 +0100 (CET) From: Aleksa Sarai To: Al Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , David Howells Cc: Aleksa Sarai , Eric Biederman , Andy Lutomirski , Jann Horn , Christian Brauner , David Drysdale , 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 v4 1/4] namei: split out nd->dfd handling to dirfd_path_init Date: Tue, 13 Nov 2018 01:26:51 +1100 Message-Id: <20181112142654.341-2-cyphar@cyphar.com> In-Reply-To: <20181112142654.341-1-cyphar@cyphar.com> References: <20181112142654.341-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 fb913148d4d1..faefca58348d 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.19.1