From aee4515ff0e5100d1a6280df6b69aa07780cedb3 Mon Sep 17 00:00:00 2001 From: Qian Wang Date: Wed, 1 Jul 2020 09:53:21 +0800 Subject: [PATCH] HV: restrict conditions to assign/deassign pcidev hv: hypercall: restrict the condition to assign/deassign a pci device to a post-launched VM for safety For the safety of post-launched VMs, pci devices assignments should occur only when VM is being created (at VM_CREATED STATUS), and pci devices de-assignment should occur only when VM is being created or shutdown/reset (at VM_CREATED or VM_PAUSED status) Tracked-On: #4995 Acked-by: Eddie Done Reviewed-by: Li Fei Signed-off-by: Wang Qian --- hypervisor/common/hypercall.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/hypervisor/common/hypercall.c b/hypervisor/common/hypercall.c index 6cbb77fc6..5e6434d8e 100644 --- a/hypervisor/common/hypercall.c +++ b/hypervisor/common/hypercall.c @@ -835,12 +835,13 @@ int32_t hcall_assign_pcidev(struct acrn_vm *vm, uint16_t vmid, uint64_t param) struct acrn_assign_pcidev pcidev; struct acrn_vm *target_vm = get_vm_from_vmid(vmid); - if (!is_poweroff_vm(target_vm) && is_postlaunched_vm(target_vm)) { + /* We should only assign a device to a post-launched VM at creating time for safety, not runtime or other cases*/ + if (is_created_vm(target_vm) && is_postlaunched_vm(target_vm)) { if (copy_from_gpa(vm, &pcidev, param, sizeof(pcidev)) == 0) { ret = vpci_assign_pcidev(target_vm, &pcidev); } } else { - pr_err("%s, vm[%d] is invalid\n", __func__, vm->vm_id); + pr_err("%s, vm[%d] is not a postlaunched VM, or not in CREATED status to be assigned with a pcidev\n", __func__, vm->vm_id); } return ret; @@ -863,12 +864,13 @@ int32_t hcall_deassign_pcidev(struct acrn_vm *vm, uint16_t vmid, uint64_t param) struct acrn_assign_pcidev pcidev; struct acrn_vm *target_vm = get_vm_from_vmid(vmid); - if (!is_poweroff_vm(target_vm) && is_postlaunched_vm(target_vm)) { + /* We should only de-assign a device from a post-launched VM at creating/shutdown/reset time */ + if ((is_paused_vm(target_vm) || is_created_vm(target_vm)) && is_postlaunched_vm(target_vm)) { if (copy_from_gpa(vm, &pcidev, param, sizeof(pcidev)) == 0) { ret = vpci_deassign_pcidev(target_vm, &pcidev); } } else { - pr_err("%s, vm[%d] is invalid\n", __func__, vm->vm_id); + pr_err("%s, vm[%d] is not a postlaunched VM, or not in PAUSED/CREATED status to be deassigned from a pcidev\n", __func__, vm->vm_id); } return ret;