Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3860694pxb; Tue, 17 Nov 2020 05:37:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJzeHWBHu+09yv993H/VUt/byLR8NbhR4XmGgFOu0RJ5DbFEDCDy1a1/RScysgMb9FIe+G5S X-Received: by 2002:a17:906:7c9:: with SMTP id m9mr19066956ejc.178.1605620225559; Tue, 17 Nov 2020 05:37:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605620225; cv=none; d=google.com; s=arc-20160816; b=rOoPs7FjN9LTNZOae4Sj9BBRYbObeD0zghZRyUarw6jjVqd4/c9htIQkG576hAr8zA 8YXNWs2AJi9cvI0M8KrPWGDW0Ja2oPCim1DW5SYx1l+RdG7JOTKhGggSnfWfg1Y851sc Z7xmywo/+wgiUYsLT6LFeJ5ZoKtCiGQPE8nw07Igiwah+xbf5VBqRgfXgaqCXYuV/h/e SmYKRxZloupsHQ0aVT9RorbORhGG8S5Z/o6VM+k7l1kUXaEKqLcdhg/9vfqIQ4603crd CJIl5iOD0PF2jqn9BxL31WpFzVSkic0ky1hIB7tP/2YyTJhzY1VhKWy2EXBNUmzt/yoD SyDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=F9jBCvnJM1mRLPzicYRMp7T7agZsrQRLU+z94EDFb0g=; b=iaR1sdLmfr1LtYxmtD3GXX/6tZY+Mlgy1yt7aMcaLCb2sv9sEOlZg8Tgy+/jSplmFB rXEueGvnwmeHhPZDZPHl5s07uequEM4R5ArCMKlIfXHJ4B9/t/sTmSto/QyU8al7KYuc wtPjT3mSd7Xl9y30thCHrKsKVxTKOTB8idLu1h5f+aj7/WfAgbo1vKl2gBpULjr3tYsN QUI7e4SAvvpbAKxH7DI+FWOKkBmPIMYD4aXmkXP4BzmDH/nnpQvD4D3CVShc7DYFZHRp veJwhWLxWxGNP3L2BEWcIIzqwUiEK7cx+2FOmx2s44nKM4cp52CSGnz1bWPje8G78nR7 1XJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=joZ81RrG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ga5si6586270ejc.696.2020.11.17.05.36.42; Tue, 17 Nov 2020 05:37:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=joZ81RrG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732382AbgKQNdR (ORCPT + 99 others); Tue, 17 Nov 2020 08:33:17 -0500 Received: from mail.kernel.org ([198.145.29.99]:43098 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732377AbgKQNdL (ORCPT ); Tue, 17 Nov 2020 08:33:11 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 675E82078E; Tue, 17 Nov 2020 13:33:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605619990; bh=HCe0DP9BnJK7T3S+o+8ivohKlRajP+v2pafi63MpJ3I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=joZ81RrGKc3oBo1FSFRaBsfo7i9wMm0wybtuGFWNQeYqLXkz1AKx/m2hJa9WBgJVv rzJ7hrf+WbAkfpjMeL27657lXdC5FEgm3GHtcYpQVHLDdHn3qKKYhh8gMZrdwCyp8D 91DhG3M0V/s7arpu/rkjRn/k5AhrZEG6n78XIBzE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kai-Heng Feng , "Kenneth R. Crudup" , Mimi Zohar , =?UTF-8?q?Thi=C3=A9baud=20Weksteen?= , Ard Biesheuvel , Tyler Hicks , Jarkko Sakkinen , Sasha Levin Subject: [PATCH 5.9 072/255] tpm: efi: Dont create binary_bios_measurements file for an empty log Date: Tue, 17 Nov 2020 14:03:32 +0100 Message-Id: <20201117122142.454279382@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201117122138.925150709@linuxfoundation.org> References: <20201117122138.925150709@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tyler Hicks [ Upstream commit 8ffd778aff45be760292225049e0141255d4ad6e ] Mimic the pre-existing ACPI and Device Tree event log behavior by not creating the binary_bios_measurements file when the EFI TPM event log is empty. This fixes the following NULL pointer dereference that can occur when reading /sys/kernel/security/tpm0/binary_bios_measurements after the kernel received an empty event log from the firmware: BUG: kernel NULL pointer dereference, address: 000000000000002c #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: 0000 [#1] SMP PTI CPU: 2 PID: 3932 Comm: fwupdtpmevlog Not tainted 5.9.0-00003-g629990edad62 #17 Hardware name: LENOVO 20LCS03L00/20LCS03L00, BIOS N27ET38W (1.24 ) 11/28/2019 RIP: 0010:tpm2_bios_measurements_start+0x3a/0x550 Code: 54 53 48 83 ec 68 48 8b 57 70 48 8b 1e 65 48 8b 04 25 28 00 00 00 48 89 45 d0 31 c0 48 8b 82 c0 06 00 00 48 8b 8a c8 06 00 00 <44> 8b 60 1c 48 89 4d a0 4c 89 e2 49 83 c4 20 48 83 fb 00 75 2a 49 RSP: 0018:ffffa9c901203db0 EFLAGS: 00010246 RAX: 0000000000000010 RBX: 0000000000000000 RCX: 0000000000000010 RDX: ffff8ba1eb99c000 RSI: ffff8ba1e4ce8280 RDI: ffff8ba1e4ce8258 RBP: ffffa9c901203e40 R08: ffffa9c901203dd8 R09: ffff8ba1ec443300 R10: ffffa9c901203e50 R11: 0000000000000000 R12: ffff8ba1e4ce8280 R13: ffffa9c901203ef0 R14: ffffa9c901203ef0 R15: ffff8ba1e4ce8258 FS: 00007f6595460880(0000) GS:ffff8ba1ef880000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 000000000000002c CR3: 00000007d8d18003 CR4: 00000000003706e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: ? __kmalloc_node+0x113/0x320 ? kvmalloc_node+0x31/0x80 seq_read+0x94/0x420 vfs_read+0xa7/0x190 ksys_read+0xa7/0xe0 __x64_sys_read+0x1a/0x20 do_syscall_64+0x37/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xa9 In this situation, the bios_event_log pointer in the tpm_bios_log struct was not NULL but was equal to the ZERO_SIZE_PTR (0x10) value. This was due to the following kmemdup() in tpm_read_log_efi(): int tpm_read_log_efi(struct tpm_chip *chip) { ... /* malloc EventLog space */ log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL); if (!log->bios_event_log) { ret = -ENOMEM; goto out; } ... } When log_size is zero, due to an empty event log from firmware, ZERO_SIZE_PTR is returned from kmemdup(). Upon a read of the binary_bios_measurements file, the tpm2_bios_measurements_start() function does not perform a ZERO_OR_NULL_PTR() check on the bios_event_log pointer before dereferencing it. Rather than add a ZERO_OR_NULL_PTR() check in functions that make use of the bios_event_log pointer, simply avoid creating the binary_bios_measurements_file as is done in other event log retrieval backends. Explicitly ignore all of the events in the final event log when the main event log is empty. The list of events in the final event log cannot be accurately parsed without referring to the first event in the main event log (the event log header) so the final event log is useless in such a situation. Fixes: 58cc1e4faf10 ("tpm: parse TPM event logs based on EFI table") Link: https://lore.kernel.org/linux-integrity/E1FDCCCB-CA51-4AEE-AC83-9CDE995EAE52@canonical.com/ Reported-by: Kai-Heng Feng Reported-by: Kenneth R. Crudup Reported-by: Mimi Zohar Cc: ThiƩbaud Weksteen Cc: Ard Biesheuvel Signed-off-by: Tyler Hicks Reviewed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Sasha Levin --- drivers/char/tpm/eventlog/efi.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/char/tpm/eventlog/efi.c b/drivers/char/tpm/eventlog/efi.c index 6bb023de17f1f..35229e5143cac 100644 --- a/drivers/char/tpm/eventlog/efi.c +++ b/drivers/char/tpm/eventlog/efi.c @@ -41,6 +41,11 @@ int tpm_read_log_efi(struct tpm_chip *chip) log_size = log_tbl->size; memunmap(log_tbl); + if (!log_size) { + pr_warn("UEFI TPM log area empty\n"); + return -EIO; + } + log_tbl = memremap(efi.tpm_log, sizeof(*log_tbl) + log_size, MEMREMAP_WB); if (!log_tbl) { -- 2.27.0