Intel microcode: Return when `microcode_updates` is `NULL`

Add a safety check in function `intel_update_microcode` to return when
accidentally `NULL` is passed as `microcode_updates`, which would lead
to a null pointer dereference later on.

    for (c = microcode_updates; m->hdrver; m = (const struct microcode *)c) {

While at it, use `return NULL` for clarity in function
`intel_microcode_find` and include the header file `stddef.h`. for it.

The review of this patch had some more discussion on adding more
comments and more detailed error messages. But this should be done in
a separate patch.

For clarity here some history, on how this was found and what caused
the discussion and confusion.

Originally when Vladimir made this improvement, selecting
`CPU_MICROCODE_IN_CBFS` in Kconfig but not having the microcode blob
`cpu_microcode_blob.bin` in CBFS resulted in a null pointer dereference
later on causing a crash.

    for (c = microcode_updates; m->hdrver; m = (const struct microcode *)c) {

Vladimir fixed this by returning if `microcode_updates` is `NULL`,
that means no file is found and successfully tested this on his
Lenovo X201.

When pushing the patch to Gerrit for review, the code was rewritten
though by Aaron in commit »intel microcode: split up microcode loading
stages« (98ffb426) [1], which also returns when no file is found. So
the other parts of the code were checked and the safety check as
described above is added.

[1] http://review.coreboot.org/2778

Change-Id: I6e18fd37256910bf047061e4633a66cf29ad7b69
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Signed-off-by: Paul Menzel <paulepanter@users.sourceforge.net>
Reviewed-on: http://review.coreboot.org/2990
Reviewed-by: Aaron Durbin <adurbin@google.com>
Tested-by: build bot (Jenkins)
This commit is contained in:
Vladimir Serbinenko 2013-03-30 12:04:23 +01:00 committed by Ronald G. Minnich
parent aee444f453
commit 2c88cc0696
1 changed files with 9 additions and 1 deletions

View File

@ -21,6 +21,7 @@
/* Microcode update for Intel PIII and later CPUs */ /* Microcode update for Intel PIII and later CPUs */
#include <stdint.h> #include <stdint.h>
#include <stddef.h>
#if !defined(__ROMCC__) #if !defined(__ROMCC__)
#include <console/console.h> #include <console/console.h>
#endif #endif
@ -131,7 +132,7 @@ const void *intel_microcode_find(void)
#endif #endif
if (!microcode_updates) if (!microcode_updates)
return microcode_updates; return NULL;
/* CPUID sets MSR 0x8B iff a microcode update has been loaded. */ /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
msr.lo = 0; msr.lo = 0;
@ -202,6 +203,13 @@ void intel_update_microcode(const void *microcode_updates)
const char *c; const char *c;
msr_t msr; msr_t msr;
if (!microcode_updates) {
#if !defined(__ROMCC__)
printk(BIOS_WARNING, "No microcode updates found.\n");
#endif
return;
}
/* CPUID sets MSR 0x8B iff a microcode update has been loaded. */ /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
msr.lo = 0; msr.lo = 0;
msr.hi = 0; msr.hi = 0;