From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8F993BFAF7 for ; Thu, 30 Jul 2026 09:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785403039; cv=none; b=Xcz7tEDUzXJtvxrL9MrJE0Lwu863DXt+l2X3g4aQVGw+QOfIaXm7FCQjeayfHPzzTN++lygYgaWZG9yUBf+Rbt+Lk95PyF/ARDD2mxpahNc7hSgh3Wy8AhGunRfy53UHNpZGiyB3+vKcmebInvlUiURYp1/px0L0nKjyQnmCs6c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785403039; c=relaxed/simple; bh=60vYiIIgj+T21RufVqRtXsKfrK60n4K9r1ZXF+KK7CM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iARIxb63lCVljHXYHZllCQjJ0UV6xTM74WOdf9Ltz1aWfvTfI4EGgcDyjiqzbNUbOVVER+2cYjQj5cUOHHQUt1x3BBfEov77GMD1MpV1M1qApQeuLQG/gzjkH38r5uPBfAMg+fm6xU+yq6E74rUT4uneFmowv7K+zK/EDXSlLXw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EiXUnJXR; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EiXUnJXR" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2cfff5f88dbso21750165ad.3 for ; Thu, 30 Jul 2026 02:17:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785403037; x=1786007837; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=luAWvof38jeXPbjahnYlH8nET33JhWw2Qecg1TNtU7U=; b=EiXUnJXRZzwFhKAI7KmeapaoA9CUpCJ8+F1LXQoOzbvfr0YsZGRgFFEXq3rutMAxNv Jni8v1FFzx1HSsMcy0RTns4sY5R0kdGNy5wsta56RoOdW8y4VjT9w38SiFtKVqyfE7VN 1P4lTZ8EbjrXYyOjn/3KbX2h0ZJ5oap5qOaZ9jaCvcE3tUjXyvtF30BSfkzr2hFPmCJ3 oXOb0905yF1nuS0hwqtfeQz0aI7WN1ArLd0EJ9/cK05eJRzrfbH1ZB/VdlJJwEycx5p6 2etTA8vupSrp+Udb4AUr62x5l4l6Q7hwrfu28wi01x2TxtgWZrGBFZ0omaU+3MlqjTPn yyJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785403037; x=1786007837; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=luAWvof38jeXPbjahnYlH8nET33JhWw2Qecg1TNtU7U=; b=mboIInjC+t9MYSdNLC05tmb9phWs+kt7lhTzxZNeCdrCmtoAB/MKpCbyW57EirWtEY yiy0WixpUncB0iswqEZrRdPHFvtGjCD3m6GxGaUftFlmthgVUuFNfzQDXJi7CLSsBUV6 q5OgnRlMooEMgJzyk+Mh+3iB41gg2/liThaamxCJTFncSTKnBJKy96c11rWeXL8zZzXd J28C3HtiQQPOQqbVdW4rEUhfntv5HLAt55hsnbTnK88INO7yOszUeCjy4u0pYDXv011h 4QUj6KxAGwK2ChLS7WrMV7ego6ws8sdkST4Aik4ORHmC6ZfPndVnZtC9ub7PC6zdUGt3 p+yg== X-Forwarded-Encrypted: i=1; AHgh+RqFYBZxzV/5xsqH6xH+yB7IqjvuPRWTt0BMq8k6HiLqV+Y0+o0Q70twhVx4eBJhrdGYRrw+UAG2ccrPEGc=@vger.kernel.org X-Gm-Message-State: AOJu0YwHz/fdRpm5/cbj5CMZTm1hC4iShHBKYBNN1YXxZi7OP82mxPTI SIgvms7M+piXaC491OPcLNXWb4vaauvBlz72WRGSbaQ+qrkOzpyKFQ3U X-Gm-Gg: AR+sD10YaS/LzJftTfk68HQ8O0I+g8PameqUIxBFjszp0HO+uzJE2MEQtHKu7OPPP+o IlegjOdqZV/MDSCPS1ILpJJ00PYTDNGPih9NqN6xGaN58b6gs33+p5z9issvwEKodvRJTH+e4UP z++uuqBAAP3eunBCn9jJDimC3JJpPHl4GI5EqZkUvQryAyE4fUEmJw7VSgGlvaugeM57tJ87awJ oyOgcBhoEqrzPO98VwNHMcrzZFCX+zCCVkh9a4tNEOU9pbYOsBf3WsBp2BZ9seyG+bdpVKR3DVy +H6Inh3bLv3RmVXz8cUW83prSmcgelkN/bm5hD4OAUa1WHUJswR+yJcM0ASFIVkvC1QdWq7K0sm zvJewWCocsE5wB3qleHHZp91QIqWqyMGj9OXZeH6AKWxY/P5uSpHrAnTkDSaZSXHYMxv2oZPi48 J9Uhh4Jz7GEntzehY1UqI7g4fFHifJ4ZPfc/fgOlEI4TgHLIK1kurnK4Oo+i7Oeh3vYgr0icIym 6KlIA== X-Received: by 2002:a17:902:d4d1:b0:2cc:f43b:fa4a with SMTP id d9443c01a7336-2d035ce64fdmr16534475ad.24.1785403036908; Thu, 30 Jul 2026 02:17:16 -0700 (PDT) Received: from 2045L.tailb1cd6d.ts.net ([36.112.122.101]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-2d022bca9d7sm23779615ad.52.2026.07.30.02.17.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2026 02:17:16 -0700 (PDT) From: Guidong Han <2045gemini@gmail.com> To: Anup Patel , Atish Patra Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Naveed Khan , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Guidong Han <2045gemini@gmail.com> Subject: [PATCH v2] RISC-V: KVM: Fix PMU event info array size overflow Date: Thu, 30 Jul 2026 17:17:10 +0800 Message-ID: <20260730091710.1366757-1-2045gemini@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260718153620.829378-1-2045gemini@gmail.com> References: <20260718153620.829378-1-2045gemini@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit SBI PMU EVENT_GET_INFO stores guest-controlled num_events * sizeof(*einfo) in an int. On RV64, num_events = 0x10000001 makes 0x100000010 truncate to 16. KVM then allocates one entry but loops over the original num_events, causing out-of-bounds reads and writes. A nested guest triggered: BUG: KASAN: slab-out-of-bounds in kvm_riscv_vcpu_pmu_event_info+0xa4/0x142 Read of size 4 at addr ff600000074d46b0 by task init/1 Call Trace: [] kvm_riscv_vcpu_pmu_event_info+0xa4/0x142 [] kvm_sbi_ext_pmu_handler+0xca/0x268 [] kvm_riscv_vcpu_sbi_ecall+0xec/0x1e6 [] kvm_riscv_vcpu_exit+0x48c/0x540 [] kvm_arch_vcpu_ioctl_run+0x37e/0xc80 Allocated by task 1: __kmalloc_noprof+0x19e/0x4b0 kvm_riscv_vcpu_pmu_event_info+0x72/0x142 kvm_sbi_ext_pmu_handler+0xca/0x268 kvm_riscv_vcpu_sbi_ecall+0xec/0x1e6 kvm_riscv_vcpu_exit+0x48c/0x540 kvm_arch_vcpu_ioctl_run+0x37e/0xc80 The buggy address is located 0 bytes to the right of allocated 16-byte region [ff600000074d46a0, ff600000074d46b0) Store the shared-memory size in size_t and reject multiplication overflow. Allocate the guest-driven array with GFP_KERNEL_ACCOUNT so it is charged to kmemcg, and use __GFP_NOWARN to suppress allocation failure warnings. Use kvcalloc() to allow vmalloc fallback and an unsigned long loop index to match num_events. Reported-by: Naveed Khan Closes: https://lore.kernel.org/kvm/178345245327.72065.13249716450708539854@digiscrypt.com/ Fixes: e309fd113b9f ("RISC-V: KVM: Implement get event info function") Cc: stable@vger.kernel.org Signed-off-by: Guidong Han <2045gemini@gmail.com> --- Changes in v2: - Use an accounted, no-warning kvcalloc() allocation and kvfree(), as suggested by Sashiko. - Naveed Khan reported the issue before v1. I missed his report at the time, so add Reported-by and Closes tags to credit him. Link to v1: https://lore.kernel.org/kvm/20260718153620.829378-1-2045gemini@gmail.com/ --- arch/riscv/kvm/vcpu_pmu.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index bb46dcbfb24d..8e1f6abb84e2 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -12,7 +12,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -479,13 +481,14 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low unsigned long flags, struct kvm_vcpu_sbi_return *retdata) { struct riscv_pmu_event_info *einfo = NULL; - int shmem_size = num_events * sizeof(*einfo); + size_t shmem_size; gpa_t shmem; u32 eidx, etype; u64 econfig; int ret; - if (flags != 0 || (saddr_low & (SZ_16 - 1) || num_events == 0)) { + if (flags != 0 || (saddr_low & (SZ_16 - 1)) || num_events == 0 || + check_mul_overflow(num_events, sizeof(*einfo), &shmem_size)) { ret = SBI_ERR_INVALID_PARAM; goto out; } @@ -500,7 +503,8 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low } } - einfo = kzalloc(shmem_size, GFP_KERNEL); + einfo = kvcalloc(num_events, sizeof(*einfo), + GFP_KERNEL_ACCOUNT | __GFP_NOWARN); if (!einfo) { ret = SBI_ERR_FAILURE; goto out; @@ -512,7 +516,7 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low goto free_mem; } - for (int i = 0; i < num_events; i++) { + for (unsigned long i = 0; i < num_events; i++) { eidx = einfo[i].event_idx; etype = kvm_pmu_get_perf_event_type(eidx); econfig = kvm_pmu_get_perf_event_config(eidx, einfo[i].event_data); @@ -525,7 +529,7 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low ret = SBI_ERR_INVALID_ADDRESS; free_mem: - kfree(einfo); + kvfree(einfo); out: retdata->err_val = ret; -- 2.43.0