linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2][next] stddef: Introduce TRAILING_OVERLAP() helper macro
@ 2025-06-17 19:05 Gustavo A. R. Silva
  2025-06-18 21:31 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Gustavo A. R. Silva @ 2025-06-17 19:05 UTC (permalink / raw)
  To: Kees Cook; +Cc: linux-kernel, Gustavo A. R. Silva, linux-hardening

Add new TRAILING_OVERLAP() helper macro to create a union between
a flexible-array member (FAM) and a set of members that would
otherwise follow it. This overlays the trailing members onto the
FAM while preserving the original memory layout.

Co-developed-by: Kees Cook <kees@kernel.org>
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
Changes in v2:
 - Use `__offset_of_##FAM` instead of `__offset_of_fam` for the
   offset variable.
 - Update changelog text.

v1:
 - Link: https://lore.kernel.org/linux-hardening/aFDIBBeYgRSx1ekO@kspp/

 include/linux/stddef.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index 929d67710cc5..dab49e2ec8c0 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -93,4 +93,24 @@ enum {
 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
 	__DECLARE_FLEX_ARRAY(TYPE, NAME)
 
+/**
+ * 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.
+ *
+ * @TYPE: Flexible structure type name, including "struct" keyword.
+ * @NAME: Name for a variable to define.
+ * @FAM: The flexible-array member within @TYPE
+ * @MEMBERS: Trailing overlapping members.
+ */
+#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS)				\
+	union {									\
+		TYPE NAME;							\
+		struct {							\
+			unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)];	\
+			MEMBERS							\
+		};								\
+	}
+
 #endif
-- 
2.43.0


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

* Re: [PATCH v2][next] stddef: Introduce TRAILING_OVERLAP() helper macro
  2025-06-17 19:05 [PATCH v2][next] stddef: Introduce TRAILING_OVERLAP() helper macro Gustavo A. R. Silva
@ 2025-06-18 21:31 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2025-06-18 21:31 UTC (permalink / raw)
  To: Gustavo A. R. Silva; +Cc: Kees Cook, linux-kernel, linux-hardening

On Tue, 17 Jun 2025 13:05:36 -0600, Gustavo A. R. Silva wrote:
> Add new TRAILING_OVERLAP() helper macro to create a union between
> a flexible-array member (FAM) and a set of members that would
> otherwise follow it. This overlays the trailing members onto the
> FAM while preserving the original memory layout.
> 
> 

Applied to for-next/hardening, thanks!

[1/1] stddef: Introduce TRAILING_OVERLAP() helper macro
      https://git.kernel.org/kees/c/29bb79e9dbf1

Take care,

-- 
Kees Cook


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

end of thread, other threads:[~2025-06-18 21:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-17 19:05 [PATCH v2][next] stddef: Introduce TRAILING_OVERLAP() helper macro Gustavo A. R. Silva
2025-06-18 21:31 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).