RFC: riscv: evaluate put_user() arg before enabling user access
PATCH 1/1 RFC: riscv: evaluate put_user() arg before enabling user access
The <asm/uaccess.h> header has a problem with put_user(a, ptr) if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so:
1: enable_user_access()
2: evaluate 'a'
3: put 'a' to 'ptr'
4: disable_user_acess()
The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assinging 'x' to __val at the start, and hoping the compiler barriers in enable_user_access() do the job of ordering step 2 before step 1.
This has shown up in a bug where 'a' sleeps and thus schedules out and loses the SR_SUM flag. This isn't sufficient to fully fix, but should reduce the window of opportunity.
--- arch/riscv/include/asm/uaccess.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/riscv/include/asm/uaccess.h +6 -2
@@ -306,7 +306,10 @@ do { \* data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable -* to the result of dereferencing @ptr. +* to the result of dereferencing @ptr. The @x is copied inside the macro +* to avoid code re-ordering where @x gets evaulated within the block that +* enables user-space access (thus possibly bypassing some of the protection +* this feautre provides). * * Caller must check the pointer with access_ok() before calling this * function.
@@ -316,12 +319,13 @@ do { \#define __put_user (x, ptr) \ ({ \ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*__gu_ptr) __val = (x); \ long __pu_err = 0; \ \ __chk_user_ptr(__gu_ptr); \ \ __enable_user_access(); \ - __put_user_nocheck(x, __gu_ptr, __pu_err); \ + __put_user_nocheck(__val, __gu_ptr, __pu_err); \ __disable_user_access(); \ \ __pu_err; \
Signature · 7 lines
2.30.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 18 lines · Ben Dooks
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so:1: enable_user_access() 2: evaluate 'a' 3: put 'a' to 'ptr' 4: disable_user_acess()The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assinging 'x' to
__valat the start, and hoping the compiler barriers inenable_user_access()do the job of ordering step 2 before step 1.
Quoted 6 lines
This has shown up in a bug where 'a' sleeps and thus schedules out and loses the
SR_SUMflag. This isn't sufficient to fully fix, but should reduce the window of opportunity.Cc Arnd Bergman <[email protected]>
Note: your Signed-off-by seems to be missing.
Quoted 21 lines
--- arch/riscv/include/asm/uaccess.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)arch/riscv/include/asm/uaccess.h +4
-1@@ -306,7 +306,10 @@ do { \* data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable -* to the result of dereferencing @ptr. +* to the result of dereferencing @ptr. The @x is copied inside the macro +* to avoid code re-ordering where @x gets evaulated within the block that +* enables user-space access (thus possibly bypassing some of the protection +* this feautre provides). * * Caller must check the pointer with access_ok() before calling this * function.
arch/riscv/include/asm/uaccess.h do { \ · line 323
Quoted 5 lines
arch/riscv/include/asm/uaccess.h +1
-0@@ -316,12 +319,13 @@ do { \#define __put_user (x, ptr) \ ({ \ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*__gu_ptr) __val = (x); \ long __pu_err = 0; \
Side note: We should really change the first typeof to an __auto_type here (and on all other architectures), but that's a separate change and not strictly a bugfix.
Arnd
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 24 lines · Arnd Bergmann
Quoted 23 lines · Ben Dooks
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so:1: enable_user_access() 2: evaluate 'a' 3: put 'a' to 'ptr' 4: disable_user_acess()The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assinging 'x' to
__valat the start, and hoping the compiler barriers inenable_user_access()do the job of ordering step 2 before step 1.This has shown up in a bug where 'a' sleeps and thus schedules out and loses the
SR_SUMflag. This isn't sufficient to fully fix, but should reduce the window of opportunity.
Quoted 5 lines
Quoted 1 line
Cc Arnd Bergman <[email protected]>Reviewed-by Arnd Bergman <[email protected]>Note: your Signed-off-by seems to be missing.
Sorry, forgot as was intending smaller RFC release.
Thanks for the help sorting out the compile issues
I need to review the patch description anyway and make it flow closer to 73 columns instead of the rather truncated version it is.
Signature · 9 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 4 lines · Ben Dooks
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple
variable, such as a function. This can lead
to the compiler producing code as so:
Nit: your commit log seeems to truncate lines after 50 chars, you can and should use almost 1.5 as much.
Quoted 6 lines
* @ptr must have pointer-to-simple-variable type, and @x must be assignable
- * to the result of dereferencing @ptr.
+ * to the result of dereferencing @ptr. The @x is copied inside the macro
+ * to avoid code re-ordering where @x gets evaulated within the block that
+ * enables user-space access (thus possibly bypassing some of the protection
+ * this feautre provides).
Well, hopefully the compiler is smart enought to not actually copy. So we should probably talk about evaluating the argument here.
arch/riscv/include/asm/uaccess.h do { \ · line 329
Quoted 12 lines
#define
__put_user(x, ptr)\
({ \arch/riscv/include/asm/uaccess.h +2
-1__typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*__gu_ptr) __val = (x); \long __pu_err =0 ; \ \__chk_user_ptr (__gu_ptr); \ \__enable_user_access (); \ -__put_user_nocheck (x, __gu_ptr, __pu_err); \ +__put_user_nocheck (__val, __gu_ptr, __pu_err); \__disable_user_access (); \
It looks like __get_user needs the same treatment.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Ben Dooks
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple
variable, such as a function. This can lead
to the compiler producing code as so:Nit: your commit log seeems to truncate lines after 50 chars, you can and should use almost 1.5 as much.
Thanks, noted this once I'd re-read the patch. I have a few other minor bits to test and to credit Arnd with helping out after failing to get the first attempt to compile.
Quoted 10 lines
Quoted 6 lines
* @ptr must have pointer-to-simple-variable type, and @x must be assignable
- * to the result of dereferencing @ptr.
+ * to the result of dereferencing @ptr. The @x is copied inside the macro
+ * to avoid code re-ordering where @x gets evaulated within the block that
+ * enables user-space access (thus possibly bypassing some of the protection
+ * this feautre provides).Well, hopefully the compiler is smart enought to not actually copy. So we should probably talk about evaluating the argument here.
Quoted 14 lines
arch/riscv/include/asm/uaccess.h do { \ · line 329
Quoted 12 lines
#define __put_user(x, ptr) \ ({ \ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \arch/riscv/include/asm/uaccess.h +2
-1+ __typeof__(*__gu_ptr) __val = (x); \long __pu_err =0 ; \ \__chk_user_ptr (__gu_ptr); \ \__enable_user_access (); \ -__put_user_nocheck (x, __gu_ptr, __pu_err); \ +__put_user_nocheck (__val, __gu_ptr, __pu_err); \__disable_user_access (); \It looks like
__get_userneeds the same treatment.
I will check that, then again I don't think people do anything that would be an issue. We caught this from the put_user() in the schedule_tail() code which causes the pid fetch function to be called within the __enable_user_access().
Signature · 9 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Alex Ghiti
reviewerLe 3/18/21 à 6:41 PM, Ben Dooks a écrit :
Quoted 17 lines
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so:1:
enable_user_access()
2: evaluate 'a'
3: put 'a' to 'ptr'
4:disable_user_acess()The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assinging 'x' to
__valat the start, and hoping the compiler barriers inenable_user_access()do the job of ordering step 2 before step 1.
Quoted 4 lines
This has shown up in a bug where 'a' sleeps and thus schedules out and loses the
SR_SUMflag. This isn't sufficient to fully fix, but should reduce the window of opportunity.
I would say this patch is enough to fix the issue because it only happens when 'a' *explicitly schedules* when in __enable_user_access()/__disable_user_access(). Otherwise, I see 2 cases:
- user memory is correctly mapped and nothing stops the current process.
- an exception (interrupt or trap) is triggered: in those cases, the exception path correctly saves and restores SR_SUM.
Quoted 28 lines
Cc Arnd Bergman <[email protected]>--- arch/riscv/include/asm/uaccess.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)arch/riscv/include/asm/uaccess.h +5
-1@@ -306,7 +306,10 @@ do { \* data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable -* to the result of dereferencing @ptr. +* to the result of dereferencing @ptr. The @x is copied inside the macro +* to avoid code re-ordering where @x gets evaulated within the block that +* enables user-space access (thus possibly bypassing some of the protection +* this feautre provides). * * Caller must check the pointer with access_ok() before calling this * function. @@ -316,12 +319,13 @@ do { \#define __put_user (x, ptr) \ ({ \ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*__gu_ptr) __val = (x); \
arch/riscv/include/asm/uaccess.h do { \ · line 331
Quoted 10 lines
arch/riscv/include/asm/uaccess.h +1
-1long __pu_err =0 ; \ \__chk_user_ptr (__gu_ptr); \ \__enable_user_access (); \ -__put_user_nocheck (x, __gu_ptr, __pu_err); \ +__put_user_nocheck (__val, __gu_ptr, __pu_err); \__disable_user_access (); \ \ __pu_err; \
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 23 lines · Alex Ghiti
Le 3/18/21 à 6:41 PM, Ben Dooks a écrit :
Quoted 17 lines
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so:1:
enable_user_access()
2: evaluate 'a'
3: put 'a' to 'ptr'
4:disable_user_acess()The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assinging 'x' to
__valat the start, and hoping the compiler barriers inenable_user_access()do the job of ordering step 2 before step 1.Quoted 4 lines
This has shown up in a bug where 'a' sleeps and thus schedules out and loses the
SR_SUMflag. This isn't sufficient to fully fix, but should reduce the window of opportunity.
Quoted 5 lines
I would say this patch is enough to fix the issue because it only happens when 'a' *explicitly schedules* when in __enable_user_access()/__disable_user_access(). Otherwise, I see 2 cases:
- user memory is correctly mapped and nothing stops the current process.
- an exception (interrupt or trap) is triggered: in those cases, the exception path correctly saves and restoresSR_SUM.
This fixes part of the other issue.
I did point out in the other email there could be longer cases where the protections are disabled. The saving of the flags over switch_to() is still necessary.
Also, I am not sure if this will guarantee ordering. It does seem to fix it for the cases I checked
Quoted 47 lines
Quoted 31 lines
Cc Arnd Bergman <[email protected]>---
arch/riscv/include/asm/uaccess.h| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)arch/riscv/include/asm/uaccess.h +1
-1@@ -306,7 +306,10 @@ do { \ * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable - * to the result of dereferencing @ptr. + * to the result of dereferencing @ptr. The @x is copied inside themacro
+ * to avoid code re-ordering where @x gets evaulated within the block that
+ * enables user-space access (thus possibly bypassing some of the protection
+ * this feautre provides).
*
* Caller must check the pointer withaccess_ok()before calling this
* function.arch/riscv/include/asm/uaccess.h +1
-0@@ -316,12 +319,13 @@ do { \ #define __put_user(x, ptr) \ ({ \ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*__gu_ptr) __val = (x); \arch/riscv/include/asm/uaccess.h do { \ · line 331
Quoted 10 lines
arch/riscv/include/asm/uaccess.h +1
-1long __pu_err = 0; \ \ __chk_user_ptr(__gu_ptr); \ \ __enable_user_access(); \ - __put_user_nocheck(x, __gu_ptr, __pu_err); \ + __put_user_nocheck(__val, __gu_ptr, __pu_err); \ __disable_user_access(); \ \ __pu_err; \_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Signature · 9 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Alex Ghiti
reviewerLe 3/19/21 à 11:09 AM, Ben Dooks a écrit :
Quoted 30 lines · Alex Ghiti
Quoted 23 lines
Le 3/18/21 à 6:41 PM, Ben Dooks a écrit :
Quoted 17 lines
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so:1:
enable_user_access()
2: evaluate 'a'
3: put 'a' to 'ptr'
4:disable_user_acess()The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assinging 'x' to
__valat the start, and hoping the compiler barriers inenable_user_access()do the job of ordering step 2 before step 1.Quoted 4 lines
This has shown up in a bug where 'a' sleeps and thus schedules out and loses the
SR_SUMflag. This isn't sufficient to fully fix, but should reduce the window of opportunity.Quoted 5 lines
I would say this patch is enough to fix the issue because it only happens when 'a' *explicitly schedules* when in __enable_user_access()/__disable_user_access(). Otherwise, I see 2 cases:
- user memory is correctly mapped and nothing stops the current process.
- an exception (interrupt or trap) is triggered: in those cases, the exception path correctly saves and restoresSR_SUM.
Quoted 5 lines
This fixes part of the other issue.
I did point out in the other email there could be longer cases where the protections are disabled. The saving of the flags over
switch_to()is still necessary.
I can't find your explanation, could you elaborate a bit more here on why this fix is not enough ?
Thanks !
Quoted 53 lines
Also, I am not sure if this will guarantee ordering. It does seem to fix it for the cases I checked
Quoted 47 lines
Quoted 31 lines
Cc Arnd Bergman <[email protected]>---
arch/riscv/include/asm/uaccess.h| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)arch/riscv/include/asm/uaccess.h +1
-1@@ -306,7 +306,10 @@ do { \ * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable - * to the result of dereferencing @ptr. + * to the result of dereferencing @ptr. The @x is copied inside themacro
+ * to avoid code re-ordering where @x gets evaulated within the block that
+ * enables user-space access (thus possibly bypassing some of the protection
+ * this feautre provides).
*
* Caller must check the pointer withaccess_ok()before calling this
* function.arch/riscv/include/asm/uaccess.h +1
-0@@ -316,12 +319,13 @@ do { \ #define __put_user(x, ptr) \ ({ \ __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*__gu_ptr) __val = (x); \arch/riscv/include/asm/uaccess.h do { \ · line 331
Quoted 10 lines
arch/riscv/include/asm/uaccess.h +1
-1long __pu_err = 0; \ \ __chk_user_ptr(__gu_ptr); \ \ __enable_user_access(); \ - __put_user_nocheck(x, __gu_ptr, __pu_err); \ + __put_user_nocheck(__val, __gu_ptr, __pu_err); \ __disable_user_access(); \ \ __pu_err; \_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 31 lines · Alex Ghiti
Le 3/19/21 à 11:09 AM, Ben Dooks a écrit :
Quoted 30 lines · Alex Ghiti
Quoted 28 lines
Le 3/18/21 à 6:41 PM, Ben Dooks a écrit :
Quoted 17 lines
The <
asm/uaccess.h> header has a problem withput_user(a, ptr)if the 'a' is not a simple variable, such as a function. This can lead to the compiler producing code as so:1:
enable_user_access()
2: evaluate 'a'
3: put 'a' to 'ptr'
4:disable_user_acess()The issue is that 'a' is now being evaluated with the user memory protections disabled. So we try and force the evaulation by assinging 'x' to
__valat the start, and hoping the compiler barriers inenable_user_access()do the job of ordering step 2 before step 1.Quoted 4 lines
This has shown up in a bug where 'a' sleeps and thus schedules out and loses the
SR_SUMflag. This isn't sufficient to fully fix, but should reduce the window of opportunity.I would say this patch is enough to fix the issue because it only happens when 'a' *explicitly schedules* when in __enable_user_access()/__disable_user_access(). Otherwise, I see 2 cases:
- user memory is correctly mapped and nothing stops the current process.
- an exception (interrupt or trap) is triggered: in those cases, the exception path correctly saves and restoresSR_SUM.
Quoted 7 lines
Quoted 5 lines
This fixes part of the other issue.
I did point out in the other email there could be longer cases where the protections are disabled. The saving of the flags over
switch_to()is still necessary.I can't find your explanation, could you elaborate a bit more here on why this fix is not enough ?
I would have to check if this current applies to riscv, but there is code that does the following (fs/select.c does this):
if (!user_read_access_begin(from, sizeof(*from)))
return -EFAULT;
unsafe_get_user(to->p, &from->p, Efault);
unsafe_get_user(to->size, &from->size, Efault);
user_read_access_end();My argument for fixing with both is:
- cover more than the put_user() case
- try and avoid any future bug
- ensure we do not leak SR_SUM elsewhere
I may also have a quick check to see if we don't also leak other flags during these swaps. It might be we should save and restore all flags.
I do see that this fix is going to hit a good proportion of the cases we've seen so far. I could try and run a stress test with just this in over the weekend (so far syz-stress has been running for over 24hrs with minimal issues)
Signature · 9 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv