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=1796380878; b=Ycb1rUCJZBlsJ1ztRsrtVNF8g5qyHOTAflWGtXHpfgxMWvsJnzd6rqJM/A795yZFXSF8 CjPTPhlxCm9zlz3Wvrj+YqMSrJs2Oi2gU0VSr38qF8k2CcE8Dmq7f+aQR2Wxl340Atq0l LWUFwrDTKh8yumSy8fp8zauzBMW+pieRlwzSubTofcpuX0COnDmwcrfELsppMZVE9Xx0E PljPCLnhETynosAlMUHE8cawVdVan/Ap1/79O7hEaKPu9ISWBehuTSXcP7s4Z0H8V2Iml epbqhfAFCPtRc++5FGd32bUy1/UYdxi3ofVX+JqwB28pAO9Cr/eqEPHUtR1pgg0gfLQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380878; 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=Gr43VddnY6AE3iJqi8kmGh1nAFbzNdNg/wPFsqlUD7w=; b=Z4kVQ4uhcRTI1JwqzBQ9D/aMsgFE0TM/Kezu8PscnVvXBGeCpuiVFVl8Ebzy/1dU39PH Tj6wu/8sNX6ghHNRLBdtxWeTYZhz9igzYAcXgZG22GLn1tZi9drIwWmBkTaeLNbxyW15v Yx70/XuqEHKcbuLJyL+g+rIaKME5H4zQMC+nzS2PQfhcxVnelCdqTaY7ZRfhRFVSZpJqa NKXEkzwwlZdD/1PLzFHRDIAfw8zvhsF2mo6XR7UiV3NMzuEvWzlDPxLMMW164QVU1tedS s6WseNTWO1xPTSXYtuFYD+MpwYKu2z/Dxxr4OmcJH7QfUyvSxRw3yEljPVwA1k1Gpwg== 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=TmULfvMY; 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 6FCBB606A32F; Fri, 4 Dec 2026 10:41:18 +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=TmULfvMY Received: by smtp.kernel.org (Postfix) with ESMTPSA id DEAD31F000CE; Fri, 4 Dec 2026 10:41:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380878; bh=Gr43VddnY6AE3iJqi8kmGh1nAFbzNdNg/wPFsqlUD7w=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TmULfvMYNP4eM7mKHwDlR3jgVbQYyDfjldMRqSywLk12Nv6FPPg847zwdk+QfJ3Uk QI7wROC6CRjsuEZMbekPouH1zlNLzgQ5qL2n1zwl4HNw90Y9dbv5BlRac5HATyC++f P6/+SfMDS9QW4WVJphdSa7x02U1zM7imoawsQOMA= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ada Smith , Eric Biggers Subject: [PATCH 7.3 463/742] fscrypt: Initialize ->ci_master_key before publication Date: Fri, 4 Dec 2026 19:05:27 +0900 Message-ID: <20261204100647.457854402@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers commit 2de253924266eeedb798fb662173726c90b0cf04 upstream. fscrypt_prepare_setflags() assumes that if the key for an inode with a v2 encryption policy has been set up, then ->ci_master_key is non-NULL. This is a reasonable assumption, but it does not take into account the very short gap in fscrypt_setup_encryption_info() between the inode's fscrypt_inode_info being set and ->ci_master_key being initialized. Thus, a NULL pointer dereference was possible. Fix this by closing the gap: make fscrypt_setup_encryption_info() initialize ->ci_master_key before publication. If it loses the cmpxchg race afterwards, set it back to NULL so that put_crypt_info() continues to have the same behavior. Fixes: b1c0ec3599f4 ("fscrypt: add FS_IOC_REMOVE_ENCRYPTION_KEY ioctl") Cc: stable@vger.kernel.org Reported-by: Ada Smith Link: https://patch.msgid.link/20261110212011.35487-1-ebiggers@kernel.org Signed-off-by: Eric Biggers Signed-off-by: Greg Kroah-Hartman --- fs/crypto/keysetup.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) --- a/fs/crypto/keysetup.c +++ b/fs/crypto/keysetup.c @@ -622,6 +622,13 @@ fscrypt_setup_encryption_info(struct ino goto out; /* + * Preemptively initialize ->ci_master_key, even though we don't know + * whether we'll win the race below yet, because if we do win the race + * then other tasks can see it right away and need ->ci_master_key. + */ + crypt_info->ci_master_key = mk; + + /* * For existing inodes, multiple tasks may race to set the inode's * fscrypt info pointer. So use cmpxchg_release(). This pairs with the * smp_load_acquire() in fscrypt_get_inode_info(). I.e., publish the @@ -634,7 +641,6 @@ fscrypt_setup_encryption_info(struct ino * crypt_info. Now link it into the master key's inode list. */ if (mk) { - crypt_info->ci_master_key = mk; refcount_inc(&mk->mk_active_refs); spin_lock(&mk->mk_decrypted_inodes_lock); list_add(&crypt_info->ci_master_key_link, @@ -642,6 +648,12 @@ fscrypt_setup_encryption_info(struct ino spin_unlock(&mk->mk_decrypted_inodes_lock); } crypt_info = NULL; + } else { + /* + * Lost the race, so we won't be taking an active reference to + * the master key. Clear ->ci_master_key accordingly. + */ + crypt_info->ci_master_key = NULL; } res = 0; out: