From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C853542BC5E for ; Fri, 31 Jul 2026 14:09:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785506979; cv=none; b=S/h4h3vpqjQ5ZzNcEp888OgIZvNOBDQsGFTAWljr/6ztWW/6W8SubodW1g+kc65kjsVrgPTPdBm6pOX8QRCyyTE29PZI74q5XsFWpIVz9AIz9a1D2dw4uctGtPV5I14fmuIpm+TqhZ1ny2HHGr+7vCNudnq75usCz+xMknWPIcU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785506979; c=relaxed/simple; bh=3EOf2/jCD8AncItiAAHs6oeZqNAYPx5oM9R4lgo8xmE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Q4T+7NQks9aYjgmseNdk6NmzAAQ+5HNUPy6Bmy4fquSS/ij2tv3vsWFwzMRokpoeIVGKDYPqcYnHVAAV6r8/Qtv2eB6AAGmicQXrSfAaONdZHU2F1aCfYaSAFLX4dtF8F6EQZSDPEQcOhhD33oxvspBr9enHlT8+0cZ76n3/Uj0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mhkWKX5P; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mhkWKX5P" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2cf110997efso1597195ad.2 for ; Fri, 31 Jul 2026 07:09:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785506977; x=1786111777; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=qfTZLKX5MXlI+65GiAcv4sDb6zyKTd9ns9ffnwjJXko=; b=mhkWKX5PDoWgAmcXfVNp0XUlYz1PPHyqI7gvQ1vZLKrPHmfMAJDHQnoosd+NaU4JR4 R1G0fOpNuDGm5Kxx7EOXloQvCxIaBMUVNm18xEocs1JoP/ZgW9yTUybsD1zgb/+6jhd8 u30EcU0j1yJ+FJokT7DsaflSDIdy5wKqxPS6YEdADFFmPvrBKNmqxpVphiQ9baOqxdqI 7qIiRrP7I5ytwBKZYLJMk9QKpYq7DhVVqJ83xsIbK/3hIq3RAGlOZvqwcgFepYpYP7ax vSt+8eywrUF955DmnPLFeEvwtWd4PQie1pVjKz3GWeudAAgFrDkwARs88dZ7YGprg8vi Otxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785506977; x=1786111777; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qfTZLKX5MXlI+65GiAcv4sDb6zyKTd9ns9ffnwjJXko=; b=mJaMaeB1tw58T1N38j7eWafmrCetLP+hMAJc8ethtJwm183v5+XADIb99i/QDyylrE pQIQhAURwCkUFUl+5SHhM1u2dr8HaRndgyW9vp3WDr5IGpAvBFY784LpmMSPyZlMrNTZ PFYTCkAiuXy7uyUER26DhxBmmDE8d55UbcUmW3x5wKNxDNN4aR82M/o58DRWJ18azHoE rtDAL12PLk/eIJjWQCqgpjheJhPu9LHNjP80/xVzUtK4wGSIwGcQdA1nSVoWQR9+Yctb biFdnQ07wAjHBLHwvPTyKw91+85YdKC1A8RluyBwtEFKD8TnMD7e0Q/3W5zLDLEkPJ/z LWBA== X-Gm-Message-State: AOJu0YyX+B7KCq/CBtT08Cby8EpJHkrqF2ShXGp0TAdOj4eafMyfqNAs fnLVE1J1QYLhNAl6gt/1Vsf9OfYDFrbVNHNZxCkkGjgpgddO8IJOlkUJRRfpebcB X-Gm-Gg: AR+sD115Seh/qUlNSnQuAR/YhaM+jRZGQ+b8PA/ybq+BIXtQ3BqxMF4VwzahyQQ3jZa m5RmSA5ZWRKYkML/AI2tii281RiC2jUxUfaBRPM0a32W1sjXkalyNsC2o/V6Ob5WiuEuqxArSJy 6TGZR3vNWYELt6uyJTso9IyunghXAkRy9B/Apl/GMSuKQA0IKdlCx/Ai6zmxazEJDH2aHVjoj7C 43R3GTRXdmuZQDVAcqshdQEGAIFtmrJoztUykUNEFP32fLluLGFbaP9BYBOLIFzNr01t5Glbw9v yDF+5lMoeU3v2316qMNXP28n3xSTyZkgr2OaCNN+4x5PrQr4H2uzH+2V4Zj5v+lqeSzUuE/V/3z fXLvwu9b+PYG2RD1pm8eULtKx/Ok2k9C3GDZdKSnzq4lQL5jmeLROnG1qcTw8YtS8zTb4BePBVb mUeG7pnI57CG8kgwpg4BALKBiScT5o8EvZ8ucHgF9Bb+ji8qlpiBbVsFkGk11A9m5RDVZhm7ON3 SmxXCJlUNtvEQIJqZZZkXmjGAM0ntqdLod2gF7CjHBStpWtfqnZTh4= X-Received: by 2002:a17:902:f546:b0:2c9:b96a:2855 with SMTP id d9443c01a7336-2d051f078e3mr3246845ad.0.1785506976879; Fri, 31 Jul 2026 07:09:36 -0700 (PDT) Received: from localhost.localdomain (45.78.65.84.16clouds.com. [45.78.65.84]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3153ddd7a33sm6268159eec.12.2026.07.31.07.09.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2026 07:09:36 -0700 (PDT) From: Chengfeng Ye To: James Bottomley , Jarkko Sakkinen , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , Roberto Sassu Cc: linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chengfeng Ye , stable@vger.kernel.org Subject: [PATCH] KEYS: trusted: Fix TPM teardown ordering Date: Fri, 31 Jul 2026 22:09:24 +0800 Message-ID: <20260731140925.2973492-1-nicoyip.dev@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit trusted_tpm_exit() drops the TPM chip reference and frees the digest array before unregistering the trusted key type. key_type_lookup() holds key_types_sem for reading until the key operation finishes, while unregister_key_type() takes it for writing. It therefore provides the synchronization point that must precede backend teardown. The current order permits this interleaving: CPU 0 CPU 1 trusted_tpm_exit() key_type_lookup("trusted") put_device(&chip->dev) trusted_tpm_seal() kfree(digests) pcrlock() unregister_key_type() tpm_pcr_extend(..., digests) CPU 1 can consequently dereference the freed digest array. The chip can also be released before callbacks stop using it. KASAN reported: BUG: KASAN: slab-use-after-free in tpm_pcr_extend+0x1f0/0x200 Read of size 2 at addr ffff88810872d000 by task poc/89 Call Trace: tpm_pcr_extend+0x1f0/0x200 pcrlock+0x42/0x70 [trusted] trusted_tpm_seal+0x1b6/0x570 [trusted] trusted_instantiate+0x293/0x340 [trusted] __key_instantiate_and_link+0xb2/0x2b0 __key_create_or_update+0x61e/0xb50 __do_sys_add_key+0x1b8/0x310 Allocated by task 88: __kmalloc_noprof+0x1a7/0x490 do_one_initcall+0xa1/0x390 do_init_module+0x2df/0x840 Freed by task 90: kfree+0x131/0x3c0 trusted_tpm_exit+0x59/0xa0 [trusted] __do_sys_delete_module+0x346/0x510 Move unregister_key_type() before releasing either resource. This stops new lookups and waits for in-flight key operations to finish before the backend state is destroyed. Fixes: 0b6cf6b97b7e ("tpm: pass an array of tpm_extend_digest structures to tpm_pcr_extend()") Cc: stable@vger.kernel.org Signed-off-by: Chengfeng Ye --- security/keys/trusted-keys/trusted_tpm1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c index 13513819991e..8f57c6111e7e 100644 --- a/security/keys/trusted-keys/trusted_tpm1.c +++ b/security/keys/trusted-keys/trusted_tpm1.c @@ -987,9 +987,9 @@ static int __init trusted_tpm_init(void) static void trusted_tpm_exit(void) { if (chip) { + unregister_key_type(&key_type_trusted); put_device(&chip->dev); kfree(digests); - unregister_key_type(&key_type_trusted); } } -- 2.43.0