Skip to contentSkip to conversation

overflow: Fix direct struct member initialization in _DEFINE_FLEX()

PATCH Superseded Gustavo A. R. Silva 3 participants 3 messages Apr 30, 2025 – May 1, 2025 Kernel hardening (not covered by other areas)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Gustavo A. R. Silva

author reviewer
#0 lore raw

PATCH 1/1 overflow: Fix direct struct member initialization in _DEFINE_FLEX()

Currently, to statically initialize the struct members of the type object created by _DEFINE_FLEX(), the internal obj member must be explicitly referenced at the call site. See:

struct flex {
	int a;
	int b;
	struct foo flex_array[];
};

_DEFINE_FLEX(struct flex, instance, flex_array,

	     FIXED_SIZE, = {
		.obj = {
			.a = 0,
			.b = 1,
		},
	});

This leaks _DEFINE_FLEX() internal implementation details and make the helper harder to use and read.

Fix this and allow for a more natural and intuitive C99 init-style:

_DEFINE_FLEX(struct flex, instance, flex_array,

	     FIXED_SIZE, = {
		.a = 0,
		.b = 1,
	});

Also, update "counter" member initialization in DEFINE_FLEX().

Fixes 26dd68d293fd ("overflow: add DEFINE_FLEX() for on-stack allocs")
---
 include/linux/overflow.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/overflow.h +2 -2
index 69533e703be5..170d3cfe7ecc 100644
@@ -404,7 +404,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
 	union {									\
 		u8 bytes[struct_size_t(type, member, count)];			\
 		type obj;							\
-	} name##_u initializer;							\
+	} name##_u = { .obj initializer };					\
 	type *name = (type *)&name##_u
 
 /**
@@ -444,7 +444,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * elements in array @member.
  */
 #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
-	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
+	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })
 
 /**
  * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.
Signature · 1 line
2.43.0
maintainer
#1 lore raw

include/linux/overflow.h static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)

Quoted 43 lines · Gustavo A. R. Silva

Currently, to statically initialize the struct members of the type object created by _DEFINE_FLEX(), the internal obj member must be explicitly referenced at the call site. See:

struct flex {
	int a;
	int b;
	struct foo flex_array[];
};

_DEFINE_FLEX(struct flex, instance, flex_array,

	     FIXED_SIZE, = {
		.obj = {
			.a = 0,
			.b = 1,
		},
	});

This leaks _DEFINE_FLEX() internal implementation details and make the helper harder to use and read.

Fix this and allow for a more natural and intuitive C99 init-style:

_DEFINE_FLEX(struct flex, instance, flex_array,

	     FIXED_SIZE, = {
		.a = 0,
		.b = 1,
	});

Also, update "counter" member initialization in DEFINE_FLEX().

Fixes 26dd68d293fd ("overflow: add DEFINE_FLEX() for on-stack allocs")
---
 include/linux/overflow.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/overflow.h +0 -0
index 69533e703be5..170d3cfe7ecc 100644
@@ -404,7 +404,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)

Earlier up in the file:

...
* @initializer: initializer expression (could be empty for no init). ^^^^^^^^^
*/
#define _DEFINE_FLEX(type, name, member, count, initializer...)

This argument now becomes required, which is fine, but we should keep the docs updated and double check any existing "_DEFINE_FLEX" users that may have an empty final argument (I don't see any, so that's nice).

Quoted 9 lines
include/linux/overflow.h +1 -1
 	union {									\
 		u8 bytes[struct_size_t(type, member, count)];			\
 		type obj;							\
-	} name##_u initializer;							\
+	} name##_u = { .obj initializer };					\
 	type *name = (type *)&name##_u
 
 /**

include/linux/overflow.h static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) · line 450

Quoted 8 lines
include/linux/overflow.h +1 -1
@@ -444,7 +444,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * elements in array @member.
  */
 #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
-	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
+	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })
 
 /**
  * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.

But otherwise, yes, let's do this!

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 10 lines

Earlier up in the file:

...
* @initializer: initializer expression (could be empty for no init).

    ^^^^^^^^^

*/
#define _DEFINE_FLEX(type, name, member, count, initializer...)

This argument now becomes required, which is fine, but we should keep

Nice catch!

Quoted 2 lines

the docs updated and double check any existing "_DEFINE_FLEX" users that may have an empty final argument (I don't see any, so that's nice).

Yep, I build-tested it and saw no errors.

Quoted 16 lines
Quoted 16 lines
  	union {									\
  		u8 bytes[struct_size_t(type, member, count)];			\
  		type obj;							\
include/linux/overflow.h +2 -2
-	} name##_u initializer;							\
+	} name##_u = { .obj initializer };					\
  	type *name = (type *)&name##_u
 
  /**
@@ -444,7 +444,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
   * elements in array @member.
   */
  #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
-	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
+	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })
 
Quoted 5 lines

include/linux/overflow.h static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) · line 450

Quoted 2 lines
include/linux/overflow.h +0 -0
  /**
   * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.

But otherwise, yes, let's do this!

Here you go :)

https://lore.kernel.org/linux-hardening/aBP0b3gfurLFDlwY@kspp/

Thanks!
-Gustavo