Skip to content

Commit 7e94a7b

Browse files
thejhsuryasaimadhu
authored andcommitted
x86/microcode/intel: Refactor Intel microcode blob loading
Change generic_load_microcode() to use the iov_iter API instead of a clumsy open-coded version which has to pay attention to __user data or kernel data, depending on the loading method. This allows to avoid explicit casting between user and kernel pointers. Because the iov_iter API makes it hard to read the same location twice, as a side effect, also fix a double-read of the microcode header (which could e.g. lead to out-of-bounds reads in microcode_sanity_check()). Not that it matters much, only root is allowed to load microcode anyway... [ bp: Massage a bit, sort function-local variables. ] Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: x86-ml <x86@kernel.org> Link: https://lkml.kernel.org/r/20190404111128.131157-1-jannh@google.com
1 parent 79a3aaa commit 7e94a7b

1 file changed

Lines changed: 36 additions & 35 deletions

File tree

  • arch/x86/kernel/cpu/microcode

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/kernel.h>
3232
#include <linux/slab.h>
3333
#include <linux/cpu.h>
34+
#include <linux/uio.h>
3435
#include <linux/mm.h>
3536

3637
#include <asm/microcode_intel.h>
@@ -861,32 +862,33 @@ static enum ucode_state apply_microcode_intel(int cpu)
861862
return ret;
862863
}
863864

864-
static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
865-
int (*get_ucode_data)(void *, const void *, size_t))
865+
static enum ucode_state generic_load_microcode(int cpu, struct iov_iter *iter)
866866
{
867867
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
868-
u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
869-
int new_rev = uci->cpu_sig.rev;
870-
unsigned int leftover = size;
871868
unsigned int curr_mc_size = 0, new_mc_size = 0;
872-
unsigned int csig, cpf;
873869
enum ucode_state ret = UCODE_OK;
870+
int new_rev = uci->cpu_sig.rev;
871+
u8 *new_mc = NULL, *mc = NULL;
872+
unsigned int csig, cpf;
874873

875-
while (leftover) {
874+
while (iov_iter_count(iter)) {
876875
struct microcode_header_intel mc_header;
877-
unsigned int mc_size;
876+
unsigned int mc_size, data_size;
877+
u8 *data;
878878

879-
if (leftover < sizeof(mc_header)) {
880-
pr_err("error! Truncated header in microcode data file\n");
879+
if (!copy_from_iter_full(&mc_header, sizeof(mc_header), iter)) {
880+
pr_err("error! Truncated or inaccessible header in microcode data file\n");
881881
break;
882882
}
883883

884-
if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
885-
break;
886-
887884
mc_size = get_totalsize(&mc_header);
888-
if (!mc_size || mc_size > leftover) {
889-
pr_err("error! Bad data in microcode data file\n");
885+
if (mc_size < sizeof(mc_header)) {
886+
pr_err("error! Bad data in microcode data file (totalsize too small)\n");
887+
break;
888+
}
889+
data_size = mc_size - sizeof(mc_header);
890+
if (data_size > iov_iter_count(iter)) {
891+
pr_err("error! Bad data in microcode data file (truncated file?)\n");
890892
break;
891893
}
892894

@@ -899,7 +901,9 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
899901
curr_mc_size = mc_size;
900902
}
901903

902-
if (get_ucode_data(mc, ucode_ptr, mc_size) ||
904+
memcpy(mc, &mc_header, sizeof(mc_header));
905+
data = mc + sizeof(mc_header);
906+
if (!copy_from_iter_full(data, data_size, iter) ||
903907
microcode_sanity_check(mc, 1) < 0) {
904908
break;
905909
}
@@ -914,14 +918,11 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
914918
mc = NULL; /* trigger new vmalloc */
915919
ret = UCODE_NEW;
916920
}
917-
918-
ucode_ptr += mc_size;
919-
leftover -= mc_size;
920921
}
921922

922923
vfree(mc);
923924

924-
if (leftover) {
925+
if (iov_iter_count(iter)) {
925926
vfree(new_mc);
926927
return UCODE_ERROR;
927928
}
@@ -945,12 +946,6 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
945946
return ret;
946947
}
947948

948-
static int get_ucode_fw(void *to, const void *from, size_t n)
949-
{
950-
memcpy(to, from, n);
951-
return 0;
952-
}
953-
954949
static bool is_blacklisted(unsigned int cpu)
955950
{
956951
struct cpuinfo_x86 *c = &cpu_data(cpu);
@@ -977,10 +972,12 @@ static bool is_blacklisted(unsigned int cpu)
977972
static enum ucode_state request_microcode_fw(int cpu, struct device *device,
978973
bool refresh_fw)
979974
{
980-
char name[30];
981975
struct cpuinfo_x86 *c = &cpu_data(cpu);
982976
const struct firmware *firmware;
977+
struct iov_iter iter;
983978
enum ucode_state ret;
979+
struct kvec kvec;
980+
char name[30];
984981

985982
if (is_blacklisted(cpu))
986983
return UCODE_NFOUND;
@@ -993,26 +990,30 @@ static enum ucode_state request_microcode_fw(int cpu, struct device *device,
993990
return UCODE_NFOUND;
994991
}
995992

996-
ret = generic_load_microcode(cpu, (void *)firmware->data,
997-
firmware->size, &get_ucode_fw);
993+
kvec.iov_base = (void *)firmware->data;
994+
kvec.iov_len = firmware->size;
995+
iov_iter_kvec(&iter, WRITE, &kvec, 1, firmware->size);
996+
ret = generic_load_microcode(cpu, &iter);
998997

999998
release_firmware(firmware);
1000999

10011000
return ret;
10021001
}
10031002

1004-
static int get_ucode_user(void *to, const void *from, size_t n)
1005-
{
1006-
return copy_from_user(to, from, n);
1007-
}
1008-
10091003
static enum ucode_state
10101004
request_microcode_user(int cpu, const void __user *buf, size_t size)
10111005
{
1006+
struct iov_iter iter;
1007+
struct iovec iov;
1008+
10121009
if (is_blacklisted(cpu))
10131010
return UCODE_NFOUND;
10141011

1015-
return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
1012+
iov.iov_base = (void __user *)buf;
1013+
iov.iov_len = size;
1014+
iov_iter_init(&iter, WRITE, &iov, 1, size);
1015+
1016+
return generic_load_microcode(cpu, &iter);
10161017
}
10171018

10181019
static struct microcode_ops microcode_intel_ops = {

0 commit comments

Comments
 (0)