Skip to content

Commit 1a371e6

Browse files
yu-chen-surfsuryasaimadhu
authored andcommitted
x86/microcode/intel: Check patch signature before saving microcode for early loading
Currently, scan_microcode() leverages microcode_matches() to check if the microcode matches the CPU by comparing the family and model. However, the processor stepping and flags of the microcode signature should also be considered when saving a microcode patch for early update. Use find_matching_signature() in scan_microcode() and get rid of the now-unused microcode_matches() which is a good cleanup in itself. Complete the verification of the patch being saved for early loading in save_microcode_patch() directly. This needs to be done there too because save_mc_for_early() will call save_microcode_patch() too. The second reason why this needs to be done is because the loader still tries to support, at least hypothetically, mixed-steppings systems and thus adds all patches to the cache that belong to the same CPU model albeit with different steppings. For example: microcode: CPU: sig=0x906ec, pf=0x2, rev=0xd6 microcode: mc_saved[0]: sig=0x906e9, pf=0x2a, rev=0xd6, total size=0x19400, date = 2020-04-23 microcode: mc_saved[1]: sig=0x906ea, pf=0x22, rev=0xd6, total size=0x19000, date = 2020-04-27 microcode: mc_saved[2]: sig=0x906eb, pf=0x2, rev=0xd6, total size=0x19400, date = 2020-04-23 microcode: mc_saved[3]: sig=0x906ec, pf=0x22, rev=0xd6, total size=0x19000, date = 2020-04-27 microcode: mc_saved[4]: sig=0x906ed, pf=0x22, rev=0xd6, total size=0x19400, date = 2020-04-23 The patch which is being saved for early loading, however, can only be the one which fits the CPU this runs on so do the signature verification before saving. [ bp: Do signature verification in save_microcode_patch() and rewrite commit message. ] Fixes: ec400dd ("x86/microcode_intel_early.c: Early update ucode on Intel's CPU") Signed-off-by: Chen Yu <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Cc: [email protected] Link: https://bugzilla.kernel.org/show_bug.cgi?id=208535 Link: https://lkml.kernel.org/r/[email protected]
1 parent 8986f22 commit 1a371e6

File tree

1 file changed

+10
-53
lines changed
  • arch/x86/kernel/cpu/microcode

1 file changed

+10
-53
lines changed

arch/x86/kernel/cpu/microcode/intel.c

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -100,53 +100,6 @@ static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev
100100
return find_matching_signature(mc, csig, cpf);
101101
}
102102

103-
/*
104-
* Given CPU signature and a microcode patch, this function finds if the
105-
* microcode patch has matching family and model with the CPU.
106-
*
107-
* %true - if there's a match
108-
* %false - otherwise
109-
*/
110-
static bool microcode_matches(struct microcode_header_intel *mc_header,
111-
unsigned long sig)
112-
{
113-
unsigned long total_size = get_totalsize(mc_header);
114-
unsigned long data_size = get_datasize(mc_header);
115-
struct extended_sigtable *ext_header;
116-
unsigned int fam_ucode, model_ucode;
117-
struct extended_signature *ext_sig;
118-
unsigned int fam, model;
119-
int ext_sigcount, i;
120-
121-
fam = x86_family(sig);
122-
model = x86_model(sig);
123-
124-
fam_ucode = x86_family(mc_header->sig);
125-
model_ucode = x86_model(mc_header->sig);
126-
127-
if (fam == fam_ucode && model == model_ucode)
128-
return true;
129-
130-
/* Look for ext. headers: */
131-
if (total_size <= data_size + MC_HEADER_SIZE)
132-
return false;
133-
134-
ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
135-
ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
136-
ext_sigcount = ext_header->count;
137-
138-
for (i = 0; i < ext_sigcount; i++) {
139-
fam_ucode = x86_family(ext_sig->sig);
140-
model_ucode = x86_model(ext_sig->sig);
141-
142-
if (fam == fam_ucode && model == model_ucode)
143-
return true;
144-
145-
ext_sig++;
146-
}
147-
return false;
148-
}
149-
150103
static struct ucode_patch *memdup_patch(void *data, unsigned int size)
151104
{
152105
struct ucode_patch *p;
@@ -164,7 +117,7 @@ static struct ucode_patch *memdup_patch(void *data, unsigned int size)
164117
return p;
165118
}
166119

167-
static void save_microcode_patch(void *data, unsigned int size)
120+
static void save_microcode_patch(struct ucode_cpu_info *uci, void *data, unsigned int size)
168121
{
169122
struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
170123
struct ucode_patch *iter, *tmp, *p = NULL;
@@ -210,6 +163,9 @@ static void save_microcode_patch(void *data, unsigned int size)
210163
if (!p)
211164
return;
212165

166+
if (!find_matching_signature(p->data, uci->cpu_sig.sig, uci->cpu_sig.pf))
167+
return;
168+
213169
/*
214170
* Save for early loading. On 32-bit, that needs to be a physical
215171
* address as the APs are running from physical addresses, before
@@ -344,13 +300,14 @@ scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
344300

345301
size -= mc_size;
346302

347-
if (!microcode_matches(mc_header, uci->cpu_sig.sig)) {
303+
if (!find_matching_signature(data, uci->cpu_sig.sig,
304+
uci->cpu_sig.pf)) {
348305
data += mc_size;
349306
continue;
350307
}
351308

352309
if (save) {
353-
save_microcode_patch(data, mc_size);
310+
save_microcode_patch(uci, data, mc_size);
354311
goto next;
355312
}
356313

@@ -483,14 +440,14 @@ static void show_saved_mc(void)
483440
* Save this microcode patch. It will be loaded early when a CPU is
484441
* hot-added or resumes.
485442
*/
486-
static void save_mc_for_early(u8 *mc, unsigned int size)
443+
static void save_mc_for_early(struct ucode_cpu_info *uci, u8 *mc, unsigned int size)
487444
{
488445
/* Synchronization during CPU hotplug. */
489446
static DEFINE_MUTEX(x86_cpu_microcode_mutex);
490447

491448
mutex_lock(&x86_cpu_microcode_mutex);
492449

493-
save_microcode_patch(mc, size);
450+
save_microcode_patch(uci, mc, size);
494451
show_saved_mc();
495452

496453
mutex_unlock(&x86_cpu_microcode_mutex);
@@ -935,7 +892,7 @@ static enum ucode_state generic_load_microcode(int cpu, struct iov_iter *iter)
935892
* permanent memory. So it will be loaded early when a CPU is hot added
936893
* or resumes.
937894
*/
938-
save_mc_for_early(new_mc, new_mc_size);
895+
save_mc_for_early(uci, new_mc, new_mc_size);
939896

940897
pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
941898
cpu, new_rev, uci->cpu_sig.rev);

0 commit comments

Comments
 (0)