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=1796239593; b=4wN8eNFxOIgtNUceVZDhCFbAqDuO8NN2zVlNf8DX6TO9D0c6SqO4uNYl7Z37jiFZXClv PXhg+HZ2VlW+ynzzvTT0+9DuO2Dob0DjcCXXYR2nMQ40ZUUiUD11wkmSqfJCdJLnKN4Q0 1X5/84XzWqA4RCP78sMK5//bwPaiEPd/MGIdhVfk7Pcy/1Gy65VC86iP9zGvYZpz2s7Yq iA5+wCAxHp5nGF9aHatZ1+9NNOGWhLpSZB/AxUNHxey1MOGHyzarbbiPKJH0MF0vM3wiT IcvvBHR/rxqUH8FCGrLINB3IhvjDQdZFcUcaLLwjb6gC6zBc1XJj0QE7zBe/+m/xpQQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796239593; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=0ha+/N1/8qDQ3YcTVkEtEhzRN/yU+764//e64QFo9vE=; b=l39/E+Q6rZ7GB8vPrcvKkk/IJGfsi2gwecTbKwwbL2fubE20IBuhXrMkwb4EkWIsitgf XO66QQKH+HrVuGJN8DyRnAtfU6bCW11dt4Yp4xlHMjWvXBgeyoBL5BOimGxXWoW6n5VX5 p8dkmkI28ptUjhlNqSm4DGavekrNu02IFB0Lh1iV7KsC1dKsyZtOKXFkJ2xrcR8p2JpsO RHQmVyfAnq1Fro5xLFoFh6Vf/5nfESzQVZdapy0vBOuDFQJ/TNVvrDo7mhlrQ79Eu4qzk 2IS7SFoTDfUjLkbzrQo+V+xMPhdNx3QUdQDJEPn96iipFRC6Vn5EMyrmU0/UgVv2DOA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RzSjechS; 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 5845D606A322; Wed, 2 Dec 2026 19:26:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RzSjechS Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDF7C1F000C1; Wed, 2 Dec 2026 19:26:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796239593; bh=0ha+/N1/8qDQ3YcTVkEtEhzRN/yU+764//e64QFo9vE=; h=From:To:Cc:Subject:Date; b=RzSjechSvUGASa4wnXBTG4U4o71+2YNE4MWBx2QLEduauQIinTArQRSzaYHm32VQw WtNevok32oxHLnYWZAg5xwsd1oOwZJG0v4beaGCGj7z/ANPSkeeRMWzmNA9FIbUgc8 R411FR/f3OJyLM7dc1f/mkT1zMr8LDq7WpEncluNwzn10rVxAlmh1RjTOedwjwZfQo bJfunviPlIBBi374SyyvAkJM/EQIw/b0D2/X/0F3zoLBs0fnqkmnd3/6F+VPCxd6KF nsAQ9+4RQrBMTNK0ajBO1uX1+etQ+FZ6oH7XpHtX1fAxPhPe8yEtGfwmevLFiaA9GJ B9SNybYaoXnug== From: Eric Biggers To: stable@vger.kernel.org Cc: linux-fscrypt@vger.kernel.org, Eric Biggers , Ada Smith Subject: [PATCH 6.1] fscrypt: Initialize ->ci_master_key before publication Date: Wed, 2 Dec 2026 19:26:14 +0000 Message-ID: <20261202192614.1371515-1-ebiggers@kernel.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog X-Mailing-List: linux-fscrypt@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 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 [ Backport-note: resolved trivial conflict due to a comment change. ] Signed-off-by: Eric Biggers --- fs/crypto/keysetup.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c index 94ad0024c5298..183d913e57f22 100644 --- a/fs/crypto/keysetup.c +++ b/fs/crypto/keysetup.c @@ -551,6 +551,13 @@ fscrypt_setup_encryption_info(struct inode *inode, if (res) 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 ->i_crypt_info. * So use cmpxchg_release(). This pairs with the smp_load_acquire() in @@ -563,7 +570,6 @@ fscrypt_setup_encryption_info(struct inode *inode, * 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, @@ -571,6 +577,12 @@ fscrypt_setup_encryption_info(struct inode *inode, 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: base-commit: 1ca1436ccc68275d0b43b6a730b3940fcc46b7c2 -- 2.56.1.911.g883d8bcb20-goog