arm64: uaccess: disable preempt during uaccess through ttbr0
PATCH 1/2 arm64: uaccess: disable preempt during uaccess through ttbr0
If using software PAN, the ttbr0 should keep unchanged, otherwise, considering the following scenario:
task1
__uaccess_ttbr0_enable()
switch_mm(this,next,tsk), which resets ttbr0 to __pa_symbol(reserved_pg_dir) switch_mm(prev,this,tsk), which can not re-install the user page table automatically
Tackle this issue by disabling preemption.
--- arch/arm64/include/asm/uaccess.h | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/include/asm/uaccess.h +2 -0
@@ -121,6 +121,7 @@ static inline bool uaccess_ttbr0_disable(void)if (!system_uses_ttbr0_pan ())return false;__uaccess_ttbr0_disable (); +preempt_enable ();return true; }
@@ -128,6 +129,7 @@ static inline bool uaccess_ttbr0_enable(void) {if (!system_uses_ttbr0_pan ())return false; +preempt_disable ();__uaccess_ttbr0_enable ();return true; }
Signature · 7 lines
2.31.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 2/2 arm64: uaccess: disable preempt during uaccess PSTATE.{PAN, TCO}
For the pair of uaccess_enable_privileged()/uaccess_disable_privileged(), if preemption happens, the exposure of the access to user space by another code piece is unexpected.
For the pair of __uaccess_enable_tco_async()/__uaccess_disable_tco_async(), the change of PSTATE.TCO is exposed unexpectedly.
For the two cases, simply disable preemption to fix the issue.
--- arch/arm64/include/asm/uaccess.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
arch/arm64/include/asm/uaccess.h +11 -4
@@ -193,14 +193,18 @@ static inline void __uaccess_enable_tco(void)*/ static inline void __uaccess_disable_tco_async (void ) { -if (system_uses_mte_async_or_asymm_mode ()) -__uaccess_disable_tco (); +if (system_uses_mte_async_or_asymm_mode ()) { +__uaccess_disable_tco (); +preempt_enable (); + } }static inline void __uaccess_enable_tco_async (void ) { -if (system_uses_mte_async_or_asymm_mode ()) +if (system_uses_mte_async_or_asymm_mode ()) { +preempt_disable ();__uaccess_enable_tco (); + } }static inline void uaccess_disable_privileged (void )
@@ -208,13 +212,16 @@ static inline void uaccess_disable_privileged(void)__uaccess_disable_tco ();if (uaccess_ttbr0_disable ()) -return ; + goto out;__uaccess_enable_hw_pan (); +out: +preempt_enable (); }static inline void uaccess_enable_privileged (void ) { +preempt_disable ();__uaccess_enable_tco ();if (uaccess_ttbr0_enable ())
Signature · 7 lines
2.31.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Catalin Marinas
maintainerRe: [PATCH 2/2] arm64: uaccess: disable preempt during uaccess PSTATE.{PAN,TCO}
Quoted 4 lines · Pingfan Liu
For the pair of uaccess_enable_privileged()/uaccess_disable_privileged(), if preemption happens, the exposure of the access to user space by another code piece is unexpected.
How can this happen? We set PAN on exception entry.
Quoted 3 lines
For the pair of __uaccess_enable_tco_async()/__uaccess_disable_tco_async(), the change of PSTATE.TCO is exposed unexpectedly.
As above, we disable TCO on exception entry.
Quoted 28 lines
For the two cases, simply disable preemption to fix the issue.
Signed-off-by Pingfan Liu <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Mark Rutland <[email protected]>Cc Vincenzo Frascino <[email protected]>Cc Andrey Konovalov <[email protected]>Cc Arnd Bergmann <[email protected]>--- arch/arm64/include/asm/uaccess.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)arch/arm64/include/asm/uaccess.h +4
-2@@ -193,14 +193,18 @@ static inline void __uaccess_enable_tco(void)*/ static inline void __uaccess_disable_tco_async (void ) { -if (system_uses_mte_async_or_asymm_mode ()) -__uaccess_disable_tco (); +if (system_uses_mte_async_or_asymm_mode ()) { +__uaccess_disable_tco (); +preempt_enable (); + }
arch/arm64/include/asm/uaccess.h static inline void __uaccess_enable_tco(void) · line 208
Quoted 10 lines
arch/arm64/include/asm/uaccess.h +3
-1}static inline void __uaccess_enable_tco_async (void ) { -if (system_uses_mte_async_or_asymm_mode ()) +if (system_uses_mte_async_or_asymm_mode ()) { +preempt_disable ();__uaccess_enable_tco (); + } }
As with the previous patch, disabling preemption prior to a might_sleep() is wrong.
Signature · 6 lines
Catalin _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 2/2] arm64: uaccess: disable preempt during uaccess PSTATE.{PAN,TCO}
Quoted 8 lines · Catalin Marinas
Quoted 4 lines · Pingfan Liu
For the pair of uaccess_enable_privileged()/uaccess_disable_privileged(), if preemption happens, the exposure of the access to user space by another code piece is unexpected.
How can this happen? We set PAN on exception entry.
Yes, I make the some mistake as [1/2]
Quoted 6 lines
Quoted 3 lines
For the pair of __uaccess_enable_tco_async()/__uaccess_disable_tco_async(), the change of PSTATE.TCO is exposed unexpectedly.
As above, we disable TCO on exception entry.
Yes.
Quoted 30 lines
Quoted 30 lines
For the two cases, simply disable preemption to fix the issue.
Signed-off-by Pingfan Liu <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Mark Rutland <[email protected]>Cc Vincenzo Frascino <[email protected]>Cc Andrey Konovalov <[email protected]>Cc Arnd Bergmann <[email protected]>--- arch/arm64/include/asm/uaccess.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)arch/arm64/include/asm/uaccess.h +4
-2@@ -193,14 +193,18 @@ static inline void __uaccess_enable_tco(void)*/ static inline void __uaccess_disable_tco_async (void ) { -if (system_uses_mte_async_or_asymm_mode ()) -__uaccess_disable_tco (); +if (system_uses_mte_async_or_asymm_mode ()) { +__uaccess_disable_tco (); +preempt_enable (); + } }
Quoted 12 lines
arch/arm64/include/asm/uaccess.h static inline void __uaccess_enable_tco(void) · line 208
Quoted 8 lines
arch/arm64/include/asm/uaccess.h +3
-1static inline void __uaccess_enable_tco_async (void ) { -if (system_uses_mte_async_or_asymm_mode ()) +if (system_uses_mte_async_or_asymm_mode ()) { +preempt_disable ();__uaccess_enable_tco (); + } }As with the previous patch, disabling preemption prior to a
might_sleep()is wrong.
Right.
I will make clear the kernel_{entry/_exit} part. Appreciate for your help again.
Thanks,
Pingfan
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Catalin Marinas
maintainerQuoted 6 lines · Pingfan Liu
If using software PAN, the ttbr0 should keep unchanged, otherwise, considering the following scenario:
task1
__uaccess_ttbr0_enable()
switch_mm(this,next,tsk), which resets ttbr0 to__pa_symbol(reserved_pg_dir)switch_mm(prev,this,tsk), which can not re-install the user page table automatically
Have you found a real problem with this in practice or just by code inspection?
The assumption is that during uaccess_ttbr0_enable/disable regions, the only way to get into switch_mm() is as a result of a page fault or interrupt. The __swpan_{entry,exit}_el1 functions should take care of restoring ttbr0 when returning to the interrupted context.
Quoted 27 lines
Tackle this issue by disabling preemption.
Signed-off-by Pingfan Liu <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Mark Rutland <[email protected]>Cc Vincenzo Frascino <[email protected]>Cc Andrey Konovalov <[email protected]>Cc Arnd Bergmann <[email protected]>--- arch/arm64/include/asm/uaccess.h | 2 ++ 1 file changed, 2 insertions(+)arch/arm64/include/asm/uaccess.h +1
-0@@ -121,6 +121,7 @@ static inline bool uaccess_ttbr0_disable(void)if (!system_uses_ttbr0_pan ())return false;__uaccess_ttbr0_disable (); +preempt_enable ();return true; }
arch/arm64/include/asm/uaccess.h static inline bool uaccess_ttbr0_enable(void) · line 135
Quoted 7 lines
arch/arm64/include/asm/uaccess.h +1
-0@@ -128,6 +129,7 @@ static inline bool uaccess_ttbr0_enable(void) {if (!system_uses_ttbr0_pan ())return false; +preempt_disable ();__uaccess_ttbr0_enable ();return true; }
preempt_disable() won't help much here if, for example, the subsequent uaccess gets a fault and need to sleep until the accessed page gets available. I suspect you'd get some sleeping in atomic warning as well with the right debug options enabled.
Signature · 6 lines
Catalin _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 11 lines · Catalin Marinas
Quoted 6 lines · Pingfan Liu
If using software PAN, the ttbr0 should keep unchanged, otherwise, considering the following scenario:
task1
__uaccess_ttbr0_enable()
switch_mm(this,next,tsk), which resets ttbr0 to__pa_symbol(reserved_pg_dir)switch_mm(prev,this,tsk), which can not re-install the user page table automaticallyHave you found a real problem with this in practice or just by code inspection?
By code inspection.
Quoted 5 lines
The assumption is that during uaccess_ttbr0_enable/disable regions, the only way to get into
switch_mm()is as a result of a page fault or interrupt. The __swpan_{entry,exit}_el1 functions should take care of restoring ttbr0 when returning to the interrupted context.
Yes, I read the code careless and miss it. I am chewing it.
Thank you very much for pointing me it.
Quoted 26 lines
Quoted 26 lines
Tackle this issue by disabling preemption.
Signed-off-by Pingfan Liu <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Mark Rutland <[email protected]>Cc Vincenzo Frascino <[email protected]>Cc Andrey Konovalov <[email protected]>Cc Arnd Bergmann <[email protected]>--- arch/arm64/include/asm/uaccess.h | 2 ++ 1 file changed, 2 insertions(+)arch/arm64/include/asm/uaccess.h +1
-0@@ -121,6 +121,7 @@ static inline bool uaccess_ttbr0_disable(void)if (!system_uses_ttbr0_pan ())return false;__uaccess_ttbr0_disable (); +preempt_enable ();return true; }
Quoted 14 lines
arch/arm64/include/asm/uaccess.h static inline bool uaccess_ttbr0_enable(void) · line 135
Quoted 8 lines
arch/arm64/include/asm/uaccess.h +1
-0@@ -128,6 +129,7 @@ static inline bool uaccess_ttbr0_enable(void) {if (!system_uses_ttbr0_pan ())return false; +preempt_disable ();__uaccess_ttbr0_enable ();return true; }
preempt_disable()won't help much here if, for example, the subsequent uaccess gets a fault and need to sleep until the accessed page gets available. I suspect you'd get some sleeping in atomic warning as well with the right debug options enabled.
Yes, the user page may be swapped out.
Thank you for the guide.
Regards,
Pingfan
Quoted 7 lines
Signature · 6 lines
Catalin _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel