All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][next] stddef: Introduce struct_group_tagged_attr() helper macro
@ 2024-03-18 23:00 Gustavo A. R. Silva
  2024-03-18 23:53 ` Kees Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Gustavo A. R. Silva @ 2024-03-18 23:00 UTC (permalink / raw)
  To: Kees Cook; +Cc: linux-kernel, Gustavo A. R. Silva, linux-hardening

We need a new `struct_group()` helper that allows for both having the
struct be tagged, and specifying struct attributes like `__packed`
or `__align(x)`.

This new helper will initially be used to address
-Wflex-array-member-not-at-end warnings, where a tagged struct is used
to separate the flexible-array member from the rest of the members in
the flexible structure [1]. There are some scenarios in which those
members need to be packed, as well.

So, `struct_group_tagged_attr()` is introduced for this.

Link: https://lore.kernel.org/linux-hardening/ZeIgeZ5Sb0IZTOyt@neat/ [1]
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 include/linux/stddef.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index 929d67710cc5..919df9453257 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -80,6 +80,26 @@ enum {
 #define struct_group_tagged(TAG, NAME, MEMBERS...) \
 	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
 
+/**
+ * struct_group_tagged_attr() - Create a struct_group with a reusable
+ * tag and trailing attributes.
+ *
+ * @TAG: The tag name for the named sub-struct
+ * @NAME: The identifier name of the mirrored sub-struct
+ * @ATTRS: Any struct attributes to apply
+ * @MEMBERS: The member declarations for the mirrored structs
+ *
+ * Used to create an anonymous union of two structs with identical
+ * layout and size: one anonymous and one named. The former can be
+ * used normally without sub-struct naming, and the latter can be
+ * used to reason about the start, end, and size of the group of
+ * struct members. Includes struct tag argument for the named copy,
+ * so the specified layout can be reused later. Also includes
+ * structure attributes argument.
+ */
+#define struct_group_tagged_attr(TAG, NAME, ATTRS, MEMBERS...) \
+	__struct_group(TAG, NAME, ATTRS, MEMBERS)
+
 /**
  * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
  *
-- 
2.34.1


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

* Re: [PATCH][next] stddef: Introduce struct_group_tagged_attr() helper macro
  2024-03-18 23:00 [PATCH][next] stddef: Introduce struct_group_tagged_attr() helper macro Gustavo A. R. Silva
@ 2024-03-18 23:53 ` Kees Cook
  2024-03-18 23:59   ` Gustavo A. R. Silva
  0 siblings, 1 reply; 3+ messages in thread
From: Kees Cook @ 2024-03-18 23:53 UTC (permalink / raw)
  To: Gustavo A. R. Silva; +Cc: linux-kernel, linux-hardening

On Mon, Mar 18, 2024 at 05:00:33PM -0600, Gustavo A. R. Silva wrote:
> We need a new `struct_group()` helper that allows for both having the
> struct be tagged, and specifying struct attributes like `__packed`
> or `__align(x)`.
> 
> This new helper will initially be used to address
> -Wflex-array-member-not-at-end warnings, where a tagged struct is used
> to separate the flexible-array member from the rest of the members in
> the flexible structure [1]. There are some scenarios in which those
> members need to be packed, as well.
> 
> So, `struct_group_tagged_attr()` is introduced for this.
> 
> Link: https://lore.kernel.org/linux-hardening/ZeIgeZ5Sb0IZTOyt@neat/ [1]
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  include/linux/stddef.h | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/include/linux/stddef.h b/include/linux/stddef.h
> index 929d67710cc5..919df9453257 100644
> --- a/include/linux/stddef.h
> +++ b/include/linux/stddef.h
> @@ -80,6 +80,26 @@ enum {
>  #define struct_group_tagged(TAG, NAME, MEMBERS...) \
>  	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
>  
> +/**
> + * struct_group_tagged_attr() - Create a struct_group with a reusable
> + * tag and trailing attributes.
> + *
> + * @TAG: The tag name for the named sub-struct
> + * @NAME: The identifier name of the mirrored sub-struct
> + * @ATTRS: Any struct attributes to apply
> + * @MEMBERS: The member declarations for the mirrored structs
> + *
> + * Used to create an anonymous union of two structs with identical
> + * layout and size: one anonymous and one named. The former can be
> + * used normally without sub-struct naming, and the latter can be
> + * used to reason about the start, end, and size of the group of
> + * struct members. Includes struct tag argument for the named copy,
> + * so the specified layout can be reused later. Also includes
> + * structure attributes argument.
> + */
> +#define struct_group_tagged_attr(TAG, NAME, ATTRS, MEMBERS...) \
> +	__struct_group(TAG, NAME, ATTRS, MEMBERS)

This is the same as __struct_group() only with a longer name? Why not
just use __struct_group() directly?

-- 
Kees Cook

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

* Re: [PATCH][next] stddef: Introduce struct_group_tagged_attr() helper macro
  2024-03-18 23:53 ` Kees Cook
@ 2024-03-18 23:59   ` Gustavo A. R. Silva
  0 siblings, 0 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2024-03-18 23:59 UTC (permalink / raw)
  To: Kees Cook, Gustavo A. R. Silva; +Cc: linux-kernel, linux-hardening



On 3/18/24 17:53, Kees Cook wrote:
> On Mon, Mar 18, 2024 at 05:00:33PM -0600, Gustavo A. R. Silva wrote:
>> We need a new `struct_group()` helper that allows for both having the
>> struct be tagged, and specifying struct attributes like `__packed`
>> or `__align(x)`.
>>
>> This new helper will initially be used to address
>> -Wflex-array-member-not-at-end warnings, where a tagged struct is used
>> to separate the flexible-array member from the rest of the members in
>> the flexible structure [1]. There are some scenarios in which those
>> members need to be packed, as well.
>>
>> So, `struct_group_tagged_attr()` is introduced for this.
>>
>> Link: https://lore.kernel.org/linux-hardening/ZeIgeZ5Sb0IZTOyt@neat/ [1]
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>> ---
>>   include/linux/stddef.h | 20 ++++++++++++++++++++
>>   1 file changed, 20 insertions(+)
>>
>> diff --git a/include/linux/stddef.h b/include/linux/stddef.h
>> index 929d67710cc5..919df9453257 100644
>> --- a/include/linux/stddef.h
>> +++ b/include/linux/stddef.h
>> @@ -80,6 +80,26 @@ enum {
>>   #define struct_group_tagged(TAG, NAME, MEMBERS...) \
>>   	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
>>   
>> +/**
>> + * struct_group_tagged_attr() - Create a struct_group with a reusable
>> + * tag and trailing attributes.
>> + *
>> + * @TAG: The tag name for the named sub-struct
>> + * @NAME: The identifier name of the mirrored sub-struct
>> + * @ATTRS: Any struct attributes to apply
>> + * @MEMBERS: The member declarations for the mirrored structs
>> + *
>> + * Used to create an anonymous union of two structs with identical
>> + * layout and size: one anonymous and one named. The former can be
>> + * used normally without sub-struct naming, and the latter can be
>> + * used to reason about the start, end, and size of the group of
>> + * struct members. Includes struct tag argument for the named copy,
>> + * so the specified layout can be reused later. Also includes
>> + * structure attributes argument.
>> + */
>> +#define struct_group_tagged_attr(TAG, NAME, ATTRS, MEMBERS...) \
>> +	__struct_group(TAG, NAME, ATTRS, MEMBERS)
> 
> This is the same as __struct_group() only with a longer name? Why not
> just use __struct_group() directly?
> 

Mmmh, the rest of the helpers in the struct_group() family fall in this
same category, can we use __struct_group() indistinctly?

--
Gustavo

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

end of thread, other threads:[~2024-03-18 23:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-18 23:00 [PATCH][next] stddef: Introduce struct_group_tagged_attr() helper macro Gustavo A. R. Silva
2024-03-18 23:53 ` Kees Cook
2024-03-18 23:59   ` 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.