Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp618629rdb; Mon, 29 Jan 2024 12:29:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IH89Yb8AO8GlZm7F6VaSqP1EkpAbROJ+f6F0I7pMPiaP4Jqpx4BHC92N+JKdzzbiRRGt/JA X-Received: by 2002:a05:6a00:1d9f:b0:6db:d1d8:2e58 with SMTP id z31-20020a056a001d9f00b006dbd1d82e58mr4912846pfw.10.1706560172124; Mon, 29 Jan 2024 12:29:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706560172; cv=pass; d=google.com; s=arc-20160816; b=cyB4cEo4EKBvmB2FXAA+NsFWBx2ykel+ZyNc5Z06hYitff/NTuAA/XD3CjUnLDHgOS GX2ujw0cfxIkD9G+czUs9I8eDrdSp9ZUTnG+iniAFQzqS+K65ddMXhh8WJHvI39slOIm yRW4zkeTBmgRfx2h5Trr9Tx0d9RXt0s2YXKWevE4xkYplT5GRXFarJP7E5U/CXS77ZQB VWXj68cbPkaQRoPVajiUAV5jqkLZpSrLcEWEw4y5oM++er0bCpk3xO9kxs1NGHRfGjqZ TxpMb+rxYvMsSGO1WFR7MXnaG1j50vFiLKflzRNB5X8OnV6//yKnzsSKRiYnqwGPTIZ0 ctxA== 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=6E+CSDDSvT91f9Viy+M6uu4Auu69C79V+Tns03Gp4VA=; fh=P00+GcUmj6f6IFuN2mL4AU2Q/xhCOj8w+wJ/J8cWzD8=; b=DdbscwhvNyFE/EosofFgmps62Tp/MUwWK550e8I8PlxVzaPnhWLKqExvFgvvviXe/J I1CvRgf0/FZACsyzsHGQ2D1K1yw/dJsbDyrV2IBsBmks6U4eiKNZC1Bxauq2EaCb6vSH YfKVEjwvNr1knlQi8+fPWRnOBGjfMTkJwYDxXi2ipx+AFk2CT2KzjINQvkrSDTVVQ3Ff TUq1mZ5Rpjlzd8whWO7msiZXvgx0zah6RrGYSliknr3QI5HU+neo2sQRJJyXrClv89as mQaEtKq/D3HNZcVE0KLhNZKapt8htv7y8hOvR7FGvW//YpA7rH52Yy5FmvJHO0GLkB8f 7pfQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=C+R5wmvj; 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-43464-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43464-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 r16-20020aa79890000000b006dda0a1b3c2si6103462pfl.156.2024.01.29.12.29.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 12:29:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43464-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=C+R5wmvj; 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-43464-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43464-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 CE60FB249FF for ; Mon, 29 Jan 2024 20:26:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB6ED7605F; Mon, 29 Jan 2024 20:26:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="C+R5wmvj" Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 9F0CA6F09C for ; Mon, 29 Jan 2024 20:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706559987; cv=none; b=PEQzYV/jcXyvAe2LZobWu8V2qrFXAtPHod6LQSddiavB3Vf89PzCFUgkfu8yAYanSlQdYIgoXsZK/cVkqcRxgOquW2ySlhdie7cJfBF6vHtunZvkzAM+upLh/VOevkgC7h0s0bPt9GiK3ItsFAeLK1nqnz1gQfiH7I7ngk1jID8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706559987; c=relaxed/simple; bh=SMrAnU030wuDhlboOt55byceqkItemYdSY9Vo59Y2yw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=AOcsliQ4DDBwt6JxxOCWhWWjj7STclUUNKzC2cWIBUWQkdyo/TAGKDAlUcSFlWfuHwgyXalibwK+iQWKa4hCRp8tg7/uFTocLRzSEES9PCkKBStsNHT6ad3ejQtk5nnp/idFjFejx8X8ngdZoIMzXZGLFQCWDUHdkjy135lcSU8= 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=C+R5wmvj; arc=none smtp.client-ip=209.85.210.178 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-pf1-f178.google.com with SMTP id d2e1a72fcca58-6ddc1fad6ddso2690207b3a.0 for ; Mon, 29 Jan 2024 12:26:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1706559985; x=1707164785; 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=6E+CSDDSvT91f9Viy+M6uu4Auu69C79V+Tns03Gp4VA=; b=C+R5wmvjfctGYZ9SDDMSVdhjyUSByuz1xutV7NvLPV+DijWDZeL0ByHkiMBD9nuV42 cUMhqHqVCk9SaqAvZ09kpnC7D/IFzz2OjBu1Th9eqTBS2gWlDRpmew+m+G0g9QDSQJk8 hgt7tmGFi3bGZp1p+rPFWDyzoPpr8I4X1fysM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706559985; x=1707164785; 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=6E+CSDDSvT91f9Viy+M6uu4Auu69C79V+Tns03Gp4VA=; b=L++LGq5fEIqT5tJZH+VNyzvhr47HlSjsr4aSJdMQDxysGI5HV0Lar2sZ+tiAdH7T4D +f2S1hguWfBDqWI/CaVxZUcMnjUM7B81M6fHB3irqRjc66cvN0Nn2nUvmJOErFalE/cE oqC4jZh6Hlr1fkfBioAjTt9AqnsCVvT3c+k1H5Mq3wVikbmq9XtSCPuB/35IA+oOiBXF M9Xe1xAR1m80rlVXPHRrqGmgFJwNXoI5OnFCoDU0Baaad6obLvrpdixBNQtTWJ0iHhij yVc0JnDel0TuT2qG7pQX1QrBhNySodBCo/u9sC0pd5tnZdsHhnmanxfJAmwVpLMqhf3i 8IZA== X-Gm-Message-State: AOJu0Yyhl0CAG1lWh0uXdqvICUWGNJ1vjx0ghgS44ZnepKM1ouZ5DF0r Z062Jrqb/0lJokVPi32LlfQ4IzBsHcERK1TW0r+RJjxEEViYedoaosyLPAPD3g== X-Received: by 2002:a05:6a00:d74:b0:6dd:8a25:e167 with SMTP id n52-20020a056a000d7400b006dd8a25e167mr4255359pfv.34.1706559985074; Mon, 29 Jan 2024 12:26:25 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id a15-20020aa78e8f000000b006ddc3794de7sm6239422pfr.134.2024.01.29.12.26.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 12:26:24 -0800 (PST) Date: Mon, 29 Jan 2024 12:26:23 -0800 From: Kees Cook To: Rasmus Villemoes Cc: Mark Rutland , linux-hardening@vger.kernel.org, "Gustavo A. R. Silva" , Nathan Chancellor , Bill Wendling , Justin Stitt , Miguel Ojeda , Marco Elver , linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [PATCH 4/5] overflow: Introduce add_wrap(), sub_wrap(), and mul_wrap() Message-ID: <202401291224.53CA9C17E0@keescook> References: <20240129182845.work.694-kees@kernel.org> <20240129183411.3791340-4-keescook@chromium.org> <8dead2fb-d522-492b-89f2-1358198c1cdf@prevas.dk> 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: <8dead2fb-d522-492b-89f2-1358198c1cdf@prevas.dk> On Mon, Jan 29, 2024 at 09:08:43PM +0100, Rasmus Villemoes wrote: > On 29/01/2024 19.34, Kees Cook wrote: > > Provide helpers that will perform wrapping addition, subtraction, or > > multiplication without tripping the arithmetic wrap-around sanitizers. The > > first argument is the type under which the wrap-around should happen > > with. In other words, these two calls will get very different results: > > > > add_wrap(int, 50, 50) == 2500 > > add_wrap(u8, 50, 50) == 196 > > s/add/mul/g I suppose. Oops, yes. > > Cc: Rasmus Villemoes > > Cc: Mark Rutland > > Cc: linux-hardening@vger.kernel.org > > Signed-off-by: Kees Cook > > --- > > include/linux/overflow.h | 54 ++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 54 insertions(+) > > > > diff --git a/include/linux/overflow.h b/include/linux/overflow.h > > index 3c46c648d2e8..4f945e9e7881 100644 > > --- a/include/linux/overflow.h > > +++ b/include/linux/overflow.h > > @@ -120,6 +120,24 @@ static inline bool __must_check __must_check_overflow(bool overflow) > > check_add_overflow(var, offset, &__result); \ > > })) > > > > +/** > > + * add_wrap() - Intentionally perform a wrapping addition > > + * @type: type to check overflow against > > Well, nothing is "checked", so why not just say "type of result"? Yeah, that's better. I was trying to describe that @type will affect the value of the result. > > +/** > > + * sub_wrap() - Intentionally perform a wrapping subtraction > > + * @type: type to check underflow against > > The terminology becomes muddy, is (INT_MAX) - (-1) an underflow or > overflow? Anyway, see above. Right, I should explicitly say "wrap-around". > > > > > +/** > > + * mul_wrap() - Intentionally perform a wrapping multiplication > > + * @type: type to check underflow against > > And here there's definitely a copy-pasto. Ek, yes. > The code itself looks fine. Thanks! -- Kees Cook