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=1796383862; b=2U01SRjNJvpB+udnFcrJ2936UZeygWhv8DrFfAQ3u1zXb8WlvkF0M9ffpM2XcD7DeXwp VK1OnWhAWeCpDivplpaVy7GSgSZ+VE848/ra8/ZsVAWV5CEnefo9cK2izxyoxwZe04MZa vrnMd9m1XsoxPrGSkRUaFu/AKIHN6nWBcolDgS28PXu4DjWGqtZxdNHmqgcBcKGzSAbls i1PaTk65de77VQP/EVS4nZ3bG4TmMiK+Mb1dymvL4Xd04w7zUVfDBEtnrT8qrHWmGyIfz UK6xQrlbUWIonKyYALGf+6PGoKf+5xZwBqFf8jM5UNFYfrqnnZ4tLSja83yYhM+nKQQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383862; 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=dCHf1c3oFPRqZC+l4R8ObfgXAbuqAKZcGet+nLvGesQ=; b=ouUTdRYi2W9vd8flA5nRqVpuTKlXYd6emke2wCUWuAhMDJjwyyVNlHOtk++Wqbt8cN+8 6kKQBECHKz32wLcJ69T7wJ91hkwiReDjUiIiLdmBf/2ItMBkEKbAjXwo/bIuSS0owPSnC 5yp23ub+YtFa1ZPrrBFJO41ER5Y7JhkPUIlqGb8LhZF62WXJWDwPL0uDwIvjZJtmlo9Vu Cu6QhO8Zx2R36dhvWb2Tca1eCPpagj7Q716ZvzNveco2D2Pjg+x9/dS0guDNeo2S87dF9 U+LDcGMjpJcLcZ7AgmaCur78J0QBt6L8CMvR0Itwr1n6xvB5w7aVuzcsLktD9J5x6Ww== 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=xarnuBoO; 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 E1FFF606A322; Fri, 4 Dec 2026 11:31:01 +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=xarnuBoO Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5742A1F00897; Fri, 4 Dec 2026 11:31:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383861; bh=dCHf1c3oFPRqZC+l4R8ObfgXAbuqAKZcGet+nLvGesQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=xarnuBoOj+veiiwB288ahfoY1AMuPkJ6cKlzG/sBx1MoHvHSMLSWXopOfZAxMJ+8i ogFb8/VyQwAFvuFoSRLyWxQTmdtAT4NjtaB1b2Rz7G04xPQM5Cy8BBvFgOjK1lZIOn POTajChXHfo3qN9iBp1ICY99GzqVIsRnofN/SF7E= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ada Smith , Eric Biggers Subject: [PATCH 6.12 372/555] fscrypt: Initialize ->ci_master_key before publication Date: Fri, 4 Dec 2026 19:06:55 +0900 Message-ID: <20261204100624.089376488@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@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.12-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 @@ -632,6 +632,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 ->i_crypt_info. * So use cmpxchg_release(). This pairs with the smp_load_acquire() in * fscrypt_get_inode_info(). I.e., here we publish ->i_crypt_info with @@ -643,7 +650,6 @@ fscrypt_setup_encryption_info(struct ino * 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, @@ -651,6 +657,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: