From patchwork Tue Feb 13 22:10:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 200701 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp838453dyb; Tue, 13 Feb 2024 14:16:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWqGXZRXM3B8wMvoMimm+FIFlaYMERXixIzyxlHaFA7MyV+bmPkgLdy0tCBAm7dF6byx1syFS7CxNFyH1uAjs3QoNA8KA== X-Google-Smtp-Source: AGHT+IGbCeH5SPcPfs5uAMPKjGkItnUfnkj+QMFJ1K4W4gF/t6VJ5GIXS5HyeekDag3LJDo8+fub X-Received: by 2002:a17:906:d92b:b0:a3c:fd4b:43ff with SMTP id rn11-20020a170906d92b00b00a3cfd4b43ffmr422313ejb.22.1707862604408; Tue, 13 Feb 2024 14:16:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707862604; cv=pass; d=google.com; s=arc-20160816; b=JOzaVByAnRi2Jt/G+1s62RzQh974CDCkb6J2vz1UENrzks5Slg+uG3Ph/JWJjkHslr pH7qkSc+p0ANohvVjNaQtnN4PZKB9L86ahXlB2LZSmAT+mSKZgrzxb4ki5eKYKNsP2TU AsGOi7F2OiZCrdxal6d/BCI/xHwYsrmFhJlAdkWpM/Cmfz33KB5wNDu2ENsRaJTpq+T4 pDcbLVcFEpTsIxyCuENsqaN6tu6EZ5A0JcQejOnEADYop5bgJLIza+xoZtHkGPI620zo xT06TMoKsixyqd+ME8ebrki9oqoq0MQo0Qox/3q6FpDAibAZbyONDtz+wXiUcIcIEkG1 ukaw== 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=RerG7d2KkWMxwjKi3Bk1dJMaOiX+RZ04/h8EQNw22Ro=; fh=5RB5iA4afHyW44b6dvG+645F9f2bAI6y4YRbRvD5YUM=; b=D2RieMCblLAfqQMVhSO5+NIgqgMS0QRkGDSVp51Sh+E590qxRkSh3TTeIe/idl+JKD 1s3F09ueHO3/9bnkY6V/26osQSn9dBxKPJZaODqbm2+yzzphnI67917PPwv0rc/h1P// 0h1L+CFTStsbZYn6PMdGODQxG2g2HGInry2eOprBLVWfBbY9Ma0FsG1bq7v6t5HCSTsg 2r4ERU5wgkQ6MSiWaw0dav1VSdWyrSXbWAyd1U7YUuo8rb6IAS5qmHK4im9qZBt1pKHG 67VgzMfpAOaiObxBRuWsQ57sqkTVUsBYGappWifDZVcueNFYK3r6+YMBHHkAquhgh3gq z4MQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=JWZQLKXQ; 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-64444-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64444-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCVDlP7QWVcKBaHEjaVWfOYVKDE7coQ6KShurcIdeXz2mNJ8jCklRDWR/Hbkpkd+hZa9FqzMz3oMZe9GOpxc/R+Mb56EnA== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id z11-20020a170906074b00b00a3cd0c0103bsi1584872ejb.147.2024.02.13.14.16.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 14:16:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64444-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=JWZQLKXQ; 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-64444-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64444-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 B74D21F279B1 for ; Tue, 13 Feb 2024 22:16:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED8FA63502; Tue, 13 Feb 2024 22:11:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="JWZQLKXQ" Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.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 4C99F629FB for ; Tue, 13 Feb 2024 22:11:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707862266; cv=none; b=bMM06wZApENOPZnh9b15GsVj9V6f1nB158mDge5ma02OcoIunAGYWsf5sT9TlEa909BX99sLnlxXYwUwzsHM6enSmwGqN2uyPywrEu6fuNebSMwnFNM3GrWhU5BodFQ3DVOxfOxILwahqIeoy/PPjQNyek/iha3cKZ+y+JJCQmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707862266; c=relaxed/simple; bh=ixNr6X1gB7VeIiW3pk9Za3U1z37zW3zrQNIbl69tCT0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SoVnTU0scVA0N/EE8KOzCrLxB6FGA/eryhhqGRSJ+ObhRtouBVaIJetLrPk3VGDpjrz0+L+qfT7KslmkEi6GTE+Gz8iorEL/L8ZpOj2D47xQyzvYAtbYFIzTfvVvnCc6zqduKtCzgBBAq4RCiY0RH2i6fG/ZKZlRLq9TL+xRyCY= 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=JWZQLKXQ; arc=none smtp.client-ip=209.85.210.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-pf1-f182.google.com with SMTP id d2e1a72fcca58-6e0a4823881so2008080b3a.0 for ; Tue, 13 Feb 2024 14:11:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707862264; x=1708467064; 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=RerG7d2KkWMxwjKi3Bk1dJMaOiX+RZ04/h8EQNw22Ro=; b=JWZQLKXQ7T5nBIvtqjRlLDOkqLx/3xem2stwSt3WYI0ul4zJVVoGmQpDWVxMnbNK1x 43EeI1qLFf+mpUjx73fUs0rrot3TBVTpnzqjkDnSlwKgkLAeBtMy78TmxMd7dqm2AVBZ xJcvr/eVV5DxuU/DNQd6+VzVdkyC0tY3qC/ps= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707862264; x=1708467064; 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=RerG7d2KkWMxwjKi3Bk1dJMaOiX+RZ04/h8EQNw22Ro=; b=GcDIMtTNbBeJNWISdgVmW/d9IQlBUv7e9k4/TFOXkmA+DuZYoqJY1YtL07hOr+B70F sRq99ZWa8MWaof7Ix2E/3seXYqeGEtztM/uU4nDr99xbP1jHXuMEJFumSUayQq8Vg2e9 HytX3HU6VX3+oqr+B0dBwZMPUpxaWmtcXFyMK2O/M3a9vYz2aExRV+EgrtiMyyZ1ueqC ylrGICKYrapw5bL6xFV2nlg55P2RfSqTEUMOQ9qcO4TBaJtn4N3MtIjVRLB7W9wGCGT/ Pfx5z2BAwxjKkagRIpsL4lBFs94ERySRObuWNg3CzoBLCS41+l2Kqse4EAQPHrrWk0eV +Hwg== X-Forwarded-Encrypted: i=1; AJvYcCW1RasMSZZIiEjnMXSS0/G1zTp2NCAmgEYO44B9MdnnyA6dpepp/a/IsSuZPomYBzFlvk+GGL5pXqy4cYI+Fz2LyCz+fDSBAdjETNhL X-Gm-Message-State: AOJu0YyyQE2GZ/A9yOAJy8KFFyRitfyt/Tg2TYO1fxX7ZaaUIo7gBEKY CEt2JvLUktav9JP1N8la2KO5je9knkx2DwEnRRY7Kb3sFTjCdz1H5wdDG2QUCxfFl/UQbBm93fI = X-Received: by 2002:aa7:86d4:0:b0:6e0:7249:3712 with SMTP id h20-20020aa786d4000000b006e072493712mr570641pfo.30.1707862264512; Tue, 13 Feb 2024 14:11:04 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWG0I05D43cXz6pmvatWee6vACRFos2fSr/9+WHG2nK1f/UYfNW+7eWWIw4eIfWq/vA+HGauyJqPsCZJ80hnxfLFBwBq3uPVnMRdaNPNZB1MyQfy+TXAnLoCuKwSQceiWCYmeaJfuLq15dkX9YXmUqiUQVGNvO5LlrMLHp6kW9Yra5xSBjoL77/d3M9mK06GQur19MMvy1ZDd7MpCRIrsezLNkT/TykNgyFRET2YkS1hB9oEdXd5AA1U+FH9YWgNWhAd8Hga1iJoC8MBs+if20iUnkMVCBxCOPryuc4ZQOs2De7hpJ+grI8PkLNekrn+0FQ3q/sanwR2g== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id du25-20020a056a002b5900b006e09ec7bdb6sm7557609pfb.193.2024.02.13.14.11.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 14:11:01 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , "Gustavo A . R . Silva" , linux-hardening@vger.kernel.org, Rasmus Villemoes , Marco Elver , Eric Biggers , Mark Rutland , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v6 1/3] overflow: Adjust check_*_overflow() kern-doc to reflect results Date: Tue, 13 Feb 2024 14:10:57 -0800 Message-Id: <20240213221100.3556356-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240213220844.it.345-kees@kernel.org> References: <20240213220844.it.345-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=2680; i=keescook@chromium.org; h=from:subject; bh=ixNr6X1gB7VeIiW3pk9Za3U1z37zW3zrQNIbl69tCT0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBly+jz2+8A/TrJjsBiIAx2op9/6wn1k5bn1wnZL w9hYjTQS5WJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcvo8wAKCRCJcvTf3G3A Jj3IEACgnAjrCNHfc2TogKvuIOOkNxLx2HcMUaBKVj882SXJHeFdXmSoQGn15EuraVDEmvypDWy vrMsFC7cLVtlv9pea0o/Inq44EYlbjVtjqgO+Ze2uYUrsz23WFrrdje1i4cQO6asAow+aGx5NeA SPO1aH9lBKCcUwEtrr/qLSYPFuRRs7fbV+uJhF0P6tgf8/Ar2R+yp8Qb/BoXGga8XDix93f8mU5 BlI47n7ViCdP8wjHAoSKI4dmf2z9WjfReQ/nR2Ls7UQt+B+UCzPc/ChfllIT3iEARSwQg9fyTQy urEXQCKa3nvT0FM3Yx2nmum5GgjMpQG9lbXGycGlVAmH8RU4KIj1UAuP3JEYoEwk47HzyoOWyxL CfoOG+9nrhjRukZfvZ+K3G4z8t01HcTrFj5L6Q1pdjlCMiN3X7tKU59+l19J8s12ByuXadrEgi2 eYZP6bgvE7h8wFi/9IOCdiZ45Biji+UalfjNyipfHyaAfaqeabilwYjJWBSDHommvtG0XSOVMoa YSVqRRDZz26U6Q4qdL5uMEffUzkyjojKwgN/C/tBGMmo9bnQUazKZCxRfJdbCWtTuJFNel8rPEp S5FZcScFTA71DFlOTfU22qn+e2S1BXtEQ/whA12aoZ8NdjA170gMzcDiEO7Kcvja2a4bi1/vwH8 QM1atjqVmuvRSHQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790823738385182539 X-GMAIL-MSGID: 1790823738385182539 The check_*_overflow() helpers will return results with potentially wrapped-around values. These values have always been checked by the selftests, so avoid the confusing language in the kern-doc. The idea of "safe for use" was relative to the expectation of whether or not the caller wants a wrapped value -- the calculation itself will always follow arithmetic wrapping rules. Reviewed-by: Gustavo A. R. Silva Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- include/linux/overflow.h | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 7b5cf4a5cd19..4e741ebb8005 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -57,11 +57,9 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: second addend * @d: pointer to store sum * - * Returns 0 on success. + * Returns 0 on success, 1 on wrap-around. * - * *@d holds the results of the attempted addition, but is not considered - * "safe for use" on a non-zero return value, which indicates that the - * sum has overflowed or been truncated. + * *@d holds the results of the attempted addition, which may wrap-around. */ #define check_add_overflow(a, b, d) \ __must_check_overflow(__builtin_add_overflow(a, b, d)) @@ -72,11 +70,9 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: subtrahend; value to subtract from @a * @d: pointer to store difference * - * Returns 0 on success. + * Returns 0 on success, 1 on wrap-around. * - * *@d holds the results of the attempted subtraction, but is not considered - * "safe for use" on a non-zero return value, which indicates that the - * difference has underflowed or been truncated. + * *@d holds the results of the attempted subtraction, which may wrap-around. */ #define check_sub_overflow(a, b, d) \ __must_check_overflow(__builtin_sub_overflow(a, b, d)) @@ -87,11 +83,9 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: second factor * @d: pointer to store product * - * Returns 0 on success. + * Returns 0 on success, 1 on wrap-around. * - * *@d holds the results of the attempted multiplication, but is not - * considered "safe for use" on a non-zero return value, which indicates - * that the product has overflowed or been truncated. + * *@d holds the results of the attempted multiplication, which may wrap-around. */ #define check_mul_overflow(a, b, d) \ __must_check_overflow(__builtin_mul_overflow(a, b, d)) From patchwork Tue Feb 13 22:10:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 200720 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp852933dyb; Tue, 13 Feb 2024 14:54:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX0nfj1Ddlu6TvNTZJsHqq8v2PYwp0Ml4rlEd0cHzc1HRd9SNHqlVygyzHNYVYxtOdpt3G0AzKrKxWSzcryCK0PPi7wxA== X-Google-Smtp-Source: AGHT+IF1Ei4I2je18UUNvxiN/oW3JxHfjcCSA7EbOGNfUmNPVaqhkhtw6vb4vNRKz9HO1ByRlOGV X-Received: by 2002:a17:903:22cf:b0:1da:2a0d:fea8 with SMTP id y15-20020a17090322cf00b001da2a0dfea8mr288067plg.24.1707864861699; Tue, 13 Feb 2024 14:54:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707864861; cv=pass; d=google.com; s=arc-20160816; b=zF9b7/u+GbJUODPsjMdYwkP5/ZqqnbN0pjb0qwMg5YBlafLCCxH+dMtwUUt/Yk/lii Nj5pGqNscYM3TJFchglJSxrFNOhszRbSTLv5Qm6cEm3cahSHs63GR3AoTzJslEPyB3/B LOBnVo5gfcNNZ5FaEE7NlwWNNIbP9OupriN/nApT4CWYKw/qGrRFcWqzIksbSiI1ipkZ 2hkGroGiy/PveEvNhx14Srv2WomR3LMwIKAChUt37UxqaLh6IaxnDW6tqKa+5bHvS5V2 lIA2TgWez9WV8eUmiglDLMSgqZRA5r96y4dRMNjRHgV9Y6H+WDq+O1YO6+JrXdBoI+Lf XplA== 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=2vaQBRrGxq8giGU/rUfJzaBqC9E236GkqOs6N9h8ECk=; fh=pnSUEEGlMgKyCopIHfZihveoabWfwkx0jEN1ICZBTAE=; b=uAloFw0JUAUg5znSCtzh+3ECEZqAl6IsuKBWhMCgkJvhn7tyIfnpk5PTO3biA3Ku8r DEQRzvyxyIcvTpQ9mz64dO7TjE0kkX6giLVM9gu+g7eYpC1sUod/6OXVwD4ZweJJYV+Q DG7o0N4NleNdCFzgeqP+JAmCCzUIZ3JtiYdwJx1QB2n7LH/e9bFc+TmR2Lv0+EPTrrly WNXsbYaQWUPt0QIVm9vEaOusTiLs842u/mcg/wj+yqFRtEzPgVBzsbcIQQfhsg8G8SS+ UDYWxErbhFAdf0uTWrLYs8CvCAakPOyRRP+Roe2J/jJGJlwAkLaKgXmatne2t/RyLNW1 Muiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=e3VkhR0d; 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-64445-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64445-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCW7k75Xk9Qwt0LvGQ+0ML2J0pYn5Y6c6bF/0BQrIXK97ViLhjw33ULEXc+GHKuzrJabzeI2L5xB0uoJqxEsIkXNFoIw/w== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id j12-20020a170903024c00b001d9bdc912ffsi2619444plh.403.2024.02.13.14.54.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 14:54:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64445-ouuuleilei=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=e3VkhR0d; 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-64445-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64445-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id A6DD7B303D8 for ; Tue, 13 Feb 2024 22:16:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EAE3563CAE; Tue, 13 Feb 2024 22:11:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="e3VkhR0d" Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 BEBFD633E7 for ; Tue, 13 Feb 2024 22:11:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707862268; cv=none; b=FA3d/d5v6qkVCDh9MptbtlwcMjIwcUaqNqxEMVclXtmaGt7VjIwLRmmhWKHZ2QL/9hibHIvfRpj0gp0ZAGfngWTQcXxhB5FldIj0WXIU8uo5efPnJqe4uK46iGV+RPW18wtN0kdqz+m2/7Ul2sztTB0Q33f86Fi4JSBuhll28G8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707862268; c=relaxed/simple; bh=XF4r45LSthMB7YVQUTdzEjocvHZZ2Akb/A9zylSTGZU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RCBSmjvrkZYNLUvQQcbt77UiTz2U0PzQ6121LTKCI5faD17O8mIOl/kFP7EyGSzawO1gb8PvdOzUQqL48tdn+D7H90ox24YF749fv8XU61WspiE8Ht27Jak6ZH6m/azd4PtS6fYlLJ2gzuVy81Yh+j9Zjb7OInZTjfEOOaSmleI= 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=e3VkhR0d; arc=none smtp.client-ip=209.85.214.172 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-pl1-f172.google.com with SMTP id d9443c01a7336-1d9b2400910so1405445ad.0 for ; Tue, 13 Feb 2024 14:11:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707862266; x=1708467066; 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=2vaQBRrGxq8giGU/rUfJzaBqC9E236GkqOs6N9h8ECk=; b=e3VkhR0deJoEpDmUgUpa6BbMWuCN/l75oidfDfyZtFJQmjdA8un/F5NP1arrLcHRmr yz4L2hszW5LsCLNRr+Rf1UiDKXxp/OlH1Qv7EcfgiK/KJHObMVbXZPyx/+TqQ1Vx111G YkpcziF2Zqzgyma2aEWUqB4ivrbI/ybpikN4c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707862266; x=1708467066; 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=2vaQBRrGxq8giGU/rUfJzaBqC9E236GkqOs6N9h8ECk=; b=LRsMQs4RYeo2OcvauJmn7bFh4f4+k6tnZbwCZaZxLnavR6vHJpCkz/6+z2ak5EEGjU EKrRzFxc8/Ytf4ft5Pv7HQlXCsiS9A3RB2rxnSav4CpviqaKG21g7lRI9iIopcEFOaWq K6SQrnhcHjz+fb74MaMo12W76O2teNVapRnQuEKuqYXjT4sRBCetKrxDV6kl+bu8X6BM UAT63xYjwH3nMAVpiwPbrXt/RZirLyEW3wWz0fKdZpDTD/QLAztgMWm4z5u/fZOWiLj2 Gd4A1+HGXS4AATS15gVUvQhORXjnqVhah9ctjIzszoNBr/YhPKSNvPdysHvqzf663AuO V1/Q== X-Forwarded-Encrypted: i=1; AJvYcCUBJ0UUGfxQg5KjNxZXbg6rFDRICqNhs0j5AT9Bz2gPdy/p+IyBb1hM9THcD0chIiQYjoImGDDL97ebXDTG4BZadPG30oh3ULYWpjsI X-Gm-Message-State: AOJu0Yzc2xyLxV822S+sAMKKeJ6gBfOZqPfiP15U0Xv7db2Qqe9WZKV/ 8H23buxw36lQU3axQ5AW4RLOgqOQ5ahJrlyVS+XSx4UKAGBD50wjli2KrCkmtg== X-Received: by 2002:a17:903:41c3:b0:1d9:2e9d:8cb6 with SMTP id u3-20020a17090341c300b001d92e9d8cb6mr222238ple.15.1707862266128; Tue, 13 Feb 2024 14:11:06 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWsCsed9m6sJYBvwIZFjsK6qniGUXhULSPYvW/Fm5655xVVKS7LE9Ox5dfg+uAbDK3w29HesPwnOvD5w3zDbrWtu0ljz8fsbu1RDdsu0dgWJyWCFIenNBLdx9+ps4myuDL4j87bm3PJ1f995ip5A3A9hGQHz21Wd584a4Ph6xqGsmKTSiSSlvZgo1pYxJIm9UZhjBk/amSv0TAt2eSP1CLJ3MXjL/Jcs322I8WkuNR6Wel83/CJ1nOLMHagp74kNXg1hvK9XoIJ4UWYnXCJYXR269vdZ4J1CPrLbZKqWOTVRVCvcH7JL38iO10dNY5KevJZRdvFyanOAQ== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id kd12-20020a17090313cc00b001d706e373a9sm104564plb.292.2024.02.13.14.11.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 14:11:04 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Rasmus Villemoes , Marco Elver , Eric Biggers , Mark Rutland , linux-hardening@vger.kernel.org, "Gustavo A . R . Silva" , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v6 2/3] overflow: Introduce wrapping_add(), wrapping_sub(), and wrapping_mul() Date: Tue, 13 Feb 2024 14:10:58 -0800 Message-Id: <20240213221100.3556356-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240213220844.it.345-kees@kernel.org> References: <20240213220844.it.345-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=5647; i=keescook@chromium.org; h=from:subject; bh=XF4r45LSthMB7YVQUTdzEjocvHZZ2Akb/A9zylSTGZU=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBly+jzl2oYM+h6ESvayR0VXH2Ls/OCkv3ZYMMGL cj9eHUrGjGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcvo8wAKCRCJcvTf3G3A Jt2nEAChpDMVoEbTWwliB3MsMGyfVB0zWz6dz9iKPEfgZ9bW4GBCPzr4hsMkIRjqSy6Xk0tvTGP N7cOJfQ4ufVEh6s/6rA3XeQ8IMIpCJaBQdPFAZjIIBJGwpEF9WNW3s1ac9lJ0xf72hOkSylmMXk Xbv7mYpdsj0f4nvJprR5WNlri8RVmfz4Oycg0GLG0DnZ0xYd24KAV1m7sHlZwbWMRqco5RqC69O G+l2b5AEsSs1KR8UdnNOvM0xS3eIu+U/yXxlpuUOejjKWdFlLk+AR/7LWGn3JEQA+5jgl1nwt4+ IS/sIMMF4AzygjoFSbYDhI0W+Q9fCHOZp3pXUSj9vPtG7Qhi6/Y8aEOCWjVsLf4sJdLS28AP9lq EVpm3C/eC66TPsNfSdwQtFk7JK1lWb1n2ZwyJaTkxI42g2BB+5dECKQJJTqR704zK+zsvxD3pYx SOw3QL4vLNyXDL3Ica/rm3v/9h6CbgVPnAhngs+3110a/jeGjiMXynKy0D/1geRx5f/kpS1uo0z oQHs0AesDBMDW+0RNWWGON/xuGAyCDfjQP4ojwsZ/LXlarSfGdLHnfDQ0461ZHFcVwTPF9PZWWZ s2bZtJyVG5GqqMpYn1GE0bm4pcVWJnLhGU1ttPCTBR2DxzeUn+IjN7wSxXN7IQ5fsIZ9IPMKROs 1RwK3x6pKCLxtyA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790826105200051160 X-GMAIL-MSGID: 1790826105200051160 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 Reviewed-by: Marco Elver Signed-off-by: Kees Cook Acked-by: Mark Rutland --- 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); \ From patchwork Tue Feb 13 22:10:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 200700 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp838394dyb; Tue, 13 Feb 2024 14:16:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVT9DBlh3fUYBBusuEk+wfd+Rdq0A5YkFlWrpltsc8IgmoEV1s8tetlp1cRtbOS8dBL9XgFAWUBUA8ZIUfD0SIGhDVqAw== X-Google-Smtp-Source: AGHT+IGaF3foT5fafnZHADj4oiJ5peyhclqF35uyo8f/CUWwmwpYNxIZlAFGxZKebJ2y+lwtschO X-Received: by 2002:a17:906:4158:b0:a3d:26b2:94f4 with SMTP id l24-20020a170906415800b00a3d26b294f4mr447761ejk.73.1707862596546; Tue, 13 Feb 2024 14:16:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707862596; cv=pass; d=google.com; s=arc-20160816; b=jVqKnvqxKfNqwvSCKNGEyjqumNqTlh2ITk80Rx5N2WY125Z4wpOz5hQJXKnuqllf/H 3HRkumU//YUiEUsf5Adgffmu1LP7IsSfvzqPE8l+AqPiMsO6+5ET8Hv6h1SWjPd2/dvF uw51xBmuoao4LrIb5y5Dh2sIVpP7f3K+uAWqOXaTwCtauLI/LoT7OtJeKlPPZVMCy/cZ Qu3UD9UvqZE9I69/Ghy5K5zJ7+A/ggE/swD2B2N9tHcWKEzY4319xjX6vNnxifC+dzW9 Vos5zLeX004QAjw3y4RvU8hJLBTz82VvKQsGNPe7JSOpv7+lK4H28CstcQMUBJdsVjcI tyfA== 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=Bq+faa+uRnVdoYt5O1MIXKxtE4nBr6/M5LGffuvux8w=; fh=GmJjv2UfMX3YVvdrzV6jbPJd9UVk0usmUyQzoeJUFwQ=; b=OXhH6v/DT/B5P0NshOfvRN155ph/86pc51bYtjkASUrGWBvkHVZ9ueJNLm8mTFubTI XHz61TdC/rqaaP7KswWSJ94E09LtpCMjzRfSHXB86MOxhhlbLlcenobHcl+qA/viYn+w UaV0yldg6LooxIJIUYsSLFcTfSiEgjtU3dUy9f43wcVLs9yJ/cHDOFAtTIxVewH2PVLU C/u7+SZYdV1f4UspsXq7TKIoibk4eVM1Y9bysMuaku+6G19N5z/0IwNgZ16NQOsDraiS 47pkdd7zGmapBBs2BpBIwnyEo7H4gvL/QqFnJz3z67Dsgb0G1pwDt7quq0JkgcybIT6D rCrQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BTph8EnT; 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-64442-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64442-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCU1bhucutTHDLTJnkqZNCkUOExqkdjdjNZmKF0Uhf4OsmcfTSaXmDvGQ1Ix/1FyImDzVBOgQTpoghCwjLSPzf0cTt2E1g== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g11-20020a170906394b00b00a3d3c86fef0si60860eje.389.2024.02.13.14.16.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 14:16:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64442-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=BTph8EnT; 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-64442-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64442-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 EDECF1F2801F for ; Tue, 13 Feb 2024 22:16:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0BB7B6340F; Tue, 13 Feb 2024 22:11:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BTph8EnT" Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 ED15C6281F for ; Tue, 13 Feb 2024 22:11:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707862264; cv=none; b=J/F6sqwvNzXl4PpXbvIMAxHr84vQ40e3Lf3kA2ZG90XZqOq7/oipOqN1gMUzE3gTJDKJPB0+IPSF9hJo1tn8GrLMOGtn85yBuA85PlFUO6RdBWJbpJ1n+WvcOopvhOyrW7SQ4wYHzlPHosIkRVfUzRnCQoQvmv79WoOKKjGymDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707862264; c=relaxed/simple; bh=w7zw3RDumDcJw4/l4C5YmBZxl4Zt2x5/fyHOMuQVKJ0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e7jGDDdqC9sN5dNnUOslhZ6tf6oOslgPvYch4jIk8Q19GZlPmCPVl9/udehjdjA2q80YlPqARFqtL7jQA5cH1oS2sabAtmYs3fQ9U3UkJ4moWVUUpcY9aAH6DC4gTi7E0o6f2EraT3Jlo6EFD37T2CvKuTlk+raopAgdFfY6I+Q= 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=BTph8EnT; arc=none smtp.client-ip=209.85.214.169 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-pl1-f169.google.com with SMTP id d9443c01a7336-1d93ddd76adso34912895ad.2 for ; Tue, 13 Feb 2024 14:11:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707862262; x=1708467062; 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=Bq+faa+uRnVdoYt5O1MIXKxtE4nBr6/M5LGffuvux8w=; b=BTph8EnTNQY9ngXMcbbMYn8NCRgzv6DueVRHqDpFZ4jhdFdbY6EqwZEaDkmRdtAEGN Fh3NW/TSwP8io3RKGPM44hVGwmanJtIMatiSjJw0ad7vbOTtfhY15M3pcHYWSassdsA/ NP7YiHTpVxPDrMjT0Lc87WpnR5AhrxBBcwgRA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707862262; x=1708467062; 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=Bq+faa+uRnVdoYt5O1MIXKxtE4nBr6/M5LGffuvux8w=; b=Sn0abuNzni6VUohn+WUmoBLbD82hiOeLPhGRRo3bH8Q/jD/XHMi7mORYlgVln5Seex aA9SPHM1GGq5FO/42t0HT0sOVq74mFS4D9IWZ0E+7yHhqikftCyNBrs3ywkpsyY6OmOh TbrajiWbmMMA8PjQL91sYNhIqiTY8L255TN6oSJYBUzCcIBOATo+QEtTTW4DwRml7TpT c6BIfrIoCGRTuAP0kE3SIGqx7VZnEIMfO6ugmT5X6F87kHr5+k2oXX+Q5HzJ7RGPQwXf eeaC+vNLQJfTaULQxnZKrIIR/PycVIX8bVAkNev8ldkI3pDHBRlBPxuU7J0eIpko7hqD rHbQ== X-Forwarded-Encrypted: i=1; AJvYcCVtH3wz8832b2cq2Zbu+2kRalViTG8eQ4HyHaxwO6HCuiGcQYyy1JBS12z4JHxuNXeWyS5lzXrr/6QUZuVOHN77PZ4VX2G0ARct85U1 X-Gm-Message-State: AOJu0YxrBS3tyMDQOwBOyl5seTE0IJkE/CvrqqbT4BxBow1mo+xugkA3 go93sRvyzRu2sSauQEvPBBXg/CjbYnIbuNYSBJX2nQFOEZsJl2JQN3ZlNALmIQ== X-Received: by 2002:a17:902:c106:b0:1db:2ad2:6501 with SMTP id 6-20020a170902c10600b001db2ad26501mr846839pli.47.1707862262251; Tue, 13 Feb 2024 14:11:02 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXfsMXWI9wSc/hKos78Dh7EFaVBrFk1uq7D5K1pro/Flltvy7lLL625xtkYXBRpkX8Zq8qj8yhH9pZkpU8VZ3BaO7ZeaV5yb2xk+WoT5qN8lhUpGnOmKc7Fn0jOqPk3xQIcHwAxNcQaLlllkB11WhWnBUZW0QOmp2P/NVRLJVJ5aUysqpLj7Ps4MvYYjcSbu73Kcp5krjacV/rZz9TpDCBcn09YGc8QZzA6LZn1yBTRAM6HpIKh63cfLtYarQxOg79z2WMkTy4TT96qMe1SMD9DdYWmBHdAhG8E7hRdxQgbu23y3+VYFyo5pytbB+ofyP/VMJjFgquF5A== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id b5-20020a170902d60500b001d8f81ecea1sm2531294plp.172.2024.02.13.14.11.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 14:11:01 -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 v6 3/3] overflow: Introduce wrapping_assign_add() and wrapping_assign_sub() Date: Tue, 13 Feb 2024 14:10:59 -0800 Message-Id: <20240213221100.3556356-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240213220844.it.345-kees@kernel.org> References: <20240213220844.it.345-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=4679; i=keescook@chromium.org; h=from:subject; bh=w7zw3RDumDcJw4/l4C5YmBZxl4Zt2x5/fyHOMuQVKJ0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBly+jzo8h8GwH2EtVZTjBhXMhC4DMegJkHNB+k7 lYuBbCOfMCJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZcvo8wAKCRCJcvTf3G3A JsPjD/9Azkvy9kxSMrniMPJ+LGPUl/M+RKbUrzU+zP9jToskR7pC94WwXHn1fCwodshc0LbJV6c z+vDxi+VWJFHccxl2QWClIfZbMv2/ITVxAMoNrSjLWV7/fRpUNL7SHeKwwgbPNwsYyWqhFGdBpk zlhaFVhzlHbGdI+42Wqsjc/oQCR2zPbfngje7EX7WChZi0eU4rUOzjfLiyKwHaOtZapXLkUPHJ1 eFsPwzvQTU68dQnQvs0G1Q74LFLUJhhNeTi8KeYrtND1o46VBtHSucW2zc1qTfCjcqMSlyc4pTR 8oh46/zxjPUAb7tGjCdQUAYvkiLWD+qNW6zluKXlGURltHSO8bzHb0Hn/qqbbfdPXSCBInD6vv9 qtZbmj+JXE3y56VSK9rbaEBp1v8327SkolPTmxttjf92xb521/I2UzJUkYwtPb1Q3tiVvzhhxN5 ldO7XiEo+EbHRFk78hx9PvjxO0PPw+lOjn+8AjsPsqGwjIcdvhHXdqnWr3f0tdwNWLng1upEt3J HM/SneD+0nCIf5wVQN/Tw/WesXDeb47hzgKF+uhvzQpYCVBizDll+kIFt7pcN8GUA62aOSuzT33 JpwcVMsG5DDI+0S6WscVCu8gk0Q+NepEkvltVa9sioHq3andA9Yhp7ZgaFZC+4n4NQXyilD1ODc INpWqjU1LHccF3A== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790823729689320659 X-GMAIL-MSGID: 1790823729689320659 This allows replacements of the idioms "var += offset" and "var -= offset" with the wrapping_assign_add() and wrapping_assign_sub() 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..09019b19876f 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_assign_add() - Intentionally perform a wrapping increment assignment + * @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_assign_add(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_assign_sub() - Intentionally perform a wrapping decrement assign + * @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_assign_sub(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..65e8a72a83bf 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_assign_{add,sub}() */ \ + check_self_op(fmt, assign_add, +=, p->a, p->b); \ + check_self_op(fmt, assign_add, +=, p->b, p->a); \ + check_self_op(fmt, assign_sub, -=, p->a, p->b); \ } \ \ static void n ## _overflow_test(struct kunit *test) { \