* [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities
@ 2025-09-17 13:25 Gustavo A. R. Silva
2025-09-17 13:26 ` [PATCH 1/2][next] stddef: Remove token-pasting in TRAILING_OVERLAP() Gustavo A. R. Silva
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-09-17 13:25 UTC (permalink / raw)
To: Kees Cook; +Cc: linux-hardening, linux-kernel, Gustavo A. R. Silva
Small series aimed at expanding TRAILING_OVERLAP() capabilities.
Gustavo A. R. Silva (2):
stddef: Remove token-pasting in TRAILING_OVERLAP()
stddef: Introduce __TRAILING_OVERLAP()
include/linux/stddef.h | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2][next] stddef: Remove token-pasting in TRAILING_OVERLAP()
2025-09-17 13:25 [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities Gustavo A. R. Silva
@ 2025-09-17 13:26 ` Gustavo A. R. Silva
2025-09-17 13:28 ` [PATCH 2/2][next] stddef: Introduce __TRAILING_OVERLAP() Gustavo A. R. Silva
2025-09-17 16:30 ` [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities Kees Cook
2 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-09-17 13:26 UTC (permalink / raw)
To: Kees Cook; +Cc: linux-hardening, linux-kernel, Gustavo A. R. Silva
Currently, TRAILING_OVERLAP() token-pastes the FAM parameter into the
name of internal pdding member `__offset_to_##FAM`. This forces FAM to
be a single identifier, which prevents callers from using a FAM when
it's a nested member. For instance, see the following scenario:
| struct flex {
| size_t count;
| int data[];
| };
| struct foo {
| int hdr_foo;
| struct flex f;
| };
| struct composite {
| struct foo hdr;
| int data[100];
| };
In this case, it'd be useful if TRAILING_OVERLAP() could be used in
the following way:
| struct composite {
| TRAILING_OVERLAP(struct foo, hdr, f.data,
| int data[100];
| );
| };
However, this is not current possible due to the token concatenation
in `__offset_to_##FAM`, which fails when FAM contains a dot.
So, remove token-pasting and use the fixed internal name
`__offset_to_FAM` and, with this, expand the capabilities of
TRAILING_OVERLAP(). :)
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
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 dab49e2ec8c0..701099c67c24 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -108,7 +108,7 @@ enum {
union { \
TYPE NAME; \
struct { \
- unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)]; \
+ unsigned char __offset_to_FAM[offsetof(TYPE, FAM)]; \
MEMBERS \
}; \
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2][next] stddef: Introduce __TRAILING_OVERLAP()
2025-09-17 13:25 [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities Gustavo A. R. Silva
2025-09-17 13:26 ` [PATCH 1/2][next] stddef: Remove token-pasting in TRAILING_OVERLAP() Gustavo A. R. Silva
@ 2025-09-17 13:28 ` Gustavo A. R. Silva
2025-09-17 16:30 ` [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities Kees Cook
2 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-09-17 13:28 UTC (permalink / raw)
To: Kees Cook; +Cc: linux-hardening, linux-kernel, Gustavo A. R. Silva
Introduce underlying __TRAILING_OVERLAP() macro to let callers apply
atributes to trailing overlapping members.
For instance, the code below:
| struct flex {
| size_t count;
| int data[];
| };
| struct {
| struct flex f;
| struct foo a;
| struct boo b;
| } __packed instance;
can now be changed to the following, and preserve the __packed
attribute:
| __TRAILING_OVERLAP(struct flex, f, data, __packed,
| struct foo a;
| struct boo b;
| ) instance;
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
include/linux/stddef.h | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index 701099c67c24..80b6bfb944f0 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -94,7 +94,8 @@ enum {
__DECLARE_FLEX_ARRAY(TYPE, NAME)
/**
- * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
+ * __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.
@@ -102,15 +103,30 @@ enum {
* @TYPE: Flexible structure type name, including "struct" keyword.
* @NAME: Name for a variable to define.
* @FAM: The flexible-array member within @TYPE
+ * @ATTRS: Any struct attributes (usually empty)
* @MEMBERS: Trailing overlapping members.
*/
-#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS) \
+#define __TRAILING_OVERLAP(TYPE, NAME, FAM, ATTRS, MEMBERS) \
union { \
TYPE NAME; \
struct { \
unsigned char __offset_to_FAM[offsetof(TYPE, FAM)]; \
MEMBERS \
- }; \
+ } ATTRS; \
}
+/**
+ * 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) \
+ __TRAILING_OVERLAP(TYPE, NAME, FAM, /* no attrs */, MEMBERS)
+
#endif
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities
2025-09-17 13:25 [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities Gustavo A. R. Silva
2025-09-17 13:26 ` [PATCH 1/2][next] stddef: Remove token-pasting in TRAILING_OVERLAP() Gustavo A. R. Silva
2025-09-17 13:28 ` [PATCH 2/2][next] stddef: Introduce __TRAILING_OVERLAP() Gustavo A. R. Silva
@ 2025-09-17 16:30 ` Kees Cook
2 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2025-09-17 16:30 UTC (permalink / raw)
To: Gustavo A. R. Silva; +Cc: Kees Cook, linux-hardening, linux-kernel
On Wed, 17 Sep 2025 15:25:44 +0200, Gustavo A. R. Silva wrote:
> Small series aimed at expanding TRAILING_OVERLAP() capabilities.
>
> Gustavo A. R. Silva (2):
> stddef: Remove token-pasting in TRAILING_OVERLAP()
> stddef: Introduce __TRAILING_OVERLAP()
>
> include/linux/stddef.h | 24 ++++++++++++++++++++----
> 1 file changed, 20 insertions(+), 4 deletions(-)
>
> [...]
Applied to for-next/hardening, thanks!
[1/2] stddef: Remove token-pasting in TRAILING_OVERLAP()
https://git.kernel.org/kees/c/413187f79062
[2/2] stddef: Introduce __TRAILING_OVERLAP()
https://git.kernel.org/kees/c/2bbdcf02c3f3
Take care,
--
Kees Cook
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-09-17 16:30 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-17 13:25 [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities Gustavo A. R. Silva
2025-09-17 13:26 ` [PATCH 1/2][next] stddef: Remove token-pasting in TRAILING_OVERLAP() Gustavo A. R. Silva
2025-09-17 13:28 ` [PATCH 2/2][next] stddef: Introduce __TRAILING_OVERLAP() Gustavo A. R. Silva
2025-09-17 16:30 ` [PATCH 0/2][next] Expand TRAILING_OVERLAP() capabilities Kees Cook
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.