From patchwork Mon Feb 5 09:12:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 196696 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp755809dyb; Mon, 5 Feb 2024 01:13:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IF0P8T2l2X0db3BZSJvNZG0w4KR42VPOt79zzv00V7sqZA5xDsfiQWwXOr9zzIG7d3mU4DT X-Received: by 2002:a17:907:7ea8:b0:a37:65ce:ac47 with SMTP id qb40-20020a1709077ea800b00a3765ceac47mr4572813ejc.66.1707124419109; Mon, 05 Feb 2024 01:13:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707124419; cv=pass; d=google.com; s=arc-20160816; b=D/b5R9zIzuqbGn1H4r9i7v35q57DZkuch/bhtoplXEbokvRzDnwXz0RJL30VAVxkgu fUgPmqP3I+4h+0fk2oY3U/d9x2WAnrZVM0vrNKKTSmhwbNYOo0gwrjD+oC7G5YVyACZC /BsxVVvFdrUfB3hefAu5kWBf74sWWY5eeB8h3G7uKo/Jkd/nzlhMLT98kpua9duK4fDu u46KeSyxMeTb/GGbWDrTGdP0U7bJdw2h2zxJs7f23NZWpVlbvZChpUQuk0/Srzs6e1HV 0K0fmvXcxqqY26v0D1VSPu98T9Sm2PCufEdx4gKzmuafARbUBn8bNMq2twH5MHJUaLHO d4TA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=jniMpSsyDsU5fAoe54+F89x9cv/B23a7/rxO8q9YHEU=; fh=tk8vxAgpWyKGwJyDd2xrlVk2+ri47WgchN+l/OwbA8I=; b=yKGpjiu6Z+oF9W5IMjSsAxWPaZDC4b7PG5PubPkaiaSwQC72QJarXH0uBoUOxWMI7d h1oI5pNbkDe351fOeH7VQZvqQwEzdUDcNIA/9D2bQq78HfG6HtjXJX8yOb/XBWi7kbes N4qp45RHSAqBVedZexrP5Gx9Nk8E/p/4KY1Z6kLgiVJgk86KS973YEHBuec8U/v6XXnL lsZ+IGd5nw8cK+iBoAVwN0Rj+ADxCqXffgajsMtiUfeh/7fRX602JOPirQKtNzoZP6Fy TA7FDMgxLu5Y7/4nsLAyPhywl755k8WpyDBYD4RVMbjL9eOFoc1ZLPiA//5vGGcW9cqo c1xw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XQfGdFVW; 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-52271-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52271-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=1; AJvYcCUZY+uyqNP+awhPSPTFDICitkUWfK43dlDmg24lca7b6p8prbrJHpvK4ylJRPiHZ+xkp1W8br8glg+UnVaEhkpgpP3P0g== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r12-20020a170906350c00b00a37be224c02si830382eja.135.2024.02.05.01.13.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:13:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52271-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XQfGdFVW; 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-52271-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52271-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 814721F22E18 for ; Mon, 5 Feb 2024 09:13:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7C5CF13AC7; Mon, 5 Feb 2024 09:12:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XQfGdFVW" Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 60FD5125B8 for ; Mon, 5 Feb 2024 09:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707124358; cv=none; b=GaHrGBQv4ELdjGmpal1lYotoFu8wtASbiLKRLVR7hkmZLBNm62ht8vudqn3CLOiG4jLKOeo7WXAJDZ8t9oqtqYz77dV09DAel1MjQAAlieWKqDTzu4H3LwiywyganKzgKkq958aho4zWI4I60BIBYlzBVeDEhaqyxxgf6GRuKyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707124358; c=relaxed/simple; bh=BleaLxaprNdd2Ls0XK+kcFDPaLkJ843EpGNnQRcoyMU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i3DV9UYY0/xsT9LMZliX5sk24WQ7x15aTEinCTlQnq4lq0f1CrpezXwwHMIZ4XpP8p18gvwI+chu/Xs1bFjtCnrY61eG2V7m8UXEia9ity1lyZ0lskz72s8sN3zBt6NZzEEFGxkVvd56Cjgct2EyUGJ+rM7aRlxDATVJEZP8c8k= 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=XQfGdFVW; arc=none smtp.client-ip=209.85.215.179 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-pg1-f179.google.com with SMTP id 41be03b00d2f7-5cddc5455aeso3585338a12.1 for ; Mon, 05 Feb 2024 01:12:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707124356; x=1707729156; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jniMpSsyDsU5fAoe54+F89x9cv/B23a7/rxO8q9YHEU=; b=XQfGdFVWO0p+PX12ExVRiTRWXeOYHViZ/JWxQHMN38xoApPy9zCKbb1DGOGAxtYMqc j9NWO3cR4GGqytLtYTf1bgy4MlguQcBFkHrucXIdn+WMLg2FJSOe1eDkK4GrbmV/Rbjf JkodyDGqWIKIKAC8cTY/m4JkHecomh5MNNzvw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707124356; x=1707729156; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jniMpSsyDsU5fAoe54+F89x9cv/B23a7/rxO8q9YHEU=; b=SRXr0TLIJxoQVYpWvenhW1VtvKTGLVyAPSavNyuLbGHSzIYRoTHE8Ja3gA5SFhPOF9 JHHvktNjhJdr6j4zjRCmVpjNdSdiBIwcfWY7HJQUIKJWfPO3S4tOVHDUcPvE0Bydgs/b ngMormbH4PkvEAqnUOHM96KVmMSG3XDb6vvr0DWvbB+K1vkizImLIKklViYlqUD2YAnM bWRy7pbR4nplyVfKfcpyqIWcrBE3yxHCW4VTsEv2ey/auX0CqSNoxwn4uLwpoC1OTKS0 cYVFnGhDkK5Hu6JADp4tC0Uw+tM3VqZYaog2gKMpfzsmuBYdmZtci24u6tPqsIrwz8Ax 3/NA== X-Gm-Message-State: AOJu0YyosbjNIieSc5rogIWDz4saDR1wdmZC3CqfCm8brNrfpVLi/2yQ P7dVqEOrr09IJKqyX/L49SGD7Dg9g4GP75qJ34eST6Jwx4YoOu+WFchjlncAww== X-Received: by 2002:a05:6a21:2d8b:b0:19c:61b9:d9bb with SMTP id ty11-20020a056a212d8b00b0019c61b9d9bbmr9712518pzb.8.1707124356612; Mon, 05 Feb 2024 01:12:36 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVDvA96yjO/KwcQR41cix9tuSjfftoIF+xmpKioEFUhdprdWIt0pZSh61sqHp33TIoS1UHA9/wAuFVqJCk7QKpDxgUJ9KXq4shewdbUN3sU7aQv08Gu6dD8fcnrUbhHiinpqZdJ00donD8iL/jnNk/Wgvjq7JfBxofQ+Yg/3scTTEGOLttrfvC+8+E6YMxmmLoR3IA76abHYWvdrrPMaEpiSam2wpymyQ== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id r22-20020a62e416000000b006e03cce3f4asm2280702pfh.25.2024.02.05.01.12.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:12:34 -0800 (PST) From: Kees Cook To: linux-hardening@vger.kernel.org Cc: Kees Cook , Rasmus Villemoes , Mark Rutland , "Gustavo A. R. Silva" , Marco Elver , linux-kernel@vger.kernel.org Subject: [PATCH v3 3/3] overflow: Introduce inc_wrap() and dec_wrap() Date: Mon, 5 Feb 2024 01:12:31 -0800 Message-Id: <20240205091233.1357377-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240205090854.make.507-kees@kernel.org> References: <20240205090854.make.507-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4480; i=keescook@chromium.org; h=from:subject; bh=BleaLxaprNdd2Ls0XK+kcFDPaLkJ843EpGNnQRcoyMU=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlwKZ/47AUqLm5ToJr980/JQTh0fsearVme/cqG RQaW2oif+qJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcCmfwAKCRCJcvTf3G3A Jig5D/9LTGnRArg0ixC/Igu7Weo0f3WLiXQISsIPY7j85lWdvoVeZyJ0V87CuTxxChHPBIDb0Ze g/y15S7sis0rYZa2ZtEF6dgK9GCv08iOgJbkLWO/RJ58Mcltp6AkmzpcrAbVNiV0CY3yhZzq4aE /7o3sYZfqZv4bDrUuzaCnOlBcEdcVW1jHLJIkfklpFd1Eyxoq9Ec/nsN88H4K8HrjZLpXwQpe1k cAU1LsePQNAOvkmjkBF2pm12SsTu2gehJJ31jNczjIL6LtJ9uhiOUUZ6i3KiIz0l0cNlDC3N26H YVTRkOLO11m6cL3GGORuZ5R6fNuUYCpmLvsaRjEZx4fXlHzpk9G+cqqUEgNGd9REKFcRkXXMQ85 tL0i1KFlUXs7JXdeMicgHrT6Ub+mii46osYMpJ4xzLSs7jFAzodrToS9+N3GwzMZYBxGuDI3o+3 K35QXFDSVLKhJ9fYxEmC5qppuQkWKzPqW0Q8RjEsGpRFdVreVm1vQzCi1NJ/Q49fNjljmCru+e2 9/y79fOVtlpqZMVsvRILZL79qVbGBDJ/KuMAeH4UrjBPsEJuQCoZf27NLy5CPnuJ+UBGNtIBiHn lkMMWm6QOiQcXZkC3Q9f+selb3BETCjUv1U9aR0qquKjV0fqsoSzVCSwYrhWOGSt00cP0Ongw3b aCoe6Sbbn5LaNOg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790049695093627997 X-GMAIL-MSGID: 1790049695093627997 This allows replacements of the idioms "var += offset" and "var -= offset" with the inc_wrap() and dec_wrap() helpers respectively. They will avoid wrap-around sanitizer instrumentation. Add to the selftests to validate behavior and lack of side-effects. Cc: Rasmus Villemoes Cc: Mark Rutland Cc: "Gustavo A. R. Silva" Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- include/linux/overflow.h | 28 +++++++++++++++++++++++++++ lib/overflow_kunit.c | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 9b8c05bdb788..6a4c18544ab1 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -82,6 +82,20 @@ static inline bool __must_check __must_check_overflow(bool overflow) __val; \ }) +/** + * inc_wrap() - Intentionally perform a wrapping increment + * @var: variable to be incremented + * @offset: amount to add + * + * Increments @var by @offset with wrap-around. Returns the resulting + * value of @var. Will not trip any wrap-around sanitizers. + */ +#define inc_wrap(var, offset) \ + ({ \ + typeof(var) *__ptr = &(var); \ + *__ptr = add_wrap(typeof(var), *__ptr, offset); \ + }) + /** * check_sub_overflow() - Calculate subtraction with overflow checking * @a: minuend; value to subtract from @@ -113,6 +127,20 @@ static inline bool __must_check __must_check_overflow(bool overflow) __val; \ }) +/** + * dec_wrap() - Intentionally perform a wrapping decrement + * @var: variable to be decremented + * @offset: amount to subtract + * + * Decrements @var by @offset with wrap-around. Returns the resulting + * value of @var. Will not trip any wrap-around sanitizers. + */ +#define dec_wrap(var, offset) \ + ({ \ + typeof(var) *__ptr = &(var); \ + *__ptr = sub_wrap(typeof(var), *__ptr, offset); \ + }) + /** * check_mul_overflow() - Calculate multiplication with overflow checking * @a: first factor diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c index 064dccd973ad..23e37bce0022 100644 --- a/lib/overflow_kunit.c +++ b/lib/overflow_kunit.c @@ -284,14 +284,56 @@ DEFINE_TEST_ARRAY(s64) = { "Unexpected wrap " #op " macro side-effect!\n"); \ } while (0) +static int global_counter; +static void bump_counter(void) { + global_counter ++; +} + +static int get_index(void) { + volatile int index = 0; + bump_counter(); + return index; +} + +#define check_self_op(fmt, op, sym, a, b) do { \ + typeof(a + 0) _a = a; \ + typeof(b + 0) _b = b; \ + typeof(a + 0) _a_sym = a; \ + typeof(a + 0) _a_orig[1] = { a }; \ + typeof(b + 0) _b_orig = b; \ + typeof(b + 0) _b_bump = b + 1; \ + typeof(a + 0) _r; \ + \ + _a_sym sym _b; \ + _r = op ## _wrap(_a, _b); \ + KUNIT_EXPECT_TRUE_MSG(test, _r == _a_sym, \ + "expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n", \ + a, b, _a_sym, _r); \ + KUNIT_EXPECT_TRUE_MSG(test, _a == _a_sym, \ + "expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n", \ + a, b, _a_sym, _a); \ + /* Check for internal macro side-effects. */ \ + global_counter = 0; \ + op ## _wrap(_a_orig[get_index()], _b_orig++); \ + KUNIT_EXPECT_EQ_MSG(test, global_counter, 1, \ + "Unexpected " #op "_wrap() macro side-effect on arg1!\n"); \ + KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \ + "Unexpected " #op "_wrap() macro side-effect on arg2!\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 {add,sub,mul}_wrap() */ \ 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); \ check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \ check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \ + /* {inc,dec}_wrap() */ \ + check_self_op(fmt, inc, +=, p->a, p->b); \ + check_self_op(fmt, inc, +=, p->b, p->a); \ + check_self_op(fmt, dec, -=, p->a, p->b); \ } \ \ static void n ## _overflow_test(struct kunit *test) { \