From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30ABE5C8ED; Tue, 23 Jan 2024 10:49:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706006982; cv=none; b=nZAdFYUbRhhJV0eayiR15gouw9ZqBTIiNfo3A0ZruzqH3qpYDKOenMB4WuBGFKWbhPMvAC4uAwfo8o/0o5+Y1S6Wr25MEUxEeE0Sq17mkWyqmCAJBwbjPTJDlHomb6ACJuFZtiWA4W2iJxnITyQIQKSIeLhgMpXXl+HMtNUihk0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706006982; c=relaxed/simple; bh=gHsDyfesb/mmGoU8vrHS9IRNcLNE0C+m0hCx7hzoAdQ=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=egQgHP6yypiumoEJ4lVvWY5KwBFha6LRMl49QvBH+fci83xURu7VR4UHQvc5tDazEb/fcObVno8HpTeVzFN9vH+rTRSO6/zb5ZTX6PnNPUwgb+8ORJc6LR7adyAhYT9ULqA7pZFet6Q6d9cs7r6jtWm/MzYDCOWh1ir8qYX0vVo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GdBE0Yme; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GdBE0Yme" 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) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII 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 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.