Re: [PATCH v2] x86: disable non-instrumented version of copy_mc when KMSAN is enabled

From: Alexander Potapenko
Date: Tue Mar 19 2024 - 08:39:34 EST


On Thu, Mar 7, 2024 at 1:09 AM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Wed, 6 Mar 2024 at 14:08, Tetsuo Handa
> <penguin-kernel@xxxxxxxxxxxxxxxxxxx> wrote:
> >
> > Something like below one?
>
> I'd rather leave the regular fallbacks (to memcpy and copy_to_user())
> alone, and I'd just put the
>
> kmsan_memmove(dst, src, len - ret);
>
> etc in the places that currently just call the MC copy functions.

(sorry for being late to the party)

We should probably use <linux/instrumented.h> here, as other tools
(KASAN and KCSAN) do not instrument copy_mc_to_kernel() either, and
might benefit from the same checks.

Something along the lines of:

================================
static __always_inline void
instrument_memcpy_before(const void *to, const void *from, unsigned long n)
{
kasan_check_write(to, n);
kasan_check_read(from, n);
kcsan_check_write(to, n);
kcsan_check_read(from, n);
}

static __always_inline void instrument_memcpy_after(const void *to,
const void *from,
unsigned long n,
unsigned long left)
{
kmsan_memcpy(to, n - left);
}
================================

(with kmsan_memcpy() working as Tetsuo described).

We can also update copy_mc_fragile_handle_tail() and copy_mc_to_user()
to call the instrumentation hooks.
Let me send the patches.