Skip to contentSkip to conversation

Add STACK_FLEX_ARRAY_SIZE() helper

PATCH Superseded Gustavo A. R. Silva 3 participants 6 messages Apr 22, 2025 Kernel hardening (not covered by other areas)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gustavo A. R. Silva

author reviewer
#0 lore raw

cover Add STACK_FLEX_ARRAY_SIZE() helper

Add new STACK_FLEX_ARRAY_SIZE() helper to get the size of a flexible-array member defined using DEFINE_FLEX()/DEFINE_RAW_FLEX() at compile time.

This is essentially the same as ARRAY_SIZE() but for on-stack flexible-array members.

Gustavo A. R. Silva (2):
overflow: Add STACK_FLEX_ARRAY_SIZE() helper kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper

 include/linux/overflow.h   | 13 +++++++++++++
 lib/tests/overflow_kunit.c |  3 +++
 2 files changed, 16 insertions(+)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH 1/2 overflow: Add STACK_FLEX_ARRAY_SIZE() helper

Add new STACK_FLEX_ARRAY_SIZE() helper to get the size of a flexible-array member defined using DEFINE_FLEX()/DEFINE_RAW_FLEX() at compile time.

This is essentially the same as ARRAY_SIZE() but for on-stack flexible-array members.

---
 include/linux/overflow.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/overflow.h +13 -0
index 6ee67c20b575..5b2e0cc9aba2 100644
@@ -420,6 +420,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * 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.
+ * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member.
  */
 #define DEFINE_RAW_FLEX(type, name, member, count)	\
 	_DEFINE_FLEX(type, name, member, count, = {})
@@ -438,8 +439,20 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * 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.
+ * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member.
  */
 #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
 	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
 
+/**
+ * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.
+ * Returns the number of elements in @array.
+ *
+ * @name: Name for a variable defined in DEFINE_RAW_FLEX()/DEFINE_FLEX().
+ * @array: Name of the array member.
+ */
+#define STACK_FLEX_ARRAY_SIZE(name, array)						\
+	(__member_size((name)->array) / sizeof(*(name)->array) +			\
+						__must_be_array((name)->array))
+
 #endif /* __LINUX_OVERFLOW_H */
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 1/2][next] overflow: Add STACK_FLEX_ARRAY_SIZE() helper

Quoted 17 lines · Gustavo A. R. Silva

Add new STACK_FLEX_ARRAY_SIZE() helper to get the size of a flexible-array member defined using DEFINE_FLEX()/DEFINE_RAW_FLEX() at compile time.

This is essentially the same as ARRAY_SIZE() but for on-stack flexible-array members.

---
 include/linux/overflow.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/overflow.h +0 -0
index 6ee67c20b575..5b2e0cc9aba2 100644

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

Quoted 4 lines
include/linux/overflow.h +1 -0
@@ -420,6 +420,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * 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.
+ * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member.

Instead of "size of" how about "number of elements in"

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

Quoted 8 lines
include/linux/overflow.h +1 -0
  */
 #define DEFINE_RAW_FLEX(type, name, member, count)	\
 	_DEFINE_FLEX(type, name, member, count, = {})
@@ -438,8 +439,20 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * 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.
+ * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member.

Same here.

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

Quoted 7 lines
include/linux/overflow.h +3 -0
  */
 #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
 	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
 
+/**
+ * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.
+ * Returns the number of elements in @array.

Which matches these docs.

Quoted 12 lines
include/linux/overflow.h +8 -0
+ *
+ * @name: Name for a variable defined in DEFINE_RAW_FLEX()/DEFINE_FLEX().
+ * @array: Name of the array member.
+ */
+#define STACK_FLEX_ARRAY_SIZE(name, array)						\
+	(__member_size((name)->array) / sizeof(*(name)->array) +			\
+						__must_be_array((name)->array))
+
 #endif /* __LINUX_OVERFLOW_H */
Signature · 1 line
2.43.0
Signature · 1 line
Kees Cook
author reviewer
#3 lore raw

PATCH 2/2 kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper

In reply to Gustavo A. R. Silva

Add a couple of tests for new STACK_FLEX_ARRAY_SIZE() helper.

---
 lib/tests/overflow_kunit.c | 3 +++
 1 file changed, 3 insertions(+)
lib/tests/overflow_kunit.c +3 -0
index 894691b4411a..3beb497a44be 100644
@@ -1210,6 +1210,9 @@ static void DEFINE_FLEX_test(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, __struct_size(empty->array), 0);
 	KUNIT_EXPECT_EQ(test, __member_size(empty->array), 0);
 
+	KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(two, array), 2);
+	KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(eight, array), 8);
+
 	/* If __counted_by is not being used, array size will have the on-stack size. */
 	if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY))
 		array_size_override = 2 * sizeof(s16);
Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH 2/2][next] kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper

Quoted 12 lines · Gustavo A. R. Silva

Add a couple of tests for new STACK_FLEX_ARRAY_SIZE() helper.

---
 lib/tests/overflow_kunit.c | 3 +++
 1 file changed, 3 insertions(+)
lib/tests/overflow_kunit.c +0 -0
index 894691b4411a..3beb497a44be 100644

lib/tests/overflow_kunit.c static void DEFINE_FLEX_test(struct kunit *test) · line 1214

Quoted 5 lines
lib/tests/overflow_kunit.c +2 -0
@@ -1210,6 +1210,9 @@ static void DEFINE_FLEX_test(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, __struct_size(empty->array), 0);
 	KUNIT_EXPECT_EQ(test, __member_size(empty->array), 0);
 
+	KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(two, array), 2);
+	KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(eight, array), 8);

Nice! Can you add a 0 test for "empty" as well?

Quoted 7 lines
lib/tests/overflow_kunit.c +1 -0
+
 	/* If __counted_by is not being used, array size will have the on-stack size. */
 	if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY))
 		array_size_override = 2 * sizeof(s16);
Signature · 1 line
2.43.0
Signature · 1 line
Kees Cook
#5 lore raw

Re: [PATCH 2/2][next] kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper

Quoted 16 lines · Kees Cook
Quoted 15 lines · Gustavo A. R. Silva

Add a couple of tests for new STACK_FLEX_ARRAY_SIZE() helper.

---
  lib/tests/overflow_kunit.c | 3 +++
  1 file changed, 3 insertions(+)
lib/tests/overflow_kunit.c +0 -0
index 894691b4411a..3beb497a44be 100644
@@ -1210,6 +1210,9 @@ static void DEFINE_FLEX_test(struct kunit *test)
  	KUNIT_EXPECT_EQ(test, __struct_size(empty->array), 0);
  	KUNIT_EXPECT_EQ(test, __member_size(empty->array), 0);
 
Quoted 4 lines

lib/tests/overflow_kunit.c static void DEFINE_FLEX_test(struct kunit *test) · line 1214

Quoted 2 lines
lib/tests/overflow_kunit.c +2 -0
+	KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(two, array), 2);
+	KUNIT_EXPECT_EQ(test, STACK_FLEX_ARRAY_SIZE(eight, array), 8);

Nice! Can you add a 0 test for "empty" as well?

Sure thing. I had actually added it, but for some reason I removed at the last minute.

-Gustavo