From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387483; b=d/aUjHieb/ioL66uApHaFuTgrMcFKykp16Sg9ZJqcTHFmFw/2iRP8LIN1Ji6qXg/y3nV zbwHsJPOVTfuWOZq8HRMr0I/PZOty2VA3pxgLXFT7qqwLqsVNazphXlNKFdYwd8iTsgA7 2e5S1SpKg9ineLt4ZDgG+j0NGVhq1naCSk3E92LxCl+oj5rqNCtsxtgFFt7ZxLGsOhbmf kazGrtrL+zNOK0QR7bmCX89pALfba6FvhMgX7tC/QjtMi4KzqNrzSm0ltLlvwcMu6D9d/ caKXxSBT7J2ecWvifPXkXSNyugZJ804MZEOPHmyRRtqLFtLGnTEGz2eOi4fgODkGdsg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387483; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=KZKml5Wfp5ahxVVqDW9007Z5le9Zfa8uD1vKEYHxZ8s=; b=iRR3tGQaxytFdAVtQ6UTLN2zXYesjxiuPBv8blgcIiui+PEj2bgt27vovP7BOGXLXjqv VvS7yje4fCCXKJf7JCK43ScPM6Et0RXQ96i3LXXUvxVBv7lUcDI+jz77E/32eVdiR3KKL qkAqiWX1L9LvqpukyDSPCpRfrQBUWLLAn27kdVtJIUEGMSdQrRn6D2Z7WInZgajFJUWAP 2ChI98COZ675SCP+7YY1zUwv0Mlk7eJr6sIMrRVP1oPOMe5j15h+Gl/0zkC0mZCRySHad oKk20HRBroBbjzChnkBZrHL/EEG58H0wOeQ1pVzItoqS3tDsbEoPsVOnok6n0dnbf6w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=cf1E4Rmv; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1D5F606A32C; Fri, 4 Dec 2026 12:31:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=cf1E4Rmv Received: by smtp.kernel.org (Postfix) with ESMTPSA id 34CEB1F000CE; Fri, 4 Dec 2026 12:31:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387483; bh=KZKml5Wfp5ahxVVqDW9007Z5le9Zfa8uD1vKEYHxZ8s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cf1E4RmvmIQqoUO9Zo4eQfGzENy/F7Hay8PUk4ij5/32d6lzxe3b1Jxgo8TvKQaIq cmHYCtTWvh7IZBIle94JrW6WRc5VfhNh/yarn6aU6s2/1EuWR+v4Xz2IASdXsbaUKC Qi/d+kGwtY0ep4BB02v0bhB6nYYbmvkWKAV4iDa8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tejun Heo , Shakeel Butt , "Christian Brauner (Amutable)" , Sasha Levin Subject: [PATCH 6.1 0262/1059] kernfs: fix up the unlocked attribute reads on the creation paths Date: Fri, 4 Dec 2026 18:56:33 +0900 Message-ID: <20261204100651.060550501@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Shakeel Butt [ Upstream commit 81bdc85b4c086502f4446a58eb5d25f29bb5e9f7 ] Two creation paths read a live node's attributes without holding kernfs_iattr_rwsem, which kernfs_iop_setattr() takes for writing. They need different fixes. kernfs_create_link() copies the target's ia_uid and then its ia_gid into the new link. A chown of the target between the two reads leaves the link with the old uid and the new gid, an owner the target never had. Read both under the rwsem. kernfs_new_node() reads the parent's mode and ia_gid for S_ISGID inheritance. Either value is fine there: the node does not exist yet, so nothing orders a racing chmod or chown against the creation. Taking the rwsem would only pick between two answers that are both right. Mark the reads with READ_ONCE() instead. The pointer that leads to them is already fine: __kernfs_iattrs() publishes kernfs_node::iattr with try_cmpxchg(), and both sides read it with READ_ONCE(), like the rest of fs/kernfs. The Fixes tag is for the symlink half. kernfs_create_link() has read the pair unlocked since it started copying the target's owner; only the name of the lock its writer takes has changed. The READ_ONCE() markings are not a fix. Fixes: 488dee96bb62 ("kernfs: allow creating kernfs objects with arbitrary uid/gid") Acked-by: Tejun Heo Signed-off-by: Shakeel Butt Link: https://patch.msgid.link/20260905191613.3143937-5-shakeel.butt@linux.dev Signed-off-by: Christian Brauner (Amutable) [ sashal: Replaced kernfs_iattr_rwsem with kernfs_rwsem in the symlink.c lock and its comment, since the former doesn't exist in this tree. ] Signed-off-by: Sasha Levin --- fs/kernfs/dir.c | 12 +++++++++--- fs/kernfs/symlink.c | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index a259fe3471a98..c01d900f09de1 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -675,13 +675,19 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, { struct kernfs_node *kn; - if (parent->mode & S_ISGID) { + /* + * The mode and the gid below are read unlocked on purpose: they feed + * a node that does not exist yet, so nothing orders a racing chmod or + * chown against this creation. + */ + if (READ_ONCE(parent->mode) & S_ISGID) { /* this code block imitates inode_init_owner() for * kernfs */ + struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr); - if (parent->iattr) - gid = parent->iattr->ia_gid; + if (attrs) + gid = READ_ONCE(attrs->ia_gid); if (flags & KERNFS_DIR) mode |= S_ISGID; diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c index 45371a70caa71..dcf7d8c40548b 100644 --- a/fs/kernfs/symlink.c +++ b/fs/kernfs/symlink.c @@ -31,9 +31,20 @@ struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, kuid_t uid = GLOBAL_ROOT_UID; kgid_t gid = GLOBAL_ROOT_GID; - if (target->iattr) { - uid = target->iattr->ia_uid; - gid = target->iattr->ia_gid; + /* + * A symlink takes its owner from its target, so both fields have to + * come from the same moment: read them under kernfs_rwsem, or + * a chown of the target racing this could leave the link with the + * old uid and the new gid. The section ends before kernfs_add_one() + * takes kernfs_rwsem. + */ + scoped_guard(rwsem_read, &kernfs_root(target)->kernfs_rwsem) { + struct kernfs_iattrs *attrs = READ_ONCE(target->iattr); + + if (attrs) { + uid = attrs->ia_uid; + gid = attrs->ia_gid; + } } kn = kernfs_new_node(parent, name, S_IFLNK|0777, uid, gid, KERNFS_LINK); -- 2.53.0