Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp754967rdb; Tue, 23 Jan 2024 14:07:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IHUFfPN3jiMcwjb4zU0scQ7MQAw7nYAupAmDanyz5jsyEmTG7WgjQiyDhMZI6/4vtCm1DkV X-Received: by 2002:a05:6358:6f84:b0:176:3a6:d856 with SMTP id s4-20020a0563586f8400b0017603a6d856mr6494328rwn.46.1706047668306; Tue, 23 Jan 2024 14:07:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706047668; cv=pass; d=google.com; s=arc-20160816; b=j9yJORgkJ0qBbXeGIBBNdK+rBRC3NS6CunE7cY6w8FKWiZuNr6W3V3uVIHauWKEUF0 M18YcWwNT85AcoOYG3Soiejm3heXzfGC61cuzFLc7wDuYmkm5DJDfx7dxOAk5rbcCwfU Cxd5McyJCnnsW/gE1s89nN52DGMZKLYUEJNe6oniCFJdLI9vxbSQ5r0UN5tFxyAA44Vw qayeiT3ZyDM9YTnnBLToGXtRU6ZMiAyc3exyKuDGg/E9vNJYd8Xc9BbUYkMsBHh5wgiD etij+YOMBI05La9XCzcqEeZj45rZYVdHgNFtRAwRdXk86wUFmev2QkeCy8UvDHE4fsEX 6J6g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=sj6Rxvm207/3+H8NF8WKC7zBV6u0ee5y25nooD5f1tg=; fh=CIc4AFBkognnOSEW2UguNSGi4Dj25ijZHXatpWMY+ow=; b=MZOkemts/D2i+gAM07dAZEjozrfl4cZ9n0l+LtPP0Py8pq6hvZbnyHnYDbRqT/HySP HLsWvTvBUY6PHxcFKLB4QZP6d+z25Y3pC/PJI6yXP7kdqJfejW0GWaO96IKVF98LG1Pi Gp3Ga5ga7X/4P5FNFFpHyBjHi2mf0RXE2DwLpGqMOtCDN/A2VoH+VEcK68WQvnjOnjEW cQvSGC+0AfHkZuQGxljGsZr+lFosDfgxshvxlKiLE380HGs5QMg94baY8XvpqebPmC0f cJyO4+0+x+/0shEqLQJT6psg+o/t4vk6Td0wEYuFd1Gg5d5hikPKdAe+9zaGaIBxdVMS 8wQQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="P/fYp5fR"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-36094-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36094-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 f36-20020a631f24000000b0058556a636dbsi10138968pgf.434.2024.01.23.14.07.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 14:07:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36094-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=@chromium.org header.s=google header.b="P/fYp5fR"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-36094-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36094-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 0B80EB289A8 for ; Tue, 23 Jan 2024 21:52:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 20F2C48CC5; Tue, 23 Jan 2024 21:52:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="P/fYp5fR" Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDD78481A8 for ; Tue, 23 Jan 2024 21:52:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706046756; cv=none; b=jAzWkEyi86YI081hBZ6Rq+xhTkODL2iWAyru2kh5cA01GG4N1GJJ/R4t1aOxmsHXwN8Se+jdF97fyGe2JbiRdA0CStkmibzSgEf+wKJORJXvmG+Sm7SYCaSRaCaPTax5fWGTXcbE8Qrbs8kc0tGAy79GfBxFlciEzvVe6+ZRdvc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706046756; c=relaxed/simple; bh=VUhlMv0YIbLBaprVX+mmJCjmL8UnZ1q4FSBZdfn36Hg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OFh54bARh5aFe0aqbPrpigq74hhF6MwF7Lq4oIuBQdM6G/bKRaX99yyDNJxYwRbQ4KCoF3KQAi85FZrgKeAq0H030DMYxtAbFfzFrt5JRpzA9NFwc0hgbD6Xo0WYfgLRqY6OJAtmoiz5a/n2ByHd4stnN3qsp8wFFadnOOwyZHc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=P/fYp5fR; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1d7431e702dso19685145ad.1 for ; Tue, 23 Jan 2024 13:52:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1706046754; x=1706651554; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=sj6Rxvm207/3+H8NF8WKC7zBV6u0ee5y25nooD5f1tg=; b=P/fYp5fRzOsBDHkrucMCFg3aa0kOezEUiLMjLvA3Jud+3RS3VWgOm6rAu8HZMbRuF2 J3s7mpYUvE0JjafMvkqHHs80gYIR+X9M+Ci77C97ue9UOdV5a6CybMTKzifJDZYFQSgO w1HqjBzbXWdcXrpkKp6OLIEAzLHdFV2P6Jm+4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706046754; x=1706651554; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=sj6Rxvm207/3+H8NF8WKC7zBV6u0ee5y25nooD5f1tg=; b=reKqHPZji8Ua3PvWMQyuUIt3VI1vMuM7BgSSo9lHpkIbOc3ChKI2vP7uT9WMCqFI8o q/DL9XWI706MoYTsvsF2F0PLhQjXPwAUZCEFcXwoe8e9U5+l5V3Wi+KeZN+egeo9fGnm 4F1W+333I8RnAuNyANw3Js1gyw7BOktrX5KQ16zn4U1mEhvf3jTAY5hk2J+hk1q5YeLB /NGQkWmmaKzYiV4N2xz7epqidkaaVXsvVAPzjtrklmK0547qY1LT2/X11KNK1QOpRwLJ CIQ7GH4A8gQqIB0SORViwIG4QqihUJsibeJaoTbZK91gxdUxkINLcF4edleKreY+QPuY OdLA== X-Gm-Message-State: AOJu0YzCGgwLo+aYdqv769dBh0tkNpFKXgEuZeIYSadizE2Q3hYAFqCZ mxAb9NKJMcvSkyIuTt3h3Jiu5g6s6uMeBg3+2NG1sPgAqpsZud+SLTmJdoOqcCBvqZjsnYeQaxw = X-Received: by 2002:a17:902:e810:b0:1d7:6f5a:d390 with SMTP id u16-20020a170902e81000b001d76f5ad390mr1575176plg.55.1706046754292; Tue, 23 Jan 2024 13:52:34 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id mf11-20020a170902fc8b00b001d748fb211csm4331068plb.276.2024.01.23.13.52.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 13:52:33 -0800 (PST) Date: Tue, 23 Jan 2024 13:52:33 -0800 From: Kees Cook To: Mark Rutland Cc: linux-hardening@vger.kernel.org, "Gustavo A. R. Silva" , Bill Wendling , Justin Stitt , linux-kernel@vger.kernel.org Subject: Re: [PATCH 03/82] overflow: Introduce add_wrap() Message-ID: <202401231352.7CE79C5F@keescook> References: <20240122235208.work.748-kees@kernel.org> <20240123002814.1396804-3-keescook@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Jan 23, 2024 at 09:22:52AM +0000, Mark Rutland wrote: > On Mon, Jan 22, 2024 at 04:26:38PM -0800, Kees Cook wrote: > > Provide a helper that will perform wrapping addition without tripping > > the arithmetic wrap-around sanitizers. > > > > Cc: "Gustavo A. R. Silva" > > Cc: linux-hardening@vger.kernel.org > > Signed-off-by: Kees Cook > > --- > > include/linux/overflow.h | 16 ++++++++++++++++ > > 1 file changed, 16 insertions(+) > > > > diff --git a/include/linux/overflow.h b/include/linux/overflow.h > > index ac088f73e0fd..30779905a77a 100644 > > --- a/include/linux/overflow.h > > +++ b/include/linux/overflow.h > > @@ -124,6 +124,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) > > check_add_overflow(a, b, &__result);\ > > })) > > > > +/** > > + * add_wrap() - Intentionally perform a wrapping addition > > + * @a: first addend > > + * @b: second addend > > + * > > + * Return the potentially wrapped-around addition without > > + * tripping any overflow sanitizers that may be enabled. > > + */ > > +#define add_wrap(a, b) \ > > + ({ \ > > + typeof(a) __sum; \ > > + if (check_add_overflow(a, b, &__sum)) \ > > + /* do nothing */; \ > > + __sum; \ > > + }) > > It's really difficult to see the semicolon for the empty statement here; could > we make that part: > > if ((check_add_overflow(a, b, &__sum)) { \ > /* do nothing */ \ > } \ > > ... to be a little clearer (and less at risk of breakage in a refactoring)? Yeah, agreed -- that stands out more clearly. -Kees -- Kees Cook