x86/microcode/intel: Do the mc_saved_src NULL check first
... and only then deref it. Also, shorten some variable names and rename others so as to diminish the ubiquitous presence of the "mc_" prefix everywhere and make it a bit more readable. Use kcalloc so that we don't kfree() uninitialized memory on the unwind path, as suggested by Quentin. Signed-off-by: Borislav Petkov <bp@suse.de> Cc: Quentin Casasnovas <quentin.casasnovas@oracle.com>
This commit is contained in:
@@ -203,7 +203,7 @@ save_microcode(struct mc_saved_data *mc_saved_data,
|
|||||||
unsigned int mc_saved_count)
|
unsigned int mc_saved_count)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
struct microcode_intel **mc_saved_p;
|
struct microcode_intel **saved_ptr;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (!mc_saved_count)
|
if (!mc_saved_count)
|
||||||
@@ -212,39 +212,45 @@ save_microcode(struct mc_saved_data *mc_saved_data,
|
|||||||
/*
|
/*
|
||||||
* Copy new microcode data.
|
* Copy new microcode data.
|
||||||
*/
|
*/
|
||||||
mc_saved_p = kmalloc(mc_saved_count*sizeof(struct microcode_intel *),
|
saved_ptr = kcalloc(mc_saved_count, sizeof(struct microcode_intel *), GFP_KERNEL);
|
||||||
GFP_KERNEL);
|
if (!saved_ptr)
|
||||||
if (!mc_saved_p)
|
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
for (i = 0; i < mc_saved_count; i++) {
|
for (i = 0; i < mc_saved_count; i++) {
|
||||||
struct microcode_intel *mc = mc_saved_src[i];
|
struct microcode_header_intel *mc_hdr;
|
||||||
struct microcode_header_intel *mc_header = &mc->hdr;
|
struct microcode_intel *mc;
|
||||||
unsigned long mc_size = get_totalsize(mc_header);
|
unsigned long size;
|
||||||
mc_saved_p[i] = kmalloc(mc_size, GFP_KERNEL);
|
|
||||||
if (!mc_saved_p[i]) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
if (!mc_saved_src[i]) {
|
if (!mc_saved_src[i]) {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
memcpy(mc_saved_p[i], mc, mc_size);
|
|
||||||
|
mc = mc_saved_src[i];
|
||||||
|
mc_hdr = &mc->hdr;
|
||||||
|
size = get_totalsize(mc_hdr);
|
||||||
|
|
||||||
|
saved_ptr[i] = kmalloc(size, GFP_KERNEL);
|
||||||
|
if (!saved_ptr[i]) {
|
||||||
|
ret = -ENOMEM;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(saved_ptr[i], mc, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Point to newly saved microcode.
|
* Point to newly saved microcode.
|
||||||
*/
|
*/
|
||||||
mc_saved_data->mc_saved = mc_saved_p;
|
mc_saved_data->mc_saved = saved_ptr;
|
||||||
mc_saved_data->mc_saved_count = mc_saved_count;
|
mc_saved_data->mc_saved_count = mc_saved_count;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
for (j = 0; j <= i; j++)
|
for (j = 0; j <= i; j++)
|
||||||
kfree(mc_saved_p[j]);
|
kfree(saved_ptr[j]);
|
||||||
kfree(mc_saved_p);
|
kfree(saved_ptr);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user