Re: [PATCH v4 04/45] x86: asm: instrument usercopy in get_user() and __put_user_size()

From: Marco Elver
Date: Thu Jul 07 2022 - 06:13:59 EST


On Fri, 1 Jul 2022 at 16:23, Alexander Potapenko <glider@xxxxxxxxxx> wrote:
>
> Use hooks from instrumented.h to notify bug detection tools about
> usercopy events in get_user() and put_user_size().
>
> It's still unclear how to instrument put_user(), which assumes that
> instrumentation code doesn't clobber RAX.

do_put_user_call() has a comment about KASAN clobbering %ax, doesn't
this also apply to KMSAN? If not, could we have a <asm/instrumented.h>
that provides helpers to push registers on the stack and pop them back
on return?

Also it seems the test robot complained about this patch.

> Signed-off-by: Alexander Potapenko <glider@xxxxxxxxxx>
> ---
> Link: https://linux-review.googlesource.com/id/Ia9f12bfe5832623250e20f1859fdf5cc485a2fce
> ---
> arch/x86/include/asm/uaccess.h | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> index 913e593a3b45f..1a8b5a234474f 100644
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
> @@ -5,6 +5,7 @@
> * User space memory access functions
> */
> #include <linux/compiler.h>
> +#include <linux/instrumented.h>
> #include <linux/kasan-checks.h>
> #include <linux/string.h>
> #include <asm/asm.h>
> @@ -99,11 +100,13 @@ extern int __get_user_bad(void);
> int __ret_gu; \
> register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
> __chk_user_ptr(ptr); \
> + instrument_copy_from_user_before((void *)&(x), ptr, sizeof(*(ptr))); \
> asm volatile("call __" #fn "_%P4" \
> : "=a" (__ret_gu), "=r" (__val_gu), \
> ASM_CALL_CONSTRAINT \
> : "0" (ptr), "i" (sizeof(*(ptr)))); \
> (x) = (__force __typeof__(*(ptr))) __val_gu; \
> + instrument_copy_from_user_after((void *)&(x), ptr, sizeof(*(ptr)), 0); \
> __builtin_expect(__ret_gu, 0); \
> })
>
> @@ -248,7 +251,9 @@ extern void __put_user_nocheck_8(void);
>
> #define __put_user_size(x, ptr, size, label) \
> do { \
> + __typeof__(*(ptr)) __pus_val = x; \
> __chk_user_ptr(ptr); \
> + instrument_copy_to_user(ptr, &(__pus_val), size); \
> switch (size) { \
> case 1: \
> __put_user_goto(x, ptr, "b", "iq", label); \
> @@ -286,6 +291,7 @@ do { \
> #define __get_user_size(x, ptr, size, label) \
> do { \
> __chk_user_ptr(ptr); \
> + instrument_copy_from_user_before((void *)&(x), ptr, size); \
> switch (size) { \
> case 1: { \
> unsigned char x_u8__; \
> @@ -305,6 +311,7 @@ do { \
> default: \
> (x) = __get_user_bad(); \
> } \
> + instrument_copy_from_user_after((void *)&(x), ptr, size, 0); \
> } while (0)
>
> #define __get_user_asm(x, addr, itype, ltype, label) \
> --
> 2.37.0.rc0.161.g10f37bed90-goog
>