All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs
@ 2025-09-12 10:28 Uwe Kleine-König
  2025-09-12 10:28 ` [PATCH 1/2] stddef: Add a comment about why TRAILING_OVERLAP() exists Uwe Kleine-König
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2025-09-12 10:28 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Kees Cook; +Cc: linux-kernel

Hello,

as a follup to the discussion I had with Gustavo in reply to
https://lore.kernel.org/linux-pwm/aJtRPZpc-Lv-C6zD@kspp here comes my
suggestion to improve TRAILING_OVERLAP() for wider audience.

While working at it, I wonder if __packed should also better be part of the
macro to ensure that


	struct myfamstruct {
		char c;
		char payload[];
	};

	TRAILING_OVERLAP(struct myfamstruct, mfs, payload,
		unsigned int payload_as_int;
	) fs;

does the right thing. This however is orthogonal and better discussed
separately from this patch set.

Best regards
Uwe

Uwe Kleine-König (2):
  stddef: Add a comment about why TRAILING_OVERLAP() exists
  stddef: Stop using FAM parameter of TRAILING_OVERLAP()

 include/linux/stddef.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)


base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
-- 
2.50.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] stddef: Add a comment about why TRAILING_OVERLAP() exists
  2025-09-12 10:28 [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs Uwe Kleine-König
@ 2025-09-12 10:28 ` Uwe Kleine-König
  2025-09-12 10:28 ` [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP() Uwe Kleine-König
  2025-09-12 10:51 ` [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs Gustavo A. R. Silva
  2 siblings, 0 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2025-09-12 10:28 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Kees Cook; +Cc: linux-kernel

Just from reading the definition it's not obvious what the macro does
and why it's needed and sensible to be used.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 include/linux/stddef.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index dab49e2ec8c0..a748efcd626f 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -97,7 +97,17 @@ enum {
  * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
  *
  * Creates a union between a flexible-array member (FAM) in a struct and a set
- * of additional members that would otherwise follow it.
+ * of additional members that would otherwise follow it. This is needed because
+ * the traditional
+ *
+ * 	struct {
+ * 		struct some_struct_with_FAM mystruct;
+ * 		struct some_other_struct fill_FAM;
+ * 	}
+ *
+ * with the purpose that fill_FAM overlaps the flexible-array in mystruct
+ * triggers a compiler warning about the flexible array member not being at the
+ * end of the structure.
  *
  * @TYPE: Flexible structure type name, including "struct" keyword.
  * @NAME: Name for a variable to define.
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP()
  2025-09-12 10:28 [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs Uwe Kleine-König
  2025-09-12 10:28 ` [PATCH 1/2] stddef: Add a comment about why TRAILING_OVERLAP() exists Uwe Kleine-König
@ 2025-09-12 10:28 ` Uwe Kleine-König
  2025-09-12 11:00   ` Gustavo A. R. Silva
  2025-09-12 10:51 ` [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs Gustavo A. R. Silva
  2 siblings, 1 reply; 5+ messages in thread
From: Uwe Kleine-König @ 2025-09-12 10:28 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Kees Cook; +Cc: linux-kernel

According to my understanding of flexible array members and the c99 docs
("In most situations, the flexible array member is ignored. In
particular, the size of the structure is as if the flexible array member
were omitted except that it may have more trailing padding than the
omission would imply.") sizeof(TYPE) yields the same value as
offsetof(TYPE, FAM). To make FAM unused rename the padding struct member
to use NAME instead of FAM. This makes the macro easier to use and thus
less prone to error.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
--
Hello,

This allows in a followup change to drop the parameter. As this has to
happen in sync with all users of the macro, I delay this change until
after this patch was accepted and hits Linus' tree and then coordinate a
tree-wide change with him before the next -rc1.

Best regards
Uwe
---
 include/linux/stddef.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index a748efcd626f..305e25a67271 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -118,7 +118,7 @@ enum {
 	union {									\
 		TYPE NAME;							\
 		struct {							\
-			unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)];	\
+			unsigned char __offset_to_end_of_##NAME[sizeof(TYPE)];	\
 			MEMBERS							\
 		};								\
 	}
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs
  2025-09-12 10:28 [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs Uwe Kleine-König
  2025-09-12 10:28 ` [PATCH 1/2] stddef: Add a comment about why TRAILING_OVERLAP() exists Uwe Kleine-König
  2025-09-12 10:28 ` [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP() Uwe Kleine-König
@ 2025-09-12 10:51 ` Gustavo A. R. Silva
  2 siblings, 0 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2025-09-12 10:51 UTC (permalink / raw)
  To: Uwe Kleine-König, Gustavo A. R. Silva, Kees Cook; +Cc: linux-kernel



On 9/12/25 12:28, Uwe Kleine-König wrote:
> Hello,
> 
> as a follup to the discussion I had with Gustavo in reply to
> https://lore.kernel.org/linux-pwm/aJtRPZpc-Lv-C6zD@kspp here comes my
> suggestion to improve TRAILING_OVERLAP() for wider audience.
> 
> While working at it, I wonder if __packed should also better be part of the
> macro to ensure that

I have patch ready for this:

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index dab49e2ec8c0..b20ff76778d5 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -93,6 +93,14 @@ enum {
  #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
         __DECLARE_FLEX_ARRAY(TYPE, NAME)

+#define __TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS, ATTRS)                            \
+       union {                                                                 \
+               TYPE NAME;                                                      \
+               struct {                                                        \
+                       unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)];   \
+                       MEMBERS                                                 \
+               } ATTRS;                                                                \
+       }
  /**
   * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
   *

Thanks
-Gustavo



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP()
  2025-09-12 10:28 ` [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP() Uwe Kleine-König
@ 2025-09-12 11:00   ` Gustavo A. R. Silva
  0 siblings, 0 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2025-09-12 11:00 UTC (permalink / raw)
  To: Uwe Kleine-König, Gustavo A. R. Silva, Kees Cook; +Cc: linux-kernel



On 9/12/25 12:28, Uwe Kleine-König wrote:
> According to my understanding of flexible array members and the c99 docs
> ("In most situations, the flexible array member is ignored. In
> particular, the size of the structure is as if the flexible array member
> were omitted except that it may have more trailing padding than the
> omission would imply.") sizeof(TYPE) yields the same value as
> offsetof(TYPE, FAM). To make FAM unused rename the padding struct member

No, as I commented here[1]:

"Flexible structures (structs that contain a FAM) may have trailing padding.
Under that scenario sizeof(TYPE) causes the overlay between FAM and MEMBERS
to be misaligned.

On the other hand, offsetof(TYPE, FAM) precisely positions the trailing
MEMBERS where the FAM begins, which is correct and safe."

sizeof(TYPE) does not always equal to offsetof(TYPE, FAM).

See this:

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

Thanks
-Gustavo

[1] https://lore.kernel.org/linux-hardening/4b9eea66-f004-4b5f-bf48-4c32205cc8ee@embeddedor.com/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-09-12 11:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-12 10:28 [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs Uwe Kleine-König
2025-09-12 10:28 ` [PATCH 1/2] stddef: Add a comment about why TRAILING_OVERLAP() exists Uwe Kleine-König
2025-09-12 10:28 ` [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP() Uwe Kleine-König
2025-09-12 11:00   ` Gustavo A. R. Silva
2025-09-12 10:51 ` [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs Gustavo A. R. Silva

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.