[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240123002814.1396804-5-keescook@chromium.org>
Date: Mon, 22 Jan 2024 16:26:40 -0800
From: Kees Cook <keescook@...omium.org>
To: linux-hardening@...r.kernel.org
Cc: Kees Cook <keescook@...omium.org>,
Julia Lawall <Julia.Lawall@...ia.fr>,
Nicolas Palix <nicolas.palix@...g.fr>,
"Gustavo A. R. Silva" <gustavoars@...nel.org>,
Justin Stitt <justinstitt@...gle.com>,
cocci@...ia.fr,
Bill Wendling <morbo@...gle.com>,
linux-kernel@...r.kernel.org
Subject: [PATCH 05/82] cocci: Refactor open-coded arithmetic wrap-around
In pursuit of gaining full kernel instrumentation for signed[1],
unsigned[2], and pointer[3] arithmetic overflow, we need to replace
the handful of instances in the kernel where we intentionally depend on
arithmetic wrap-around. Introduce Coccinelle script for finding these
and replacing them with the new add_would_overflow() helper, for this
common code pattern:
if (VAR + OFFSET < VAR) ...
Link: https://github.com/KSPP/linux/issues/26 [1]
Link: https://github.com/KSPP/linux/issues/27 [2]
Link: https://github.com/KSPP/linux/issues/344 [3]
Cc: Julia Lawall <Julia.Lawall@...ia.fr>
Cc: Nicolas Palix <nicolas.palix@...g.fr>
Cc: "Gustavo A. R. Silva" <gustavoars@...nel.org>
Cc: Justin Stitt <justinstitt@...gle.com>
Cc: cocci@...ia.fr
Signed-off-by: Kees Cook <keescook@...omium.org>
---
.../coccinelle/misc/add_would_overflow.cocci | 70 +++++++++++++++++++
1 file changed, 70 insertions(+)
create mode 100644 scripts/coccinelle/misc/add_would_overflow.cocci
diff --git a/scripts/coccinelle/misc/add_would_overflow.cocci b/scripts/coccinelle/misc/add_would_overflow.cocci
new file mode 100644
index 000000000000..b9b67c9c3714
--- /dev/null
+++ b/scripts/coccinelle/misc/add_would_overflow.cocci
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Replace intentional wrap-around addition with calls to
+/// check_add_overflow() and add_would_overflow(), see
+/// Documentation/process/deprecated.rst
+///
+//
+// Confidence: High
+// Comments:
+// Options: --no-includes --include-headers
+
+virtual context
+virtual report
+virtual org
+virtual patch
+
+@...ort_wrap_sum depends on !patch@
+type RESULT;
+RESULT VAR;
+expression OFFSET;
+@@
+
+ {
+ RESULT sum;
+ ...
+ (
+* VAR + OFFSET < VAR
+ )
+ ...
+ (
+ VAR + OFFSET
+ )
+ ...
+ }
+
+@...p_sum depends on patch@
+type RESULT;
+RESULT VAR;
+expression OFFSET;
+@@
+
+ {
++ RESULT sum;
+ ...
+ (
+- VAR + OFFSET < VAR
++ check_add_overflow(VAR, OFFSET, &sum)
+ )
+ ...
+ (
+- VAR + OFFSET
++ sum
+ )
+ ...
+ }
+
+@...ort_wrap depends on !patch && !report_wrap_sum@
+identifier PTR;
+expression OFFSET;
+@@
+
+* PTR + OFFSET < PTR
+
+@...ch_wrap depends on patch && !wrap_sum@
+identifier PTR;
+expression OFFSET;
+@@
+
+- PTR + OFFSET < PTR
++ add_would_overflow(PTR, OFFSET)
--
2.34.1
Powered by blists - more mailing lists