Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp707292pxb; Tue, 5 Apr 2022 19:39:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw0ocXSmAHWOYSb0kBNsmVBTNS5mk/7AkeJAD5UZEo45mz+gCRUopm7gf6ZOP3dZ8yWZhSR X-Received: by 2002:a17:907:6d96:b0:6df:f199:6a7c with SMTP id sb22-20020a1709076d9600b006dff1996a7cmr6319194ejc.137.1649212794978; Tue, 05 Apr 2022 19:39:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649212794; cv=none; d=google.com; s=arc-20160816; b=ej+1nOpTeaDiVWN4RXtmv+3AcQpdOohFip3oOcGZtQ4WqefxS5aEex8qHgm/DnyVe3 9PDKEfDIxMZOlK6avK3RXSoTOcpT9iSw0NCaQa+iguOLuVcMO+d0wkrtJeqg9/Bm13Px 1GwjLypV5NeSc0ZzTvLDYh2HBPjbzrCV1uqwftpUGC6eROl4elBAGkd82NW6fV1UT2i8 VvpjWXOtYz99qq3IXJPSfS1Z1WocDb1K8F+OYZkHAJC3IlhFukQtzMhr3vrojEcThge6 ksr8ek/eF6iGL8qAS0ZyqMY2uF45+9o6ZpOdTT/tuMbQLzPCXXOX87yUGR4oG0b3V3ta qBWA== 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=rZmRWCoSlSS6DfFPGVVvHIt9N6hS4H5NooJo6CJxEIy3+Pdz3PINUeI8CskVRpktZM 78Ojv0cJEsTLCStS9AQxah9c2CvUwgXgLDBFGb8QpcK5ZrxuEwr/Jhm7lOgmPULy+vKy umBxGqzJIW4wufaJM75HVPNuoAxkhWkaWxlHquHY1HqsgdwTkr+yDNYo7tJF/Wm80Onr 2IoOWf3Q7dP20Jrrc86dwhMD/ZlbHcEbePZ18r81pbD7uRH4hyRAjIiLSJImBirZwLgl 327NXJNhBv9vw85+Bl7lQx3EoQ7FFhw0AtiN6gj0DQH9u8OdtPT13c6akGeiSG3n91yO R58g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="gVroX/AH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f10-20020a170906c08a00b006df76385e26si10195108ejz.710.2022.04.05.19.39.24; Tue, 05 Apr 2022 19:39:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="gVroX/AH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1388755AbiDENdA (ORCPT + 99 others); Tue, 5 Apr 2022 09:33:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346071AbiDEJX0 (ORCPT ); Tue, 5 Apr 2022 05:23:26 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 537B1AFB02; Tue, 5 Apr 2022 02:12:53 -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 ams.source.kernel.org (Postfix) with ESMTPS id D5CF5B81B14; Tue, 5 Apr 2022 09:12:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40D4FC385AC; Tue, 5 Apr 2022 09:12:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649149970; bh=fywS0SO7tp8sPRJmZFhQFeRorWt9giLj0GkaAOpgxdc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gVroX/AHlqntmnOuk0VTt2kzLGV5Y5sqgZnIFAihm1FpZ4zXu8soOAIJYB4XdSY/R /76nPTwAqdXchE10G1eLu3F8v18Ukn1FD6tS90IRI0EA2yFxsSMyCFPVrxKEOvBd5W V62J6hQWmuKi1rxaMBK7KFIKVXAXrfI2Pl6zJ+v0= 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.16 0906/1017] KVM: SVM: fix panic on out-of-bounds guest IRQ Date: Tue, 5 Apr 2022 09:30:19 +0200 Message-Id: <20220405070421.117379364@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@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=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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;