Skip to contentSkip to conversation

overflow: Clarify expectations for getting DEFINE_FLEX variable sizes

PATCH Applied Kees Cook 2 participants 3 messages Apr 16, 2025 Kernel hardening (not covered by other areas)

lore
Share & tools
lore

Kees Cook

author maintainer
#0 lore raw

PATCH 1/1 overflow: Clarify expectations for getting DEFINE_FLEX variable sizes

Mention the use of __member_size() for DEFINE_FLEX variables as a hint for getting at the compile-time size of the resulting flexible array member.

---
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]

---
 include/linux/overflow.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/overflow.h +2 -0
index 0c7e3dcfe867..6ee67c20b575 100644
@@ -419,6 +419,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * Define a zeroed, on-stack, instance of @type structure with a trailing
  * flexible array member.
  * Use __struct_size(@name) to get compile-time size of it afterwards.
+ * Use __member_size(@name->member) to get compile-time size of @name members.
  */
 #define DEFINE_RAW_FLEX(type, name, member, count)	\
 	_DEFINE_FLEX(type, name, member, count, = {})
@@ -436,6 +437,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * Define a zeroed, on-stack, instance of @TYPE structure with a trailing
  * flexible array member.
  * Use __struct_size(@NAME) to get compile-time size of it afterwards.
+ * Use __member_size(@NAME->member) to get compile-time size of @NAME members.
  */
 #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
 	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Kees Cook

Mention the use of __member_size() for DEFINE_FLEX variables as a hint for getting at the compile-time size of the resulting flexible array member.

Thanks!
--
Gustavo

Quoted 27 lines

---
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]

---
  include/linux/overflow.h | 2 ++
  1 file changed, 2 insertions(+)
include/linux/overflow.h +2 -0
index 0c7e3dcfe867..6ee67c20b575 100644
@@ -419,6 +419,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
   * Define a zeroed, on-stack, instance of @type structure with a trailing
   * flexible array member.
   * Use __struct_size(@name) to get compile-time size of it afterwards.
+ * Use __member_size(@name->member) to get compile-time size of @name members.
   */
  #define DEFINE_RAW_FLEX(type, name, member, count)	\
  	_DEFINE_FLEX(type, name, member, count, = {})
@@ -436,6 +437,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
   * Define a zeroed, on-stack, instance of @TYPE structure with a trailing
   * flexible array member.
   * Use __struct_size(@NAME) to get compile-time size of it afterwards.
+ * Use __member_size(@NAME->member) to get compile-time size of @NAME members.
   */
  #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
  	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
author maintainer Applied
#2 lore raw
In reply to Kees Cook
Quoted 5 lines · Kees Cook

Mention the use of __member_size() for DEFINE_FLEX variables as a hint for getting at the compile-time size of the resulting flexible array member.

Applied to for-next/hardening, thanks!

[1/1] overflow: Clarify expectations for getting DEFINE_FLEX variable sizes

      https://git.kernel.org/kees/c/2122b4571745

Take care,

Signature · 1 line
Kees Cook