[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CANpmjNNExn8DX2+Cnz3GDWXNZv-jZcpFSedCYx+y6HEemFoHRw@mail.gmail.com>
Date: Mon, 5 Feb 2024 14:31:04 +0100
From: Marco Elver <elver@...gle.com>
To: Kees Cook <keescook@...omium.org>
Cc: linux-hardening@...r.kernel.org,
Rasmus Villemoes <rasmus.villemoes@...vas.dk>, Mark Rutland <mark.rutland@....com>,
"Gustavo A. R. Silva" <gustavoars@...nel.org>, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3 2/3] overflow: Introduce add_wrap(), sub_wrap(), and mul_wrap()
On Mon, 5 Feb 2024 at 10:12, Kees Cook <keescook@...omium.org> wrote:
>
> Provide helpers that will perform wrapping addition, subtraction, or
> multiplication without tripping the arithmetic wrap-around sanitizers. The
> first argument is the type under which the wrap-around should happen
> with. In other words, these two calls will get very different results:
>
> mul_wrap(int, 50, 50) == 2500
> mul_wrap(u8, 50, 50) == 196
>
> Add to the selftests to validate behavior and lack of side-effects.
>
> Cc: Rasmus Villemoes <rasmus.villemoes@...vas.dk>
> Cc: Mark Rutland <mark.rutland@....com>
> Cc: linux-hardening@...r.kernel.org
> Signed-off-by: Kees Cook <keescook@...omium.org>
> ---
> include/linux/overflow.h | 54 ++++++++++++++++++++++++++++++++++++++++
> lib/overflow_kunit.c | 23 ++++++++++++++---
> 2 files changed, 73 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index 4e741ebb8005..9b8c05bdb788 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -64,6 +64,24 @@ static inline bool __must_check __must_check_overflow(bool overflow)
> #define check_add_overflow(a, b, d) \
> __must_check_overflow(__builtin_add_overflow(a, b, d))
>
> +/**
> + * add_wrap() - Intentionally perform a wrapping addition
> + * @type: type for result of calculation
> + * @a: first addend
> + * @b: second addend
> + *
> + * Return the potentially wrapped-around addition without
> + * tripping any wrap-around sanitizers that may be enabled.
> + */
> +#define add_wrap(type, a, b) \
> + ({ \
> + type __val; \
> + if (check_add_overflow(a, b, &__val)) { \
> + /* do nothing */ \
The whole reason check_*_overflow() exists is to wrap the builtin in a
function with __must_check. Here the result is explicitly ignored, so
do we have to go through the check_add_overflow indirection? Why not
just use the builtin directly? It might make sense to make the
compiler's job a little easier, because I predict that
__must_check_overflow will be outlined with enough instrumentation
(maybe it should have been __always_inline).
Powered by blists - more mailing lists