Skip to content

Commit 9b80e4c

Browse files
committed
overflow: Add __must_check attribute to check_*() helpers
Since the destination variable of the check_*_overflow() helpers will contain a wrapped value on failure, it would be best to make sure callers really did check the return result of the helper. Adjust the macros to use a bool-wrapping static inline that is marked with __must_check. This means the macros can continue to have their type-agnostic behavior while gaining the function attribute (that cannot be applied directly to macros). Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Link: https://lore.kernel.org/lkml/202008151007.EF679DF@keescook/ Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 9123e3a commit 9b80e4c

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

include/linux/overflow.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
#define is_non_negative(a) ((a) > 0 || (a) == 0)
4444
#define is_negative(a) (!(is_non_negative(a)))
4545

46+
/*
47+
* Allows for effectively applying __must_check to a macro so we can have
48+
* both the type-agnostic benefits of the macros while also being able to
49+
* enforce that the return value is, in fact, checked.
50+
*/
51+
static inline bool __must_check __must_check_overflow(bool overflow)
52+
{
53+
return unlikely(overflow);
54+
}
55+
4656
#ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
4757
/*
4858
* For simplicity and code hygiene, the fallback code below insists on
@@ -52,32 +62,32 @@
5262
* alias for __builtin_add_overflow, but add type checks similar to
5363
* below.
5464
*/
55-
#define check_add_overflow(a, b, d) ({ \
65+
#define check_add_overflow(a, b, d) __must_check_overflow(({ \
5666
typeof(a) __a = (a); \
5767
typeof(b) __b = (b); \
5868
typeof(d) __d = (d); \
5969
(void) (&__a == &__b); \
6070
(void) (&__a == __d); \
6171
__builtin_add_overflow(__a, __b, __d); \
62-
})
72+
}))
6373

64-
#define check_sub_overflow(a, b, d) ({ \
74+
#define check_sub_overflow(a, b, d) __must_check_overflow(({ \
6575
typeof(a) __a = (a); \
6676
typeof(b) __b = (b); \
6777
typeof(d) __d = (d); \
6878
(void) (&__a == &__b); \
6979
(void) (&__a == __d); \
7080
__builtin_sub_overflow(__a, __b, __d); \
71-
})
81+
}))
7282

73-
#define check_mul_overflow(a, b, d) ({ \
83+
#define check_mul_overflow(a, b, d) __must_check_overflow(({ \
7484
typeof(a) __a = (a); \
7585
typeof(b) __b = (b); \
7686
typeof(d) __d = (d); \
7787
(void) (&__a == &__b); \
7888
(void) (&__a == __d); \
7989
__builtin_mul_overflow(__a, __b, __d); \
80-
})
90+
}))
8191

8292
#else
8393

@@ -190,21 +200,20 @@
190200
})
191201

192202

193-
#define check_add_overflow(a, b, d) \
203+
#define check_add_overflow(a, b, d) __must_check_overflow( \
194204
__builtin_choose_expr(is_signed_type(typeof(a)), \
195205
__signed_add_overflow(a, b, d), \
196-
__unsigned_add_overflow(a, b, d))
206+
__unsigned_add_overflow(a, b, d)))
197207

198-
#define check_sub_overflow(a, b, d) \
208+
#define check_sub_overflow(a, b, d) __must_check_overflow( \
199209
__builtin_choose_expr(is_signed_type(typeof(a)), \
200210
__signed_sub_overflow(a, b, d), \
201-
__unsigned_sub_overflow(a, b, d))
211+
__unsigned_sub_overflow(a, b, d)))
202212

203-
#define check_mul_overflow(a, b, d) \
213+
#define check_mul_overflow(a, b, d) __must_check_overflow( \
204214
__builtin_choose_expr(is_signed_type(typeof(a)), \
205215
__signed_mul_overflow(a, b, d), \
206-
__unsigned_mul_overflow(a, b, d))
207-
216+
__unsigned_mul_overflow(a, b, d)))
208217

209218
#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
210219

@@ -227,7 +236,7 @@
227236
* '*d' will hold the results of the attempted shift, but is not
228237
* considered "safe for use" if false is returned.
229238
*/
230-
#define check_shl_overflow(a, s, d) ({ \
239+
#define check_shl_overflow(a, s, d) __must_check_overflow(({ \
231240
typeof(a) _a = a; \
232241
typeof(s) _s = s; \
233242
typeof(d) _d = d; \
@@ -237,7 +246,7 @@
237246
*_d = (_a_full << _to_shift); \
238247
(_to_shift != _s || is_negative(*_d) || is_negative(_a) || \
239248
(*_d >> _to_shift) != _a); \
240-
})
249+
}))
241250

242251
/**
243252
* array_size() - Calculate size of 2-dimensional array.

0 commit comments

Comments
 (0)