From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from flow-a1-smtp.messagingengine.com (flow-a1-smtp.messagingengine.com [103.168.172.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACECE231C9F; Mon, 23 Feb 2026 01:13:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.136 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771809233; cv=none; b=RFsyepYh7JCh3hZCgvDn4wirK3BOs2FfYAS3YlpUsXSuTAcXS9bk1P+z6Xg1ZGcNcR4wmADK3/qYvEkPgjhvsalF81S+vOc9aGMlsII7Q+psKraTEHE1fVoq/crTsQA2nJ3lIzJRFUOFe0OWn9Fzakqu6XxauX2CynGM4f/IO6o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771809233; c=relaxed/simple; bh=XjOWdC+g4FNsjlHlt6f2jss0p2+dkDuiZErV2U861I0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oTzPJJvsuLKwtu7FFIixwfMOBY6dJ435uT7BVldMd9+rfFuVEPjxojFklz6juqvjAGozIdcg4XnNr5028jsn8KX3caVdkOK9o8+JsBlkj04nBmyoWNe7m4mhH/630FVJ+UjiRbjuy1Ak+jP+P1Gxuax7yXkvUZs4twJD0nd29kg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ownmail.net; spf=pass smtp.mailfrom=ownmail.net; dkim=pass (2048-bit key) header.d=ownmail.net header.i=@ownmail.net header.b=COIrnCuI; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=qDOVUob1; arc=none smtp.client-ip=103.168.172.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ownmail.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ownmail.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ownmail.net header.i=@ownmail.net header.b="COIrnCuI"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="qDOVUob1" Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailflow.phl.internal (Postfix) with ESMTP id 25BDB13807AC; Sun, 22 Feb 2026 20:13:52 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Sun, 22 Feb 2026 20:13:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ownmail.net; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to; s=fm3; t=1771809232; x=1771816432; bh=ml1LiguIrJHeWfdJIINaTOuxtTcyMpy4ph9bxhYGeRY=; b= COIrnCuIhyBmcEbOHolu6EeslkemNH1XFmqSMkEWj0PEU8jeHq3vddKgIkyIzAeA LZJ0CLcS9NQPpdUJbW4rswtgt7padwgTQ5SL4/62XzUTwE5B2rKkbrmcgC1daYSX L9pklLkeEnykthJdkggHE4L3yoDgBnNQDorHw3vCjwkqE+y8g5xuH/2QJluFTTjc Ck++t4GkfGJ56WLS9RquhT2KJHB86+JqS1fsEQ7imW6d6i2r9YKu/OOv3KavLOjN +AkGe8xiZPusauaOU8LwxjwwnCGD/ZUH4a01XuOO4/G7MZtLHNU+ZIoNGdFbBNmM 0K2ZNACsr8O/y+7T/P89Bw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1771809232; x=1771816432; bh=m l1LiguIrJHeWfdJIINaTOuxtTcyMpy4ph9bxhYGeRY=; b=qDOVUob1aQNH9fKFM fTfGOsurhB3PMpUX6gvkYTI+/a/dxyojQU3vVlf7BiWMfcl6jLs6mMq3j9E2JQPJ 2Z0T7gaMvvaNbP6CbHTMtwkzpN45/0V5iNrjF7NnvpDezvm8gNYMY/Pr9dydIEdQ SGa6DP941VRn/7ePqWMleZfbQbAU3jdx+eD8C2kqDGDQBn/NgXbQdZ9bD0Ygi9zh QCcn3L1cM2uL1rXleTdxG5pGnBEKT/1IczkuU9byzVCjQFD3NBRIrudunYsyhin9 kAwJtOrN1C3xZcqTVwo/Ph8Kz6BaDsZXWZB3o1x4U8gnYM6Tai7nrX48dyxov98l 6E1nw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvfeehkeegucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhhrggfgsedtkeertdertddtnecuhfhrohhmpefpvghilheu rhhofihnuceonhgvihhlsgesohifnhhmrghilhdrnhgvtheqnecuggftrfgrthhtvghrnh epveevkeffudeuvefhieeghffgudektdelkeejiedtjedugfeukedvkeffvdefvddunecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepnhgvihhlsg esohifnhhmrghilhdrnhgvthdpnhgspghrtghpthhtohepvddvpdhmohguvgepshhmthhp ohhuthdprhgtphhtthhopehvihhrohesiigvnhhivhdrlhhinhhugidrohhrghdruhhkpd hrtghpthhtohepshgvlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphht thhopehlihhnuhigqdhunhhiohhnfhhssehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtg hpthhtoheplhhinhhugidqshgvtghurhhithihqdhmohguuhhlvgesvhhgvghrrdhkvghr nhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhnfhhssehvghgvrhdrkhgvrhhnvg hlrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhn vghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqfhhsuggvvhgvlhesvhhgvghrrdhkvg hrnhgvlhdrohhrghdprhgtphhtthhopehmihhklhhoshesshiivghrvgguihdrhhhupdhr tghpthhtohepjhgrtghksehsuhhsvgdrtgii X-ME-Proxy: Feedback-ID: i9d664b8f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 22 Feb 2026 20:13:45 -0500 (EST) From: NeilBrown To: Christian Brauner , Alexander Viro , David Howells , Jan Kara , Chuck Lever , Jeff Layton , Miklos Szeredi , Amir Goldstein , John Johansen , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , "Darrick J. Wong" Cc: linux-kernel@vger.kernel.org, netfs@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-unionfs@vger.kernel.org, apparmor@lists.ubuntu.com, linux-security-module@vger.kernel.org, selinux@vger.kernel.org Subject: [PATCH v2 09/15] ovl: Simplify ovl_lookup_real_one() Date: Mon, 23 Feb 2026 12:06:24 +1100 Message-ID: <20260223011210.3853517-10-neilb@ownmail.net> X-Mailer: git-send-email 2.50.0.107.gf914562f5916.dirty In-Reply-To: <20260223011210.3853517-1-neilb@ownmail.net> References: <20260223011210.3853517-1-neilb@ownmail.net> Reply-To: NeilBrown Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: NeilBrown The primary purpose of this patch is to remove the locking from ovl_lookup_real_one() as part of centralising all locking of directories for name operations. The locking here isn't needed. By performing consistency tests after the lookup we can be sure that the result of the lookup was valid at least for a moment, which is all the original code promised. lookup_noperm_unlocked() is used for the lookup and it will take the lock if needed only where it is needed. Also: - don't take a reference to real->d_parent. The parent is only use for a pointer comparison, and no reference is needed for that. - Several "if" statements have a "goto" followed by "else" - the else isn't needed: the following statement can directly follow the "if" as a new statement - Use a consistent pattern of setting "err" before performing a test and possibly going to "fail". - remove the "out" label (now that we don't need to dput(parent) or unlock) and simply return from fail:. Reviewed-by: Jeff Layton Signed-off-by: NeilBrown --- fs/overlayfs/export.c | 71 ++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/fs/overlayfs/export.c b/fs/overlayfs/export.c index 83f80fdb1567..b448fc9424b6 100644 --- a/fs/overlayfs/export.c +++ b/fs/overlayfs/export.c @@ -349,69 +349,64 @@ static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx) return NULL; } -/* - * Lookup a child overlay dentry to get a connected overlay dentry whose real - * dentry is @real. If @real is on upper layer, we lookup a child overlay - * dentry with the same name as the real dentry. Otherwise, we need to consult - * index for lookup. +/** + * ovl_lookup_real_one - Lookup a child overlay dentry to get an overlay dentry whose real dentry is given + * @connected: parent overlay dentry + * @real: given child real dentry + * @layer: layer in which @real exists + * + * + * Lookup a child overlay dentry in @connected with the same name as the @real + * dentry. Then check that the parent of the result is the real dentry for + * @connected, and @real is the real dentry for the result. + * + * Returns: + * %-ECHILD if the parent of @real is no longer the real dentry for @connected. + * %-ESTALE if @real is no the real dentry of the found dentry. + * Otherwise the found dentry is returned. */ static struct dentry *ovl_lookup_real_one(struct dentry *connected, struct dentry *real, const struct ovl_layer *layer) { - struct inode *dir = d_inode(connected); - struct dentry *this, *parent = NULL; + struct dentry *this; struct name_snapshot name; int err; /* - * Lookup child overlay dentry by real name. The dir mutex protects us - * from racing with overlay rename. If the overlay dentry that is above - * real has already been moved to a parent that is not under the - * connected overlay dir, we return -ECHILD and restart the lookup of - * connected real path from the top. - */ - inode_lock_nested(dir, I_MUTEX_PARENT); - err = -ECHILD; - parent = dget_parent(real); - if (ovl_dentry_real_at(connected, layer->idx) != parent) - goto fail; - - /* - * We also need to take a snapshot of real dentry name to protect us + * We need to take a snapshot of real dentry name to protect us * from racing with underlying layer rename. In this case, we don't * care about returning ESTALE, only from dereferencing a free name * pointer because we hold no lock on the real dentry. */ take_dentry_name_snapshot(&name, real); - /* - * No idmap handling here: it's an internal lookup. - */ - this = lookup_noperm(&name.name, connected); + this = lookup_noperm_unlocked(&name.name, connected); release_dentry_name_snapshot(&name); + + err = -ECHILD; + if (ovl_dentry_real_at(connected, layer->idx) != real->d_parent) + goto fail; + err = PTR_ERR(this); - if (IS_ERR(this)) { + if (IS_ERR(this)) goto fail; - } else if (!this || !this->d_inode) { - dput(this); - err = -ENOENT; + + err = -ENOENT; + if (!this || !this->d_inode) goto fail; - } else if (ovl_dentry_real_at(this, layer->idx) != real) { - dput(this); - err = -ESTALE; + + err = -ESTALE; + if (ovl_dentry_real_at(this, layer->idx) != real) goto fail; - } -out: - dput(parent); - inode_unlock(dir); return this; fail: pr_warn_ratelimited("failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", real, layer->idx, connected, err); - this = ERR_PTR(err); - goto out; + if (!IS_ERR(this)) + dput(this); + return ERR_PTR(err); } static struct dentry *ovl_lookup_real(struct super_block *sb, -- 2.50.0.107.gf914562f5916.dirty