Skip to content

Commit 08d45ee

Browse files
committed
overflow: Introduce wrapping_assign_add() and wrapping_assign_sub()
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. Reviewed-by: Marco Elver <elver@google.com> Acked-by: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent d70de80 commit 08d45ee

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

include/linux/overflow.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ static inline bool __must_check __must_check_overflow(bool overflow)
8181
__val; \
8282
})
8383

84+
/**
85+
* wrapping_assign_add() - Intentionally perform a wrapping increment assignment
86+
* @var: variable to be incremented
87+
* @offset: amount to add
88+
*
89+
* Increments @var by @offset with wrap-around. Returns the resulting
90+
* value of @var. Will not trip any wrap-around sanitizers.
91+
*
92+
* Returns the new value of @var.
93+
*/
94+
#define wrapping_assign_add(var, offset) \
95+
({ \
96+
typeof(var) *__ptr = &(var); \
97+
*__ptr = wrapping_add(typeof(var), *__ptr, offset); \
98+
})
99+
84100
/**
85101
* check_sub_overflow() - Calculate subtraction with overflow checking
86102
* @a: minuend; value to subtract from
@@ -111,6 +127,22 @@ static inline bool __must_check __must_check_overflow(bool overflow)
111127
__val; \
112128
})
113129

130+
/**
131+
* wrapping_assign_sub() - Intentionally perform a wrapping decrement assign
132+
* @var: variable to be decremented
133+
* @offset: amount to subtract
134+
*
135+
* Decrements @var by @offset with wrap-around. Returns the resulting
136+
* value of @var. Will not trip any wrap-around sanitizers.
137+
*
138+
* Returns the new value of @var.
139+
*/
140+
#define wrapping_assign_sub(var, offset) \
141+
({ \
142+
typeof(var) *__ptr = &(var); \
143+
*__ptr = wrapping_sub(typeof(var), *__ptr, offset); \
144+
})
145+
114146
/**
115147
* check_mul_overflow() - Calculate multiplication with overflow checking
116148
* @a: first factor

lib/overflow_kunit.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,45 @@ DEFINE_TEST_ARRAY(s64) = {
284284
"Unexpected wrap " #op " macro side-effect!\n"); \
285285
} while (0)
286286

287+
static int global_counter;
288+
static void bump_counter(void)
289+
{
290+
global_counter++;
291+
}
292+
293+
static int get_index(void)
294+
{
295+
volatile int index = 0;
296+
bump_counter();
297+
return index;
298+
}
299+
300+
#define check_self_op(fmt, op, sym, a, b) do { \
301+
typeof(a + 0) _a = a; \
302+
typeof(b + 0) _b = b; \
303+
typeof(a + 0) _a_sym = a; \
304+
typeof(a + 0) _a_orig[1] = { a }; \
305+
typeof(b + 0) _b_orig = b; \
306+
typeof(b + 0) _b_bump = b + 1; \
307+
typeof(a + 0) _r; \
308+
\
309+
_a_sym sym _b; \
310+
_r = wrapping_ ## op(_a, _b); \
311+
KUNIT_EXPECT_TRUE_MSG(test, _r == _a_sym, \
312+
"expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n", \
313+
a, b, _a_sym, _r); \
314+
KUNIT_EXPECT_TRUE_MSG(test, _a == _a_sym, \
315+
"expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n", \
316+
a, b, _a_sym, _a); \
317+
/* Check for internal macro side-effects. */ \
318+
global_counter = 0; \
319+
wrapping_ ## op(_a_orig[get_index()], _b_orig++); \
320+
KUNIT_EXPECT_EQ_MSG(test, global_counter, 1, \
321+
"Unexpected wrapping_" #op " macro side-effect on arg1!\n"); \
322+
KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, \
323+
"Unexpected wrapping_" #op " macro side-effect on arg2!\n"); \
324+
} while (0)
325+
287326
#define DEFINE_TEST_FUNC_TYPED(n, t, fmt) \
288327
static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
289328
{ \
@@ -293,6 +332,10 @@ static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
293332
check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \
294333
check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \
295334
check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \
335+
/* wrapping_assign_{add,sub}() */ \
336+
check_self_op(fmt, assign_add, +=, p->a, p->b); \
337+
check_self_op(fmt, assign_add, +=, p->b, p->a); \
338+
check_self_op(fmt, assign_sub, -=, p->a, p->b); \
296339
} \
297340
\
298341
static void n ## _overflow_test(struct kunit *test) { \

0 commit comments

Comments
 (0)