linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: X86 ML <x86@kernel.org>
Cc: "Maciej S . Szmigiero" <mail@maciej.szmigiero.name>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH 02/16] x86/microcode/AMD: Add microcode container verification
Date: Wed,  7 Nov 2018 18:02:04 +0100	[thread overview]
Message-ID: <20181107170218.7596-3-bp@alien8.de> (raw)
In-Reply-To: <20181107170218.7596-1-bp@alien8.de>

From: "Maciej S. Szmigiero" <mail@maciej.szmigiero.name>

Add container and patch verification functions to the AMD microcode
update driver.

These functions check whether a passed buffer contains the relevant
structure, whether it isn't truncated and (for actual microcode patches)
whether the size of a patch is not too large for a particular CPU family.
By adding these checks as separate functions the actual microcode loading
code won't get interspersed with a lot of checks and so will be more
readable.

 [ bp: Make all pr_err() calls into pr_debug(). ]

Signed-off-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
Cc: x86-ml <x86@kernel.org>
Link: http://lkml.kernel.org/r/3014e96c82cd90761b4601bd2cfe59c4119e46a7.1529424596.git.mail@maciej.szmigiero.name
[ Drop the verify_patch() bits. ]
Signed-off-by: Borislav Petkov <bp@suse.de>
---
 arch/x86/kernel/cpu/microcode/amd.c | 101 ++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index b44c9029860c..36aeb01b3ae8 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -73,6 +73,107 @@ static u16 find_equiv_id(struct equiv_cpu_entry *equiv_table, u32 sig)
 	return 0;
 }
 
+/*
+ * Check whether there is a valid microcode container file at the beginning
+ * of @buf of size @buf_size. Set @early to use this function in the early path.
+ */
+static bool verify_container(const u8 *buf, size_t buf_size, bool early)
+{
+	u32 cont_magic;
+
+	if (buf_size <= CONTAINER_HDR_SZ) {
+		if (!early)
+			pr_debug("Truncated microcode container header.\n");
+
+		return false;
+	}
+
+	cont_magic = *(const u32 *)buf;
+	if (cont_magic != UCODE_MAGIC) {
+		if (!early)
+			pr_debug("Invalid magic value (0x%08x).\n", cont_magic);
+
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * Check whether there is a valid, non-truncated CPU equivalence table at the
+ * beginning of @buf of size @buf_size. Set @early to use this function in the
+ * early path.
+ */
+static bool verify_equivalence_table(const u8 *buf, size_t buf_size, bool early)
+{
+	const u32 *hdr = (const u32 *)buf;
+	u32 cont_type, equiv_tbl_len;
+
+	if (!verify_container(buf, buf_size, early))
+		return false;
+
+	cont_type = hdr[1];
+	if (cont_type != UCODE_EQUIV_CPU_TABLE_TYPE) {
+		if (!early)
+			pr_debug("Wrong microcode container equivalence table type: %u.\n",
+			       cont_type);
+
+		return false;
+	}
+
+	buf_size -= CONTAINER_HDR_SZ;
+
+	equiv_tbl_len = hdr[2];
+	if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) ||
+	    buf_size < equiv_tbl_len) {
+		if (!early)
+			pr_debug("Truncated equivalence table.\n");
+
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * 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.
+ */
+static bool verify_patch_section(const u8 *buf, size_t buf_size, bool early)
+{
+	const u32 *hdr;
+	u32 patch_type, patch_size;
+
+	if (buf_size < SECTION_HDR_SIZE) {
+		if (!early)
+			pr_debug("Truncated patch section.\n");
+
+		return false;
+	}
+
+	hdr = (const u32 *)buf;
+	patch_type = hdr[0];
+	patch_size = hdr[1];
+
+	if (patch_type != UCODE_UCODE_TYPE) {
+		if (!early)
+			pr_debug("Invalid type field (0x%x) in container file section header.\n",
+				patch_type);
+
+		return false;
+	}
+
+	if (patch_size < sizeof(struct microcode_header_amd)) {
+		if (!early)
+			pr_debug("Patch of size %u too short.\n", patch_size);
+
+		return false;
+	}
+
+	return true;
+}
+
 /*
  * This scans the ucode blob for the proper container as we can have multiple
  * containers glued together. Returns the equivalence ID from the equivalence
-- 
2.19.1


  parent reply	other threads:[~2018-11-07 17:02 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 ` Borislav Petkov [this message]
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:x86/microcode] " tip-bot for Borislav Petkov
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=20181107170218.7596-3-bp@alien8.de \
    --to=bp@alien8.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mail@maciej.szmigiero.name \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).