overflow update for v5.10-rc1

- Add __must_check to check_*_overflow() helpers
 -----BEGIN PGP SIGNATURE-----
 
 iQJKBAABCgA0FiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAl+E2o4WHGtlZXNjb29r
 QGNocm9taXVtLm9yZwAKCRCJcvTf3G3AJj43EACSc7useUYYd5KPNyJ3IJL/kZ4o
 i879uiXdEddf4M//uF9+9WeORki23w0jP4zr7gEuA0yk7yTl7HcZzBuxUxpPZN+K
 C7dmifQrg9FejQGomaxCiBIu6jtz3pK01DTV6NVZ0MOpbjmqB2ZCCGuJrPXwEkpJ
 zCQ0qJvlJRC9wMWU7HH+cMaC3tHj2tIoCUClL3HvhO7WthpjC/R0ieF6onTgt7YX
 NC2kxnIjFo3AwXF+ZzvaZNegmRBGfrYjysqP2LtwifhyfdZ6vKzPtEnv5nRM2Vy+
 Q4PytoYIZ/IPWTLJPzBRLozvyy9iPuxVJFabKMkEHug7I6kQ3Q6+qbng+aAjVTI9
 yjETd9mZSLvS+hP29oWDvBbk82OGbBLYwSfOfn7IDLulV+8HViHE7F93I+3GSPIU
 bTfA1Y1TQ9MBV8oqFfTd6lHhH8nc++k0SkxSksajChl8osMoImP4qbQbBkjPF3lI
 0YRvW291qGldywGDw9BxviNpGfySYHneElBVjVGTfPQZAmlnDSsYvs66SBbmHQDM
 O7WIa6Af4fszLWGtC6q0aftKwSuTURxEuyZWNfInXCgAI5B1wC3v1SM/DSld35A6
 FBZjrl7As09itYLj0UG4v2hxu+fGNUNZiFk+jNLEwKEzocbRT+0FRcUByim1Gksc
 qi13pCX8QWQwmRWAgg==
 =YVvX
 -----END PGP SIGNATURE-----

Merge tag 'overflow-v5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux

Pull overflow update from Kees Cook:
 "Just a single change to help enforce all callers are actually checking
  the results of the helpers"

* tag 'overflow-v5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
  overflow: Add __must_check attribute to check_*() helpers
This commit is contained in:
Linus Torvalds 2020-10-13 16:37:39 -07:00
commit b5fc7a89e5
1 changed files with 24 additions and 15 deletions

View File

@ -43,6 +43,16 @@
#define is_non_negative(a) ((a) > 0 || (a) == 0)
#define is_negative(a) (!(is_non_negative(a)))
/*
* Allows for effectively applying __must_check to a macro so we can have
* both the type-agnostic benefits of the macros while also being able to
* enforce that the return value is, in fact, checked.
*/
static inline bool __must_check __must_check_overflow(bool overflow)
{
return unlikely(overflow);
}
#ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
/*
* For simplicity and code hygiene, the fallback code below insists on
@ -52,32 +62,32 @@
* alias for __builtin_add_overflow, but add type checks similar to
* below.
*/
#define check_add_overflow(a, b, d) ({ \
#define check_add_overflow(a, b, d) __must_check_overflow(({ \
typeof(a) __a = (a); \
typeof(b) __b = (b); \
typeof(d) __d = (d); \
(void) (&__a == &__b); \
(void) (&__a == __d); \
__builtin_add_overflow(__a, __b, __d); \
})
}))
#define check_sub_overflow(a, b, d) ({ \
#define check_sub_overflow(a, b, d) __must_check_overflow(({ \
typeof(a) __a = (a); \
typeof(b) __b = (b); \
typeof(d) __d = (d); \
(void) (&__a == &__b); \
(void) (&__a == __d); \
__builtin_sub_overflow(__a, __b, __d); \
})
}))
#define check_mul_overflow(a, b, d) ({ \
#define check_mul_overflow(a, b, d) __must_check_overflow(({ \
typeof(a) __a = (a); \
typeof(b) __b = (b); \
typeof(d) __d = (d); \
(void) (&__a == &__b); \
(void) (&__a == __d); \
__builtin_mul_overflow(__a, __b, __d); \
})
}))
#else
@ -190,21 +200,20 @@
})
#define check_add_overflow(a, b, d) \
#define check_add_overflow(a, b, d) __must_check_overflow( \
__builtin_choose_expr(is_signed_type(typeof(a)), \
__signed_add_overflow(a, b, d), \
__unsigned_add_overflow(a, b, d))
__unsigned_add_overflow(a, b, d)))
#define check_sub_overflow(a, b, d) \
#define check_sub_overflow(a, b, d) __must_check_overflow( \
__builtin_choose_expr(is_signed_type(typeof(a)), \
__signed_sub_overflow(a, b, d), \
__unsigned_sub_overflow(a, b, d))
__unsigned_sub_overflow(a, b, d)))
#define check_mul_overflow(a, b, d) \
#define check_mul_overflow(a, b, d) __must_check_overflow( \
__builtin_choose_expr(is_signed_type(typeof(a)), \
__signed_mul_overflow(a, b, d), \
__unsigned_mul_overflow(a, b, d))
__unsigned_mul_overflow(a, b, d)))
#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
@ -227,7 +236,7 @@
* '*d' will hold the results of the attempted shift, but is not
* considered "safe for use" if false is returned.
*/
#define check_shl_overflow(a, s, d) ({ \
#define check_shl_overflow(a, s, d) __must_check_overflow(({ \
typeof(a) _a = a; \
typeof(s) _s = s; \
typeof(d) _d = d; \
@ -237,7 +246,7 @@
*_d = (_a_full << _to_shift); \
(_to_shift != _s || is_negative(*_d) || is_negative(_a) || \
(*_d >> _to_shift) != _a); \
})
}))
/**
* array_size() - Calculate size of 2-dimensional array.