public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Borislav Petkov <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, hpa@zytor.com, tglx@linutronix.de,
	bp@suse.de, mingo@kernel.org
Subject: [tip:x86/microcode] x86/microcode/AMD: Concentrate patch verification
Date: Mon, 19 Nov 2018 02:15:46 -0800	[thread overview]
Message-ID: <tip-2b8d34b1ece506a9bbd47b56d266ee020a0c65ac@git.kernel.org> (raw)
In-Reply-To: <20181107170218.7596-7-bp@alien8.de>

Commit-ID:  2b8d34b1ece506a9bbd47b56d266ee020a0c65ac
Gitweb:     https://git.kernel.org/tip/2b8d34b1ece506a9bbd47b56d266ee020a0c65ac
Author:     Borislav Petkov <bp@suse.de>
AuthorDate: Thu, 19 Jul 2018 15:32:42 +0200
Committer:  Borislav Petkov <bp@suse.de>
CommitDate: Mon, 19 Nov 2018 10:51:05 +0100

x86/microcode/AMD: Concentrate patch verification

Add a verify_patch() function which tries to sanity-check many aspects
of a microcode patch supplied by an outside container before attempting
a load.

Prepend all sub-functions' names which verify an aspect of a microcode
patch with "__".

Call it in verify_and_add_patch() *before* looking at the microcode
header.

Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: x86@kernel.org
Link: https://lkml.kernel.org/r/20181107170218.7596-7-bp@alien8.de
---
 arch/x86/kernel/cpu/microcode/amd.c | 79 +++++++++++++++++++++++++------------
 1 file changed, 54 insertions(+), 25 deletions(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index a94a15aacfe7..8f012a7f88c4 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -139,11 +139,15 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size, bool early)
  * Check whether there is a valid, non-truncated microcode patch section at the
  * beginning of @buf of size @buf_size. Set @early to use this function in the
  * early path.
+ *
+ * On success, @sh_psize returns the patch size according to the section header,
+ * to the caller.
  */
-static bool verify_patch_section(const u8 *buf, size_t buf_size, bool early)
+static bool
+__verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize, bool early)
 {
+	u32 p_type, p_size;
 	const u32 *hdr;
-	u32 patch_type, patch_size;
 
 	if (buf_size < SECTION_HDR_SIZE) {
 		if (!early)
@@ -153,24 +157,26 @@ static bool verify_patch_section(const u8 *buf, size_t buf_size, bool early)
 	}
 
 	hdr = (const u32 *)buf;
-	patch_type = hdr[0];
-	patch_size = hdr[1];
+	p_type = hdr[0];
+	p_size = hdr[1];
 
-	if (patch_type != UCODE_UCODE_TYPE) {
+	if (p_type != UCODE_UCODE_TYPE) {
 		if (!early)
 			pr_debug("Invalid type field (0x%x) in container file section header.\n",
-				patch_type);
+				p_type);
 
 		return false;
 	}
 
-	if (patch_size < sizeof(struct microcode_header_amd)) {
+	if (p_size < sizeof(struct microcode_header_amd)) {
 		if (!early)
-			pr_debug("Patch of size %u too short.\n", patch_size);
+			pr_debug("Patch of size %u too short.\n", p_size);
 
 		return false;
 	}
 
+	*sh_psize = p_size;
+
 	return true;
 }
 
@@ -181,7 +187,7 @@ static bool verify_patch_section(const u8 *buf, size_t buf_size, bool early)
  * header.
  */
 static unsigned int
-verify_patch_size(u8 family, u32 sh_psize, unsigned int buf_size)
+__verify_patch_size(u8 family, u32 sh_psize, unsigned int buf_size)
 {
 	u32 max_size;
 
@@ -212,6 +218,34 @@ verify_patch_size(u8 family, u32 sh_psize, unsigned int buf_size)
 	return sh_psize;
 }
 
+static unsigned int
+verify_patch(u8 family, const u8 *buf, unsigned int buf_size, bool early)
+{
+	u32 sh_psize;
+
+	if (!__verify_patch_section(buf, buf_size, &sh_psize, early))
+		return 0;
+	/*
+	 * The section header length is not included in this indicated size
+	 * but is present in the leftover file length so we need to subtract
+	 * it before passing this value to the function below.
+	 */
+	buf_size -= SECTION_HDR_SIZE;
+
+	/*
+	 * Check if the remaining buffer is big enough to contain a patch of
+	 * size sh_psize, as the section claims.
+	 */
+	if (buf_size < sh_psize) {
+		if (!early)
+			pr_debug("Patch of size %u truncated.\n", sh_psize);
+
+		return 0;
+	}
+
+	return __verify_patch_size(family, sh_psize, buf_size);
+}
+
 /*
  * This scans the ucode blob for the proper container as we can have multiple
  * containers glued together. Returns the equivalence ID from the equivalence
@@ -687,7 +721,7 @@ static void cleanup(void)
 }
 
 /*
- * We return the current size even if some of the checks failed so that
+ * Return a non-negative value even if some of the checks failed so that
  * we can skip over the next patch. If we return a negative value, we
  * signal a grave error like a memory allocation has failed and the
  * driver cannot continue functioning normally. In such cases, we tear
@@ -695,14 +729,20 @@ static void cleanup(void)
  */
 static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
 {
-	unsigned int sh_psize, crnt_size, ret;
 	struct microcode_header_amd *mc_hdr;
+	unsigned int patch_size, crnt_size;
 	struct ucode_patch *patch;
 	u32 proc_fam;
 	u16 proc_id;
 
-	sh_psize    = *(u32 *)(fw + 4);
-	crnt_size   = sh_psize + SECTION_HDR_SIZE;
+	patch_size = verify_patch(family, fw, leftover, false);
+	if (!patch_size) {
+		pr_debug("Patch size mismatch.\n");
+		return 1;
+	}
+
+	/* If initial rough pokes pass, we can start looking at the header. */
+	crnt_size   = patch_size + SECTION_HDR_SIZE;
 	mc_hdr	    = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
 	proc_id	    = mc_hdr->processor_rev_id;
 
@@ -723,24 +763,13 @@ static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
 		return crnt_size;
 	}
 
-	/*
-	 * The section header length is not included in this indicated size
-	 * but is present in the leftover file length so we need to subtract
-	 * it before passing this value to the function below.
-	 */
-	ret = verify_patch_size(family, sh_psize, leftover - SECTION_HDR_SIZE);
-	if (!ret) {
-		pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
-		return crnt_size;
-	}
-
 	patch = kzalloc(sizeof(*patch), GFP_KERNEL);
 	if (!patch) {
 		pr_err("Patch allocation failure.\n");
 		return -EINVAL;
 	}
 
-	patch->data = kmemdup(fw + SECTION_HDR_SIZE, sh_psize, GFP_KERNEL);
+	patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
 	if (!patch->data) {
 		pr_err("Patch data allocation failure.\n");
 		kfree(patch);

  reply	other threads:[~2018-11-19 10:15 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-07 17:02 [PATCH 00/16] x86/microcode/AMD: Improve container verification Borislav Petkov
2018-11-07 17:02 ` [PATCH 01/16] x86/microcode/AMD: Subtract SECTION_HDR_SIZE from file leftover length Borislav Petkov
2018-11-07 17:02 ` [PATCH 02/16] x86/microcode/AMD: Add microcode container verification Borislav Petkov
2018-11-07 17:02 ` [PATCH 03/16] x86/microcode/AMD: Move verify_patch_size() up in the file Borislav Petkov
2018-11-19 10:14   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 04/16] x86/microcode/AMD: Clean up per-family patch size checks Borislav Petkov
2018-11-19 10:14   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 05/16] x86/microcode/AMD: Cleanup verify_patch_size() more Borislav Petkov
2018-11-19 10:15   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 06/16] x86/microcode/AMD: Concentrate patch verification Borislav Petkov
2018-11-19 10:15   ` tip-bot for Borislav Petkov [this message]
2018-11-07 17:02 ` [PATCH 07/16] x86/microcode/AMD: Simplify patch family detection Borislav Petkov
2018-11-19 10:16   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 08/16] x86/microcode/AMD: Move patch family check to verify_patch() Borislav Petkov
2018-11-19 10:16   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 09/16] x86/microcode/AMD: Move chipset-specific check into verify_patch() Borislav Petkov
2018-11-07 17:02 ` [PATCH 10/16] x86/microcode/AMD: Change verify_patch()'s return value Borislav Petkov
2018-11-19 10:18   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 11/16] x86/microcode/AMD: Convert early parser to the new verification routines Borislav Petkov
2018-11-19 10:18   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 12/16] x86/microcode/AMD: Fix container size's type Borislav Petkov
2018-11-10 20:59   ` kbuild test robot
2018-11-19 10:19   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-07 17:02 ` [PATCH 13/16] x86/microcode/AMD: Check microcode container data in the late loader Borislav Petkov
2018-11-19 10:19   ` [tip:x86/microcode] " tip-bot for Maciej S. Szmigiero
2018-11-07 17:02 ` [PATCH 14/16] x86/microcode/AMD: Convert CPU equivalence table variable into a struct Borislav Petkov
2018-11-19 10:20   ` [tip:x86/microcode] " tip-bot for Maciej S. Szmigiero
2018-11-07 17:02 ` [PATCH 15/16] x86/microcode/AMD: Check the equivalence table size when scanning it Borislav Petkov
2018-11-19 10:20   ` [tip:x86/microcode] " tip-bot for Maciej S. Szmigiero
2018-11-07 17:02 ` [PATCH 16/16] x86/microcode/AMD: Update copyright Borislav Petkov
2018-11-19 10:21   ` [tip:x86/microcode] " tip-bot for Borislav Petkov
2018-11-25  9:50 ` [PATCH 00/16] x86/microcode/AMD: Improve container verification Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-2b8d34b1ece506a9bbd47b56d266ee020a0c65ac@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=bp@suse.de \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox