[PATCH v4] RISC-V: KVM: Fix null pointer dereference in kvm_riscv_aia_imsic_has_attr()

Anup Patel anup at brainfault.org
Mon Jan 26 03:01:32 PST 2026


On Sun, Jan 25, 2026 at 8:03 PM Jiakai Xu <jiakaipeanut at gmail.com> wrote:
>
> Add a null pointer check for imsic_state before dereferencing it in
> kvm_riscv_aia_imsic_has_attr(). While the function checks that the
> vcpu exists, it doesn't verify that the vcpu's imsic_state has been
> initialized, leading to a null pointer dereference when accessed.
>
> This issue was discovered during fuzzing of RISC-V KVM code. The
> crash occurs when userspace calls KVM_HAS_DEVICE_ATTR ioctl on an
> AIA IMSIC device before the IMSIC state has been fully initialized
> for a vcpu.
>
> The crash manifests as:
>   Unable to handle kernel paging request at virtual address
>   dfffffff00000001
>   ...
>   epc : kvm_riscv_aia_imsic_has_attr+0x464/0x50e
>   arch/riscv/kvm/aia_imsic.c:998
>   ...
>   kvm_riscv_aia_imsic_has_attr+0x464/0x50e arch/riscv/kvm/aia_imsic.c:998
>   aia_has_attr+0x128/0x2bc arch/riscv/kvm/aia_device.c:471
>   kvm_device_ioctl_attr virt/kvm/kvm_main.c:4722 [inline]
>   kvm_device_ioctl+0x296/0x374 virt/kvm/kvm_main.c:4739
>   ...
>
> The fix adds a check to return -ENODEV if imsic_state is NULL, which
> is consistent with other error handling in the function and prevents
> the null pointer dereference.
>
> Fixes: 5463091a51cf ("RISC-V: KVM: Expose IMSIC registers as attributes of AIA irqchip")
> Signed-off-by: Jiakai Xu <xujiakai2025 at iscas.ac.cn>
> Signed-off-by: Jiakai Xu <jiakaiPeanut at gmail.com>

LGTM.

Reviewed-by: Anup Patel <anup at brainfault.org>

Queued this patch for Linux-6.20

Regards,
Anup

> ---
> V3 -> V4: Fix typo in Signed-off-by email address.
> V2 -> V3: Moved isel assignment after imsic_state NULL check.
>           Placed patch version history after '---' separator.
>           Added parentheses to function name in subject.
> V1 -> V2: Added Fixes tag and drop external link as suggested.
>
>  arch/riscv/kvm/aia_imsic.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kvm/aia_imsic.c b/arch/riscv/kvm/aia_imsic.c
> index e597e86491c3b..cd070d83663a9 100644
> --- a/arch/riscv/kvm/aia_imsic.c
> +++ b/arch/riscv/kvm/aia_imsic.c
> @@ -993,8 +993,11 @@ int kvm_riscv_aia_imsic_has_attr(struct kvm *kvm, unsigned long type)
>         if (!vcpu)
>                 return -ENODEV;
>
> -       isel = KVM_DEV_RISCV_AIA_IMSIC_GET_ISEL(type);
>         imsic = vcpu->arch.aia_context.imsic_state;
> +       if (!imsic)
> +               return -ENODEV;
> +
> +       isel = KVM_DEV_RISCV_AIA_IMSIC_GET_ISEL(type);
>         return imsic_mrif_isel_check(imsic->nr_eix, isel);
>  }
>
> --
> 2.34.1
>



More information about the linux-riscv mailing list