Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp483719rdh; Wed, 7 Feb 2024 10:13:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IETrU3dEVUpUa6gc9AplDkc9Ah9/f8GoaP2nEdtP3ukUW3LlMnwz5ucFK1zx/D+nBe3PteY X-Received: by 2002:a05:6214:d0c:b0:68c:aefb:5bf0 with SMTP id 12-20020a0562140d0c00b0068caefb5bf0mr7309562qvh.27.1707329616038; Wed, 07 Feb 2024 10:13:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707329616; cv=pass; d=google.com; s=arc-20160816; b=Sd8rRuO6pVtOeH9aFhheelGvtYK1c1LyocJxXlduW41rJlQqesMr6dtXg+5AJ3/cT1 BCqqyVihi6EiDYo6ThEkGniytkVJO2UauDKxOXcU9c+4RlC7jtptKL3dMVWFNQgcb9Y1 Wl1JiZkLDBm02I3/S56ISPXUU7T7F9UvOZAjdV5vcdafz4AKKq+6tNmMgTmKKRb4GTHL QpJyDkqPfWNz8YPKDG9ZGwNgTDbrxTgGU5QZOOYghcwnDq+iPamPti5nyP+jN7fmNYsc oXK5N13ouyt8li8HVX65rNlIX+fccC1bKrjFXVBvLI2ICR3QU32fi7rUxCaALtQDkprf 85kQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=LVOYHlYDXobk/Y8KugAL3FuLhZWY4+w3iqY9MiF2FJs=; fh=TXk2ooevurqpg76DbumwFoWfB4HZ8EEE8s8xcqUmm70=; b=QXw8UW0omw+zBCActa/adG/OzdD5uLbqwMXHtt6sw5B3eaD2d1JiDqhcr3SBttLiv3 Jz9yLx/bOecvjE2508T+85Oyf+VaLAK3YOkJs76E3/KtX0jsMGAJdWd62Ofz5GiN9ExI VVL8iyMVwo0pVSpwv91nB/lm5GKatvDsHz2B9x8Y74BBtsuWHuXGA037jDuuWWidBNYc RFW3i53TdL3c+gnmdISrNfhFnCO5hGyxYNWEHbScMlPuNJznFe1iaEMlm00romyv2c3U H2alTdK5aO1aZtuyEwpk/w2isZQpoclwVdTw6SSgdslQEIge6xxIfhtrDjVYmIAHhPP+ +Aeg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kRxaaOxO; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56890-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56890-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVp9JWvyuJ80pffZXwB237V37akgA9Er5gFIpS+a7BSbnAE2kBVQHzB8Gw3JFSxoznNP04R6B+5M+ICg/7z3TeQ8qckiBbwNGLldTFafg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id vq26-20020a05620a559a00b007839273251bsi1572127qkn.322.2024.02.07.10.13.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 10:13:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56890-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kRxaaOxO; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56890-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56890-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 9495B1C240C0 for ; Wed, 7 Feb 2024 18:13:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C860E129A79; Wed, 7 Feb 2024 18:07:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kRxaaOxO" Received: from mail-vk1-f174.google.com (mail-vk1-f174.google.com [209.85.221.174]) (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 E383A83CBE for ; Wed, 7 Feb 2024 18:07:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707329257; cv=none; b=XG99jpj/y24hLI9QOtPn6pUoDRMQAXS6Ss1BMTmobxz6Y3dCpFv3DG0sFeMEe6LF6CG5s3BSS7hy6g7JoSgT9Z8OFfMkcn49WnAVt+GZF3Ycl1BOr/4ZQdWUUJwoWLP07E4FM0eq+7ICiTZ9dVV6I/hJiYu2v1u9NLEwsM2b4m4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707329257; c=relaxed/simple; bh=u7Y5hwCXANmeUzrEMrdhHd6/7MdWaER9imC3jVr9EaQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=qhExO6YFq1xpcJp6CiouZbpNZ//OAXXE8v67hZj0ZbPWGTHn6YIG6ueCYuaWsSOvSlBjP9UdwU1kpBePkIrXz/tREj//nke9jAh9aN73fiwU3dTMzKdn2bed4pgxmpJ2I7ZFBYtIbFMQniXvmh4X5j3pzL/rys1v/MtxKJ15l6E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kRxaaOxO; arc=none smtp.client-ip=209.85.221.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-vk1-f174.google.com with SMTP id 71dfb90a1353d-4c02af5c0b6so307750e0c.0 for ; Wed, 07 Feb 2024 10:07:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707329253; x=1707934053; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=LVOYHlYDXobk/Y8KugAL3FuLhZWY4+w3iqY9MiF2FJs=; b=kRxaaOxOCWJaH9Jp0d/Eg7SLUsrMa2kc3uZAmByQ+R3PUDS4KNIO9SmsZLwFl0PadF 98f5UbmrVtVEM/OX4kM5j9vm6H2QJaR7frddmlbhC3VHtf59gLAcM2wuoSCcRBaLs9xq pZSlomwEHvUENNBVkatCrhLBufeBqaXAF2U5N5XieiZX8mnoEx6fZpOmSyhrya47wdTQ vureIfT+6GNavkV6kXVQlDMAFldPDjFAFoXIzvdpenBoD3St9ashIYDGUrvjbAXRwen/ x6K2171YR2egEps4etdhguYqOcXkEIXplro1YunXSFHto+TGmlMLn6Q4Wq8ZjSUIznh+ 9ylg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707329253; x=1707934053; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LVOYHlYDXobk/Y8KugAL3FuLhZWY4+w3iqY9MiF2FJs=; b=oXZ+iXvqSsVmn5SZX084FXSMkgfkL/dHHmkcHi0Ybb5KoIhbHlfgvIuMGNTaUEK60v nL4uE2Hj8ji0+f0wuU9YEdtGVia5W1HaDDsiTOuT7rPG+uPDlBlmWXu4ZqVMU3a/U9ix AjPfTM6dI/HMW/O0h1lbO7/KAL19vzM6D3DrA6etQhlJENPg32xKqAC1HEGN5pE6P6m1 yfVqA950v9NqVsiwIyc2iPty0b01z+6Vu3vpoqO8mizZxjlctqRDci3k1tIN2ivld69T QovBiWOBJbPaz+TKkF2dDqpca3uZANarafFTOd9/y+j0zI5GVgZcBuHZRbmIpQB3wKCF HbEg== X-Gm-Message-State: AOJu0YyuPBqppPgv8X7SoS24WyN4OQTFuYk/P4ZNF/g+VQ38Jil2gw9P abP+cP0VqdUBjmkvbD3QwgnudV3FGHaVaOsiZuWHbZimfbkzgoKT822lfM8oE0Kxk2I/z8H9cb/ U/bZVPua4FjwsSpdI+PpbuL1egaw9Te4g/u/e X-Received: by 2002:a05:6122:90f:b0:4c0:1918:27de with SMTP id j15-20020a056122090f00b004c0191827demr3588823vka.16.1707329253341; Wed, 07 Feb 2024 10:07:33 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240207152317.do.560-kees@kernel.org> <20240207152422.3855315-2-keescook@chromium.org> In-Reply-To: <20240207152422.3855315-2-keescook@chromium.org> From: Marco Elver Date: Wed, 7 Feb 2024 19:06:57 +0100 Message-ID: Subject: Re: [PATCH v5 2/3] overflow: Introduce wrapping_add(), wrapping_sub(), and wrapping_mul() To: Kees Cook Cc: Andy Shevchenko , Rasmus Villemoes , Eric Biggers , Mark Rutland , linux-hardening@vger.kernel.org, "Gustavo A . R . Silva" , Andrew Morton , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" On Wed, 7 Feb 2024 at 16:24, 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: > > wrapping_mul(int, 50, 50) == 2500 > wrapping_mul(u8, 50, 50) == 196 > > Add to the selftests to validate behavior and lack of side-effects. > > Cc: Rasmus Villemoes > Cc: Marco Elver > Cc: Eric Biggers > Cc: Mark Rutland > Cc: linux-hardening@vger.kernel.org > Reviewed-by: Gustavo A. R. Silva > Signed-off-by: Kees Cook Reviewed-by: Marco Elver > --- > include/linux/overflow.h | 48 ++++++++++++++++++++++++++++++++++++++++ > lib/overflow_kunit.c | 24 ++++++++++++++++---- > 2 files changed, 68 insertions(+), 4 deletions(-) > > diff --git a/include/linux/overflow.h b/include/linux/overflow.h > index 4e741ebb8005..849a49fb496e 100644 > --- a/include/linux/overflow.h > +++ b/include/linux/overflow.h > @@ -64,6 +64,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) > #define check_add_overflow(a, b, d) \ > __must_check_overflow(__builtin_add_overflow(a, b, d)) > > +/** > + * wrapping_add() - Intentionally perform a wrapping addition > + * @type: type for result of calculation > + * @a: first addend > + * @b: second addend > + * > + * Return the potentially wrapped-around addition without > + * tripping any wrap-around sanitizers that may be enabled. > + */ > +#define wrapping_add(type, a, b) \ > + ({ \ > + type __val; \ > + __builtin_add_overflow(a, b, &__val); \ > + __val; \ > + }) > + > /** > * check_sub_overflow() - Calculate subtraction with overflow checking > * @a: minuend; value to subtract from > @@ -77,6 +93,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) > #define check_sub_overflow(a, b, d) \ > __must_check_overflow(__builtin_sub_overflow(a, b, d)) > > +/** > + * wrapping_sub() - Intentionally perform a wrapping subtraction > + * @type: type for result of calculation > + * @a: minuend; value to subtract from > + * @b: subtrahend; value to subtract from @a > + * > + * Return the potentially wrapped-around subtraction without > + * tripping any wrap-around sanitizers that may be enabled. > + */ > +#define wrapping_sub(type, a, b) \ > + ({ \ > + type __val; \ > + __builtin_sub_overflow(a, b, &__val); \ > + __val; \ > + }) > + > /** > * check_mul_overflow() - Calculate multiplication with overflow checking > * @a: first factor > @@ -90,6 +122,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) > #define check_mul_overflow(a, b, d) \ > __must_check_overflow(__builtin_mul_overflow(a, b, d)) > > +/** > + * wrapping_mul() - Intentionally perform a wrapping multiplication > + * @type: type for result of calculation > + * @a: first factor > + * @b: second factor > + * > + * Return the potentially wrapped-around multiplication without > + * tripping any wrap-around sanitizers that may be enabled. > + */ > +#define wrapping_mul(type, a, b) \ > + ({ \ > + type __val; \ > + __builtin_mul_overflow(a, b, &__val); \ > + __val; \ > + }) > + > /** > * check_shl_overflow() - Calculate a left-shifted value and check overflow > * @a: Value to be shifted > diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c > index c527f6b75789..d3fdb906d3fe 100644 > --- a/lib/overflow_kunit.c > +++ b/lib/overflow_kunit.c > @@ -258,20 +258,36 @@ DEFINE_TEST_ARRAY(s64) = { > \ > _of = check_ ## op ## _overflow(a, b, &_r); \ > KUNIT_EXPECT_EQ_MSG(test, _of, of, \ > - "expected "fmt" "sym" "fmt" to%s overflow (type %s)\n", \ > + "expected check "fmt" "sym" "fmt" to%s overflow (type %s)\n", \ > a, b, of ? "" : " not", #t); \ > KUNIT_EXPECT_EQ_MSG(test, _r, r, \ > - "expected "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \ > + "expected check "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \ > a, b, r, _r, #t); \ > /* Check for internal macro side-effects. */ \ > _of = check_ ## op ## _overflow(_a_orig++, _b_orig++, &_r); \ > - KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump, "Unexpected " #op " macro side-effect!\n"); \ > - KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, "Unexpected " #op " macro side-effect!\n"); \ > + KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump, \ > + "Unexpected check " #op " macro side-effect!\n"); \ > + KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \ > + "Unexpected check " #op " macro side-effect!\n"); \ > + \ > + _r = wrapping_ ## op(t, a, b); \ > + KUNIT_EXPECT_TRUE_MSG(test, _r == r, \ > + "expected wrap "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \ > + a, b, r, _r, #t); \ > + /* Check for internal macro side-effects. */ \ > + _a_orig = a; \ > + _b_orig = b; \ > + _r = wrapping_ ## op(t, _a_orig++, _b_orig++); \ > + KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump, \ > + "Unexpected wrap " #op " macro side-effect!\n"); \ > + KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \ > + "Unexpected wrap " #op " macro side-effect!\n"); \ > } while (0) > > #define DEFINE_TEST_FUNC_TYPED(n, t, fmt) \ > static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \ > { \ > + /* check_{add,sub,mul}_overflow() and wrapping_{add,sub,mul} */ \ > check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \ > check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \ > check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \ > -- > 2.34.1 >