From patchwork Wed Feb 14 19:46:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 201103 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1457523dyb; Wed, 14 Feb 2024 11:46:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWeyjyGgcOhqe3jI+iNsuue/dhTvEJdNubHytxo08VYFxrs3Eq4Vk8aRlq1U1UjO7paOYB78PEj0idLdCNv5ZOu48HVAw== X-Google-Smtp-Source: AGHT+IE/eTLPABbhpvYVkYrrRtD6TdCwP3ItX+VuHWw74U4Lzo+QT/bB8BYm/qd6jWammxAJCh0K X-Received: by 2002:ac8:5713:0:b0:42c:80f3:d63c with SMTP id 19-20020ac85713000000b0042c80f3d63cmr4442179qtw.19.1707940006273; Wed, 14 Feb 2024 11:46:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707940006; cv=pass; d=google.com; s=arc-20160816; b=kUExc+JUiyhM36OBAYupyaQLlmUL54vZvS6UubPb/LUbgzHSPiloTB2ATpd8B0yTet ruMWLGMLFnnVHoIK16KDRjRrTAfSAhyDE7noZXGPVRzDKhZV1PfIlLfG4L4qPi0kkM0N IYtcv3vlu5Cb5KuIEbhUQg1XFRnM4UlMUwz7UPCvOqRT//1VEZRYtKNk1cNN5qYf6/El c5KZxCl4jPbRlX5BJhuAB1MRZkOG+RI+jwTS0kRY8/e3bxBOMMU+vPA8/m+Ody/DROcN +3D43bbcSDXjEdGzcpbwS6IVN6ru3bvM4qePMED/WU5JCwwEw1HRh7Q7P9rGrre7+Gnl GFhQ== 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=jM19jkSsuav/2hSBnxgTz0qK403f31xS1I7deDtmjCo=; fh=9sB1Gytm0h1MXI8dqro0rPbppNCR1xrMz3Hw9iL7BXE=; b=qJPB4jx9dxf/CNpZEx0kREGpdC2YA2o3wZPn5Y6Bgz9oQ6TRd5sWtXOYScsmFgidZ+ hLZkwHozFPbsKgmr3VQXX0mHVkklgzr577xwD7tdjJS6rmj5S/RWDcoWbsDvceQc5/kV 9SQNfa98dl5EFpheWpnkHVEJYf3NbsjANc9cCMjR/NWs8vyc3V70uPPes3q9aRJkZjmr 86CGhT+UpD+/qMI838MdxxQQqOk9aPt29/ptciSA/x+0JLN5VjcP5L+8yFrxy+YYx0NE 71Jh0QJF5V45ZvrMRqSwiA61nIJyE6PrzqWVTQ/yILrwGca3LCHb3t5Own7qlbDaYnAo yA/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=j6pGp4mA; 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-65892-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65892-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCXndR7K3NfpM7D9B5YT5M7Yff2YSXHZFM090IzjGZ5niYDh2QjUWx6PH168L8ekbYYSqC3DT+NMrcuYFJAdT06BE8/EIA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id s3-20020ac85283000000b0042c3502880esi5817127qtn.190.2024.02.14.11.46.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 11:46:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65892-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=j6pGp4mA; 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-65892-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65892-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id DE7971C21737 for ; Wed, 14 Feb 2024 19:46:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 94DEC13EFF8; Wed, 14 Feb 2024 19:46:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="j6pGp4mA" Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 7E9B313B7B7 for ; Wed, 14 Feb 2024 19:46:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707939969; cv=none; b=J340HHIKPqbVvCdwME6FnKblCmbjpKPxyEfUFFjWHVAy549uc99T7D46wOQ0Mv5j2oPSAEHQIRHw753OccIsLYz6g4hBJjLwyJWZK9nN1vHhV3YW2rTJf8Ino5na9T48IqVq++idH4iJ91W6grxDUbVyKyC4vkBJo8jyCQ5lT0w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707939969; c=relaxed/simple; bh=XRea/mEhcbAGWa9Y4NibhO2kDjISXokmQWKcnCxCkCA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dzEscKXKxESlYY94P9fIsDrTVmROHFcHC0Tz7Ttzw/vQ066mC1Y/PIj7lyp266mwz/+kvGlRtBjrrvuzEFgGYs2VRZBD7LZ4n6bVCMXPnZh5SRkoOsQ8kIalkdd2lg129zXhlqrSH8PZ8YVlGg1HfXRpbv6TcSr0YpWtx03gZlw= 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=j6pGp4mA; arc=none smtp.client-ip=209.85.216.42 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-pj1-f42.google.com with SMTP id 98e67ed59e1d1-296d26db6aaso129765a91.1 for ; Wed, 14 Feb 2024 11:46:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707939966; x=1708544766; 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=jM19jkSsuav/2hSBnxgTz0qK403f31xS1I7deDtmjCo=; b=j6pGp4mAUI8QjQU/ezhM62Tcu7UvHyi3oVNUWkdR1h1/8YEbn9srwSIL7qHO8jCQHk LNcRvT/e+eUkd1XwOn2fHBC9WsF8019aXjeJmHOEpSrD+KVvKJkYgcVPB0qPU5LpMiZX fj8WyNv7kDxP/jAQqrWLG2EDqZQCinqTJLp/g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707939966; x=1708544766; 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=jM19jkSsuav/2hSBnxgTz0qK403f31xS1I7deDtmjCo=; b=mhF2WPF6KR6pPeAMO4cXHmYyM1oPe3Lu7KxIBLTSepM58bsYuve9x7pxIYsw1iL0Gl lgr2SBVPJrKf7QUH5PLq/T7J+/T6mTbbwaC7AqFjzicAilv6OzmvjNbo6uEM7LKslZuM r/mgQ8iSe+bl3ACiuM9bx467KnX9Xkl+Yuj4FH0NAmv03C+hMZXbOCC4yBBVzANmavdh bBtCwNyfMIdbwBULHVD2TXC62/TfPnwdj/ru+SzU2iwHFkKHgEvUYbHIWUzl3JlkXdOF DJ6PHriWTND/fNIt/YXGYWlcMcVQwFMlIqsIv/dIVlTdeRhXXsKbiXaXdod4HnpSxapF loFQ== X-Forwarded-Encrypted: i=1; AJvYcCVfE7kLMm0l1CN1SJc2IQPPrK61ARc6hQAA4Cct8U6sT+JrssrBSjKIyrBWz5EdCviRtszntWxFLF/nhsXn7Noo5h0iUG1a15W+ioN9 X-Gm-Message-State: AOJu0YwWB26VbpcXgkkP7bHeS/0wyWfMNJ3Y4L7gUlF05KLA9KB+ftFy JHvrwSWYeYeT6MwrdFG1JAPwZe8J8W/fNkJDNkLk7z23ZVGz68rd5nAZj2wc/w== X-Received: by 2002:a17:90a:ad09:b0:298:ca21:45e8 with SMTP id r9-20020a17090aad0900b00298ca2145e8mr3554808pjq.7.1707939966688; Wed, 14 Feb 2024 11:46:06 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id 9-20020a17090a08c900b002966a13f2e9sm1794443pjn.37.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" , Mark Rutland , linux-hardening@vger.kernel.org, Rasmus Villemoes , Marco Elver , Eric Biggers , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v7 1/3] overflow: Adjust check_*_overflow() kern-doc to reflect results Date: Wed, 14 Feb 2024 11:46:01 -0800 Message-Id: <20240214194605.602505-1-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=2828; i=keescook@chromium.org; h=from:subject; bh=XRea/mEhcbAGWa9Y4NibhO2kDjISXokmQWKcnCxCkCA=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlzRh7r+ya4r7uEt9x8g08cu6WVWLE7l1GWFkDZ 9tMyyEzBZiJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZc0YewAKCRCJcvTf3G3A JokeD/4syUq1+l4y9hFWnYnriJZrkny078pHVb3PIOY5XYIudZ7ZTv5wuMAwVzHjGujXsmINAAO pvn3kJa2F6Fu75yGUOsdFeG+YwFY6VARtUIQDEgUtoGl9ulfw/jzOViuCCPRtZ8VE+6yAsrz34V cI4G7wA8IuprJ7z7dYaOGt6npq+U390i9v9w19QuSX6Q61KrNl2Oj4Sd8WAuCWs6xnUj2fGa9fy No3EaO924o3xaALxo8oKz8difZ+2TlvzxarU3j+wXtuwKLtE8xtUYcJRQoRYcWtZPH189hLRZZ/ OSs3HhcGiJyzihAAVr7oRL9Q323ECK+cbgVhHgVUNE1qrQbI8kEbKqWmqqdaEeLO1CjQVfeCbwN tlSPBwgtAkSvDTFssmMwHj+PI2dg8R6nNULk/ZYzsgBjABA/VYd6gNSmGE71Clc7t6OoJ1716iR ccUkN/TTWttr2MhLMa9f5sEsqFKd49BxDylTAqgO6NvD7qAbqb8V69oJ22k9+67QhFLnxAKNNxp kAsCJkher0VmBPGWFR7dK9KbrTYa5hPVrdKrjzTOphv+FKiWzPs5+dS4UfNQF0Ani7da/rQsisj LgY3RieGWc9W3kAP7vFeEf9JkLj7p2aAp9dVUDMObMj5wbTzD6+sK3QbS5kqou0OuVJTG3LO+6T H3GIukbfitb4mnA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790904900130052731 X-GMAIL-MSGID: 1790904900130052731 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 Signed-off-by: Kees Cook Acked-by: Mark Rutland --- Cc: Mark Rutland Cc: linux-hardening@vger.kernel.org --- include/linux/overflow.h | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 7b5cf4a5cd19..ad64d810c8aa 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -57,11 +57,10 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: second addend * @d: pointer to store sum * - * Returns 0 on success. + * Returns true on wrap-around, false otherwise. * - * *@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, regardless of whether + * wrap-around occurred. */ #define check_add_overflow(a, b, d) \ __must_check_overflow(__builtin_add_overflow(a, b, d)) @@ -72,11 +71,10 @@ 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 true on wrap-around, false otherwise. * - * *@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, regardless of whether + * wrap-around occurred. */ #define check_sub_overflow(a, b, d) \ __must_check_overflow(__builtin_sub_overflow(a, b, d)) @@ -87,11 +85,10 @@ static inline bool __must_check __must_check_overflow(bool overflow) * @b: second factor * @d: pointer to store product * - * Returns 0 on success. + * Returns true on wrap-around, false otherwise. * - * *@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, regardless of whether + * wrap-around occurred. */ #define check_mul_overflow(a, b, d) \ __must_check_overflow(__builtin_mul_overflow(a, b, d)) From patchwork Wed Feb 14 19:46:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 201114 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1479042dyb; Wed, 14 Feb 2024 12:25:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXBTpIDSxk6GgWcsJeeQ2bs1P5PfbJEoOHMz+7KEV3XrpjSLmXNt4JR3Rx2754B8BRyzwzo297WWTgRVKZLS7zW7my3+A== X-Google-Smtp-Source: AGHT+IFAARLZqdvo8lsBMTOlYVXYjiwe4vo1hlPHyxyp8sFcodxjNfUwkFUY1g8ITGjAi0EeOfV4 X-Received: by 2002:a17:903:32d0:b0:1d9:5f11:d018 with SMTP id i16-20020a17090332d000b001d95f11d018mr4087845plr.1.1707942357908; Wed, 14 Feb 2024 12:25:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707942357; cv=pass; d=google.com; s=arc-20160816; b=xkCYwmPOJkMqS+4rdrGS8GULk/ctMKVsQNpsUtV1WSET3Lk0IqdU8+QwUavjHjr4Vb 0C8fH1AeL3b/APD36hH7OIZjco+hBew9NwOSOGL6CmOhoE5Tf55miYzjCdKOL0HFu+F/ 6pYS7v/QGO+sk2pU/Lq092zosPk5FllswbAUcq3wQ5XdiMeCoB1kRgO6NnZ8yfNIEx2s K1zLWVreI3OYN4qcPzgFPkpvH3g9nGYwyiW8KyEWa7aHw5zJcGkeZ7BajNrYxzBLLyAk PTs0sep+ShiBnZbrNHKQos2JgOkBDxPnadrqtoGBcRUYVlukilt3RS2CYhTUGn9m08n2 PT8w== 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=jQMKHCgIxAVgzllui1lo4tOufIFfjmj+Ec0ifjPOvKg=; b=ZWVZfr8IuSxW1gAR2Zj0U9he+QpywwGxHbgmNp3ZpfEQFec70IlPL1UUF5bIjLBGHS 57P9GN5EzcPImIV//y68Kq9SOYgf/fTz7PDDY0dKUdUsjGAGskMjfxjqk1wdnS+PCexB GQDXEOxWh/yo9/eqG9eOqxB9378/WOLxIyebyUo9v2HuldDhEHzMa0rXHmuNlZZQNQKT YFj5PBkCpMG31LEgqqhuICJB/Nwi9oka03RsjqnYetwUHGoOdJ6kUzMkwMsaC7KpNF1T GFXCxhe+Waovp9YA/v6B/IQlDz8y3SfhSEk+Gq6xc+sp/ebZTa52AUSUIPCBFl3q77Ld qRMQ==; 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65894-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCXw7X5I+ObYD8rfS9Tx5jkizhAu6FHxP5ITDujb3ww3oneuP5UTGlGAsdkGnj2ZZ30wSGtdYcM1SHiOX66eTtTgI5OCIA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id u9-20020a17090341c900b001db2ac688dasi4007281ple.258.2024.02.14.12.25.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 12:25:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65894-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=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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65894-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 2660DB24E26 for ; Wed, 14 Feb 2024 19:46:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC9B813DB88; Wed, 14 Feb 2024 19:46:13 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790907365697918795 X-GMAIL-MSGID: 1790907365697918795 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); \ From patchwork Wed Feb 14 19:46:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 201104 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1457559dyb; Wed, 14 Feb 2024 11:46:51 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWNbehEBuhuJg/0cZ9yb1Pbji/D5faWvXV9N8xtNFy2HXyEFMQN1Y3g3Kn2MC9kwopfN13qWLifhE/G6r7hCiyy3A2tPw== X-Google-Smtp-Source: AGHT+IGLXx3V8uJOfT7lC8xJyvz8s7zE2LBslHQTjdmP3dkav/MbXN8y9mUUdKeaNCX/lQr5L+r2 X-Received: by 2002:a05:622a:15c4:b0:42c:7bb0:680c with SMTP id d4-20020a05622a15c400b0042c7bb0680cmr4440277qty.19.1707940010844; Wed, 14 Feb 2024 11:46:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707940010; cv=pass; d=google.com; s=arc-20160816; b=Rqv0eASHr9c7k/KegVuJIwkKotc7XFXnNGillXwT+FQxRCC3y0Tnw+r13VRXmpKgsw g8YTbcSM2cIOOqUkZ7PS6gDP3LKBtLsl0nSqaRXsOY87JqY5smQy9vMdfs9LS9ikElxQ hrqjSzNe22tOOstjQHK8CEmaWN856t4awuAyzN1DuyXvb/T8cSGt3fTK/yFUb3M5wpYE ZtmZst72DP9Fx8S7Qk14TdcOMHDx+6xI5lA/WsjMbbi8TL+45Sguu7A3tVsJCAYxYcdY Qa4X0Z1poIMPqngu7mSje7wuBwGiCq/hoAEzZV1lNF/nUEdghmju26ovFPBxlF2qQ3MS DtUQ== 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=bry1Iv4OwAWhs4o2wnnICjHcqHEe/LGfZjQ3JQDyLI0=; fh=JTnQOxfG2ONBgNqqw4vIuoAU6+glxqouHoNUGY4Y6fY=; b=P7MCR0dOnUgXs/0E8prBQEhNEFLNBkjY3DxIR+j0PxTXQoUDkoKeK1PzCiumyMlaUM 4KstEtp5qI/R2AIFLpZ4FVh/GG+7hf4YVI/x1FqXGARToBYbiQKbPDb/XynO0Qi4Uicf 93IwFx+DcGDt38keRVSsYRq2sG6XMun9iPaOME1CKTIxOJbZ4yGUmmOBAs18enNvaLhN DCjxHyYRiZ7I83Iu0bGwCPX7x5vfxCuGoDo3hDIjRMbmIE2py61Wc5IsDgcTX9RCZ9HM QlvthzsHb//tHvXrCdKvlgSac+gEJioi9ThX8Tgii9uuPaoH7ap/sXkKEYd2laNefkCg Rt0g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CL30+4UO; 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-65893-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65893-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCVdouIdc+KiOCjlgYwTQ+J8GYomHwm+M0moNOoOWXwZFAU8M8VZaA/mp6LzA9Xk1jrf7ThqG3OYp5tDjwF3h1unEUnM9Q== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id z10-20020ac86b8a000000b0042c364d3b31si5823967qts.96.2024.02.14.11.46.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 11:46:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65893-ouuuleilei=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=@chromium.org header.s=google header.b=CL30+4UO; 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-65893-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65893-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8F4681C221BC for ; Wed, 14 Feb 2024 19:46:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2760E13F000; Wed, 14 Feb 2024 19:46:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="CL30+4UO" Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 4055A13B7B4 for ; Wed, 14 Feb 2024 19:46:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707939969; cv=none; b=XHMyjx7p3hpuv2/5hOW1htTQ2EuCMuLayHKhL68NWC5nGo24A9Bj+nG6rlsoaJhIm1BoYqHLwwVXPxR3y+WeWWnufEZcyRRlxCkCMdHXwHAjWHAtuS2U23eB+f6V3cJxNgQvCQ3eInxxwNnT88/Xda0Z+VmNQJvggUS7zHMTb9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707939969; c=relaxed/simple; bh=WpCfOJKQI2tEPzWqXq2AwhFgSH1gItVpb6dbVuRrWwo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HnMK4TseUjqk0RcVum7+Cg5N8kXSiMH28LcuuRUMYbdWb2dQmDDHUKhK/chrIILzzi5J3gknGNxCYOnXAieKlEJLJJbWF1PfZt4Iwx6AtrQ3P53pXckP4ieqPkCtP+ZlsYojsDfL4Ooz+liTk+Em/Pz9azCWyyYSnLtqNtgXXFY= 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=CL30+4UO; arc=none smtp.client-ip=209.85.210.170 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-f170.google.com with SMTP id d2e1a72fcca58-6e0507eb60cso111820b3a.3 for ; Wed, 14 Feb 2024 11:46:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707939966; x=1708544766; 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=bry1Iv4OwAWhs4o2wnnICjHcqHEe/LGfZjQ3JQDyLI0=; b=CL30+4UONMDUamoihrAc4I7FEMnqZdRaE1YwlyxOApICXEg5hu18WE/JlOo7dLry35 amPeNtAkGSJ1SAmXCQUDnLxuGyJcZjy6SAwU/BDd8usqYQnHe+HWl946fwF+1yL/jRH9 XYDy2ayocLgcXsz6e/58BG0+ymgRJJzfWwPdU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707939966; x=1708544766; 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=bry1Iv4OwAWhs4o2wnnICjHcqHEe/LGfZjQ3JQDyLI0=; b=ZObnbFGu1SWsV6Xb/hI+3DL5cikJj6WpLqx0WGybU3lGTpjsam65cC9uMJFBiWp48y MIECAp8hMH5aAuGKqw3dXmGMBMRt0aFACoAzx627Uw468GSAz3BxFWpfPFns3Qxber4P w4j5tgv26iuRWs0SE9w60Xk873yl4Wo5EB1UryYhRwVokndYlXP3EotKWJ8jQ68J5t+p GqQmTNxJEMXLZugY5gQAdqVfDd/rVNuzdHNFF/5TRtsrD5yVwnia7hegDe518hwcB4tr 04RgYvXSF0ZjFxfM26mUGTiyeFTUS58hgTY5jFPi4EXWV3u8lLTLzb15Ougnfqchsut+ YOHg== X-Forwarded-Encrypted: i=1; AJvYcCUIGVoBsY0B1ogWG/0HbPEh0iJ32yTIEg49iMraDCdiYA1OLDkfbpX5614LVhQ4VfMOI1h8h0mTZ1DFNQLVsY5WSYzIq8TXqangseuk X-Gm-Message-State: AOJu0YwZ269CWqveg5n2OiSQbV4Xwki5AGFXLpLx5GIhDZ0e4umTF0iZ u1d86nDSOppyqjJxXtBT/dRMg7Tuz3KvQAj03K1J3VtvvCxRhvq+xi6sn4lz2Q== X-Received: by 2002:a05:6a00:4b85:b0:6e0:47b9:b727 with SMTP id ks5-20020a056a004b8500b006e047b9b727mr3372804pfb.30.1707939966471; Wed, 14 Feb 2024 11:46:06 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCW7Cwoijsp6dJM+Ekva4xtJ7poFQrc+CxsLCm0rozgF+ThOJCdpm3+MlFGTlkTSbTvAXQzO+vbs7yYuL+AF4R7WXy0Jj2xceLs+3esoKAvzv7epf+Iajsi5GfEz2f/6bDW5QSOUlMbXIxHjfnDRf9y8eb1xRHMVcAQLFXUZyXl6X/oQOSEC2tK9beZdNDpClZBEfI0MUNear1S6IPJ+s2yrz5b3XfoN4RBlh85EoL7tcIdWQRV88jI+DQ6daYac+w2yUHK4sE0Tc9sYc5q5ipsk2fys9Q24ygGmhy6JUyAZDMezJliIaNXs5fU6mUAtbaEbYZKGBFcW4A== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id g15-20020aa79dcf000000b006e09e975596sm9229771pfq.38.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 , 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 v7 3/3] overflow: Introduce wrapping_assign_add() and wrapping_assign_sub() Date: Wed, 14 Feb 2024 11:46:03 -0800 Message-Id: <20240214194605.602505-3-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=4684; i=keescook@chromium.org; h=from:subject; bh=WpCfOJKQI2tEPzWqXq2AwhFgSH1gItVpb6dbVuRrWwo=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlzRh7+CaMXERKbG/TjLkrLLxvw47pmVByFgyp2 54s77/hIuKJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZc0YewAKCRCJcvTf3G3A JsTHD/9V5W7uSsNqclqE+WRbQgUNPUGr2Z0aTYhid2E0NBqKt6TgpdzgEwYnkz9QuNi5PtqbfeT 3KTzVu2TztVAD4MhsWpK2R/8I/5oxcFJ4EmlZyWwT9dBWDK+zSRK/cEYDvgORPHFFUkL+6JLKmx IFd5SgbuvI+F9E0kHGN2iReicG9HHlGEfZSaEKLx2kyFfpupG9h+M+7kTxuH1X5ir78pyd9W6Tt J2UBp+xPAMuTNMBLRLJWuZHq+TvGU1Kz/JK5zEx0fbqz8cALwhRivwYPFrDN0Y2iEzqLgV/MpjC SHjYSsrc5iWCXjP0EemrzTB5cIGSeLBraofYV92DRO/p2mDB9MMmb7qPEfhVmUr+8f3th+vMAAc pAA0F9PgA6bqNHULJ2zElabbp5GuOulS5jBINn/wKJzioxU4878geM1q2k8PTPSUepBcZFd3UPY fbKEUR6FYlggQ/7iP6jF3xBAO1vq7KoNWQNYFjHeK54ugrWSJSFJUdV9YPPw4hQsdv9mub8VJqy Hft7QxCQWfSYhP67h/+sl5pFlyz55GXeBD61nGAQgwb+rS8SWO+zDWbxGGjYToUvmBu/lRs/TDS k1/esybUWsrTq1oqfi/k8Cy6C08yc9t8PKr+FCbSmcTCPhLiMe4Z/UIEQxMPB+w0u4eS812ZPDy k7VJ5j6GMhaPMJw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790904904936472625 X-GMAIL-MSGID: 1790904904936472625 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. Signed-off-by: Kees Cook Reviewed-by: Marco Elver Acked-by: Mark Rutland --- Cc: Rasmus Villemoes Cc: Marco Elver Cc: Eric Biggers Cc: Mark Rutland Cc: "Gustavo A. R. Silva" Cc: linux-hardening@vger.kernel.org --- 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 d3ff8e2bec29..dede374832c9 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -81,6 +81,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 @@ -111,6 +127,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) { \