From: Raghavendra Rao Ananta During ITS table restore (KVM_DEV_ARM_ITS_RESTORE_TABLES), vgic_its_restore_ite() validates only that the restored interrupt ID satisfies `lpi_id >= VGIC_MIN_LPI` (8192) and fails to validate the upper bound against max_lpis_propbaser(propbaser). Since the ITE PINTID field is 32-bit (bits 47:16), a guest can plant an out-of-range LPI INTID in its own in-memory ITS tables prior to restore. When direct MSI forwarding attaches (via kvm_vgic_v4_set_forwarding()), this unchecked intid causes two severe consequences: 1. Out-of-Bounds Memory Write: In its_vlpi_map(), lpi_write_config() calculates the configuration byte target address as `page_address(map->vm->vprop_page) + hwirq - 8192`. Since vprop_page is a 64KB page-granular host allocation and hwirq is unbounded, this causes a 1-byte read-modify-write up to 4GB past the allocation into neighboring memory, with both the offset and written value controlled by the guest. 2. Hardware Command Queue Wedge (Host MSI/IRQ DoS): Immediately following the write, its_vlpi_map() issues a physical VMAPTI command with `virt_id = map->vintid`. Because virt_id exceeds the hardware's architected ID limits, the physical GICv4.1 ITS command queue halts on the malformed descriptor. The hardware read pointer becomes stuck, causing all subsequent ITS commands to time out and permanently breaking host-wide MSI handling until reboot: ITS cmd its_build_discard_cmd failed ITS queue timeout (24768 24481) ITS cmd its_build_mapd_cmd failed ITS queue timeout (24800 24481) ITS cmd its_build_vmapp_cmd failed Fix this by validating the upper bound of lpi_id against max_lpis_propbaser() in vgic_its_restore_ite(), mirroring the check in vgic_its_cmd_handle_mapi(). Fixes: eff484e0298d ("KVM: arm64: vgic-its: ITT save and restore") Reported-by: Stefan Teodorescu Cc: stable@vger.kernel.org Signed-off-by: Raghavendra Rao Ananta Signed-off-by: Damiano Melotti Signed-off-by: Marc Zyngier --- arch/arm64/kvm/vgic/vgic-its.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/vgic/vgic-its.c b/arch/arm64/kvm/vgic/vgic-its.c index 7e9b265b1f52..82f7d052efcb 100644 --- a/arch/arm64/kvm/vgic/vgic-its.c +++ b/arch/arm64/kvm/vgic/vgic-its.c @@ -2248,7 +2248,8 @@ static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id, if (!lpi_id) return 1; /* invalid entry, no choice but to scan next entry */ - if (lpi_id < VGIC_MIN_LPI) + if (lpi_id < VGIC_MIN_LPI || + lpi_id >= max_lpis_propbaser(kvm->arch.vgic.propbaser)) return -EINVAL; offset = val >> KVM_ITS_ITE_NEXT_SHIFT; -- 2.56.1 From: Marc Zyngier Do not blindly trust the vINTID field coming from KVM, and reject anything that is out of the boundaries that the hardware ITS is configured to support. These LPIs can still be injected as purely virtual interrupt. Fixes: d011e4e654d7f ("irqchip/gic-v3-its: Add VLPI map/unmap operations") Reported-by: Stefan Teodorescu Cc: stable@vger.kernel.org Signed-off-by: Marc Zyngier Signed-off-by: Damiano Melotti Signed-off-by: Marc Zyngier --- drivers/irqchip/irq-gic-v3-its.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c index 282e4bafb084..0bb7a37f2b83 100644 --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -1922,7 +1922,8 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) struct its_device *its_dev = irq_data_get_irq_chip_data(d); u32 event = its_get_event_id(d); - if (!info->map) + if (!info->map || info->map->vintid < 8192 || + info->map->vintid >= BIT_ULL(LPI_NRBITS)) return -EINVAL; if (!its_dev->event_map.vm) { -- 2.56.1 From: Raghavendra Rao Ananta During ITS table restore (KVM_DEV_ARM_ITS_RESTORE_TABLES), scan_its_table() walks guest RAM to restore Device Table Entries (DTEs) and Interrupt Translation Tables (ITTs) while holding kvm->lock, all vCPU mutexes, kvm->arch.config_lock, and its->its_lock. Because a guest can plant valid DTEs with num_eventid_bits = 16 (size = 15, or 65,536 EventIDs per device) pointing to zeroed/sparse ITT memory (!lpi_id), vgic_its_restore_ite() returns +1 for each unmapped entry. This forces scan_its_table() to scan up to 65,536 entries per device (up to 4.30B reads for a 1-level flat table or 21.48B reads for a 2-level indirect table). Because scan_its_table() lacks signal checks, executing KVM_DEV_ARM_ITS_RESTORE_TABLES monopolizes the host CPU in kernel space for tens to hundreds of seconds. Fix this by checking signal_pending(current) at the start of the scan_its_table() loop to allow immediate cancellation (-EINTR) if a VMM timeout or termination signal arrives. Fixes: 920a7a8fa92a ("KVM: arm64: vgic-its: Add infrastructure for table lookup") Reported-by: Stefan Teodorescu Cc: stable@vger.kernel.org Signed-off-by: Raghavendra Rao Ananta Signed-off-by: Damiano Melotti Signed-off-by: Marc Zyngier --- arch/arm64/kvm/vgic/vgic-its.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/arm64/kvm/vgic/vgic-its.c b/arch/arm64/kvm/vgic/vgic-its.c index 82f7d052efcb..9c81a8656e94 100644 --- a/arch/arm64/kvm/vgic/vgic-its.c +++ b/arch/arm64/kvm/vgic/vgic-its.c @@ -2171,6 +2171,9 @@ static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz, int next_offset; size_t byte_offset; + if (signal_pending(current)) + return -EINTR; + ret = kvm_read_guest_lock(kvm, gpa, entry, esz); if (ret) return ret; -- 2.56.1 From: Marc Zyngier The algorithm used to prune IRQs sitting in the AP list has a quadratic complexity, as any concurrent change in the list results in a restart of the processing. The locking is also rather complicated, as we have to take the locks for both source and target vcpus, plus the per-irq lock. While this has served us well so far, people are finding new and amusing ways to abuse the locking and perform a DoS on the host. Not fun. Instead, switch to a linear approach where potentially moved IRQs are taken off the AP list and then dispatched onto their new targets. This simplifies the locking (only one vcpu lock at any given time), and only the interrupts that have been changed during that process are evaluated again. This allows the pruning process to converge much quicker. Signed-off-by: Marc Zyngier Reviewed-by: Oliver Upton Cc: stable@vger.kernel.org --- arch/arm64/kvm/vgic/vgic.c | 175 ++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 88 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c index bea848c93a99..a7e3e1a8a64f 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -754,6 +754,29 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner) return ret; } +/* + * This helper checks the target *and* does the laundry, nuking the irq off + * the ap_list when necessary, and doing the refcount drop on LPIs. -ENOENT + * is returned when LPI release is warranted. + */ +static struct kvm_vcpu *vgic_predict_target(struct kvm *kvm, struct vgic_irq *irq) +{ + struct kvm_vcpu *target; + + lockdep_assert_held(&irq->irq_lock); + + target = vgic_target_oracle(irq); + if (!target) { + list_del(&irq->ap_list); + irq->vcpu = NULL; + + if (vgic_put_irq_norelease(kvm, irq)) + target = ERR_PTR(-ENOENT); + } + + return target; +} + /** * vgic_prune_ap_list - Remove non-relevant interrupts from the list * @@ -765,109 +788,83 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner) static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) { struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + DECLARE_BITMAP(pend_map, VGIC_V3_MAX_CPUS) = {}; + DECLARE_BITMAP(kick_map, VGIC_V3_MAX_CPUS) = {}; + struct kvm_vcpu *irq_target; struct vgic_irq *irq, *tmp; bool deleted_lpis = false; + LIST_HEAD(culling_list); + int i; DEBUG_SPINLOCK_BUG_ON(!irqs_disabled()); -retry: raw_spin_lock(&vgic_cpu->ap_list_lock); list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) { - struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB; - bool target_vcpu_needs_kick = false; - - raw_spin_lock(&irq->irq_lock); - + guard(raw_spinlock)(&irq->irq_lock); BUG_ON(vcpu != irq->vcpu); - target_vcpu = vgic_target_oracle(irq); - - if (!target_vcpu) { - /* - * We don't need to process this interrupt any - * further, move it off the list. - */ - list_del(&irq->ap_list); - irq->vcpu = NULL; - raw_spin_unlock(&irq->irq_lock); - - /* - * This vgic_put_irq call matches the - * vgic_get_irq_ref in vgic_queue_irq_unlock, - * where we added the LPI to the ap_list. As - * we remove the irq from the list, we drop - * also drop the refcount. - */ - deleted_lpis |= vgic_put_irq_norelease(vcpu->kvm, irq); - continue; - } - - if (target_vcpu == vcpu) { - /* We're on the right CPU */ - raw_spin_unlock(&irq->irq_lock); + /* + * We skip the processing of an irq in a few cases: + * - the irq is on the correct target: nothing to do. + * - the target is NULL: vgic_predict_target() will have + * culled the irq from the ap_list. + * - we get an error back: the LPI has been deleted. + */ + irq_target = vgic_predict_target(vcpu->kvm, irq); + if (irq_target == vcpu || IS_ERR_OR_NULL(irq_target)) { + deleted_lpis |= IS_ERR(irq_target); continue; } /* - * This interrupt looks like it has to be migrated, - * make sure it is kept alive while locks are dropped. + * Snapshot the vcpus to visit (more may be added later), + * and move the IRQ onto the culling list. */ - vgic_get_irq_ref(irq); - - raw_spin_unlock(&irq->irq_lock); - raw_spin_unlock(&vgic_cpu->ap_list_lock); - - /* - * Ensure locking order by always locking the smallest - * ID first. - */ - if (vcpu->vcpu_id < target_vcpu->vcpu_id) { - vcpuA = vcpu; - vcpuB = target_vcpu; - } else { - vcpuA = target_vcpu; - vcpuB = vcpu; - } - - raw_spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock); - raw_spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock, - SINGLE_DEPTH_NESTING); - raw_spin_lock(&irq->irq_lock); - - /* - * If the interrupt is still ours and its affinity has - * been preserved, move it around. Otherwise, it means - * things have changed while the interrupt was unlocked - * (it may even have been taken off the list with its - * affinity left untouched), and we need to replay this. - * - * In all cases, we cannot trust the list not to have - * changed, so we restart from the beginning. - */ - if (irq->vcpu == vcpu && target_vcpu == vgic_target_oracle(irq)) { - struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu; - - list_del(&irq->ap_list); - irq->vcpu = target_vcpu; - list_add_tail(&irq->ap_list, &new_cpu->ap_list_head); - target_vcpu_needs_kick = true; - } - - raw_spin_unlock(&irq->irq_lock); - raw_spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock); - raw_spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock); - - deleted_lpis |= vgic_put_irq_norelease(vcpu->kvm, irq); - - if (target_vcpu_needs_kick) { - kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu); - kvm_vcpu_kick(target_vcpu); - } - - goto retry; + __set_bit(irq_target->vcpu_id, pend_map); + list_move_tail(&irq->ap_list, &culling_list); } + raw_spin_unlock(&vgic_cpu->ap_list_lock); + +retry: + BUG_ON(bitmap_empty(pend_map, VGIC_V3_MAX_CPUS) != list_empty(&culling_list)); + + for_each_set_bit(i, pend_map, VGIC_V3_MAX_CPUS) { + struct kvm_vcpu *target = kvm_get_vcpu_by_id(vcpu->kvm, i); + + guard(raw_spinlock)(&target->arch.vgic_cpu.ap_list_lock); + + list_for_each_entry_safe(irq, tmp, &culling_list, ap_list) { + guard(raw_spinlock)(&irq->irq_lock); + + /* + * Check again, now that we hold all the locks. If + * the irq has been moved again, leave it for later. + */ + irq_target = vgic_predict_target(vcpu->kvm, irq); + if (IS_ERR_OR_NULL(irq_target)) { + deleted_lpis |= IS_ERR(irq_target); + continue; + } + /* Catch a potential affinity change */ + if (irq_target != target) { + __set_bit(irq_target->vcpu_id, pend_map); + continue; + } + + irq->vcpu = target; + list_move_tail(&irq->ap_list, &target->arch.vgic_cpu.ap_list_head); + __set_bit(target->vcpu_id, kick_map); + } + + __clear_bit(i, pend_map); + } + + /* Leftovers? Same player shoot again! */ + if (unlikely(!list_empty(&culling_list))) + goto retry; + /* * Fix the last_lr_irq refcount which was obtained while * populating the LRs. This can also result in the LPI being @@ -879,10 +876,12 @@ static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) *host_data_ptr(last_lr_irq) = NULL; } - raw_spin_unlock(&vgic_cpu->ap_list_lock); - if (unlikely(deleted_lpis)) vgic_release_deleted_lpis(vcpu->kvm); + + for_each_set_bit(i, kick_map, VGIC_V3_MAX_CPUS) + kvm_make_request_and_kick(KVM_REQ_IRQ_PENDING, + kvm_get_vcpu_by_id(vcpu->kvm, i)); } static void vgic_fold_state(struct kvm_vcpu *vcpu) -- 2.56.1