xtensa: align: validate user access in fast_load_store under MMU
PATCH 1/1 xtensa: align: validate user access in fast_load_store under MMU
fast_load_store aligns faulting address then reads two words with l32i. With CONFIG_MMU, bad user access can fault in this path.
Replace arbitrary jump to .Linvalid_instruction with access_ok() to validate aligned address before loads and branch to .Linvalid_instruction on failure.
a0: scratch. a2: sp. Matches existing usage in
Xtensa entry.S.
Testing:
- Built Xtensa with CONFIG_MMU enabled
- objdump before/after comparison and validated code path
- Ran emulated Xtensa device with QEMU and manually triggered unaligned and aligned loads
--- arch/xtensa/include/asm/asm-uaccess.h | 2 +- arch/xtensa/kernel/align.S | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/xtensa/include/asm/asm-uaccess.h +1 -1
@@ -68,7 +68,7 @@* <aa> register containing memory address * <as> register containing memory size * <at> temp register -* <sp> +* <sp> unused; ignored * <error> label to branch to on error; implies fall-through * macro on success * On Exit:
arch/xtensa/kernel/align.S +6 -3
@@ -21,6 +21,9 @@ #include <asm/asm-offsets.h> #include <asm/asmmacro.h> #include <asm/processor.h> +#ifdef CONFIG_MMU +#include <asm/asm-uaccess.h> +#endif #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE #define LOAD_EXCEPTION_HANDLER
@@ -178,15 +181,15 @@ ENTRY(fast_load_store) bbsi.l a4, OP1_SI_BIT + INSN_OP1, .Linvalid_instruction 1: - movi a3, ~3 + movi a3, ~3 and a3, a3, a8 # align memory address __ssa8 a8 #ifdef CONFIG_MMU /* l32e can't be used here even when it's available. */ - /* TODO access_ok(a3) could be used here */ - j .Linvalid_instruction + movi a5, 8 + access_ok a3, a5, a0, a2, .Linvalid_instruction #endif l32i a5, a3, 0 l32i a6, a3, 4
Signature · 1 line
2.43.0
PATCH 1/1 xtensa: mmu: validate user access in fast_load_store with MMU
fast_load_store aligns faulting address then reads two words with l32i. With CONFIG_MMU, bad user access can fault in this path.
Replace arbitrary jump to .Linvalid_instruction with access_ok() to validate aligned address before loads and branch to .Linvalid_instruction on failure.
a0: scratch. a2: sp. Matches existing usage in
Xtensa entry.S.
Testing:
- Built Xtensa with CONFIG_MMU enabled
- objdump before/after comparison and validated code path
- Ran emulated Xtensa device with QEMU and manually triggered unaligned and aligned loads
--- arch/xtensa/kernel/align.S | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
arch/xtensa/kernel/align.S +7 -3
@@ -21,6 +21,9 @@ #include <asm/asm-offsets.h> #include <asm/asmmacro.h> #include <asm/processor.h> +#ifdef CONFIG_MMU +#include <asm/asm-uaccess.h> +#endif #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE #define LOAD_EXCEPTION_HANDLER
@@ -178,16 +181,17 @@ ENTRY(fast_load_store) bbsi.l a4, OP1_SI_BIT + INSN_OP1, .Linvalid_instruction 1: - movi a3, ~3 + movi a3, ~3 and a3, a3, a8 # align memory address __ssa8 a8 #ifdef CONFIG_MMU /* l32e can't be used here even when it's available. */ - /* TODO access_ok(a3) could be used here */ - j .Linvalid_instruction + movi a5, 8 + access_ok a3, a5, a0, a2, .Linvalid_instruction #endif + l32i a5, a3, 0 l32i a6, a3, 4 __src_b a3, a5, a6 # a3 has the data word
Signature · 1 line
2.43.0
Max Filippov
maintainerRe: [PATCH] xtensa: mmu: validate user access in fast_load_store with MMU
Hi Ricky,
Quoted 4 lines · Ricky Ringler
fast_load_storealigns faulting address then reads two words with l32i. WithCONFIG_MMU, bad user access can fault in this path.
This fault can also happen when accessing memory from the kernel mode.
Quoted 7 lines
Replace arbitrary jump to .Linvalid_instruction with
access_ok()to validate aligned address before loads and branch to .Linvalid_instruction on failure.a0: scratch. a2: sp. Matches existing usage in
Xtensaentry.S.
Quoted 7 lines
Tested-by Ricky Ringler <[email protected]>Testing:
- Built Xtensa withCONFIG_MMUenabled
- objdump before/after comparison and validated code path
- Ran emulated Xtensa device with QEMU and manually triggered unaligned and aligned loads
fast_load_store is not called for the unaligned access, it's called for the load/store exception. On the real hardware it happens e.g. when l8ui is used to load from the address range mapped to the instruction fetch bus. This doesn't happen on QEMU.
Quoted 20 lines
Signed-off-by Ricky Ringler <[email protected]>--- arch/xtensa/kernel/align.S | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)arch/xtensa/kernel/align.S +3
-0@@ -21,6 +21,9 @@ #include <asm/asm-offsets.h> #include <asm/asmmacro.h> #include <asm/processor.h> +#ifdef CONFIG_MMU +#include <asm/asm-uaccess.h> +#endif #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE #define LOAD_EXCEPTION_HANDLER
arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 184
Quoted 5 lines
arch/xtensa/kernel/align.S +1
-1@@ -178,16 +181,17 @@ ENTRY(fast_load_store) bbsi.l a4, OP1_SI_BIT + INSN_OP1, .Linvalid_instruction 1: - movi a3, ~3 + movi a3, ~3
There's a formatting change here, but the original formatting is fine.
arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 192
Quoted 10 lines
arch/xtensa/kernel/align.S +2
-2and a3, a3, a8 # align memory address __ssa8 a8 #ifdef CONFIG_MMU /* l32e can't be used here even when it's available. */ - /* TODO access_ok(a3) could be used here */ - j .Linvalid_instruction + movi a5, 8 + access_ok a3, a5, a0, a2, .Linvalid_instruction
a0 cannot be used here, it is clobbered by the macro, but the value loaded into it by the code above is used in the code below. a6 would work though.
access_ok only checks address validity for the user mode, but it is also possible to get here from the kernel mode.
New code should use tabs, not spaces, just like the existing code.
Quoted 8 lines
arch/xtensa/kernel/align.S +1
-1#endif + l32i a5, a3, 0 l32i a6, a3, 4 __src_b a3, a5, a6 # a3 has the data word --2.43.0
Signature · 2 lines
Thanks. -- Max
Max Filippov
maintainerRe: [PATCH] xtensa: mmu: validate user access in fast_load_store with MMU
Quoted 8 lines · Max Filippov
arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 192
Quoted 6 lines
#ifdef CONFIG_MMU /* l32e can't be used here even when it's available. */arch/xtensa/kernel/align.S +2
-2- /* TODO access_ok(a3) could be used here */ - j .Linvalid_instruction + movi a5, 8 + access_ok a3, a5, a0, a2, .Linvalid_instructionNew code should use tabs, not spaces, just like the existing code.
Disregard that, the other version of the patch is formatted with spaces, this one is good.
Please add patch version if you send multiple versions of the patch.
Signature · 2 lines
Thanks. -- Max
PATCH v2 1/1 xtensa: align: validate access in fast_load_store
In reply to Ricky Ringleraccess_ok() is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode behavior unchanged.
v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch
Testing:
- Built with CONFIG_MMU enabled
- objdump before/after comparison and validated code path
--- arch/xtensa/kernel/align.S | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
arch/xtensa/kernel/align.S +9 -2
@@ -21,6 +21,9 @@ #include <asm/asm-offsets.h> #include <asm/asmmacro.h> #include <asm/processor.h> +#ifdef CONFIG_MMU +#include <asm/asm-uaccess.h> +#endif #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE #define LOAD_EXCEPTION_HANDLER
@@ -185,8 +188,12 @@ ENTRY(fast_load_store) #ifdef CONFIG_MMU /* l32e can't be used here even when it's available. */ - /* TODO access_ok(a3) could be used here */ - j .Linvalid_instruction + rsr a6, ps + bbsi.l a6, PS_UM_BIT, 1f # user mode + j .Linvalid_instruction # kernel mode +1: + movi a5, 8 + access_ok a3, a5, a6, a2, .Linvalid_instruction #endif l32i a5, a3, 0 l32i a6, a3, 4
Signature · 1 line
2.43.0
Max Filippov
maintainerRe: [PATCH v2] xtensa: align: validate access in fast_load_store
Quoted 9 lines · Ricky Ringler
access_ok()is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode behavior unchanged.v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch
Please move the patch change log past the --- marker so that it is dropped when the patch is committed.
Quoted 26 lines
Tested-by Ricky Ringler <[email protected]>Testing:
- Built withCONFIG_MMUenabled
- objdump before/after comparison and validated code pathSigned-off-by Ricky Ringler <[email protected]>--- arch/xtensa/kernel/align.S | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)arch/xtensa/kernel/align.S +3
-0@@ -21,6 +21,9 @@ #include <asm/asm-offsets.h> #include <asm/asmmacro.h> #include <asm/processor.h> +#ifdef CONFIG_MMU +#include <asm/asm-uaccess.h> +#endif #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE #define LOAD_EXCEPTION_HANDLER
arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 193
Quoted 8 lines
arch/xtensa/kernel/align.S +3
-2@@ -185,8 +188,12 @@ ENTRY(fast_load_store) #ifdef CONFIG_MMU /* l32e can't be used here even when it's available. */ - /* TODO access_ok(a3) could be used here */ - j .Linvalid_instruction + rsr a6, ps + bbsi.l a6, PS_UM_BIT, 1f # user mode + j .Linvalid_instruction # kernel mode
It should be the other way around: kernel mode is allowed to access anything, user mode needs address checking.
Quoted 9 lines
arch/xtensa/kernel/align.S +3
-1+1: + movi a5, 8 + access_ok a3, a5, a6, a2, .Linvalid_instruction #endif l32i a5, a3, 0 l32i a6, a3, 4 --2.43.0
Signature · 2 lines
Thanks. -- Max
PATCH v3 1/1 xtensa: align: validate access in fast_load_store
access_ok() is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode skips access_ok().
---
v3:
- Changed kernel mode behavior to skip access_ok()
v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch
Testing:
- Built with CONFIG_MMU enabled
- objdump before/after comparison and validated code path
arch/xtensa/kernel/align.S | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
arch/xtensa/kernel/align.S +8 -2
@@ -21,6 +21,9 @@ #include <asm/asm-offsets.h> #include <asm/asmmacro.h> #include <asm/processor.h> +#ifdef CONFIG_MMU +#include <asm/asm-uaccess.h> +#endif #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE #define LOAD_EXCEPTION_HANDLER
@@ -185,8 +188,11 @@ ENTRY(fast_load_store) #ifdef CONFIG_MMU /* l32e can't be used here even when it's available. */ - /* TODO access_ok(a3) could be used here */ - j .Linvalid_instruction + rsr a6, ps + bbci.l a6, PS_UM_BIT, 1f # kernel mode + movi a5, 8 + access_ok a3, a5, a6, a2, .Linvalid_instruction +1: #endif l32i a5, a3, 0 l32i a6, a3, 4
Signature · 1 line
2.43.0
Max Filippov
maintainerRe: [PATCH v3] xtensa: align: validate access in fast_load_store
Quoted 17 lines · Ricky Ringler
access_ok()is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode skipsaccess_ok().Tested-by Ricky Ringler <[email protected]>Signed-off-by Ricky Ringler <[email protected]>---
v3:
- Changed kernel mode behavior to skipaccess_ok()v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch
Quoted 6 lines
Testing:
- Built withCONFIG_MMUenabled
- objdump before/after comparison and validated code patharch/xtensa/kernel/align.S | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
Thanks. Applied to my xtensa tree.
-- Max