overflow: Provide constant expression struct_size
Subsystems (2)
PATCH 1/1 overflow: Provide constant expression struct_size
There have been cases where struct_size() (or flex_array_size()) needs to be calculated for an initializer, which requires it be a constant expression. This is possible when the "count" argument is a constant expression, so provide this ability for the helpers.
Signed-off-by Kees Cook <[email protected]>
--- include/linux/overflow.h | 10 +++++++--- lib/test_overflow.c | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-)
include/linux/overflow.h +7 -3
@@ -4,6 +4,7 @@#include <linux/compiler.h> #include <linux/limits.h> +#include <linux/const.h> /* * We need to compute the minimum and maximum values representable in a given
@@ -221,8 +222,9 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)* Return: number of bytes needed or SIZE_MAX on overflow. */ #define flex_array_size (p, member, count) \ - size_mul(count, \ - sizeof(*(p)->member) + __must_be_array((p)->member)) + __builtin_choose_expr(__is_constexpr(count), \ + (count) * sizeof(*(p)->member) + __must_be_array((p)->member), \ + size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member)))/** * struct_size() - Calculate size of structure with trailing flexible array.
@@ -237,6 +239,8 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)* Return: number of bytes needed or SIZE_MAX on overflow. */ #define struct_size (p, member, count) \ - size_add(sizeof(*(p)), flex_array_size(p, member, count)) + __builtin_choose_expr(__is_constexpr(count), \ + sizeof(*(p)) + flex_array_size(p, member, count), \ + size_add(sizeof(*(p)), flex_array_size(p, member, count)))#endif /* __LINUX_OVERFLOW_H */
lib/test_overflow.c +7 -0
@@ -602,11 +602,18 @@ struct __test_flex_array {static int __init test_overflow_size_helpers (void ) { +/* Make sure struct_size() can be used in a constant expression. */ +u8 ce_array[struct_size ((struct __test_flex_array *)0 , data,55 )];struct __test_flex_array *obj;int count =0 ;int err =0 ;int var; +/* Verify constant expression against runtime version. */ + var =55 ; +OPTIMIZER_HIDE_VAR (var); + err |=sizeof (ce_array) !=struct_size (obj, data, var); + #define check_one_size_helper(expected, func, args...) ({ \ bool __failure = false; \ size_t _r; \
Signature · 1 line
2.30.2
Quoted 6 lines · Kees Cook
There have been cases where
struct_size()(orflex_array_size()) needs to be calculated for an initializer, which requires it be a constant expression. This is possible when the "count" argument is a constant expression, so provide this ability for the helpers.Signed-off-by Kees Cook <[email protected]>
Reviewed-by Gustavo A. R. Silva <[email protected]>
Tested-by Gustavo A. R. Silva <[email protected]>
Thanks!
--
Gustavo
Quoted 65 lines
--- include/linux/overflow.h | 10 +++++++--- lib/test_overflow.c | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-)include/linux/overflow.h +7
-3@@ -4,6 +4,7 @@#include <linux/compiler.h> #include <linux/limits.h> +#include <linux/const.h> /* * We need to compute the minimum and maximum values representable in a given @@ -221,8 +222,9 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)* Return: number of bytes needed or SIZE_MAX on overflow. */ #define flex_array_size (p, member, count) \ - size_mul(count, \ - sizeof(*(p)->member) + __must_be_array((p)->member)) + __builtin_choose_expr(__is_constexpr(count), \ + (count) * sizeof(*(p)->member) + __must_be_array((p)->member), \ + size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member)))/** * struct_size() - Calculate size of structure with trailing flexible array. @@ -237,6 +239,8 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)* Return: number of bytes needed or SIZE_MAX on overflow. */ #define struct_size (p, member, count) \ - size_add(sizeof(*(p)), flex_array_size(p, member, count)) + __builtin_choose_expr(__is_constexpr(count), \ + sizeof(*(p)) + flex_array_size(p, member, count), \ + size_add(sizeof(*(p)), flex_array_size(p, member, count)))#endif /* __LINUX_OVERFLOW_H */ lib/test_overflow.c +7
-0@@ -602,11 +602,18 @@ struct __test_flex_array {static int __init test_overflow_size_helpers (void ) { +/* Make sure struct_size() can be used in a constant expression. */ +u8 ce_array[struct_size ((struct __test_flex_array *)0 , data,55 )];struct __test_flex_array *obj;int count =0 ;int err =0 ;int var; +/* Verify constant expression against runtime version. */ + var =55 ; +OPTIMIZER_HIDE_VAR (var); + err |=sizeof (ce_array) !=struct_size (obj, data, var); + #define check_one_size_helper(expected, func, args...) ({ \ bool __failure = false; \ size_t _r; \Signature · 1 line
2.30.2