Skip to content

Commit 2b8d34b

Browse files
committed
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
1 parent 70887cb commit 2b8d34b

1 file changed

Lines changed: 54 additions & 25 deletions

File tree

  • arch/x86/kernel/cpu/microcode

‎arch/x86/kernel/cpu/microcode/amd.c‎

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,15 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size, bool early)
139139
* Check whether there is a valid, non-truncated microcode patch section at the
140140
* beginning of @buf of size @buf_size. Set @early to use this function in the
141141
* early path.
142+
*
143+
* On success, @sh_psize returns the patch size according to the section header,
144+
* to the caller.
142145
*/
143-
static bool verify_patch_section(const u8 *buf, size_t buf_size, bool early)
146+
static bool
147+
__verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize, bool early)
144148
{
149+
u32 p_type, p_size;
145150
const u32 *hdr;
146-
u32 patch_type, patch_size;
147151

148152
if (buf_size < SECTION_HDR_SIZE) {
149153
if (!early)
@@ -153,24 +157,26 @@ static bool verify_patch_section(const u8 *buf, size_t buf_size, bool early)
153157
}
154158

155159
hdr = (const u32 *)buf;
156-
patch_type = hdr[0];
157-
patch_size = hdr[1];
160+
p_type = hdr[0];
161+
p_size = hdr[1];
158162

159-
if (patch_type != UCODE_UCODE_TYPE) {
163+
if (p_type != UCODE_UCODE_TYPE) {
160164
if (!early)
161165
pr_debug("Invalid type field (0x%x) in container file section header.\n",
162-
patch_type);
166+
p_type);
163167

164168
return false;
165169
}
166170

167-
if (patch_size < sizeof(struct microcode_header_amd)) {
171+
if (p_size < sizeof(struct microcode_header_amd)) {
168172
if (!early)
169-
pr_debug("Patch of size %u too short.\n", patch_size);
173+
pr_debug("Patch of size %u too short.\n", p_size);
170174

171175
return false;
172176
}
173177

178+
*sh_psize = p_size;
179+
174180
return true;
175181
}
176182

@@ -181,7 +187,7 @@ static bool verify_patch_section(const u8 *buf, size_t buf_size, bool early)
181187
* header.
182188
*/
183189
static unsigned int
184-
verify_patch_size(u8 family, u32 sh_psize, unsigned int buf_size)
190+
__verify_patch_size(u8 family, u32 sh_psize, unsigned int buf_size)
185191
{
186192
u32 max_size;
187193

@@ -212,6 +218,34 @@ verify_patch_size(u8 family, u32 sh_psize, unsigned int buf_size)
212218
return sh_psize;
213219
}
214220

221+
static unsigned int
222+
verify_patch(u8 family, const u8 *buf, unsigned int buf_size, bool early)
223+
{
224+
u32 sh_psize;
225+
226+
if (!__verify_patch_section(buf, buf_size, &sh_psize, early))
227+
return 0;
228+
/*
229+
* The section header length is not included in this indicated size
230+
* but is present in the leftover file length so we need to subtract
231+
* it before passing this value to the function below.
232+
*/
233+
buf_size -= SECTION_HDR_SIZE;
234+
235+
/*
236+
* Check if the remaining buffer is big enough to contain a patch of
237+
* size sh_psize, as the section claims.
238+
*/
239+
if (buf_size < sh_psize) {
240+
if (!early)
241+
pr_debug("Patch of size %u truncated.\n", sh_psize);
242+
243+
return 0;
244+
}
245+
246+
return __verify_patch_size(family, sh_psize, buf_size);
247+
}
248+
215249
/*
216250
* This scans the ucode blob for the proper container as we can have multiple
217251
* containers glued together. Returns the equivalence ID from the equivalence
@@ -687,22 +721,28 @@ static void cleanup(void)
687721
}
688722

689723
/*
690-
* We return the current size even if some of the checks failed so that
724+
* Return a non-negative value even if some of the checks failed so that
691725
* we can skip over the next patch. If we return a negative value, we
692726
* signal a grave error like a memory allocation has failed and the
693727
* driver cannot continue functioning normally. In such cases, we tear
694728
* down everything we've used up so far and exit.
695729
*/
696730
static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
697731
{
698-
unsigned int sh_psize, crnt_size, ret;
699732
struct microcode_header_amd *mc_hdr;
733+
unsigned int patch_size, crnt_size;
700734
struct ucode_patch *patch;
701735
u32 proc_fam;
702736
u16 proc_id;
703737

704-
sh_psize = *(u32 *)(fw + 4);
705-
crnt_size = sh_psize + SECTION_HDR_SIZE;
738+
patch_size = verify_patch(family, fw, leftover, false);
739+
if (!patch_size) {
740+
pr_debug("Patch size mismatch.\n");
741+
return 1;
742+
}
743+
744+
/* If initial rough pokes pass, we can start looking at the header. */
745+
crnt_size = patch_size + SECTION_HDR_SIZE;
706746
mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
707747
proc_id = mc_hdr->processor_rev_id;
708748

@@ -723,24 +763,13 @@ static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
723763
return crnt_size;
724764
}
725765

726-
/*
727-
* The section header length is not included in this indicated size
728-
* but is present in the leftover file length so we need to subtract
729-
* it before passing this value to the function below.
730-
*/
731-
ret = verify_patch_size(family, sh_psize, leftover - SECTION_HDR_SIZE);
732-
if (!ret) {
733-
pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
734-
return crnt_size;
735-
}
736-
737766
patch = kzalloc(sizeof(*patch), GFP_KERNEL);
738767
if (!patch) {
739768
pr_err("Patch allocation failure.\n");
740769
return -EINVAL;
741770
}
742771

743-
patch->data = kmemdup(fw + SECTION_HDR_SIZE, sh_psize, GFP_KERNEL);
772+
patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
744773
if (!patch->data) {
745774
pr_err("Patch data allocation failure.\n");
746775
kfree(patch);

0 commit comments

Comments
 (0)