From: Kees Cook <keescook@chromium.org>
To: Kees Cook <keescook@chromium.org>
Cc: "Gustavo A . R . Silva" <gustavoars@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Vitor Massaru Iha <vitor@massaru.org>,
Daniel Latypov <dlatypov@google.com>,
David Gow <davidgow@google.com>,
Anton Ivanov <anton.ivanov@cambridgegreys.com>,
Jeff Dike <jdike@addtoit.com>,
Richard Weinberger <richard@nod.at>,
Masahiro Yamada <masahiroy@kernel.org>,
Arnd Bergmann <arnd@arndb.de>,
linux-kernel@vger.kernel.org, linux-um@lists.infradead.org,
linux-kbuild@vger.kernel.org, kunit-dev@googlegroups.com,
llvm@lists.linux.dev, x86@kernel.org,
linux-hardening@vger.kernel.org
Subject: [PATCH v3 1/7] overflow: Provide constant expression struct_size
Date: Sun, 27 Feb 2022 10:45:11 -0800 [thread overview]
Message-ID: <20220227184517.504931-2-keescook@chromium.org> (raw)
In-Reply-To: <20220227184517.504931-1-keescook@chromium.org>
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
---
v1: https://lore.kernel.org/linux-hardening/20220210004326.776574-1-keescook@chromium.org
v2:
- fix broken self-test due to CE optimization
---
include/linux/overflow.h | 10 +++++++---
lib/test_overflow.c | 26 +++++++++++++++++---------
2 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 59d7228104d0..f1221d11f8e5 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -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 */
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);
--
2.32.0
next prev parent reply other threads:[~2022-02-27 18:45 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-27 18:45 [PATCH v3 0/7] Convert overflow and stackinit to KUnit Kees Cook
2022-02-27 18:45 ` Kees Cook [this message]
2022-02-27 18:45 ` [PATCH v3 2/7] lib: overflow: Convert to Kunit Kees Cook
2022-02-27 18:45 ` [PATCH v3 3/7] um: Cleanup syscall_handler_t definition/cast, fix warning Kees Cook
2022-02-27 18:45 ` [PATCH v3 4/7] um: Remove unused timeval_to_ns() function Kees Cook
2022-02-27 18:45 ` [PATCH v3 5/7] um: Allow builds with Clang Kees Cook
2022-02-27 18:45 ` [PATCH v3 6/7] lib: stackinit: Convert to KUnit Kees Cook
2022-02-27 18:45 ` [PATCH v3 7/7] UAPI: Introduce KUnit userspace compatibility Kees Cook
2022-02-27 23:14 ` Kees Cook
2022-03-01 19:56 ` Brendan Higgins
2022-03-03 8:27 ` David Gow
2022-03-03 13:05 ` Greg KH
2022-03-03 17:28 ` Kees Cook
2022-03-03 9:21 ` [PATCH v3 0/7] Convert overflow and stackinit to KUnit David Gow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220227184517.504931-2-keescook@chromium.org \
--to=keescook@chromium.org \
--cc=anton.ivanov@cambridgegreys.com \
--cc=arnd@arndb.de \
--cc=davidgow@google.com \
--cc=dlatypov@google.com \
--cc=gustavoars@kernel.org \
--cc=jdike@addtoit.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-um@lists.infradead.org \
--cc=linux@rasmusvillemoes.dk \
--cc=llvm@lists.linux.dev \
--cc=masahiroy@kernel.org \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=richard@nod.at \
--cc=vitor@massaru.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).