Skip to content

[PW_SID:1069010] RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()#1641

Open
linux-riscv-bot wants to merge 1 commit intoworkflow__riscv__fixesfrom
pw1069010
Open

[PW_SID:1069010] RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()#1641
linux-riscv-bot wants to merge 1 commit intoworkflow__riscv__fixesfrom
pw1069010

Conversation

@linux-riscv-bot
Copy link

PR for series 1069010 applied to workflow__riscv__fixes

Name: RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()
URL: https://patchwork.kernel.org/project/linux-riscv/list/?series=1069010
Version: 1

When a guest initiates an SBI_EXT_PMU_COUNTER_CFG_MATCH call with
ctr_base=0xfffffffffffffffe, ctr_mask=0xeb5f and flags=0x1
(SBI_PMU_CFG_FLAG_SKIP_MATCH), kvm_riscv_vcpu_pmu_ctr_cfg_match()
first invokes kvm_pmu_validate_counter_mask() to verify whether
ctr_base and ctr_mask are valid, by evaluating:
 !ctr_mask || (ctr_base + __fls(ctr_mask) >= kvm_pmu_num_counters(kvpmu))

With the above inputs, __fls(0xeb5f) equals 15, and adding 15 to
0xfffffffffffffffe causes an integer overflow, wrapping around to 13.
Since 13 is less than kvm_pmu_num_counters(), the validation wrongly
succeeds.

Thereafter, since flags & SBI_PMU_CFG_FLAG_SKIP_MATCH is satisfied,
the code evaluates:
 !test_bit(ctr_base + __ffs(ctr_mask), kvpmu->pmc_in_use)

Here __ffs(0xeb5f) equals 0, so test_bit() receives 0xfffffffffffffffe
as the bit index and attempts to access the corresponding element of
the kvpmu->pmc_in_use, which results in an invalid memory access. This
triggers the following Oops:
 Unable to handle kernel paging request at virtual address e3ebffff12abba89
  generic_test_bit include/asm-generic/bitops/generic-non-atomic.h:128
  kvm_riscv_vcpu_pmu_ctr_cfg_match arch/riscv/kvm/vcpu_pmu.c:758
  kvm_sbi_ext_pmu_handler arch/riscv/kvm/vcpu_sbi_pmu.c:49
  kvm_riscv_vcpu_sbi_ecall arch/riscv/kvm/vcpu_sbi.c:608
  kvm_riscv_vcpu_exit arch/riscv/kvm/vcpu_exit.c:240

The root cause is that kvm_pmu_validate_counter_mask() does not account
for the case where ctr_base itself is out of range, allowing the
subsequent addition to silently overflow and bypass the check.

Fix this by explicitly validating ctr_base against kvm_pmu_num_counters()
before performing the addition.

This bug was found by fuzzing the KVM RISC-V PMU interface.

Fixes: 0cb74b6 ("RISC-V: KVM: Implement perf support without sampling")
Signed-off-by: Jiakai Xu <jiakaiPeanut@gmail.com>
Signed-off-by: Jiakai Xu <xujiakai2025@iscas.ac.cn>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 137.83 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 1032.99 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1396.75 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 27.06 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 28.28 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 0.81 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 83.61 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.22 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
kdoc
Desc: Detects for kdoc errors
Duration: 0.82 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
module-param
Desc: Detect module_param changes
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.26 seconds
Result: PASS

@linux-riscv-bot
Copy link
Author

Patch 1: "RISC-V: KVM: Fix integer overflow in kvm_pmu_validate_counter_mask()"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.28 seconds
Result: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants