Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp33920rdb; Wed, 14 Feb 2024 11:47:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXUNWferDHwEpJn6vAGh8ek5kdodGCfsX/CWgryUwu/fKBOXBRGB/ivQm6Re7hhvLW1PV82F0caseIRjPvViTUDGk4/7LzzbVid0+hb4w== X-Google-Smtp-Source: AGHT+IGd66BUzQQLYZu+e7d03nDcdDWgRlqFiGM7AdXZAnTnmFi7B0ghDfM7LCyyP3CV+lFmyUto X-Received: by 2002:aa7:d919:0:b0:562:1059:b27a with SMTP id a25-20020aa7d919000000b005621059b27amr2908597edr.29.1707940021304; Wed, 14 Feb 2024 11:47:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707940021; cv=pass; d=google.com; s=arc-20160816; b=R98LVC6CS1R+eLFitpdSVPlv5r2tQ7SbthrAnalf1NyE9lYvUxHbdZ8BBdI/yIk6Du ETr0bV6dRAH9N+mKEzTfWYFtLiBhSoIYCnwh/3K+8y70ggi0YNk5/12swMTWmolAEfwo SfA+E9hgu/RqoNOukMY8PO9cN1YSLVVp0m2FlEF2mBm7bJ15BacI4ejcyHjYR/cHjkX2 4kG5JJ8eKVRU7UUxswvIJ2UQtJeEuK2K2PNsj2gVZTupO5DzJ4IkFNgoS+uir/aJbbgu o9j5PI2WvHKvFpLz+Z7Hgi7WofiAnTvO78iRMYkMBZCP2ZlVpczteGzIovN4K7qfyorZ yq+Q== 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=tejg3N9iGCbYi0dxTPU89TKxpvu16yaRkXNuqXs5Q5E=; fh=OILoxUM5LgbFNEzhiJmK6SlDELsEyBuegBHOUOLAwwk=; b=duRa1lkp2YnEfBdLiEN4hbKAKDutmxbj4cJFSj9smtd3q8g812ctLOBxd3+Yo+5kRC hqlJsHHllZIhQyOnD9S5wD+ScJi+atWNM8hJzmBIVh4UBCHNZG34XW++hJtmW9XDjhwZ s4K6BMlpVWPDhcx5vysMHvRoSjvZr361bfuEZccnAKdvjKAbmgCYdIQV1HXfvFCxttFa FiF+icBBmYtUFEgqtWWtYJzynMk6JtYnvNI134GyNEBPPt8vQPWCiwW5sPUFdLO2Oxld 4Wm552Vg9ej6EIa/f2HEPBYta/AjRXdPLqfi3gdZ0fTT7ulyhjSh80lr4q0WbQlmfRiq uyTQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XDakyI+4; 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-65894-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65894-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; AJvYcCV/hvFKdi+FxizjLjVfyryqRFWpAH/snwk0KxGpyvyY+2SDyrCN1Cw13TJ1be1PQIT2I/KUNU0xHmRlAdAHYR8Td4No9YuMCjttqGKqTA== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id m14-20020a056402510e00b00561ccc91032si3073188edd.432.2024.02.14.11.47.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 11:47:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65894-linux.lists.archive=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=XDakyI+4; 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-65894-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65894-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 5A96E1F2A861 for ; Wed, 14 Feb 2024 19:46:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D86113DBB7; Wed, 14 Feb 2024 19:46:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XDakyI+4" Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 AB6F113B7A8 for ; Wed, 14 Feb 2024 19:46:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707939970; cv=none; b=a46TO+Z5DLzPbLDY2VuxJN7RVgj1k++LcUGVb2QP2Q4CYq3aiz6wS5An4VvPMnKcMvb2qGUB+byQFoNj1ELn7Itu1f1nV4+rGHQIX3OpCw9EaqI+6tr2YKi9g+dOqHTc5rJeY8/Y216E8PzsB9Xix/hXzF0haHfVoSfWjpgGkNY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707939970; c=relaxed/simple; bh=YgOkKOXlR0ASzxusUpx89CgykDXZ1IYPis0fKK7VnqI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BlXpq0LXRZXZtP2eGqjByv7XlLetD9dAf7esydwCwaTVI8HGeoiLYOM7cAe1TpJ3X/lLutMcwXwK1omtzOZGVoi/mxs8LjpDFDapLMQ0JtiS/1g7ULomrYkr4DH9gdKz23CiD8cTJfW0LSkzXsI+/asbKp86jYxrBm1ZKwndcOs= 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=XDakyI+4; arc=none smtp.client-ip=209.85.215.177 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-f177.google.com with SMTP id 41be03b00d2f7-5d4a1e66750so120425a12.0 for ; Wed, 14 Feb 2024 11:46:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707939968; x=1708544768; 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=tejg3N9iGCbYi0dxTPU89TKxpvu16yaRkXNuqXs5Q5E=; b=XDakyI+4vFORss2kyaqQ69FpmerJ42DgShcxxpOk83uc8eVjxHO4u/RY91+sX+icXk V+D8nj2HQ0DcT3saUrXWIo7luJMeOBhgfSForzI1lw8MUKpDxyO6CASe3Z/mMltKX9m5 5/ZBgJZm/qVu2HVoChhVChIFcfUNYPIfTMGd0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707939968; x=1708544768; 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=tejg3N9iGCbYi0dxTPU89TKxpvu16yaRkXNuqXs5Q5E=; b=KhYjZrGE/JLyxIIEWkZ0tCqv4s6elamDOQFu8z1ZKFsCzRCn04xmPDmF+FLer0jipa 8pWMzKmfRkxIeTLzfJjFyhA5L2pzWqiTzIaTQSxm/AbzB5baqXy/J2LfgirQZy+ctgtu nxtMacwsIEh0jSXdHukrzyzYZQ9xRHybe9sirse7blayDrr5JBsSPMb2OJ0Xkbz8N7sD YyB7bm1mDF2reoSy7Ri6EbuPnniseGzDKphgWiaH+2xyl+mM5K8KsXd8BxqCCEzXIKgY hPlv1lE5DSlFwHa1eu8Ia3v5Sb11Is9FjCPJnQGGGM8zAXV3NolSYzrhAnDAvwo3itJT 84NA== X-Forwarded-Encrypted: i=1; AJvYcCXdyQGUX4VuNPUKkJM5/yENjK1ZQOjamjApPErfxY+DelK8c6hAj+Jt/SI1gb2rZaEt1VJgq7bLtdTe8WTloMkhFBJcsEH7PVxZH+Xb X-Gm-Message-State: AOJu0Ywdu4KKBHNHLQupiUDkhVlw7z21Jy4/BXBmzkuM764r3JJ9RfAI avfsaop2IRXDK3fogKvESmO8qpYlyyADDEnPmp/LkWWbHEMghbx0lpKdtxbw5Q== X-Received: by 2002:a05:6a21:2d04:b0:19e:c9ad:68be with SMTP id tw4-20020a056a212d0400b0019ec9ad68bemr4640153pzb.43.1707939967996; Wed, 14 Feb 2024 11:46:07 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXk4JjOdU3VaIljHajvjjDeaBTKLY9UcT+t21yeudA4/9s/0YwdgSI8JHPAJiTYyGDsOwJ2XOI7F4hOyjGHcV5YPxUsqszMqx6eEnlJqHvBMu2bVLm8nuSsRrbBznJqROl9bz2TRurZ1ZcoMlfOT78ma7J0Dy7AsHm9meREoRMztnUCx0pjlDsV00uP6qYKM16ygGJ1cS/K7XIYLr5PUO3Ln1+ZSAFb+FhR+IOEDHSpLUfVPKxa566b6ivDGVqGaymtl9mlbuaUMNDuSg5jemxIzhRKyGgiaLVUt/2+xCo//nB3L8j4Cath6f7KrLbm22AjfzTcKocAJA== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id u23-20020a63b557000000b005cfb6e7b0c7sm4688648pgo.39.2024.02.14.11.46.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 11:46:05 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , "Gustavo A . R . Silva" , Marco Elver , Mark Rutland , Rasmus Villemoes , Eric Biggers , linux-hardening@vger.kernel.org, Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v7 2/3] overflow: Introduce wrapping_add(), wrapping_sub(), and wrapping_mul() Date: Wed, 14 Feb 2024 11:46:02 -0800 Message-Id: <20240214194605.602505-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214194432.makes.837-kees@kernel.org> References: <20240214194432.makes.837-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=5699; i=keescook@chromium.org; h=from:subject; bh=YgOkKOXlR0ASzxusUpx89CgykDXZ1IYPis0fKK7VnqI=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlzRh75R0GQGPPZ3V/orJDK5K3S77Rr4Gn5Trr5 BIOzQyMkSqJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZc0YewAKCRCJcvTf3G3A JmR6D/47QsZE4N+Np5gKhL0eBIwumGpqxvTd6vDXmG0nJVHUr6MjBIsXaVPK8xgyC/CBJkqHnu4 /3CHWgIQetK5nnziOHmusOYYYjzqjJ7xOwoZkv88oXIiHRBhoAJd4YGLeA4q2Xcr5R7cNm57+nI g7r7mIiy6m9H5c8409RHq8KglRXp1Fl/+DxfSSvPBmXPOciQvunRQLX7hTDqWYdoBtf7yMVic8r w3pVGGvmp1kXhNrzadU8VmDX/kI8nXu+oIuIklW6NhWUYUszF/AYva+k1jXU55cDxlpXBcWa/lP +prkhaCEwcTFYk8GubRxedPFKOf9Kh/LVKoQAJCLe7wForOeNv4FGhfj6bHQHrT1BslZK69u3H4 dTDMEbvct38Yf0Y4jhHoec64KVzGJ0dW1YMmneNkMWoEh5N6a1d6vuQRdJ3FDiDQAtzY+jMf1yE 9ICNjGGU2xplXOsF/qa52hktVd2fCKxQtOZWY697Pv8tzEnkzl6rIqdk/zl7UtNitVg7NJFp1dQ VNTe5XOaY30gijj3BGqF94elswFSPP01/Uqw1hSUVJCGoo8krNGAWp4ek1+MN0Xr4NvOwvGrFB4 TGJr+ZryV3TfTElN0qb4RVawAVOSy7Jflpogzin+/kO4/HTiDnZyBkO1Xh17AYFrH7v/d42jBrl 8juwtUGbB4vwKdQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit 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. Reviewed-by: Gustavo A. R. Silva Reviewed-by: Marco Elver Acked-by: Mark Rutland Signed-off-by: Kees Cook --- Cc: Rasmus Villemoes Cc: Marco Elver Cc: Eric Biggers Cc: Mark Rutland Cc: linux-hardening@vger.kernel.org --- 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 ad64d810c8aa..d3ff8e2bec29 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -65,6 +65,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 @@ -79,6 +95,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 @@ -93,6 +125,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