public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] overflow.h: Add flex_array_size() helper
@ 2020-06-08 22:17 Gustavo A. R. Silva
  2020-06-08 22:34 ` Joe Perches
  0 siblings, 1 reply; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-08 22:17 UTC (permalink / raw)
  To: Kees Cook; +Cc: linux-kernel, Gustavo A. R. Silva

Add flex_array_size() helper for the calculation of the size, in bytes,
of a flexible array member contained within an enclosing structure.

Example of usage:

struct something {
	size_t count;
	struct foo items[];
};

struct something *instance;

instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL);
instance->count = count;

memcpy(instance->items, source, flex_array_size(instance, items, instance->count));

The helper returns SIZE_MAX on overflow instead of wrapping around.

(Additionally replace parameter n with count in struct_size() for
homogeneity).

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 include/linux/overflow.h | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 659045046468f..473af79f11ccf 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -304,16 +304,33 @@ static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
  * struct_size() - Calculate size of structure with trailing array.
  * @p: Pointer to the structure.
  * @member: Name of the array member.
- * @n: Number of elements in the array.
+ * @count: Number of elements in the array.
  *
  * Calculates size of memory needed for structure @p followed by an
- * array of @n @member elements.
+ * array of @count @member elements.
  *
  * Return: number of bytes needed or SIZE_MAX on overflow.
  */
-#define struct_size(p, member, n)					\
-	__ab_c_size(n,							\
+#define struct_size(p, member, count)					\
+	__ab_c_size(count,							\
 		    sizeof(*(p)->member) + __must_be_array((p)->member),\
 		    sizeof(*(p)))
 
+/**
+ * flex_array_size() - Calculate size of a flexible array member within
+ * an enclosing structure.
+ *
+ * @p: Pointer to the structure.
+ * @member: Name of the flexible array member.
+ * @count: Number of elements in the array.
+ *
+ * Calculates size of memory needed for flexible array @member of @count
+ * elements within structure @p.
+ *
+ * Return: number of bytes needed or SIZE_MAX on overflow.
+ */
+#define flex_array_size(p, member, count)					\
+	array_size(count,							\
+		    sizeof((p)->member[0]) + __must_be_array((p)->member))
+
 #endif /* __LINUX_OVERFLOW_H */
-- 
2.27.0


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

* Re: [PATCH] overflow.h: Add flex_array_size() helper
  2020-06-08 22:17 [PATCH] overflow.h: Add flex_array_size() helper Gustavo A. R. Silva
@ 2020-06-08 22:34 ` Joe Perches
  2020-06-09  0:47   ` Gustavo A. R. Silva
  2020-06-09 15:26   ` Kees Cook
  0 siblings, 2 replies; 4+ messages in thread
From: Joe Perches @ 2020-06-08 22:34 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Kees Cook; +Cc: linux-kernel, Gustavo A. R. Silva

On Mon, 2020-06-08 at 17:17 -0500, Gustavo A. R. Silva wrote:
> Add flex_array_size() helper for the calculation of the size, in bytes,
> of a flexible array member contained within an enclosing structure.
> 
> Example of usage:
> foo
> struct something {
> 	size_t count;
> 	struct foo items[];
> };
> 
> struct something *instance;
[]
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
[]
> +/**
> + * flex_array_size() - Calculate size of a flexible array member within
> + * an enclosing structure.

These comment descriptions do not match

> + * Calculates size of memory needed for flexible array @member of @count
> + * elements within structure @p.

The first comment shows the size of an array member.
The second shows the size of an array member * count

Also the struct_size and flex_array_size definitions
are using two different forms:

		sizeof(*(p)->member) + __must_be_array((p)->member),\
and
		sizeof((p)->member[0]) + __must_be_array((p)->member))

Consistency would be nice.



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

* Re: [PATCH] overflow.h: Add flex_array_size() helper
  2020-06-08 22:34 ` Joe Perches
@ 2020-06-09  0:47   ` Gustavo A. R. Silva
  2020-06-09 15:26   ` Kees Cook
  1 sibling, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-09  0:47 UTC (permalink / raw)
  To: Joe Perches; +Cc: Kees Cook, linux-kernel, Gustavo A. R. Silva

On Mon, Jun 08, 2020 at 03:34:52PM -0700, Joe Perches wrote:
> On Mon, 2020-06-08 at 17:17 -0500, Gustavo A. R. Silva wrote:
> > Add flex_array_size() helper for the calculation of the size, in bytes,
> > of a flexible array member contained within an enclosing structure.
> > 
> > Example of usage:
> > foo
> > struct something {
> > 	size_t count;
> > 	struct foo items[];
> > };
> > 
> > struct something *instance;
> []
> > diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> []
> > +/**
> > + * flex_array_size() - Calculate size of a flexible array member within
> > + * an enclosing structure.
> 
> These comment descriptions do not match
> 
> > + * Calculates size of memory needed for flexible array @member of @count
> > + * elements within structure @p.
> 
> The first comment shows the size of an array member.
> The second shows the size of an array member * count
> 
> Also the struct_size and flex_array_size definitions
> are using two different forms:
> 
> 		sizeof(*(p)->member) + __must_be_array((p)->member),\
> and
> 		sizeof((p)->member[0]) + __must_be_array((p)->member))
> 

Good catch.

Thanks
--
Gustavo

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

* Re: [PATCH] overflow.h: Add flex_array_size() helper
  2020-06-08 22:34 ` Joe Perches
  2020-06-09  0:47   ` Gustavo A. R. Silva
@ 2020-06-09 15:26   ` Kees Cook
  1 sibling, 0 replies; 4+ messages in thread
From: Kees Cook @ 2020-06-09 15:26 UTC (permalink / raw)
  To: Joe Perches; +Cc: Gustavo A. R. Silva, linux-kernel, Gustavo A. R. Silva

On Mon, Jun 08, 2020 at 03:34:52PM -0700, Joe Perches wrote:
> On Mon, 2020-06-08 at 17:17 -0500, Gustavo A. R. Silva wrote:
> > Add flex_array_size() helper for the calculation of the size, in bytes,
> > of a flexible array member contained within an enclosing structure.
> > 
> > Example of usage:
> > foo
> > struct something {
> > 	size_t count;
> > 	struct foo items[];
> > };
> > 
> > struct something *instance;
> []
> > diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> []
> > +/**
> > + * flex_array_size() - Calculate size of a flexible array member within
> > + * an enclosing structure.
> 
> These comment descriptions do not match
> 
> > + * Calculates size of memory needed for flexible array @member of @count
> > + * elements within structure @p.
>
> The first comment shows the size of an array member.

Ah, I see what you mean. I was parsing it as "flexible-array structure member"
but it does say "a" and "array member". Perhaps:

Calculate size needed for entire flexible array contents within an
enclosing structure.

?

-- 
Kees Cook

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

end of thread, other threads:[~2020-06-09 15:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-08 22:17 [PATCH] overflow.h: Add flex_array_size() helper Gustavo A. R. Silva
2020-06-08 22:34 ` Joe Perches
2020-06-09  0:47   ` Gustavo A. R. Silva
2020-06-09 15:26   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox