Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp388149rdb; Tue, 23 Jan 2024 02:57:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IHe5SdNF39D+AznVjKA04w2IuiRNyvgbY2+Q2AXbtjPBUH528J5Q1KGtUVnPsMaXb8vNk5t X-Received: by 2002:a05:6a20:7352:b0:19a:419f:64c6 with SMTP id v18-20020a056a20735200b0019a419f64c6mr8382332pzc.12.1706007439779; Tue, 23 Jan 2024 02:57:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706007439; cv=pass; d=google.com; s=arc-20160816; b=BjnlTCYiqxwZlRhe4EjQJRbvF04+ni7JsJFxUMU0xLY1r3Y2mVeT2XHTbzr6qArDYC i0gQhnJq3V8qw+RmoWFVMpKA7R6fspbR4Ql06sU1MOLLsczv3B1RXDTGIWhsQT7dmjj1 OKaR4awmXoLzb1nyxzPwYH2nwArbB6okfm80aNlsqWKNXH2W42Ky8iXKB36q+Rmbl+cj cGWgyfOEn32o5S+F2GL9T4dU/RO+evxX3chsABb9eUu90YhBibJJkbhJFlrEnHcmOdGv qQWVNhGvoleMcsKQLUHKJ8SBfYt6IoY9/c2yaDEGmh0lD8wbhkHyYpIkqFbSGqN+k6ql 6wQQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature; bh=J1+HDW+cxZWtJTufEHWaXhEHuurjhxuBQDB+7VnpGKg=; fh=2TTHQBs1XYNVnaaF09bk+HaNLBmH+xtI6fw9IsMfkP0=; b=tHtkg9tnD9tMUql0H29+mtt7oLUiaVSYZrb0/7YtuiDPkMpNCUH8OaJAiDFxi7F6mj l2eLeI04uIh9rbUJsOLCxbIkqGeAW2BHnV48HvsbIRVyggSbAJySHcuYZ5cx2kD75+bw FDGfIncV1kPcbgbLIKSAcyjh6QB/6rtQTCtQWDZ34ZVxaIvL/bRimoS9awJqIKK+7L4T PtymGGbDJ/dHxoMdL5vZvecKHrkRMQsPy/X7l2eYbvbx4x570YmgYL7/0CZ/ptDPCPoC jcPB2rg2Xu/83NjY1AfS3v51rjWZaYJV4fr7msyWiG/ejmGv7CRlvw+vOII7o6Z1kUdD su8w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GdBE0Yme; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-35128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id bx31-20020a056a02051f00b005cd85b1eaf6si9982989pgb.398.2024.01.23.02.57.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 02:57:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GdBE0Yme; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-35128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 EC8FDB24577 for ; Tue, 23 Jan 2024 10:49:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 221AC5C905; Tue, 23 Jan 2024 10:49:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GdBE0Yme" 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 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.