Re: [PATCH] crypto: algapi - fix be32_to_cpu macro call in crypto_inc()
From: Alexey Khoroshilov
Date: Wed Nov 16 2022 - 18:15:55 EST
On 16.11.2022 20:39, Eric Biggers wrote:
> On Wed, Nov 16, 2022 at 04:52:51PM +0300, Alexey Khoroshilov wrote:
>> be32_to_cpu() macro in some cases may be expanded to an expression
>> that evaluates its arguments multiple times.
>
> When is that, exactly?
I considered the path
#define be32_to_cpu __be32_to_cpu
#define __be32_to_cpu(x) __swab32((__force __u32)(__be32)(x))
#define __swab32(x) \
(__builtin_constant_p((__u32)(x)) ? \
___constant_swab32(x) : \
__fswab32(x))
#define ___constant_swab32(x) ((__u32)( \
(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
(((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
(((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
(((__u32)(x) & (__u32)0xff000000UL) >> 24)))
But you are right, it is protected by __builtin_constant_p() that
prevents that for memory access.
> If that's true, then lots of other places in the kernel would need to be fixed
> too. Try running:
>
> git grep -E '[bl]e(16|32|64)_to_cpu\([^)]+\+\+\)'
>
> If true, then it would be much better to fix the macros.
And yes, anyway it makes sense to keep for be32_to_cpu()-like function
macro a contract to handle its arguments as a plain function.
Thanks,
Alexey