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=1796382354; b=t89g3VWOHcNafRrl4vhPFcLcEIThqFc0HfFrtrrG3Y2wlt06nW+77D1fnAPQD3hR1gGr cWOZ1ToBLTzxD/gpDJMqw2SMPLLxDJMeiobqzcYqzowN/nettAKcs6ITOBMgF+3TknA8c 0DPEhBtfW458sm77cWnHRunDFZqWD0l/KW/joVROprRMEPjajpbuEH9Pggo/7WLULyaP+ 1a5vK072kztFXp360cY+IPoMY/AM9uwZQhzurLSQRpHnGL6VB55xWvJ/Pl6vRAwrW4deH 1nnuflgOPm7k8Vpvrf9u9mIar5XLwNQTER+jcV0Ts2XU51Ic9y5kd91+cqj4Xi/9MAw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382354; 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=WIhMl8Hc2HOVvpmFloUTejMo/2hdnVtZY5RV1Lg+ZjU=; b=e6xxOQbc3S/534aeAOTLPvP5s+7H6plKYnA35AjRpXn8vNjBATQIspXgodPkIgnFqIsM SclmBqJu6aDtWzRNrp1V8xNnqd/baL+pK/3OQuInQCx/fvDRIgo17cn5603T3yx2neLfF 71+naLwf5FcJoQJtBbPshE3NdX4av8fEWnnJAd+YPx6zTM6ybknhohjfDppjiFT+cRojj YK5X9U6ObpMtWKkNRbEiVFc31a6FyC9D52xuki1BayKKpa+6OfdP+lNxxJ8wVKXw7tdTO 4r8rvYzI+hoPaTqMrL0s9ATlaV/M/QxlmNm99Y59sBuJCYVpFJQVU5VVyamJgcXgUXg== 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=e4iMpSB2; 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 94935606A322; Fri, 4 Dec 2026 11:05:54 +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=e4iMpSB2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7CAC1F000C2; Fri, 4 Dec 2026 11:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382354; bh=WIhMl8Hc2HOVvpmFloUTejMo/2hdnVtZY5RV1Lg+ZjU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=e4iMpSB2DntVVENr7h9jASUty5S9KD9wYqwMspxBqste0aynrl9R6QLpeeUMaQQ57 6/v7/LH5ShJyNZ2P9InZ6kPr16inA0+dxt7VzBSnVQWhBXthYcTOE9cBQSA+x3RL0z +qRr7nSEwHs00GqLaFI0e3y/ONzljVEx2WvrQtpY= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ada Smith , Eric Biggers Subject: [PATCH 6.18 418/703] fscrypt: Initialize ->ci_master_key before publication Date: Fri, 4 Dec 2026 19:05:17 +0900 Message-ID: <20261204100638.947012703@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@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 6.18-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 @@ -653,6 +653,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 @@ -665,7 +672,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, @@ -673,6 +679,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: