Skip to content

Commit d9db3a9

Browse files
superm1ij-intel
authored andcommitted
platform/x86/amd: pmf: Use device managed allocations
If setting up smart PC fails for any reason then this can lead to a double free when unloading amd-pmf. This is because dev->buf was freed but never set to NULL and is again freed in amd_pmf_remove(). To avoid subtle allocation bugs in failures leading to a double free change all allocations into device managed allocations. Fixes: 5b1122f ("platform/x86/amd/pmf: fix cleanup in amd_pmf_init_smart_pc()") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mario Limonciello <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Ilpo Järvinen <[email protected]> Signed-off-by: Ilpo Järvinen <[email protected]>
1 parent 784e48a commit d9db3a9

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

drivers/platform/x86/amd/pmf/core.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ int amd_pmf_set_dram_addr(struct amd_pmf_dev *dev, bool alloc_buffer)
280280
dev_err(dev->dev, "Invalid CPU id: 0x%x", dev->cpu_id);
281281
}
282282

283-
dev->buf = kzalloc(dev->mtable_size, GFP_KERNEL);
283+
dev->buf = devm_kzalloc(dev->dev, dev->mtable_size, GFP_KERNEL);
284284
if (!dev->buf)
285285
return -ENOMEM;
286286
}
@@ -493,7 +493,6 @@ static void amd_pmf_remove(struct platform_device *pdev)
493493
mutex_destroy(&dev->lock);
494494
mutex_destroy(&dev->update_mutex);
495495
mutex_destroy(&dev->cb_mutex);
496-
kfree(dev->buf);
497496
}
498497

499498
static const struct attribute_group *amd_pmf_driver_groups[] = {

drivers/platform/x86/amd/pmf/tee-if.c

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -358,30 +358,28 @@ static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf,
358358
return -EINVAL;
359359

360360
/* re-alloc to the new buffer length of the policy binary */
361-
new_policy_buf = memdup_user(buf, length);
362-
if (IS_ERR(new_policy_buf))
363-
return PTR_ERR(new_policy_buf);
361+
new_policy_buf = devm_kzalloc(dev->dev, length, GFP_KERNEL);
362+
if (!new_policy_buf)
363+
return -ENOMEM;
364+
365+
if (copy_from_user(new_policy_buf, buf, length)) {
366+
devm_kfree(dev->dev, new_policy_buf);
367+
return -EFAULT;
368+
}
364369

365-
kfree(dev->policy_buf);
370+
devm_kfree(dev->dev, dev->policy_buf);
366371
dev->policy_buf = new_policy_buf;
367372
dev->policy_sz = length;
368373

369-
if (!amd_pmf_pb_valid(dev)) {
370-
ret = -EINVAL;
371-
goto cleanup;
372-
}
374+
if (!amd_pmf_pb_valid(dev))
375+
return -EINVAL;
373376

374377
amd_pmf_hex_dump_pb(dev);
375378
ret = amd_pmf_start_policy_engine(dev);
376379
if (ret < 0)
377-
goto cleanup;
380+
return ret;
378381

379382
return length;
380-
381-
cleanup:
382-
kfree(dev->policy_buf);
383-
dev->policy_buf = NULL;
384-
return ret;
385383
}
386384

387385
static const struct file_operations pb_fops = {
@@ -532,35 +530,35 @@ int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
532530
dev->policy_base = devm_ioremap_resource(dev->dev, dev->res);
533531
if (IS_ERR(dev->policy_base)) {
534532
ret = PTR_ERR(dev->policy_base);
535-
goto err_free_dram_buf;
533+
goto err_cancel_work;
536534
}
537535

538-
dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL);
536+
dev->policy_buf = devm_kzalloc(dev->dev, dev->policy_sz, GFP_KERNEL);
539537
if (!dev->policy_buf) {
540538
ret = -ENOMEM;
541-
goto err_free_dram_buf;
539+
goto err_cancel_work;
542540
}
543541

544542
memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz);
545543

546544
if (!amd_pmf_pb_valid(dev)) {
547545
dev_info(dev->dev, "No Smart PC policy present\n");
548546
ret = -EINVAL;
549-
goto err_free_policy;
547+
goto err_cancel_work;
550548
}
551549

552550
amd_pmf_hex_dump_pb(dev);
553551

554-
dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL);
552+
dev->prev_data = devm_kzalloc(dev->dev, sizeof(*dev->prev_data), GFP_KERNEL);
555553
if (!dev->prev_data) {
556554
ret = -ENOMEM;
557-
goto err_free_policy;
555+
goto err_cancel_work;
558556
}
559557

560558
for (i = 0; i < ARRAY_SIZE(amd_pmf_ta_uuid); i++) {
561559
ret = amd_pmf_tee_init(dev, &amd_pmf_ta_uuid[i]);
562560
if (ret)
563-
goto err_free_prev_data;
561+
goto err_cancel_work;
564562

565563
ret = amd_pmf_start_policy_engine(dev);
566564
switch (ret) {
@@ -575,7 +573,7 @@ int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
575573
default:
576574
ret = -EINVAL;
577575
amd_pmf_tee_deinit(dev);
578-
goto err_free_prev_data;
576+
goto err_cancel_work;
579577
}
580578

581579
if (status)
@@ -584,7 +582,7 @@ int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
584582

585583
if (!status && !pb_side_load) {
586584
ret = -EINVAL;
587-
goto err_free_prev_data;
585+
goto err_cancel_work;
588586
}
589587

590588
if (pb_side_load)
@@ -600,12 +598,6 @@ int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
600598
if (pb_side_load && dev->esbin)
601599
amd_pmf_remove_pb(dev);
602600
amd_pmf_tee_deinit(dev);
603-
err_free_prev_data:
604-
kfree(dev->prev_data);
605-
err_free_policy:
606-
kfree(dev->policy_buf);
607-
err_free_dram_buf:
608-
kfree(dev->buf);
609601
err_cancel_work:
610602
cancel_delayed_work_sync(&dev->pb_work);
611603

@@ -621,11 +613,5 @@ void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
621613
amd_pmf_remove_pb(dev);
622614

623615
cancel_delayed_work_sync(&dev->pb_work);
624-
kfree(dev->prev_data);
625-
dev->prev_data = NULL;
626-
kfree(dev->policy_buf);
627-
dev->policy_buf = NULL;
628-
kfree(dev->buf);
629-
dev->buf = NULL;
630616
amd_pmf_tee_deinit(dev);
631617
}

0 commit comments

Comments
 (0)