From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 38543C47258 for ; Tue, 23 Jan 2024 10:50:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1kPfPwb9kxhRhcpdEtcPMN61J37ZU1WRIkNldE02AhA=; b=l/CxJHu89gcwt4 DDToMmfeey7CcFWMhNWb72l7LJN0n5eOd+OSxOlBPmQuKbPtADaaeA2KMNYpl4zt4sAppm9p+tngW qfoSzj7E3h7S1c8fT3NvfKUnx3OrVxDztzlDlMCXMDeBNzm8jaZpLZANyUUHOYCG1fPckMsVmv/Zj +7KIqCVOpjU0vCXnIi44l6CDY6sluNr8JCOFrPSbADTuB4gayirFUCQAiJK9t0wGbxXjPJhGfl6cb IgmQYH4UW2x9d9nczOzp0fycE9Q4kVhlnl6bGk+qVTWsCgpBvhkXSzBfB5bbXpDmID0xKEdWEWoQN Opkq9aR53u52JYGhe5tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSELk-00G15a-1U; Tue, 23 Jan 2024 10:49:48 +0000 Received: from sin.source.kernel.org ([2604:1380:40e1:4800::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSELh-00G14h-2w for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 10:49:47 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 30C6BCE2E9D; Tue, 23 Jan 2024 10:49:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BA5AC433F1; Tue, 23 Jan 2024 10:49:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706006981; bh=gHsDyfesb/mmGoU8vrHS9IRNcLNE0C+m0hCx7hzoAdQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=GdBE0YmeC5uAWTMFoCQ42sY/cye7y//Dnpf7rdVq4SvyjftkaLUOcNCG24K0gzTqA dFSGEvVyB2CRW293HZlNrP2XS9mb0CidVywNsPoJT+Nv0LlRsJGumd4HyqEzI/dQtC 8cZZhQ8iYmFcmFDldNudf6GwUGERxsOqasJhzc2Jxuo+SMTJNy5VOU0cnM6d4aQvmO 5VeqzCf31gC7IEg1x5zj45zAVrV5IbcjPxb0rENgCyuzdQi3e5q1BgqXI8C1GpfVSm 0bpoCA9evozJgwCIHwkkZ6oy1u7sBKpsItgsjORTrYunPCbA6yYv8JWg616G6cR2A5 voHUGbm3fS+VA== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rSELb-00Dw18-4f; Tue, 23 Jan 2024 10:49:39 +0000 Date: Tue, 23 Jan 2024 10:49:38 +0000 Message-ID: <86o7dc8gu5.wl-maz@kernel.org> From: Marc Zyngier To: Kees Cook Cc: linux-hardening@vger.kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Reiji Watanabe , Eric Auger , Ricardo Koller , Raghavendra Rao Ananta , Quentin Perret , Jean-Philippe Brucker , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, "Gustavo A. R. Silva" , Bill Wendling , Justin Stitt , linux-kernel@vger.kernel.org Subject: Re: [PATCH 24/82] KVM: arm64: vgic: Refactor intentional wrap-around calculation In-Reply-To: <20240123002814.1396804-24-keescook@chromium.org> References: <20240122235208.work.748-kees@kernel.org> <20240123002814.1396804-24-keescook@chromium.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: keescook@chromium.org, linux-hardening@vger.kernel.org, oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, reijiw@google.com, eric.auger@redhat.com, ricarkol@google.com, rananta@google.com, qperret@google.com, jean-philippe@linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, gustavoars@kernel.org, morbo@google.com, justinstitt@google.com, linux-kernel@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240123_024946_320988_900F8006 X-CRM114-Status: GOOD ( 28.14 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, 23 Jan 2024 00:26:59 +0000, Kees Cook wrote: > > In an effort to separate intentional arithmetic wrap-around from > unexpected wrap-around, we need to refactor places that depend on this > kind of math. One of the most common code patterns of this is: > > VAR + value < VAR > > Notably, this is considered "undefined behavior" for signed and pointer > types, which the kernel works around by using the -fno-strict-overflow > option in the build[1] (which used to just be -fwrapv). Regardless, we > want to get the kernel source to the position where we can meaningfully > instrument arithmetic wrap-around conditions and catch them when they > are unexpected, regardless of whether they are signed[2], unsigned[3], > or pointer[4] types. > > Refactor open-coded unsigned wrap-around addition test to use > check_add_overflow(), retaining the result for later usage (which removes > the redundant open-coded addition). This paves the way to enabling the > wrap-around sanitizers in the future. > > Link: https://git.kernel.org/linus/68df3755e383e6fecf2354a67b08f92f18536594 [1] > Link: https://github.com/KSPP/linux/issues/26 [2] > Link: https://github.com/KSPP/linux/issues/27 [3] > Link: https://github.com/KSPP/linux/issues/344 [4] > Cc: Marc Zyngier > Cc: Oliver Upton > Cc: James Morse > Cc: Suzuki K Poulose > Cc: Zenghui Yu > Cc: Catalin Marinas > Cc: Will Deacon > Cc: Reiji Watanabe > Cc: Eric Auger > Cc: Ricardo Koller > Cc: Raghavendra Rao Ananta > Cc: Quentin Perret > Cc: Jean-Philippe Brucker > Cc: linux-arm-kernel@lists.infradead.org > Cc: kvmarm@lists.linux.dev > Signed-off-by: Kees Cook > --- > arch/arm64/kvm/vgic/vgic-kvm-device.c | 6 ++++-- > arch/arm64/kvm/vgic/vgic-v2.c | 10 ++++++---- > 2 files changed, 10 insertions(+), 6 deletions(-) > > diff --git a/arch/arm64/kvm/vgic/vgic-kvm-device.c b/arch/arm64/kvm/vgic/vgic-kvm-device.c > index f48b8dab8b3d..0eec5344d203 100644 > --- a/arch/arm64/kvm/vgic/vgic-kvm-device.c > +++ b/arch/arm64/kvm/vgic/vgic-kvm-device.c > @@ -18,17 +18,19 @@ int vgic_check_iorange(struct kvm *kvm, phys_addr_t ioaddr, > phys_addr_t addr, phys_addr_t alignment, > phys_addr_t size) > { > + phys_addr_t sum; > + > if (!IS_VGIC_ADDR_UNDEF(ioaddr)) > return -EEXIST; > > if (!IS_ALIGNED(addr, alignment) || !IS_ALIGNED(size, alignment)) > return -EINVAL; > > - if (addr + size < addr) > + if (check_add_overflow(addr, size, &sum)) > return -EINVAL; > > if (addr & ~kvm_phys_mask(&kvm->arch.mmu) || > - (addr + size) > kvm_phys_size(&kvm->arch.mmu)) > + sum > kvm_phys_size(&kvm->arch.mmu)) nit: 'sum' doesn't mean much in this context. Something like 'end' would be much more descriptive. > return -E2BIG; > > return 0; > diff --git a/arch/arm64/kvm/vgic/vgic-v2.c b/arch/arm64/kvm/vgic/vgic-v2.c > index 7e9cdb78f7ce..c8d1e965d3b7 100644 > --- a/arch/arm64/kvm/vgic/vgic-v2.c > +++ b/arch/arm64/kvm/vgic/vgic-v2.c > @@ -273,14 +273,16 @@ void vgic_v2_enable(struct kvm_vcpu *vcpu) > /* check for overlapping regions and for regions crossing the end of memory */ > static bool vgic_v2_check_base(gpa_t dist_base, gpa_t cpu_base) > { > - if (dist_base + KVM_VGIC_V2_DIST_SIZE < dist_base) > + gpa_t dist_sum, cpu_sum; Same here: dist_end, cpu_end. > + > + if (check_add_overflow(dist_base, KVM_VGIC_V2_DIST_SIZE, &dist_sum)) > return false; > - if (cpu_base + KVM_VGIC_V2_CPU_SIZE < cpu_base) > + if (check_add_overflow(cpu_base, KVM_VGIC_V2_CPU_SIZE, &cpu_sum)) > return false; > > - if (dist_base + KVM_VGIC_V2_DIST_SIZE <= cpu_base) > + if (dist_sum <= cpu_base) > return true; > - if (cpu_base + KVM_VGIC_V2_CPU_SIZE <= dist_base) > + if (cpu_sum <= dist_base) > return true; > > return false; With these nits addressed, and assuming you intend to merge the whole series yourself: Acked-by: Marc Zyngier M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel