From: Bibo Mao <maobibo@loongson.cn>
To: Huacai Chen <chenhuacai@kernel.org>
Cc: Tianrui Zhao <zhaotianrui@loongson.cn>,
Xianglai Li <lixianglai@loongson.cn>,
kvm@vger.kernel.org, loongarch@lists.linux.dev,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v3 4/9] LoongArch: KVM: INTC: Check validation of num_cpu from user space
Date: Fri, 20 Jun 2025 09:42:00 +0800 [thread overview]
Message-ID: <5f1b9068-2d3d-2f89-4f72-85b021537f58@loongson.cn> (raw)
In-Reply-To: <CAAhV-H7ehdkKwzsFNAaX+r5eXLknvskyXLPDKei2A55LoSiJMA@mail.gmail.com>
On 2025/6/19 下午4:46, Huacai Chen wrote:
> Hi, Bibo,
>
> On Wed, Jun 11, 2025 at 9:47 AM Bibo Mao <maobibo@loongson.cn> wrote:
>>
>> The maximum supported cpu number is EIOINTC_ROUTE_MAX_VCPUS about
>> irqchip eiointc, here add validation about cpu number to avoid array
>> pointer overflow.
>>
>> Cc: stable@vger.kernel.org
>> Fixes: 1ad7efa552fd ("LoongArch: KVM: Add EIOINTC user mode read and write functions")
>> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
>> ---
>> arch/loongarch/kvm/intc/eiointc.c | 18 +++++++++++++-----
>> 1 file changed, 13 insertions(+), 5 deletions(-)
>>
>> diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c
>> index b48511f903b5..ed80bf290755 100644
>> --- a/arch/loongarch/kvm/intc/eiointc.c
>> +++ b/arch/loongarch/kvm/intc/eiointc.c
>> @@ -798,7 +798,7 @@ static int kvm_eiointc_ctrl_access(struct kvm_device *dev,
>> int ret = 0;
>> unsigned long flags;
>> unsigned long type = (unsigned long)attr->attr;
>> - u32 i, start_irq;
>> + u32 i, start_irq, val;
>> void __user *data;
>> struct loongarch_eiointc *s = dev->kvm->arch.eiointc;
>>
>> @@ -806,7 +806,12 @@ static int kvm_eiointc_ctrl_access(struct kvm_device *dev,
>> spin_lock_irqsave(&s->lock, flags);
>> switch (type) {
>> case KVM_DEV_LOONGARCH_EXTIOI_CTRL_INIT_NUM_CPU:
>> - if (copy_from_user(&s->num_cpu, data, 4))
>> + if (copy_from_user(&val, data, 4) == 0) {
>> + if (val < EIOINTC_ROUTE_MAX_VCPUS)
>> + s->num_cpu = val;
>> + else
>> + ret = -EINVAL;
> Maybe it is better to set s->num_cpu to EIOINTC_ROUTE_MAX_VCPUS (or
> other value) rather than keep it uninitialized. Because in other
> places we need to check s->num_cpu and an uninitialized value may
> cause undefined behavior.
There is error return value -EINVAL, VMM should stop running and exit
immediately if there is error return value with the ioctl command.
num_cpu is not uninitialized and it is zero by default. If VMM does not
care about the return value, VMM will fail to get coreisr information in
future.
Regards
Bibo Mao
>
>
> Huacai
>> + } else
>> ret = -EFAULT;
>> break;
>> case KVM_DEV_LOONGARCH_EXTIOI_CTRL_INIT_FEATURE:
>> @@ -835,7 +840,7 @@ static int kvm_eiointc_regs_access(struct kvm_device *dev,
>> struct kvm_device_attr *attr,
>> bool is_write)
>> {
>> - int addr, cpuid, offset, ret = 0;
>> + int addr, cpu, offset, ret = 0;
>> unsigned long flags;
>> void *p = NULL;
>> void __user *data;
>> @@ -843,7 +848,7 @@ static int kvm_eiointc_regs_access(struct kvm_device *dev,
>>
>> s = dev->kvm->arch.eiointc;
>> addr = attr->attr;
>> - cpuid = addr >> 16;
>> + cpu = addr >> 16;
>> addr &= 0xffff;
>> data = (void __user *)attr->addr;
>> switch (addr) {
>> @@ -868,8 +873,11 @@ static int kvm_eiointc_regs_access(struct kvm_device *dev,
>> p = &s->isr.reg_u32[offset];
>> break;
>> case EIOINTC_COREISR_START ... EIOINTC_COREISR_END:
>> + if (cpu >= s->num_cpu)
>> + return -EINVAL;
>> +
>> offset = (addr - EIOINTC_COREISR_START) / 4;
>> - p = &s->coreisr.reg_u32[cpuid][offset];
>> + p = &s->coreisr.reg_u32[cpu][offset];
>> break;
>> case EIOINTC_COREMAP_START ... EIOINTC_COREMAP_END:
>> offset = (addr - EIOINTC_COREMAP_START) / 4;
>> --
>> 2.39.3
>>
next prev parent reply other threads:[~2025-06-20 1:43 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-11 1:46 [PATCH v3 0/9] LoongArch: KVM: INTC: Enhancement about eiointc emulation Bibo Mao
2025-06-11 1:46 ` [PATCH v3 1/9] LoongArch: KVM: INTC: Fix interrupt route update with eiointc Bibo Mao
2025-06-11 1:46 ` [PATCH v3 2/9] LoongArch: KVM: INTC: Check interrupt route from physical cpu Bibo Mao
2025-06-11 1:46 ` [PATCH v3 3/9] LoongArch: KVM: INTC: Disable update property num_cpu and feature Bibo Mao
2025-06-11 1:46 ` [PATCH v3 4/9] LoongArch: KVM: INTC: Check validation of num_cpu from user space Bibo Mao
2025-06-19 8:46 ` Huacai Chen
2025-06-20 1:42 ` Bibo Mao [this message]
2025-06-20 14:43 ` Huacai Chen
2025-06-27 7:42 ` Bibo Mao
2025-06-27 9:04 ` Huacai Chen
2025-06-11 1:46 ` [PATCH v3 5/9] LoongArch: KVM: INTC: Avoid overflow with array index Bibo Mao
2025-06-11 1:46 ` [PATCH v3 6/9] LoongArch: KVM: INTC: Use standard bitops API with eiointc Bibo Mao
2025-06-11 1:46 ` [PATCH v3 7/9] LoongArch: KVM: INTC: Remove unused parameter len Bibo Mao
2025-06-11 1:46 ` [PATCH v3 8/9] LoongArch: KVM: INTC: Add stat information with kernel irqchip Bibo Mao
2025-06-11 1:50 ` Bibo Mao
2025-06-11 1:51 ` [PATCH v3 9/9] LoongArch: KVM: INTC: Add address alignment check Bibo Mao
2025-06-19 8:47 ` Huacai Chen
2025-06-19 9:47 ` Bibo Mao
2025-06-21 11:20 ` David Laight
2025-06-21 13:04 ` Huacai Chen
2025-06-27 1:58 ` Bibo Mao
2025-06-27 2:12 ` Bibo Mao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5f1b9068-2d3d-2f89-4f72-85b021537f58@loongson.cn \
--to=maobibo@loongson.cn \
--cc=chenhuacai@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lixianglai@loongson.cn \
--cc=loongarch@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=zhaotianrui@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®