diff mbox series

[v6,1/3] overflow: Adjust check_*_overflow() kern-doc to reflect results

Message ID 20240213221100.3556356-1-keescook@chromium.org (mailing list archive)
State Superseded
Commit afbc0e53c5a0aa8d248a48111562a715c30678f2
Headers show
Series overflow: Introduce wrapping helpers | expand

Commit Message

Kees Cook Feb. 13, 2024, 10:10 p.m. UTC
The check_*_overflow() helpers will return results with potentially
wrapped-around values. These values have always been checked by the
selftests, so avoid the confusing language in the kern-doc. The idea of
"safe for use" was relative to the expectation of whether or not the
caller wants a wrapped value -- the calculation itself will always follow
arithmetic wrapping rules.

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/linux/overflow.h | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

Comments

Mark Rutland Feb. 14, 2024, 11:57 a.m. UTC | #1
On Tue, Feb 13, 2024 at 02:10:57PM -0800, Kees Cook wrote:
> The check_*_overflow() helpers will return results with potentially
> wrapped-around values. These values have always been checked by the
> selftests, so avoid the confusing language in the kern-doc. The idea of
> "safe for use" was relative to the expectation of whether or not the
> caller wants a wrapped value -- the calculation itself will always follow
> arithmetic wrapping rules.
> 
> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  include/linux/overflow.h | 18 ++++++------------
>  1 file changed, 6 insertions(+), 12 deletions(-)
> 
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index 7b5cf4a5cd19..4e741ebb8005 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -57,11 +57,9 @@ static inline bool __must_check __must_check_overflow(bool overflow)
>   * @b: second addend
>   * @d: pointer to store sum
>   *
> - * Returns 0 on success.
> + * Returns 0 on success, 1 on wrap-around.

Sorry for the last minute bikeshedding, but could we clarify 'success' here?
e.g. I think it'd be clearer to say:

  Returns true on wrap-around, false otherwise.

Note that also uses true/false since these all return bool (as do the
underlying __builtin_*_overflow() functions).

>   *
> - * *@d holds the results of the attempted addition, but is not considered
> - * "safe for use" on a non-zero return value, which indicates that the
> - * sum has overflowed or been truncated.
> + * *@d holds the results of the attempted addition, which may wrap-around.

How about:

  @d holds the results of the attempted addition, regardless of whether
  wrap-around occurred.

... and likewise for the others below?

Mark.

>   */
>  #define check_add_overflow(a, b, d)	\
>  	__must_check_overflow(__builtin_add_overflow(a, b, d))
> @@ -72,11 +70,9 @@ static inline bool __must_check __must_check_overflow(bool overflow)
>   * @b: subtrahend; value to subtract from @a
>   * @d: pointer to store difference
>   *
> - * Returns 0 on success.
> + * Returns 0 on success, 1 on wrap-around.
>   *
> - * *@d holds the results of the attempted subtraction, but is not considered
> - * "safe for use" on a non-zero return value, which indicates that the
> - * difference has underflowed or been truncated.
> + * *@d holds the results of the attempted subtraction, which may wrap-around.
>   */
>  #define check_sub_overflow(a, b, d)	\
>  	__must_check_overflow(__builtin_sub_overflow(a, b, d))
> @@ -87,11 +83,9 @@ static inline bool __must_check __must_check_overflow(bool overflow)
>   * @b: second factor
>   * @d: pointer to store product
>   *
> - * Returns 0 on success.
> + * Returns 0 on success, 1 on wrap-around.
>   *
> - * *@d holds the results of the attempted multiplication, but is not
> - * considered "safe for use" on a non-zero return value, which indicates
> - * that the product has overflowed or been truncated.
> + * *@d holds the results of the attempted multiplication, which may wrap-around.
>   */
>  #define check_mul_overflow(a, b, d)	\
>  	__must_check_overflow(__builtin_mul_overflow(a, b, d))
> -- 
> 2.34.1
>
Kees Cook Feb. 14, 2024, 7:38 p.m. UTC | #2
On Wed, Feb 14, 2024 at 11:57:28AM +0000, Mark Rutland wrote:
> On Tue, Feb 13, 2024 at 02:10:57PM -0800, Kees Cook wrote:
> > The check_*_overflow() helpers will return results with potentially
> > wrapped-around values. These values have always been checked by the
> > selftests, so avoid the confusing language in the kern-doc. The idea of
> > "safe for use" was relative to the expectation of whether or not the
> > caller wants a wrapped value -- the calculation itself will always follow
> > arithmetic wrapping rules.
> > 
> > Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > Cc: linux-hardening@vger.kernel.org
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> >  include/linux/overflow.h | 18 ++++++------------
> >  1 file changed, 6 insertions(+), 12 deletions(-)
> > 
> > diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> > index 7b5cf4a5cd19..4e741ebb8005 100644
> > --- a/include/linux/overflow.h
> > +++ b/include/linux/overflow.h
> > @@ -57,11 +57,9 @@ static inline bool __must_check __must_check_overflow(bool overflow)
> >   * @b: second addend
> >   * @d: pointer to store sum
> >   *
> > - * Returns 0 on success.
> > + * Returns 0 on success, 1 on wrap-around.
> 
> Sorry for the last minute bikeshedding, but could we clarify 'success' here?
> e.g. I think it'd be clearer to say:
> 
>   Returns true on wrap-around, false otherwise.
> 
> Note that also uses true/false since these all return bool (as do the
> underlying __builtin_*_overflow() functions).

Yeah, that's a good point. I'll update this.

> >   *
> > - * *@d holds the results of the attempted addition, but is not considered
> > - * "safe for use" on a non-zero return value, which indicates that the
> > - * sum has overflowed or been truncated.
> > + * *@d holds the results of the attempted addition, which may wrap-around.
> 
> How about:
> 
>   @d holds the results of the attempted addition, regardless of whether
>   wrap-around occurred.
> 
> ... and likewise for the others below?

Yeah, that's more clear. Thanks!

-Kees
diff mbox series

Patch

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 7b5cf4a5cd19..4e741ebb8005 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -57,11 +57,9 @@  static inline bool __must_check __must_check_overflow(bool overflow)
  * @b: second addend
  * @d: pointer to store sum
  *
- * Returns 0 on success.
+ * Returns 0 on success, 1 on wrap-around.
  *
- * *@d holds the results of the attempted addition, but is not considered
- * "safe for use" on a non-zero return value, which indicates that the
- * sum has overflowed or been truncated.
+ * *@d holds the results of the attempted addition, which may wrap-around.
  */
 #define check_add_overflow(a, b, d)	\
 	__must_check_overflow(__builtin_add_overflow(a, b, d))
@@ -72,11 +70,9 @@  static inline bool __must_check __must_check_overflow(bool overflow)
  * @b: subtrahend; value to subtract from @a
  * @d: pointer to store difference
  *
- * Returns 0 on success.
+ * Returns 0 on success, 1 on wrap-around.
  *
- * *@d holds the results of the attempted subtraction, but is not considered
- * "safe for use" on a non-zero return value, which indicates that the
- * difference has underflowed or been truncated.
+ * *@d holds the results of the attempted subtraction, which may wrap-around.
  */
 #define check_sub_overflow(a, b, d)	\
 	__must_check_overflow(__builtin_sub_overflow(a, b, d))
@@ -87,11 +83,9 @@  static inline bool __must_check __must_check_overflow(bool overflow)
  * @b: second factor
  * @d: pointer to store product
  *
- * Returns 0 on success.
+ * Returns 0 on success, 1 on wrap-around.
  *
- * *@d holds the results of the attempted multiplication, but is not
- * considered "safe for use" on a non-zero return value, which indicates
- * that the product has overflowed or been truncated.
+ * *@d holds the results of the attempted multiplication, which may wrap-around.
  */
 #define check_mul_overflow(a, b, d)	\
 	__must_check_overflow(__builtin_mul_overflow(a, b, d))