Re: [PATCH net-next v3 1/7] overflow: add DEFINE_FLEX() for on-stack allocs

From
Kees Cook <hidden>
Date
2023-08-17 17:00:20
Also in
intel-wired-lan, linux-hardening
Reply to
David Laight, 2023-08-17
On Thu, Aug 17, 2023 at 02:35:23PM +0000, David Laight wrote:
From: Przemek Kitszel
quoted
Sent: Wednesday, August 16, 2023 3:06 PM

Using underlying array for on-stack storage lets us to declare
known-at-compile-time structures without kzalloc().
Isn't DEFINE_FLEX() a bit misleading?
One thing it isn't is 'flexible' since it has a fixed size.
It works only on flex array structs, and defines a specific instance. I
think naming is okay here.
quoted
+#define DEFINE_FLEX(type, name, member, count)					\
+	union {									\
+		u8 bytes[struct_size_t(type, member, count)];			\
+		type obj;							\
+	} name##_u __aligned(_Alignof(type)) = {};				\
You shouldn't need the _Alignof() it is the default.
In the sense that since "type" is in the union, it's okay?
I'm not sure you should be forcing the memset() either.
This already got discussed: better to fail safe.
quoted
+	type *name = (type *)&name##_u
How about?
	type *const name = &name_##_u.obj;
This is by design (see earlier threads) so that
__builtin_object_size(name, 1) will get the correct size. Otherwise it
doesn't include the FAM elements in the size.
You might want to add:
	Static_assert(is_constexpr(count), "DEFINE_FLEX: non-constant count " #count);
That would be nice, though can Static_assert()s live in the middle of
variable definitions?

-Kees

-- 
Kees Cook
Thread (21 messages) 21 messages, 7 authors, last reply 2023-08-28
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help