Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp976579pxb; Wed, 6 Apr 2022 05:50:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGgj9r2r3m9Mry/Lcx4F79ecKwYqfTzVcZZShoTU0iPrATE48ZCNFBpOcx2ItovnrS9gm+ X-Received: by 2002:a17:90a:a58c:b0:1c9:bc35:6ed9 with SMTP id b12-20020a17090aa58c00b001c9bc356ed9mr9536258pjq.146.1649249439506; Wed, 06 Apr 2022 05:50:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649249439; cv=none; d=google.com; s=arc-20160816; b=vwqOcIUYdlVVm3xv4ouxlrbIF+VW+2pN8gKNFGZTXIddt/Z3c1079bWXHjZEV2z+/7 N/gffjMBpeGYcuVCEvlAQvhlQJG24WQGhdLh2ueo7dH4YWpB2t9UgsnmTdC5oC8lGbMQ WoY9v2LOflm0LbMoIMbyVOdXn82UtPzJMWevJHn3ZinWJdqyG3Z4g0BF3b/GfvHKwC3d ei3whdqSqKBE4Y5MDgcjit3aPhF3w5RyEZAH4y3bhE+PDojn9hxtomtmgD9HK+e1GzNg ekCMy196ZOouYtW3ixv2NBY2SghQ0rmiWnnANibY9Hc+YhMk5JmHHyOvv5zWQ5W+5qF6 kEew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Eanr8BbPtkBBt4QPYpn9f4iQ7ozfYhTO3L0JZ1X0P2U=; b=nbBTFJUc8TdYi5+2rB+9TtkmBg8uYWQ8q8O+qQtQUqKYiHnV9tFHt9gc1IYwK2QYhh gcPcmYjgwGB0TpPdmrhD3UMGPy1nypzWav3P4fyVyHbly2N/6cbdJbPBr5Xp0dypUw9J V5ojMPkyaEBEE7U6kCjhfl+qb8YhloCqAGetBijBF3m5MDVcjEAEfAibvRABNeLdrkCj 83M0mhfoFMtDuISJNpeeP0XOwTozaDtPKcxagM/1rTevAllgShtp/lfILKBcX+AqW0B8 BnhZpWihEDBKPWhM7NEDLXi8JPstV5bt0l7ngPaXP7BQ3dSpSWKMwBXK1Etijq4HfshI E+5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=hyhNUsCd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n8-20020a170902d2c800b00153b2d1648esi17087361plc.150.2022.04.06.05.50.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:50:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=hyhNUsCd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with SMTP id BD96B5D7891; Wed, 6 Apr 2022 02:39:08 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1457289AbiDEWrQ (ORCPT + 99 others); Tue, 5 Apr 2022 18:47:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353921AbiDEKJs (ORCPT ); Tue, 5 Apr 2022 06:09:48 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD444C4E1B; Tue, 5 Apr 2022 02:55:48 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 31F6161577; Tue, 5 Apr 2022 09:55:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 441F1C385A1; Tue, 5 Apr 2022 09:55:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152547; bh=fywS0SO7tp8sPRJmZFhQFeRorWt9giLj0GkaAOpgxdc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hyhNUsCdQasC8ykSIyUipJJuhWRUucXp7Rqi7iqo4WjHJs83ZI5ZmBHvbAlCjX7fz wZzgm41Atr19kjbvSm9eAWv0Tglexrt0PYzqufLN+PIvkCU+iGgy2gxYGQUnUzjBgW DCFhWNFG8942pVs50C1ry3pRwzWKEXtc4wxJ4PlA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yi Liu , Yi Wang , Paolo Bonzini Subject: [PATCH 5.15 815/913] KVM: SVM: fix panic on out-of-bounds guest IRQ Date: Tue, 5 Apr 2022 09:31:17 +0200 Message-Id: <20220405070404.260348077@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yi Wang commit a80ced6ea514000d34bf1239d47553de0d1ee89e upstream. As guest_irq is coming from KVM_IRQFD API call, it may trigger crash in svm_update_pi_irte() due to out-of-bounds: crash> bt PID: 22218 TASK: ffff951a6ad74980 CPU: 73 COMMAND: "vcpu8" #0 [ffffb1ba6707fa40] machine_kexec at ffffffff8565b397 #1 [ffffb1ba6707fa90] __crash_kexec at ffffffff85788a6d #2 [ffffb1ba6707fb58] crash_kexec at ffffffff8578995d #3 [ffffb1ba6707fb70] oops_end at ffffffff85623c0d #4 [ffffb1ba6707fb90] no_context at ffffffff856692c9 #5 [ffffb1ba6707fbf8] exc_page_fault at ffffffff85f95b51 #6 [ffffb1ba6707fc50] asm_exc_page_fault at ffffffff86000ace [exception RIP: svm_update_pi_irte+227] RIP: ffffffffc0761b53 RSP: ffffb1ba6707fd08 RFLAGS: 00010086 RAX: ffffb1ba6707fd78 RBX: ffffb1ba66d91000 RCX: 0000000000000001 RDX: 00003c803f63f1c0 RSI: 000000000000019a RDI: ffffb1ba66db2ab8 RBP: 000000000000019a R8: 0000000000000040 R9: ffff94ca41b82200 R10: ffffffffffffffcf R11: 0000000000000001 R12: 0000000000000001 R13: 0000000000000001 R14: ffffffffffffffcf R15: 000000000000005f ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018 #7 [ffffb1ba6707fdb8] kvm_irq_routing_update at ffffffffc09f19a1 [kvm] #8 [ffffb1ba6707fde0] kvm_set_irq_routing at ffffffffc09f2133 [kvm] #9 [ffffb1ba6707fe18] kvm_vm_ioctl at ffffffffc09ef544 [kvm] RIP: 00007f143c36488b RSP: 00007f143a4e04b8 RFLAGS: 00000246 RAX: ffffffffffffffda RBX: 00007f05780041d0 RCX: 00007f143c36488b RDX: 00007f05780041d0 RSI: 000000004008ae6a RDI: 0000000000000020 RBP: 00000000000004e8 R8: 0000000000000008 R9: 00007f05780041e0 R10: 00007f0578004560 R11: 0000000000000246 R12: 00000000000004e0 R13: 000000000000001a R14: 00007f1424001c60 R15: 00007f0578003bc0 ORIG_RAX: 0000000000000010 CS: 0033 SS: 002b Vmx have been fix this in commit 3a8b0677fc61 (KVM: VMX: Do not BUG() on out-of-bounds guest IRQ), so we can just copy source from that to fix this. Co-developed-by: Yi Liu Signed-off-by: Yi Liu Signed-off-by: Yi Wang Message-Id: <20220309113025.44469-1-wang.yi59@zte.com.cn> Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/svm/avic.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -799,7 +799,7 @@ int svm_update_pi_irte(struct kvm *kvm, { struct kvm_kernel_irq_routing_entry *e; struct kvm_irq_routing_table *irq_rt; - int idx, ret = -EINVAL; + int idx, ret = 0; if (!kvm_arch_has_assigned_device(kvm) || !irq_remapping_cap(IRQ_POSTING_CAP)) @@ -810,7 +810,13 @@ int svm_update_pi_irte(struct kvm *kvm, idx = srcu_read_lock(&kvm->irq_srcu); irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); - WARN_ON(guest_irq >= irq_rt->nr_rt_entries); + + if (guest_irq >= irq_rt->nr_rt_entries || + hlist_empty(&irq_rt->map[guest_irq])) { + pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", + guest_irq, irq_rt->nr_rt_entries); + goto out; + } hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { struct vcpu_data vcpu_info;