All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y
@ 2021-09-13 16:35 Will Deacon
  2021-09-13 16:45 ` Nick Desaulniers
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Will Deacon @ 2021-09-13 16:35 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, Will Deacon, Nick Desaulniers, Bill Wendling,
	Linus Torvalds, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Peter Zijlstra, Greg Kroah-Hartman

Commit 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
added an optimised version of __get_user_asm() for x86 using 'asm goto'.

Like the non-optimised code, the 32-bit implementation of 64-bit get_user()
expands to a pair of 32-bit accesses. Unlike the non-optimised code, the
_original_ pointer is incremented to copy the high word instead of loading
through a new pointer explicitly constructed to point at a 32-bit type.
Consequently, if the pointer points at a 64-bit type then we end up
loading the wrong data for the upper 32-bits.

This was observed as a mount() failure in Android targetting i686 after
b0cfcdd9b967 ("d_path: make 'prepend()' fill up the buffer exactly on
overflow") because the call to copy_from_kernel_nofault() from
prepend_copy() ends up in __get_kernel_nofault() and casts the source
pointer to a 'u64 __user *'. An attempt to mount at "/debug_ramdisk"
therefore ends up failing trying to mount "/debumdismdisk".

Use the existing '__gu_ptr' source pointer to unsigned int for 32-bit
__get_user_asm_u64() instead of the original pointer.

Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Reported-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Fixes: 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
Signed-off-by: Will Deacon <will@kernel.org>
---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index c9fa7be3df82..5c95d242f38d 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -301,8 +301,8 @@ do {									\
 	unsigned int __gu_low, __gu_high;				\
 	const unsigned int __user *__gu_ptr;				\
 	__gu_ptr = (const void __user *)(ptr);				\
-	__get_user_asm(__gu_low, ptr, "l", "=r", label);		\
-	__get_user_asm(__gu_high, ptr+1, "l", "=r", label);		\
+	__get_user_asm(__gu_low, __gu_ptr, "l", "=r", label);		\
+	__get_user_asm(__gu_high, __gu_ptr+1, "l", "=r", label);	\
 	(x) = ((unsigned long long)__gu_high << 32) | __gu_low;		\
 } while (0)
 #else
-- 
2.33.0.309.g3052b89438-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y
  2021-09-13 16:35 [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y Will Deacon
@ 2021-09-13 16:45 ` Nick Desaulniers
  2021-09-13 16:48 ` Greg Kroah-Hartman
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Nick Desaulniers @ 2021-09-13 16:45 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, x86, Bill Wendling, Linus Torvalds,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Peter Zijlstra,
	Greg Kroah-Hartman

On Mon, Sep 13, 2021 at 9:36 AM Will Deacon <will@kernel.org> wrote:
>
> Commit 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
> added an optimised version of __get_user_asm() for x86 using 'asm goto'.
>
> Like the non-optimised code, the 32-bit implementation of 64-bit get_user()
> expands to a pair of 32-bit accesses. Unlike the non-optimised code, the
> _original_ pointer is incremented to copy the high word instead of loading
> through a new pointer explicitly constructed to point at a 32-bit type.
> Consequently, if the pointer points at a 64-bit type then we end up
> loading the wrong data for the upper 32-bits.
>
> This was observed as a mount() failure in Android targetting i686 after

s/targetting/targeting/

> b0cfcdd9b967 ("d_path: make 'prepend()' fill up the buffer exactly on
> overflow") because the call to copy_from_kernel_nofault() from
> prepend_copy() ends up in __get_kernel_nofault() and casts the source
> pointer to a 'u64 __user *'. An attempt to mount at "/debug_ramdisk"
> therefore ends up failing trying to mount "/debumdismdisk".
>
> Use the existing '__gu_ptr' source pointer to unsigned int for 32-bit
> __get_user_asm_u64() instead of the original pointer.
>
> Cc: Nick Desaulniers <ndesaulniers@google.com>
> Cc: Bill Wendling <morbo@google.com>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Reported-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Fixes: 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
> Signed-off-by: Will Deacon <will@kernel.org>

Sorry I missed this; I think -Wunused-variable would have helped here.
Thanks for debugging+fixing.

Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>  arch/x86/include/asm/uaccess.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> index c9fa7be3df82..5c95d242f38d 100644
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
> @@ -301,8 +301,8 @@ do {                                                                        \
>         unsigned int __gu_low, __gu_high;                               \
>         const unsigned int __user *__gu_ptr;                            \
>         __gu_ptr = (const void __user *)(ptr);                          \
> -       __get_user_asm(__gu_low, ptr, "l", "=r", label);                \
> -       __get_user_asm(__gu_high, ptr+1, "l", "=r", label);             \
> +       __get_user_asm(__gu_low, __gu_ptr, "l", "=r", label);           \
> +       __get_user_asm(__gu_high, __gu_ptr+1, "l", "=r", label);        \
>         (x) = ((unsigned long long)__gu_high << 32) | __gu_low;         \
>  } while (0)
>  #else
> --
> 2.33.0.309.g3052b89438-goog
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y
  2021-09-13 16:35 [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y Will Deacon
  2021-09-13 16:45 ` Nick Desaulniers
@ 2021-09-13 16:48 ` Greg Kroah-Hartman
  2021-09-13 17:05 ` Linus Torvalds
  2021-09-15  7:45 ` Naresh Kamboju
  3 siblings, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2021-09-13 16:48 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, x86, Nick Desaulniers, Bill Wendling,
	Linus Torvalds, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Peter Zijlstra

On Mon, Sep 13, 2021 at 05:35:47PM +0100, Will Deacon wrote:
> Commit 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
> added an optimised version of __get_user_asm() for x86 using 'asm goto'.
> 
> Like the non-optimised code, the 32-bit implementation of 64-bit get_user()
> expands to a pair of 32-bit accesses. Unlike the non-optimised code, the
> _original_ pointer is incremented to copy the high word instead of loading
> through a new pointer explicitly constructed to point at a 32-bit type.
> Consequently, if the pointer points at a 64-bit type then we end up
> loading the wrong data for the upper 32-bits.
> 
> This was observed as a mount() failure in Android targetting i686 after
> b0cfcdd9b967 ("d_path: make 'prepend()' fill up the buffer exactly on
> overflow") because the call to copy_from_kernel_nofault() from
> prepend_copy() ends up in __get_kernel_nofault() and casts the source
> pointer to a 'u64 __user *'. An attempt to mount at "/debug_ramdisk"
> therefore ends up failing trying to mount "/debumdismdisk".
> 
> Use the existing '__gu_ptr' source pointer to unsigned int for 32-bit
> __get_user_asm_u64() instead of the original pointer.
> 
> Cc: Nick Desaulniers <ndesaulniers@google.com>
> Cc: Bill Wendling <morbo@google.com>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Reported-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Fixes: 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
> Signed-off-by: Will Deacon <will@kernel.org>
> ---
>  arch/x86/include/asm/uaccess.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> index c9fa7be3df82..5c95d242f38d 100644
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
> @@ -301,8 +301,8 @@ do {									\
>  	unsigned int __gu_low, __gu_high;				\
>  	const unsigned int __user *__gu_ptr;				\
>  	__gu_ptr = (const void __user *)(ptr);				\
> -	__get_user_asm(__gu_low, ptr, "l", "=r", label);		\
> -	__get_user_asm(__gu_high, ptr+1, "l", "=r", label);		\
> +	__get_user_asm(__gu_low, __gu_ptr, "l", "=r", label);		\
> +	__get_user_asm(__gu_high, __gu_ptr+1, "l", "=r", label);	\
>  	(x) = ((unsigned long long)__gu_high << 32) | __gu_low;		\
>  } while (0)
>  #else
> -- 
> 2.33.0.309.g3052b89438-goog
> 

Tested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Thanks for finding and fixing this!

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y
  2021-09-13 16:35 [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y Will Deacon
  2021-09-13 16:45 ` Nick Desaulniers
  2021-09-13 16:48 ` Greg Kroah-Hartman
@ 2021-09-13 17:05 ` Linus Torvalds
  2021-09-15  7:45 ` Naresh Kamboju
  3 siblings, 0 replies; 5+ messages in thread
From: Linus Torvalds @ 2021-09-13 17:05 UTC (permalink / raw)
  To: Will Deacon
  Cc: Linux Kernel Mailing List, the arch/x86 maintainers,
	Nick Desaulniers, Bill Wendling, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Peter Zijlstra, Greg Kroah-Hartman

On Mon, Sep 13, 2021 at 9:36 AM Will Deacon <will@kernel.org> wrote:
>
> Use the existing '__gu_ptr' source pointer to unsigned int for 32-bit
> __get_user_asm_u64() instead of the original pointer.

Heh. And by "existing" you mean "the one that exists _purely_ for this
exact reason and that wasn't used" ;)

What a silly bug. And it's existed for a year, which I think shows
just how little 32-bit x86 is used these days (*).

            Linus

(*) but also probably how few 64-bit user accesses we do - the fact
that the bug was actually found by the "copy_from_kernel_nofault" code
that just shares the infrastructure on x86 rather than any user access
code is kind of interesting.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y
  2021-09-13 16:35 [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y Will Deacon
                   ` (2 preceding siblings ...)
  2021-09-13 17:05 ` Linus Torvalds
@ 2021-09-15  7:45 ` Naresh Kamboju
  3 siblings, 0 replies; 5+ messages in thread
From: Naresh Kamboju @ 2021-09-15  7:45 UTC (permalink / raw)
  To: Will Deacon
  Cc: open list, X86 ML, Nick Desaulniers, Bill Wendling,
	Linus Torvalds, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Peter Zijlstra, Greg Kroah-Hartman

On Mon, 13 Sept 2021 at 22:06, Will Deacon <will@kernel.org> wrote:
>
> Commit 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
> added an optimised version of __get_user_asm() for x86 using 'asm goto'.
>
> Like the non-optimised code, the 32-bit implementation of 64-bit get_user()
> expands to a pair of 32-bit accesses. Unlike the non-optimised code, the
> _original_ pointer is incremented to copy the high word instead of loading
> through a new pointer explicitly constructed to point at a 32-bit type.
> Consequently, if the pointer points at a 64-bit type then we end up
> loading the wrong data for the upper 32-bits.
>
> This was observed as a mount() failure in Android targetting i686 after
> b0cfcdd9b967 ("d_path: make 'prepend()' fill up the buffer exactly on
> overflow") because the call to copy_from_kernel_nofault() from
> prepend_copy() ends up in __get_kernel_nofault() and casts the source
> pointer to a 'u64 __user *'. An attempt to mount at "/debug_ramdisk"
> therefore ends up failing trying to mount "/debumdismdisk".
>
> Use the existing '__gu_ptr' source pointer to unsigned int for 32-bit
> __get_user_asm_u64() instead of the original pointer.
>
> Cc: Nick Desaulniers <ndesaulniers@google.com>
> Cc: Bill Wendling <morbo@google.com>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Reported-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Fixes: 865c50e1d279 ("x86/uaccess: utilize CONFIG_CC_HAS_ASM_GOTO_OUTPUT")
> Signed-off-by: Will Deacon <will@kernel.org>

This patch merged into Linux next (next-20210914) and the reported
problem has been fixed.

Tested-by: Linux Kernel Functional Testing <lkft@linaro.org>

--
Linaro LKFT
https://lkft.linaro.org

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-09-15  7:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-13 16:35 [PATCH] x86/uaccess: Fix 32-bit __get_user_asm_u64() when CC_HAS_ASM_GOTO_OUTPUT=y Will Deacon
2021-09-13 16:45 ` Nick Desaulniers
2021-09-13 16:48 ` Greg Kroah-Hartman
2021-09-13 17:05 ` Linus Torvalds
2021-09-15  7:45 ` Naresh Kamboju

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.