Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp386635rdh; Wed, 7 Feb 2024 07:36:22 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXuJs/7Ko5h6AbOR+8v4QTmJkwg/dEkQs9kY3Ssz4/shDqvOPjq8fSKCZ3Vm33L+pcY6EFl6maZxCOBqBW/TI7FKRJAa2Bnn1+VfgmOHw== X-Google-Smtp-Source: AGHT+IFW+NlkEtN/PANkueciK2ApLdCWCyPK+M2Hm7Pj/JIycqjHJw3VG7O52GCPbirOsV6X5c0u X-Received: by 2002:a05:6a00:22c3:b0:6df:ef8f:4bdc with SMTP id f3-20020a056a0022c300b006dfef8f4bdcmr4885421pfj.21.1707320182034; Wed, 07 Feb 2024 07:36:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707320182; cv=pass; d=google.com; s=arc-20160816; b=vqtEGWQYqzUOqxsLjDj60At4V84vmiM9r5pLsMv4U5FWaOin0LXujWztBdRe3AnUOV nnOqqADviwiBeRASdDDyvjgc1T5X+hkMff4KemGBs/d17LVhXRkKS8A16c5rTDxUmT6h g+ta7mTsddNEAA8SzTtUmiWlZO41Zo8KNQFEEUryY4WwGRWXT29/am17fQkb5kNu9ZCU VnGzVRqxjLm6KE1jle7oajV/jNhNeXPC5874U8BonShVOpOwnOzVhW7C6/sUI829hTch kAVSeG9UaVUHBK5gWsfgmAdNghts2+eR78UdBcnIlw+AxAceMFVROO9p/oqzr73WGoTL lXMw== 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=0EnFY1zWcdu8s/mjer9aS3MtbQycrZesMBVuK3m9B0g=; fh=0ul7WmLpIX2ldQa2NUwqSM0s6VMYxEpiCUZzseecVn8=; b=EQa/4JvFDGP4UGzuT+6zzgL+F7kU0IJzv1W8CrjIf3EbOMfsP8aNEquXf7GROqfGK4 CXQUrwTig3Fx2jl6KhKkCA6zWhBCIE07+dvEq+oZH4Pdu7+FwU9UgUVCeuVQUXvr+0xV c49vUGSzXGN04rfUx8X56jC0NHfNe1egkLI1oCAi4mwjOGkuklMrMQX7J6ohd7BtmM05 IKsmmKumGD5tvxqeVpI4W+dDyelCb0X6Q3oalDLy5N4CZPJWtq8/24V3ShCKZ8CCZ3ry iXv7Y/WDqj0MsTdlH/SRxC6+z07Cu74umWGm8aSysO/5KRW/Qtyjly0r0x5YaU6LNvwA nKhw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=it9IdQ+Y; 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-56714-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56714-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCXdky8brCdiWs9AJvdDwyOeutTPK4KeyFV+bIi0KaLYITvWJ2O0COk+MzS9k8YIl35KdbqlIvdXvX19W0+ijUK30EycUjQ/OfoW4w+G7Q== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id i129-20020a639d87000000b005dc0e9aefeasi1819666pgd.809.2024.02.07.07.36.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:36:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56714-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=it9IdQ+Y; 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-56714-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56714-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 4FC5FB2826B for ; Wed, 7 Feb 2024 15:25:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CFE678003A; Wed, 7 Feb 2024 15:24:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="it9IdQ+Y" Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (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 2EFA57F7DF for ; Wed, 7 Feb 2024 15:24:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319468; cv=none; b=vD146GQQu/iY6SCRnIbcm8ZjZS7xIuZbujHlijc8dP0pF83XFcgz0iHVNohcNQnuFYJNgqgI1hbOK2NMSttAqmRb8teAOSXeEkK1NubcycpKXA0vThxexsn3BagKh1GK4K9gSNNteB9iAMON9wawKZZCJM6/xDJ4SclA2bylXco= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319468; c=relaxed/simple; bh=UF4ZOJtI7adR1ITE12VYKN2eWkYypBgjoqWUYT5HRLA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cQu1h67W01+KasnskF2vr7l0C0EU93z3dAh91M2g9MAHqUHjZ8HWCUByWPM3jalUK4ydRQvwSSHHobiFtzp7wGiX0DoaX4XnOqBMBEYt5a1llSHlNjjoT4eVEbpYnx9fL7gihI6r1iZedABix1jDUk4a9zABRb9WVgQ1rpzu1ho= 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=it9IdQ+Y; arc=none smtp.client-ip=209.85.166.182 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-il1-f182.google.com with SMTP id e9e14a558f8ab-363b2cc93a8so2646905ab.3 for ; Wed, 07 Feb 2024 07:24:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707319466; x=1707924266; 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=0EnFY1zWcdu8s/mjer9aS3MtbQycrZesMBVuK3m9B0g=; b=it9IdQ+Y4JrTK1mcLttbVNTGIVvWN7I4MpBJRJJuefxlfo+KY8TScUC/f9iJo9SFG1 ffaDNp+ISbsxgA/vdcYKuUzzAXL5/rQI3k5gYo+AkQmzbVmZk7esj15XjMB5e+itNq9A cncRON5ewJYe40jyiEV2S0swdtwpdSfMjaSjc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707319466; x=1707924266; 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=0EnFY1zWcdu8s/mjer9aS3MtbQycrZesMBVuK3m9B0g=; b=dpXtz03/4K2paAevYiyv/FWzIgeE1YOji2fP2BJthy9zKR7IuuIKM7OP9xEUvqbaT5 GfM8klG2trQfejsYbJWbeprDvzdJk0HUpTDV2/Wp8twOKCCpzo00vfdUIUxSW7mNrrZg hDZn2iQPpba6X3k8ssf05p1iS58ik/U/9r+g17gJ/QonR5IZEHLKS+XKTzGE8rEgSUS2 nZ5e+WZ0HSeqkLKeTtBiM2/OD4wVTvLnZ+50Gahpre4iVW/tsYpK/nfwBE2QHD5UEXvt bj/vBwSr5mLtydYp5KDMT1OAp5/pB5Q5IiGVPypek5P1YvLVwouut1aIhM8k6EmZa1l2 C16Q== X-Forwarded-Encrypted: i=1; AJvYcCWlqTd4+RIt/pYHwujTBEk+xK57M4lhIS6nCmKRJELAz9qi9TRs8LsbvHmhnUV97lPGD7pOwa3qTWAUVo7XEMjpONeqXZTO8vDg37O+ X-Gm-Message-State: AOJu0Yw2O8hzvyYdjXcSBLtSEzRl/28gnu7G4blH6oV1fOx1uCbYtcPf ify/QM5jNDqLhBZGBsCDJbSFtlzXR77VhlABLavIsdSRN2VcoGMCDsHZaBL+cQ== X-Received: by 2002:a92:c5ad:0:b0:363:b519:bff0 with SMTP id r13-20020a92c5ad000000b00363b519bff0mr6021971ilt.23.1707319466265; Wed, 07 Feb 2024 07:24:26 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUeS11GMGkn+N1jnRT9igB6tGXtdjWUgYdh+a9ZUhg+63egyGammrT3oOfUDicT/SFBZod7cWWHqfpaP4Fff84UEy1gDTfc0KLGBBGRGgHBECtjrWYXPlVrjuoz7iZnqF7jb1+U1fMf0ZmxB3RkPuwtR6FSq3pEIOwXfF9yvC7uK2EV2gjvG1/68EjYurTdPZB2HUkpcGCImdt5OiWRKk8hw5Uhn+ZjY9XLNK+pMPoZMF27sUF7p4ZZkg92D9VDr5+dMIB+/0awbiK9bpqsBlfSKbs0ml3RidV2jTlCAWWD3OwC1xYX9Zd0rA8aO27skohWzu7g2vQPxQ== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id bn20-20020a056a02031400b005dbed0ffb10sm1547254pgb.83.2024.02.07.07.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:24:24 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Rasmus Villemoes , Marco Elver , Eric Biggers , Mark Rutland , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org, Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v5 3/3] overflow: Introduce wrapping_inc() and wrapping_dec() Date: Wed, 7 Feb 2024 07:24:20 -0800 Message-Id: <20240207152422.3855315-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240207152317.do.560-kees@kernel.org> References: <20240207152317.do.560-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=4594; i=keescook@chromium.org; h=from:subject; bh=UF4ZOJtI7adR1ITE12VYKN2eWkYypBgjoqWUYT5HRLA=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlw6Ck4Knv0CZm8IYel3Z4RJZj9vYED+A+0Ru26 cX6qKGKwNmJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcOgpAAKCRCJcvTf3G3A JoQzD/wI+8Lf+6AaeGFKfxalG4dQxHYlwN0aqDm8Alb9xyyctuWdjfXnTerEf7Zbl0mQKHHonps M8lLZGpLU8sHrOb5ycfRTBw09he+nj3AF9xKEuGKkZ9O8ycxUVz2HqcRyF+lqTKV063RfCkqujw zEGzjrVLjgVd0x1FxNUztDEgZV73ow9l+Cfgp8ERi+ikCTsGdUoho3AorJsW8Y0qvR3JXMM97jQ xFbGLZ0PCLojIvcxjdrKESl63H9hPevoJIB0FG/Sn6WVxyN7kot9WPDJfgQ2WIUqtwMGrXBPDjz OR4YrvEdTDY0TkalsfmYhzoanRrSbRXil3oygyW9YDJKtGsa/+yNubbxSCzacaSqIQRs1oeJmnZ 6IcVxM6bLXJWil6fIa6eUCvhAImjxYs1wh5jG9ZzqnjWfugekmyuEBd/0tHmJ+grIqjRmpVJBrb omBgutZBtt0zqBiK9fTx0LQWQmSCtFnzFvDKPYhIIY0IAYoi3Nml87y7pXmeBcuWEW7n6uUNpKx sJZqMfGtB3ozLCe+nRdTGn39Dq7lh/XJNaYN9FHtqFdo9Hi7R7iHMrW4xnR/Xt8vV4E2jesuwhs X0RHoojimonoJ4txcOJ1P4/8GMLM7KbV4havukwMIjKe34cKjTWZBEkWR09W6VfbAhNC0O9sc30 hB55mBv3p5236Ng== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit This allows replacements of the idioms "var += offset" and "var -= offset" with the wrapping_inc() and wrapping_dec() 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: Marco Elver Cc: Eric Biggers Cc: Mark Rutland Cc: "Gustavo A. R. Silva" Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- include/linux/overflow.h | 32 ++++++++++++++++++++++++++++++ lib/overflow_kunit.c | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 849a49fb496e..f4b18007fd2b 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -80,6 +80,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) __val; \ }) +/** + * wrapping_inc() - 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. + * + * Returns the new value of @var. + */ +#define wrapping_inc(var, offset) \ + ({ \ + typeof(var) *__ptr = &(var); \ + *__ptr = wrapping_add(typeof(var), *__ptr, offset); \ + }) + /** * check_sub_overflow() - Calculate subtraction with overflow checking * @a: minuend; value to subtract from @@ -109,6 +125,22 @@ static inline bool __must_check __must_check_overflow(bool overflow) __val; \ }) +/** + * wrapping_dec() - 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. + * + * Returns the new value of @var. + */ +#define wrapping_dec(var, offset) \ + ({ \ + typeof(var) *__ptr = &(var); \ + *__ptr = wrapping_sub(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 d3fdb906d3fe..6cadce8f8a47 100644 --- a/lib/overflow_kunit.c +++ b/lib/overflow_kunit.c @@ -284,6 +284,45 @@ 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 = wrapping_ ## op(_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; \ + wrapping_ ## op(_a_orig[get_index()], _b_orig++); \ + KUNIT_EXPECT_EQ_MSG(test, global_counter, 1, \ + "Unexpected wrapping_" #op " macro side-effect on arg1!\n"); \ + KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \ + "Unexpected wrapping_" #op " 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) \ { \ @@ -293,6 +332,10 @@ static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \ 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); \ + /* wrapping_{inc,dec}() */ \ + 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) { \ -- 2.34.1