Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp289849rdb; Thu, 22 Feb 2024 04:02:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVL3xmPPXftCGuWfU/pffIFh5riuXCriSihJEWVQ069+/iSXaV+eXgDgX+7aobzxEvwevubYxLarqZsG8S3ghEANPqmF5E7h0pDpngzeg== X-Google-Smtp-Source: AGHT+IF1Xv0VG3odmgUTfjb0EZDL/nstIDSDjcj9UXX2+kStS2pBHczhUpUPqEsSEnk7cDgp1MH1 X-Received: by 2002:a62:e306:0:b0:6e4:cf7c:6c28 with SMTP id g6-20020a62e306000000b006e4cf7c6c28mr872434pfh.22.1708603359290; Thu, 22 Feb 2024 04:02:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708603359; cv=pass; d=google.com; s=arc-20160816; b=jiWT7L5Yv+toYQnyCP5Adsd6F22IU99Y0Sjbb8lTfI/fyf+MtY22OzkXPhI/pm9tOS QeMCdP4HlTPPLmzo3Gbz70fzeuW+3SzPbEhgL2hbA/WkR4PnTZHQqEoBonk+bnBOHmnt E0uk5P7a8hSv2oXUfoonGA40Ibzxp3IRBTIFbPfYA5M51iMxm+qCwUVXgdZNgfkVuLrh ZrCc8Li3IyYQ6UKpBfQgcwYKqmTWBvsEJKoDoN4B8+l9ViD7maDKLb2qHQPkef+xObQg 4CJr1lboEGB3EX6SSK3Y7okXgfwmYgsCE+8bPGWKQJFGr4rly2AfcNzsfgnXtmSJamDK Xu7A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:in-reply-to:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:user-agent:date :message-id:from:references:cc:to:subject; bh=OAhIyrUUKl1FPz3GD/YvABXBM9w694GurOyfSdV6i8c=; fh=T8+fN+YA7L3YlW/NhYA+eH0/z0HvJgPwUpU2sy+Dd70=; b=jZzC0BFISYHMOpehuDMBubUb1xlMD0BfALTgAjluicMeiQqDGsSUcQcSB6iFctegX0 GtaWNlN/Eq0mjdCPaJMYFysmhhv1q9FcYBgfJJFX2fKzpf7OvTAEsCd0LWt2doAljdEp BEaXIGNEm+izcezQdJN5rqET0POeDW4JaeSGDja/YfOZh4AYiw8n2FfYHh/Lezt98g76 /9Ckl+t9T0mk9O46BpDyBf9MCRS/oqQyKicEmZvjOgFAQS/E9uISaU/Q1ZUpMjbbkN48 JEGzNYVLCSSJq1KF5jjXc4rNzqmhgFp/IdudrzhvMTYQ93ovtpfq/LBqxarlXHFTeVGn lwnQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-76469-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76469-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id x13-20020aa7918d000000b006e1358dc6fbsi10057601pfa.378.2024.02.22.04.02.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 04:02:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-76469-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-76469-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76469-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 18093B25F8A for ; Thu, 22 Feb 2024 11:57:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 04AAD4653A; Thu, 22 Feb 2024 11:57:44 +0000 (UTC) Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A687E41212; Thu, 22 Feb 2024 11:57:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708603063; cv=none; b=Tlv1JsZ87hVJSv8U3XRperxugre7M4cgVA7CPTZmvZK9q70gsQLLtHaAglTKkmLCTf3HMBLEnQGqyH1/OiuJMwpE0+06eLvQwF8EIxhonWgYtGHtq1k8iV4qu5ZSZxS/QPXUwEsjgshHI4wg00jykJD3rywFj6suYM0nvXNHpxc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708603063; c=relaxed/simple; bh=i1O0ek0muCYta3v4M3Z2ogeYhe937+UsWe85/PlS70A=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=gpSlj0igf6NLpd9BI6XmxvBEi4NpVaEXB4B1GU5COnKDud/QA3L9ZbbheKVbpqC3V93xLIRrI6A/hZxavvV2lNP0H/sZvVWfI9VPDbvyGNTA5VA/8kIcYcaAgjSKqNxnAORycZV+lQiC3/k1MPFZL6yjz7jFydtNFnKPVd1kGPA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.20.42.173]) by gateway (Coremail) with SMTP id _____8Ax++ixNtdlODMQAA--.22333S3; Thu, 22 Feb 2024 19:57:37 +0800 (CST) Received: from [10.20.42.173] (unknown [10.20.42.173]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxXs2sNtdlncg+AA--.28783S3; Thu, 22 Feb 2024 19:57:35 +0800 (CST) Subject: Re: [PATCH for-6.8 v4 1/3] LoongArch: KVM: Fix input validation of _kvm_get_cpucfg and kvm_check_cpucfg To: WANG Xuerui , Paolo Bonzini , Huacai Chen Cc: Tianrui Zhao , kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, WANG Xuerui References: <20240222105109.2042732-1-kernel@xen0n.name> <20240222105109.2042732-2-kernel@xen0n.name> From: maobibo Message-ID: Date: Thu, 22 Feb 2024 19:57:38 +0800 User-Agent: Mozilla/5.0 (X11; Linux loongarch64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <20240222105109.2042732-2-kernel@xen0n.name> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8BxXs2sNtdlncg+AA--.28783S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxXFWxAF4fCFyrJw13JF4DJrc_yoW5AF1fpF 43WF43XFW8Kr1xZasaq34DGw15urW8KrZ7ZFnYkasYvr47Jr4UGr48KFZaqryfC393Jr48 XF4UXa1akan0yacCm3ZEXasCq-sJn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUvab4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv 67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lc7I2V7IY0VAS07 AlzVAYIcxG8wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02 F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GF ylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7Cj xVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26F 4j6r4UJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU cVc_UUUUU On 2024/2/22 下午6:51, WANG Xuerui wrote: > From: WANG Xuerui > > The range check for the CPUCFG ID is wrong (should have been a || > instead of &&) and useless in effect, so fix the obvious mistake. > > Furthermore, the juggling of the temp return value is unnecessary, > because it is semantically equivalent and more readable to just > return at every switch case's end. This is done too to avoid potential > bugs in the future related to the unwanted complexity. > > Also, the return value of _kvm_get_cpucfg is meant to be checked, but > this was not done, so bad CPUCFG IDs wrongly fall back to the default > case and 0 is incorrectly returned; check the return value to fix the > UAPI behavior. > > While at it, also remove the redundant range check in kvm_check_cpucfg, > because out-of-range CPUCFG IDs are already rejected by the -EINVAL > as returned by _kvm_get_cpucfg. > > Fixes: db1ecca22edf ("LoongArch: KVM: Add LSX (128bit SIMD) support") > Fixes: 118e10cd893d ("LoongArch: KVM: Add LASX (256bit SIMD) support") > Signed-off-by: WANG Xuerui > --- > arch/loongarch/kvm/vcpu.c | 35 ++++++++++++++++++----------------- > 1 file changed, 18 insertions(+), 17 deletions(-) > > diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c > index 27701991886d..7fd32de6656b 100644 > --- a/arch/loongarch/kvm/vcpu.c > +++ b/arch/loongarch/kvm/vcpu.c > @@ -300,9 +300,7 @@ static int _kvm_setcsr(struct kvm_vcpu *vcpu, unsigned int id, u64 val) > > static int _kvm_get_cpucfg(int id, u64 *v) > { > - int ret = 0; > - > - if (id < 0 && id >= KVM_MAX_CPUCFG_REGS) > + if (id < 0 || id >= KVM_MAX_CPUCFG_REGS) > return -EINVAL; > > switch (id) { > @@ -324,32 +322,35 @@ static int _kvm_get_cpucfg(int id, u64 *v) > if (cpu_has_lasx) > *v |= CPUCFG2_LASX; > > - break; > + return 0; > default: > - ret = -EINVAL; > - break; > + /* > + * No restrictions on other valid CPUCFG IDs' values, but > + * CPUCFG data is limited to 32 bits as the LoongArch ISA > + * manual says (Volume 1, Section 2.2.10.5 "CPUCFG"). > + */ > + *v = U32_MAX; > + return 0; > } > - return ret; > } > > static int kvm_check_cpucfg(int id, u64 val) > { > - u64 mask; > - int ret = 0; > - > - if (id < 0 && id >= KVM_MAX_CPUCFG_REGS) > - return -EINVAL; > + u64 mask = 0; > + int ret; > > - if (_kvm_get_cpucfg(id, &mask)) > + ret = _kvm_get_cpucfg(id, &mask); > + if (ret) > return ret; > > + if (val & ~mask) > + /* Unsupported features and/or the higher 32 bits should not be set */ > + return -EINVAL; > + > switch (id) { > case 2: > /* CPUCFG2 features checking */ > - if (val & ~mask) > - /* The unsupported features should not be set */ > - ret = -EINVAL; > - else if (!(val & CPUCFG2_LLFTP)) > + if (!(val & CPUCFG2_LLFTP)) > /* The LLFTP must be set, as guest must has a constant timer */ > ret = -EINVAL; > else if ((val & CPUCFG2_FP) && (!(val & CPUCFG2_FPSP) || !(val & CPUCFG2_FPDP))) > Thanks for your contributions -:) Reviewed-by: Bibo Mao