Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp349542rdb; Tue, 23 Jan 2024 01:15:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXLRG8AxPgk6gwzK/G/NVhMUqJpyXgqUDwwhy/3aUmB5qKE08dDFPJLscbJzwBjoZpK52Y X-Received: by 2002:a05:6512:3b09:b0:50e:7cbe:cf36 with SMTP id f9-20020a0565123b0900b0050e7cbecf36mr1627591lfv.40.1706001344397; Tue, 23 Jan 2024 01:15:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706001344; cv=pass; d=google.com; s=arc-20160816; b=r/gWQ2IyxCQlrskSMWA/a4uv+QaxfsS1aaPoxZ6pUnK1QOFFzshplBHFqGVb8AlYGp +D7OVOBYkImxe+66wa/7mcvcIe7nDDA7vXWbqsBysGUVf/+kHqE0qhbAACCwAvoHnoWw uTkjWy9Z+ewNdc0csAmSTDsb/H62EUa+wnM2JwXRTsL6pnbMxVCbVMWVpAWmXyd3Fz6v z0Bv78cW6wm4WY9DCXWqH082nsn87WWF0vrc6CvJSfMkf0oNC+/MthkKEQRN350ModnA RRa40x3wXMSTBr9Of/G00YSfOObefQldusOuiNQW8Jbv4kf2qPwVYOM9M3UBauK/ctl1 /wmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=laSgBAkuJb+sBX4MFJWKTYiSSZJMOJ/TeT8h5cq4A58=; fh=UiPxg4mwPD9yqXsCywvzNku79T0R7ows1EbOIzaVOnw=; b=zb2Su/TMjgSDUd7radZTeo0cyxhGUlZw0t0BZGy5k0FT5NHhh2nh8WAwz8wbIST7sT bIpFOrliRwON47tWfTMUxileO8V+fmXdzvaDLynKx4c22GW03ZBZmcgWF51dx/m805e5 W/8zqNxS9D+fLcs7Th95wo8NF2hRyA8AC6P3wJ/dC5z3F2OTa9eriyIg/ZGnMeeXkpPM RHAd1tM3vIalQfSl9JC4YdI3hhM0p3xegEdw7DjlZ/HGCqyIyVRl1wVpU2PB3MOdKxnR RC2XG3PCyrY19ygPmrdtI33no9w/+htNoGEFn39DS0IPIrVOx78mEJDItbiZUxUBe8/4 GGwA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ibVNWuXQ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-34972-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34972-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id w26-20020a1709060a1a00b00a275b4b24dfsi11065156ejf.789.2024.01.23.01.15.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:15:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34972-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ibVNWuXQ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-34972-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34972-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 am.mirrors.kernel.org (Postfix) with ESMTPS id F27971F25CED for ; Tue, 23 Jan 2024 09:15:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB05C58238; Tue, 23 Jan 2024 09:15:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ibVNWuXQ" 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 6D02158ACE; Tue, 23 Jan 2024 09:15:21 +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=1706001322; cv=none; b=lHC61WI8NB0qJs0zhfwx7YFGtFCH5oSd5AOGwtn1fCOLZgQ2iiFdnPFr1538yzSKz0c3IU2oxfwu3SdbkZRN1oKMzuBycKRRVcEhCuv/r9vOlvIF7/xNmTVBEGG50GkTt6yo+Q8+CG6MZ90nkyc2lLB74hwPKQgJuldn1FRPhTU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706001322; c=relaxed/simple; bh=C/q8o5kLPhhfSoji4iZJv2bCVDUdnPxN+hjpssNxaK4=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=F+skL01WoIayh77wMBaeA24nFD7+/sN9htYQ/y0gL2OOZ3tbdxpCEIx9cHl8YKWS/AWH05phFOgdpCXMC0qagIkgvUPHbjE08N29R1fnZo6gFEhK0oxQe6yBFco3t/9m1F5w5QxQZ7ASy5+kA7+U2WxDgY8B9nWiD1kvzsy9x0Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ibVNWuXQ; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 646D4C43390; Tue, 23 Jan 2024 09:15:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706001321; bh=C/q8o5kLPhhfSoji4iZJv2bCVDUdnPxN+hjpssNxaK4=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=ibVNWuXQT2KYOYx801hv1IWlSYU1NThCA2PTo2RuwYKNESz41Xx6knxpitqpqtlpw 1AYTjNv1lnCD5wawBMm6LLGV1FCvn5LMvYdNC79EEdFpxBUQdfIit5x8l4lFpaWY54 8M2A/5Nc3TEbPRGW4+LnrUsi/1kM9995h3aUyP3zzWSZWFfUg5VkEVJDkHix5cUh1i rOEDrwY6ocw2WmDjMmoJInkDYej3SSX9K1GArnxZNZcWfQ5sUo4LWaRXCRXi0UUXc1 PXKugZlQbCtoRCZVTmPGTFxAsSXdc09waRZeSvjzwrmW1eyULUM702YpGvuVfRnRx9 5pWCYXOytk2Eg== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 23 Jan 2024 09:15:16 +0000 Message-Id: Cc: "Dave Hansen" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , , "H. Peter Anvin" , , "Gustavo A. R. Silva" , "Bill Wendling" , "Justin Stitt" , Subject: Re: [PATCH 22/82] x86/sgx: Refactor intentional wrap-around calculation From: "Jarkko Sakkinen" To: "Kees Cook" , X-Mailer: aerc 0.15.2 References: <20240122235208.work.748-kees@kernel.org> <20240123002814.1396804-22-keescook@chromium.org> In-Reply-To: <20240123002814.1396804-22-keescook@chromium.org> On Tue Jan 23, 2024 at 12:26 AM UTC, 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/68df3755e383e6fecf2354a67b08f92f185365= 94 [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: Jarkko Sakkinen > Cc: Dave Hansen > Cc: Thomas Gleixner > Cc: Ingo Molnar > Cc: Borislav Petkov > Cc: x86@kernel.org > Cc: "H. Peter Anvin" > Cc: linux-sgx@vger.kernel.org > Signed-off-by: Kees Cook > --- > arch/x86/kernel/cpu/sgx/ioctl.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/io= ctl.c > index b65ab214bdf5..4b8f6c9f8ef5 100644 > --- a/arch/x86/kernel/cpu/sgx/ioctl.c > +++ b/arch/x86/kernel/cpu/sgx/ioctl.c > @@ -350,16 +350,18 @@ static int sgx_validate_offset_length(struct sgx_en= cl *encl, > unsigned long offset, > unsigned long length) > { > + unsigned long sum; > + > if (!IS_ALIGNED(offset, PAGE_SIZE)) > return -EINVAL; > =20 > if (!length || !IS_ALIGNED(length, PAGE_SIZE)) > return -EINVAL; > =20 > - if (offset + length < offset) > + if (check_add_overflow(offset, length, &sum)) > return -EINVAL; > =20 > - if (offset + length - PAGE_SIZE >=3D encl->size) > + if (sum - PAGE_SIZE >=3D encl->size) > return -EINVAL; > =20 > return 0; Reviewed-by: Jarkko Sakkinen BR, Jarkko