summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2022-02-10 03:40:41 +0300
committerKees Cook <keescook@chromium.org>2022-02-17 01:30:37 +0300
commit230f6fa2c1db6a3f3e668cfe95995ac8e6eee212 (patch)
tree0efbee5d1df73b95483adf495bb6e2232342fc7d /lib
parente1be43d9b5d0d1310dbd90185a8e5c7145dde40f (diff)
downloadlinux-230f6fa2c1db6a3f3e668cfe95995ac8e6eee212.tar.xz
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. Cc: Gustavo A. R. Silva <gustavoars@kernel.org> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk> Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org> Tested-by: Gustavo A. R. Silva <gustavoars@kernel.org> Link: https://lore.kernel.org/lkml/20220210010407.GA701603@embeddedor
Diffstat (limited to 'lib')
-rw-r--r--lib/test_overflow.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/lib/test_overflow.c b/lib/test_overflow.c
index 712fb2351c27..f6530fce799d 100644
--- a/lib/test_overflow.c
+++ b/lib/test_overflow.c
@@ -602,10 +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;
+ volatile int unconst = 0;
+
+ /* 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; \
@@ -663,28 +671,28 @@ static int __init test_overflow_size_helpers(void)
flex_array_size, obj, data, var++);
err |= check_one_size_helper(5 * sizeof(*obj->data),
flex_array_size, obj, data, var++);
- err |= check_one_size_helper(0, flex_array_size, obj, data, 0);
+ err |= check_one_size_helper(0, flex_array_size, obj, data, 0 + unconst);
err |= check_one_size_helper(sizeof(*obj->data),
- flex_array_size, obj, data, 1);
+ flex_array_size, obj, data, 1 + unconst);
err |= check_one_size_helper(7 * sizeof(*obj->data),
- flex_array_size, obj, data, 7);
+ flex_array_size, obj, data, 7 + unconst);
err |= check_one_size_helper(SIZE_MAX,
- flex_array_size, obj, data, -1);
+ flex_array_size, obj, data, -1 + unconst);
err |= check_one_size_helper(SIZE_MAX,
- flex_array_size, obj, data, SIZE_MAX - 4);
+ flex_array_size, obj, data, SIZE_MAX - 4 + unconst);
var = 4;
err |= check_one_size_helper(sizeof(*obj) + (4 * sizeof(*obj->data)),
struct_size, obj, data, var++);
err |= check_one_size_helper(sizeof(*obj) + (5 * sizeof(*obj->data)),
struct_size, obj, data, var++);
- err |= check_one_size_helper(sizeof(*obj), struct_size, obj, data, 0);
+ err |= check_one_size_helper(sizeof(*obj), struct_size, obj, data, 0 + unconst);
err |= check_one_size_helper(sizeof(*obj) + sizeof(*obj->data),
- struct_size, obj, data, 1);
+ struct_size, obj, data, 1 + unconst);
err |= check_one_size_helper(SIZE_MAX,
- struct_size, obj, data, -3);
+ struct_size, obj, data, -3 + unconst);
err |= check_one_size_helper(SIZE_MAX,
- struct_size, obj, data, SIZE_MAX - 3);
+ struct_size, obj, data, SIZE_MAX - 3 + unconst);
pr_info("%d overflow size helper tests finished\n", count);