Skip to contentSkip to conversation

overflow: Add safe add/sub and compare helpers

PATCH In review Michael Margolin 2 participants 2 messages Jul 7, 2025 – Jul 8, 2025 Kernel hardening (not covered by other areas)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 overflow: Add safe add/sub and compare helpers

Following a discussion on changes in RDMA subsystem [1], there is a use for helpers that allow overflow safe comparison between the result of a sum or a diff between two variables and some third operand. The classic use case is checking that end address is in range, given start address and length, but there are probably others. Add helpers that perform mathematically correct comparison regardles of the types being used.

[1] https://lore.kernel.org/all/[email protected]/

---
 include/linux/overflow.h   |  34 ++++++++
 lib/tests/overflow_kunit.c | 171 +++++++++++++++++++++++++++++++++++++
 2 files changed, 205 insertions(+)
include/linux/overflow.h +34 -0
index 154ed0dbb43f..c79bde6cee3d 100644
@@ -99,6 +99,23 @@ static inline bool __must_check __must_check_overflow(bool overflow)
 		*__ptr = wrapping_add(typeof(var), *__ptr, offset);	\
 	})
 
+/**
+ * overflow_safe_add_cmp() - Calculate addition of first two parameters and
+ * compare to the third parameter. The returned result is correct even when the
+ * sum wraps-around.
+ * @a: first addend
+ * @b: second addend
+ * @c: comparison operand
+ *
+ * Returns -1 if @a + @b < @c, 0 if @a + @b == @c and 1 if @a + @b > @c.
+ */
+#define overflow_safe_add_cmp(a, b, c)                                        \
+	({                                                                    \
+		typeof(c) __val, __c = c;                                     \
+		__builtin_add_overflow(a, b, &__val) || __val > __c ? 1 :     \
+		__val == __c ? 0 : -1;                                        \
+	})
+
 /**
  * check_sub_overflow() - Calculate subtraction with overflow checking
  * @a: minuend; value to subtract from
@@ -145,6 +162,23 @@ static inline bool __must_check __must_check_overflow(bool overflow)
 		*__ptr = wrapping_sub(typeof(var), *__ptr, offset);	\
 	})
 
+/**
+ * overflow_safe_sub_cmp() - Calculate subtraction of second parameter from
+ * the first and compare to the third parameter. The returned result is correct
+ * even when the subtraction wraps-around.
+ * @a: minuend; value to subtract from
+ * @b: subtrahend; value to subtract from @a
+ * @c: comparison operand
+ *
+ * Returns -1 if @a - @b < @c, 0 if @a - @b == @c and 1 if @a - @b > @c.
+ */
+#define overflow_safe_sub_cmp(a, b, c)                                        \
+	({                                                                    \
+		typeof(c) __val, __c = c;                                     \
+		__builtin_sub_overflow(a, b, &__val) || __val < __c ? -1 :    \
+			__val == __c ? 0 : 1;                                 \
+	})
+
 /**
  * check_mul_overflow() - Calculate multiplication with overflow checking
  * @a: first factor
lib/tests/overflow_kunit.c +171 -0

183 lines not on this page · raw message

Signature · 1 line
2.47.1
#1 lore raw
Quoted 7 lines · Michael Margolin

Following a discussion on changes in RDMA subsystem [1], there is a use for helpers that allow overflow safe comparison between the result of a sum or a diff between two variables and some third operand. The classic use case is checking that end address is in range, given start address and length, but there are probably others. Add helpers that perform mathematically correct comparison regardles of the types being used.

Quoted 7 lines

[1] https://lore.kernel.org/all/[email protected]/

---
 include/linux/overflow.h   |  34 ++++++++
 lib/tests/overflow_kunit.c | 171 +++++++++++++++++++++++++++++++++++++
 2 files changed, 205 insertions(+)

You should make this a series and add at least one user..

Jason