Skip to contentSkip to conversation

overflow.h: Rename __ab_c_size() to __calc_size()

PATCH In review Borislav Petkov 5 participants 11 messages Apr 5, 2019 – Apr 12, 2019 Kernel hardening (not covered by other areas)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 overflow.h: Rename __ab_c_size() to __calc_size()

From: Borislav Petkov <[email protected]>

... to make its name readable to humans so that it can denote what that helper does.

No functional changes.

Cc Kees Cook <[email protected]>
Cc Matthew Wilcox <[email protected]>
---
 include/linux/overflow.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/overflow.h +2 -2
index 40b48e2133cb..a9cb77d54df0 100644
@@ -278,7 +278,7 @@ static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
 	return bytes;
 }
 
-static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
+static inline __must_check size_t __calc_size(size_t n, size_t size, size_t c)
 {
 	size_t bytes;
 
@@ -302,7 +302,7 @@ static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
  * Return: number of bytes needed or SIZE_MAX on overflow.
  */
 #define struct_size(p, member, n)					\
-	__ab_c_size(n,							\
+	__calc_size(n,							\
 		    sizeof(*(p)->member) + __must_be_array((p)->member),\
 		    sizeof(*(p)))
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 3 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

... to make its name readable to humans so that it can denote what that helper does.

I like the current color. It computes a*b+c with overflow checking at each step. calc_size is way too generic and doesn't say anything at all about how the calc(ulation) is done.

Rasmus

#2 lore raw
Quoted 1 line · Rasmus Villemoes

I like the current color.

Color?

Quoted 3 lines

It computes a*b+c with overflow checking at each step. calc_size is way too generic and doesn't say anything at all about how the calc(ulation) is done.

Ok, whatever.

Then it would need at least a comment above it to state what it does. I don't want to go and parse the macros each time.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#3 lore raw
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Rasmus Villemoes

It computes a*b+c with overflow checking at each step. calc_size is way too generic and doesn't say anything at all about how the calc(ulation) is done.

Ok, whatever.

Then it would need at least a comment above it to state what it does. I don't want to go and parse the macros each time.

It's an internal helper, and struct_size is fully kernel-doc'ed. But yeah, a comment wouldn't hurt, and let's rename the parameters so they match the abc naming.

include/linux/overflow.h +0 -0
index 40b48e2133cb..6534a727cadb 100644
@@ -278,11 +278,15 @@ static inline __must_check size_t

array3_size(size_t a, size_t b, size_t c)

        return bytes;

}

-static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
+/*
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
+ * struct_size() below.
+ */
+static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)

 {
        size_t bytes;

- if (check_mul_overflow(n, size, &bytes))
+ if (check_mul_overflow(a, b, &bytes))

                return SIZE_MAX;
        if (check_add_overflow(bytes, c, &bytes))
                return SIZE_MAX;

Rasmus

author Acked-by
#4 lore raw
Quoted 38 lines · Rasmus Villemoes
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Rasmus Villemoes

It computes a*b+c with overflow checking at each step. calc_size is way too generic and doesn't say anything at all about how the calc(ulation) is done.

Ok, whatever.

Then it would need at least a comment above it to state what it does. I don't want to go and parse the macros each time.

It's an internal helper, and struct_size is fully kernel-doc'ed. But yeah, a comment wouldn't hurt, and let's rename the parameters so they match the abc naming.

include/linux/overflow.h +6 -2
index 40b48e2133cb..6534a727cadb 100644
@@ -278,11 +278,15 @@ static inline __must_check size_t
 array3_size(size_t a, size_t b, size_t c)
        return bytes;
 }
 
-static inline __must_check size_t __ab_c_size(size_t n, size_t size,
 size_t c)
+/*
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
+ * struct_size() below.
+ */
+static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
 {
        size_t bytes;
 
-       if (check_mul_overflow(n, size, &bytes))
+       if (check_mul_overflow(a, b, &bytes))
                return SIZE_MAX;
Quoted 4 lines

if (check_add_overflow(bytes, c, &bytes))
return SIZE_MAX;

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
Acked-by
#5 lore raw
Quoted 27 lines · Borislav Petkov
Quoted 25 lines · Rasmus Villemoes
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Rasmus Villemoes

It computes a*b+c with overflow checking at each step. calc_size is way too generic and doesn't say anything at all about how the calc(ulation) is done.

Ok, whatever.

Then it would need at least a comment above it to state what it does. I don't want to go and parse the macros each time.

It's an internal helper, and struct_size is fully kernel-doc'ed. But yeah, a comment wouldn't hurt, and let's rename the parameters so they match the abc naming.

include/linux/overflow.h +0 -0
index 40b48e2133cb..6534a727cadb 100644
@@ -278,11 +278,15 @@ static inline __must_check size_t
 array3_size(size_t a, size_t b, size_t c)
        return bytes;
 }
 
Quoted 6 lines

include/linux/overflow.h static inline __must_check size_t · line 286

Quoted 6 lines
include/linux/overflow.h +4 -1
-static inline __must_check size_t __ab_c_size(size_t n, size_t size,
 size_t c)
+/*
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
+ * struct_size() below.
+ */

May as well make this kern-doc too?

Quoted 12 lines
Quoted 11 lines
include/linux/overflow.h +2 -1
+static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
 {
        size_t bytes;
 
-       if (check_mul_overflow(n, size, &bytes))
+       if (check_mul_overflow(a, b, &bytes))
                return SIZE_MAX;

if (check_add_overflow(bytes, c, &bytes))
return SIZE_MAX;

Quoted 2 lines
Signature · 1 line
Kees Cook
#6 lore raw
Quoted 28 lines · Kees Cook
Quoted 27 lines · Borislav Petkov
Quoted 25 lines · Rasmus Villemoes
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Rasmus Villemoes

It computes a*b+c with overflow checking at each step. calc_size is way too generic and doesn't say anything at all about how the calc(ulation) is done.

Ok, whatever.

Then it would need at least a comment above it to state what it does. I don't want to go and parse the macros each time.

It's an internal helper, and struct_size is fully kernel-doc'ed. But yeah, a comment wouldn't hurt, and let's rename the parameters so they match the abc naming.

include/linux/overflow.h +0 -0
index 40b48e2133cb..6534a727cadb 100644
@@ -278,11 +278,15 @@ static inline __must_check size_t
 array3_size(size_t a, size_t b, size_t c)
        return bytes;
 }
 
Quoted 8 lines
Quoted 6 lines

include/linux/overflow.h static inline __must_check size_t · line 286

Quoted 6 lines
include/linux/overflow.h +4 -1
-static inline __must_check size_t __ab_c_size(size_t n, size_t size,
 size_t c)
+/*
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
+ * struct_size() below.
+ */

May as well make this kern-doc too?

I don't think that's a good idea; we should only document functions we want other people to use.

Quoted 1 line
Quoted 1 line

include/linux/overflow.h static inline __must_check size_t · line 287

Quoted 1 line
include/linux/overflow.h +1 -0
+static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)

I could also go for renaming this to __size_ab_plus_c.

For a bit of history ... the origins of this was a system which had alloc_ab (a * b)
alloc_abc (a * b * c)
alloc_ab_c (a * b + c)
alloc_ab_cd (a * b + c * d)

In the process of getting it upstreamed, it changed from kmalloc_ab_c to kmalloc(struct_size(...)) and we never bothered to change the name of __ab_c_size().

#7 lore raw
Quoted 2 lines · Matthew Wilcox

I don't think that's a good idea; we should only document functions we want other people to use.

Yap.

Quoted 1 line

I could also go for renaming this to __size_ab_plus_c.

Let's just leave it short as it is now - the comment should be good enough.

Btw, is anyone picking this up or should I?

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#8 lore raw
Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Matthew Wilcox

I don't think that's a good idea; we should only document functions we want other people to use.

Yap.

Exactly, not kernel-doc'ing was a deliberate choice.

Quoted 6 lines
Quoted 1 line

I could also go for renaming this to __size_ab_plus_c.

Let's just leave it short as it is now - the comment should be good enough.

Btw, is anyone picking this up or should I?

I'm fine with you picking it up. Here's a real changelog, feel free to edit.

overflow.h: add comment documenting __ab_c_size()

__ab_c_size() is a somewhat opaque name. Document its purpose, and while at it, rename the parameters to actually match the abc naming.

Rasmus

#9 lore raw

[tip:core/core] overflow.h: Add comment documenting __ab_c_size()

In reply to Borislav Petkov
overflow.h: Add comment documenting __ab_c_size()
Commit-ID:  899cbdfa8d147c873fe4e66c38d2cca3c1ac6286
Gitweb:     https://git.kernel.org/tip/899cbdfa8d147c873fe4e66c38d2cca3c1ac6286
Author:     Rasmus Villemoes <[email protected]>
AuthorDate: Wed, 10 Apr 2019 22:27:25 +0200
Committer:  Borislav Petkov <[email protected]>
CommitDate: Wed, 10 Apr 2019 22:35:47 +0200

overflow.h: Add comment documenting __ab_c_size()

__ab_c_size() is a somewhat opaque name. Document its purpose, and while at it, rename the parameters to actually match the abc naming.

[ bp: glued a complete patch from chunks on LKML. ]

---
 include/linux/overflow.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/overflow.h +6 -2
index 40b48e2133cb..6534a727cadb 100644
@@ -278,11 +278,15 @@ static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
 	return bytes;
 }
 
-static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
+/*
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
+ * struct_size() below.
+ */
+static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
 {
 	size_t bytes;
 
-	if (check_mul_overflow(n, size, &bytes))
+	if (check_mul_overflow(a, b, &bytes))
 		return SIZE_MAX;
 	if (check_add_overflow(bytes, c, &bytes))
 		return SIZE_MAX;
#10 lore raw

[tip:core/core] overflow.h: Add comment documenting __ab_c_size()

In reply to Borislav Petkov
overflow.h: Add comment documenting __ab_c_size()
Commit-ID:  e0478542cfd4d993e38d5f92a3f3ecd238805e96
Gitweb:     https://git.kernel.org/tip/e0478542cfd4d993e38d5f92a3f3ecd238805e96
Author:     Rasmus Villemoes <[email protected]>
AuthorDate: Wed, 10 Apr 2019 22:27:25 +0200
Committer:  Borislav Petkov <[email protected]>
CommitDate: Fri, 12 Apr 2019 13:44:24 +0200

overflow.h: Add comment documenting __ab_c_size()

__ab_c_size() is a somewhat opaque name. Document its purpose, and while at it, rename the parameters to actually match the abc naming.

[ bp: glued a complete patch from chunks on LKML. ]

---
 include/linux/overflow.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/overflow.h +6 -2
index 40b48e2133cb..6534a727cadb 100644
@@ -278,11 +278,15 @@ static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
 	return bytes;
 }
 
-static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
+/*
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
+ * struct_size() below.
+ */
+static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
 {
 	size_t bytes;
 
-	if (check_mul_overflow(n, size, &bytes))
+	if (check_mul_overflow(a, b, &bytes))
 		return SIZE_MAX;
 	if (check_add_overflow(bytes, c, &bytes))
 		return SIZE_MAX;