[RFC v2][PATCH 01/11] Introduce rare_write() infrastructure

Kees Cook keescook at chromium.org
Thu Mar 30 10:02:55 PDT 2017


On Thu, Mar 30, 2017 at 12:44 AM, Ho-Eun Ryu <hoeun.ryu at gmail.com> wrote:
>
>> On 30 Mar 2017, at 3:23 AM, Kees Cook <keescook at chromium.org> wrote:
>>
>> On Wed, Mar 29, 2017 at 11:15 AM, Kees Cook <keescook at chromium.org> wrote:
>>> +/*
>>> + * Build "write rarely" infrastructure for flipping memory r/w
>>> + * on a per-CPU basis.
>>> + */
>>> +#ifndef CONFIG_HAVE_ARCH_RARE_WRITE
>>> +# define __wr_rare
>>> +# define __wr_rare_type
>>> +# define __rare_write(__var, __val)    (__var = (__val))
>>> +# define rare_write_begin()            do { } while (0)
>>> +# define rare_write_end()              do { } while (0)
>>> +#else
>>> +# define __wr_rare                     __ro_after_init
>>> +# define __wr_rare_type                        const
>>> +# ifdef CONFIG_HAVE_ARCH_RARE_WRITE_MEMCPY
>>> +#  define __rare_write_n(dst, src, len)        ({                      \
>>> +               BUILD_BUG(!builtin_const(len));                 \
>>> +               __arch_rare_write_memcpy((dst), (src), (len));  \
>>> +       })
>>> +#  define __rare_write(var, val)  __rare_write_n(&(var), &(val), sizeof(var))
>>> +# else
>>> +#  define __rare_write(var, val)  ((*(typeof((typeof(var))0) *)&(var)) = (val))
>>> +# endif
>>> +# define rare_write_begin()    __arch_rare_write_begin()
>>> +# define rare_write_end()      __arch_rare_write_end()
>>> +#endif
>>> +#define rare_write(__var, __val) ({                    \
>>> +       rare_write_begin();                             \
>>> +       __rare_write(__var, __val);                     \
>>> +       rare_write_end();                               \
>>> +       __var;                                          \
>>> +})
>>> +
>>
>> Of course, only after sending this do I realize that the MEMCPY case
>> will need to be further adjusted, since it currently can't take
>> literals. I guess something like this needs to be done:
>>
>> #define __rare_write(var, val) ({ \
>>    typeof(var) __src = (val);     \
>>    __rare_write_n(&(var), &(__src), sizeof(var)); \
>> })
>>
>
> Right, and it has a problem with BUILD_BUG, which causes compilation error when CONFIG_HABE_ARCH_RARE_WRITE_MEMCPY is true
> BUILD_BUG is defined in <linux/bug.h> but <linux/bug.h> includes <linux/compiler.h>
>
> Please see the following.
>
> diff --git a/include/linux/compiler.h b/include/linux/compiler.h
> index 3334fa9..3fa50e1 100644
> --- a/include/linux/compiler.h
> +++ b/include/linux/compiler.h
> @@ -350,11 +350,11 @@ static __always_inline void __write_once_size(volatile vo\
> id *p, void *res, int s
>  # define __wr_rare                     __ro_after_init
>  # define __wr_rare_type                        const
>  # ifdef CONFIG_HAVE_ARCH_RARE_WRITE_MEMCPY
> -#  define __rare_write_n(dst, src, len)        ({                      \
> -               BUILD_BUG(!builtin_const(len));                         \
> -               __arch_rare_write_memcpy((dst), (src), (len));   \
> +#  define __rare_write_n(var, val, len)        ({                                 \
> +               typeof(val) __val = val;                                                    \
> +               __arch_rare_write_memcpy(&(var), &(__val), (len));      \
>         })
> -#  define __rare_write(var, val)  __rare_write_n(&(var), &(val), sizeof(var))
> +#  define __rare_write(var, val)  __rare_write_n((var), (val), sizeof(var))
>  # else
>  #  define __rare_write(var, val)  ((*(typeof((typeof(var))0) *)&(var)) = (val)\
> )
>  # endif

Thanks for the catch, I'll update this (I'll use compiletime_assert,
which is defined in compiler.h).

-Kees

-- 
Kees Cook
Pixel Security



More information about the linux-arm-kernel mailing list