From patchwork Mon Nov 4 19:15:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 13861886 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C0177D1BDE7 for ; Mon, 4 Nov 2024 19:26:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=ADElOVsxwIIC8Rj8Eeyd37AF7YFbDPER97irh9fGxH4=; b=CzwyG4XEy42ZAH K8zZCFzOuz5w+1U/AwC1l83uWjIKsoTzTkBgA6z/pzLpF80eJuuupbx2Dw0/QVtYbqpVi9La5xwFu hSlX7ubWEuHjAKrob/sO1QD9F8HJ5PAHtTq36fakZAUzKMS9wA8mTb0qjlSEzWF0188ahx7AqDTa/ Sw598gD5rL23Bh3z0krdeb/tMnOF/kYAgN/fhSiZnLCKDULNdySRJy3uJXH39yTCaE+r4nqjeFvjN L2F8uekQPrZNNi8jzFfSO1EzZtegXVw40Fv6HdcYB3EPKwmUADxN+a6tDXbI3SJiW6wzXNOdy9df3 ti3eT55T/ncqynl2wyNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t82i5-0000000EtL1-2esN; Mon, 04 Nov 2024 19:25:57 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t82Xn-0000000Es2R-3Fdp; Mon, 04 Nov 2024 19:15:21 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 52D3A5C54B8; Mon, 4 Nov 2024 19:14:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A51EC4CECE; Mon, 4 Nov 2024 19:15:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730747718; bh=ynCoOheYITSrvR0l1B39csTWu2Mu+AVasOWlOaMf7pE=; h=From:To:Cc:Subject:Date:From; b=UckN6poH2bAbu2sww/PQHNDSlbChui8JoY02ti74E41RmREqtVFhEk9KW8sjW2uVB gaq0mUMB6NbOeFFIfkomR3RwnCxSkrhgLetGEO2sGj+xLFA3ncO4YFCsCaNCGTZU1T +Z0gS78hM3T0Njfvo9eDqJFrFso7XI7hOAsaiF3J440t7eEz+4CODWT2a75ZKLF7j5 EhluggIvLloouuuOp0kp/DBGjLTZ6OMrC/tU+pFChydMkzsQb5G1mzn4irrSbvbdpn uRRGSvuK+xE+rJV9QKt53Yi+C5AGyMpJkH7So48HlGjLwaSnzals1PSBottuj8cc0g KZJw9kQ21AYTA== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: Anup Patel , Atish Patra , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-kernel@vger.kernel.org Subject: [PATCH] riscv: kvm: Fix out-of-bounds array access Date: Mon, 4 Nov 2024 20:15:01 +0100 Message-ID: <20241104191503.74725-1-bjorn@kernel.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241104_111519_891702_F2CAA614 X-CRM114-Status: GOOD ( 11.22 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Björn Töpel In kvm_riscv_vcpu_sbi_init() the entry->ext_idx can contain an out-of-bound index. This is used as a special marker for the base extensions, that cannot be disabled. However, when traversing the extensions, that special marker is not checked prior indexing the array. Add an out-of-bounds check to the function. Fixes: 56d8a385b605 ("RISC-V: KVM: Allow some SBI extensions to be disabled by default") Signed-off-by: Björn Töpel Reviewed-by: Anup Patel --- Don't know if it matters, but I hit this when trying kvmtool. Björn --- arch/riscv/kvm/vcpu_sbi.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) base-commit: 59b723cd2adbac2a34fc8e12c74ae26ae45bf230 diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index 7de128be8db9..6e704ed86a83 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -486,19 +486,22 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu) struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context; const struct kvm_riscv_sbi_extension_entry *entry; const struct kvm_vcpu_sbi_extension *ext; - int i; + int idx, i; for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) { entry = &sbi_ext[i]; ext = entry->ext_ptr; + idx = entry->ext_idx; + + if (idx < 0 || idx >= ARRAY_SIZE(scontext->ext_status)) + continue; if (ext->probe && !ext->probe(vcpu)) { - scontext->ext_status[entry->ext_idx] = - KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE; + scontext->ext_status[idx] = KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE; continue; } - scontext->ext_status[entry->ext_idx] = ext->default_disabled ? + scontext->ext_status[idx] = ext->default_disabled ? KVM_RISCV_SBI_EXT_STATUS_DISABLED : KVM_RISCV_SBI_EXT_STATUS_ENABLED; }