arm64: Rework valid_user_regs
Subsystems (2)
Mark Rutland
reviewerPATCH v4 1/1 arm64: Rework valid_user_regs
We validate pstate using PSR_MODE32_BIT, which is part of the user-provided pstate (and cannot be trusted). Also, we conflate validation of AArch32 and AArch64 pstate values, making the code difficult to reason about.
Instead, validate the pstate value based on the associated task. The task may or may not be current (e.g. when using ptrace), so this must be passed explicitly by callers. To avoid circular header dependencies via sched.h, is_compat_task is pulled out of asm/ptrace.h.
To make the code possible to reason about, the AArch64 and AArch32 validation is split into separate functions. Software must respect the RES0 policy for SPSR bits, and thus the kernel mirrors the hardware policy (RAZ/WI) for bits as-yet unallocated. When these acquire an architected meaning writes may be permitted (potentially with additional validation).
--- arch/arm64/include/asm/ptrace.h | 33 +++-------------- arch/arm64/kernel/ptrace.c | 80 +++++++++++++++++++++++++++++++++++++++-- arch/arm64/kernel/signal.c | 4 +-- arch/arm64/kernel/signal32.c | 2 +- 4 files changed, 85 insertions(+), 34 deletions(-)
Since v1 [1]:
* Clear the full complement of RES0 bits
* Treat IL as RES0
* Treat SS as RES0 when not tracing
* renames: s/aa64/native, s/aa32/compat/
Since v2 [2]:
* Kill PSR_{f,s,x} and refer to fields explicitly
* Add COMPAT_PSR_GE_MASK
Since v3 [3]:
* Add comment for AArch32 preserved bits
Mark.
[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2016-February/406424.html
[2] http://lists.infradead.org/pipermail/linux-arm-kernel/2016-February/407386.html
[3] http://lists.infradead.org/pipermail/linux-arm-kernel/2016-February/408675.html
arch/arm64/include/asm/ptrace.h +4 -29
@@ -58,6 +58,7 @@#define COMPAT_PSR_Z_BIT 0x40000000#define COMPAT_PSR_N_BIT 0x80000000#define COMPAT_PSR_IT_MASK 0x0600fc00/* If-Then execution state mask */ +#define COMPAT_PSR_GE_MASK 0x000f0000#ifdef CONFIG_CPU_BIG_ENDIAN #define COMPAT_PSR_ENDSTATE COMPAT_PSR_E_BIT
@@ -151,35 +152,9 @@ static inline unsigned long regs_return_value(struct pt_regs *regs)return regs->regs[0 ]; } -/* -* Are the current registers suitable for user mode? (used to maintain -* security in signal handlers) -*/ -static inline int valid_user_regs (struct user_pt_regs *regs) -{ -if (user_mode (regs) && (regs->pstate &PSR_I_BIT ) ==0 ) { - regs->pstate &= ~(PSR_F_BIT |PSR_A_BIT ); - -/* The T bit is reserved for AArch64 */ -if (!(regs->pstate &PSR_MODE32_BIT )) - regs->pstate &= ~COMPAT_PSR_T_BIT ; - -return 1 ; - } - -/* -* Force PSR to something logical... -*/ - regs->pstate &= PSR_f | PSR_s | (PSR_x & ~PSR_A_BIT ) | \ -COMPAT_PSR_T_BIT |PSR_MODE32_BIT ; - -if (!(regs->pstate &PSR_MODE32_BIT )) { - regs->pstate &= ~COMPAT_PSR_T_BIT ; - regs->pstate |= PSR_MODE_EL0t; - } - -return 0 ; -} +/* We must avoid circular header include via sched.h */ +struct task_struct ; +int valid_user_regs (struct user_pt_regs *regs,struct task_struct *task);#define instruction_pointer (regs) ((unsigned long)(regs)->pc)
arch/arm64/kernel/ptrace.c +78 -2
95 lines not on this page · raw message
arch/arm64/kernel/signal.c +2 -2
@@ -115,7 +115,7 @@ static int restore_sigframe(struct pt_regs *regs,*/ regs->syscallno = ~0UL ; - err |= !valid_user_regs (®s->user_regs); + err |= !valid_user_regs (®s->user_regs, current);if (err ==0 ) { struct fpsimd_context *fpsimd_ctx =
@@ -307,7 +307,7 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)/* * Check that the resulting registers are actually sane. */ - ret |= !valid_user_regs (®s->user_regs); + ret |= !valid_user_regs (®s->user_regs, current);/* * Fast forward the stepping logic so we step into the signal
arch/arm64/kernel/signal32.c +1 -1
@@ -356,7 +356,7 @@ static int compat_restore_sigframe(struct pt_regs *regs,*/ regs->syscallno = ~0UL ; - err |= !valid_user_regs (®s->user_regs); + err |= !valid_user_regs (®s->user_regs, current); aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;if (err ==0 )
Signature · 1 line
1.9.1
PATCH v4 1/1 arm64: Rework valid_user_regs
Quoted 23 lines · Mark Rutland
We validate pstate using
PSR_MODE32_BIT, which is part of the user-provided pstate (and cannot be trusted). Also, we conflate validation of AArch32 and AArch64 pstate values, making the code difficult to reason about.Instead, validate the pstate value based on the associated task. The task may or may not be current (e.g. when using ptrace), so this must be passed explicitly by callers. To avoid circular header dependencies via
sched.h,is_compat_taskis pulled out ofasm/ptrace.h.To make the code possible to reason about, the AArch64 and AArch32 validation is split into separate functions. Software must respect the RES0 policy for SPSR bits, and thus the kernel mirrors the hardware policy (RAZ/WI) for bits as-yet unallocated. When these acquire an architected meaning writes may be permitted (potentially with additional validation).
Signed-off-by Mark Rutland <[email protected]>Cc Catalin Marinas <[email protected]>Cc Dave Martin <[email protected]>Cc James Morse <[email protected]>Cc Peter Maydell <[email protected]>Cc Will Deacon <[email protected]>
Quoted 6 lines
--- arch/arm64/include/asm/ptrace.h | 33 +++-------------- arch/arm64/kernel/ptrace.c | 80 +++++++++++++++++++++++++++++++++++++++-- arch/arm64/kernel/signal.c | 4 +-- arch/arm64/kernel/signal32.c | 2 +- 4 files changed, 85 insertions(+), 34 deletions(-)
We'll also need to backport this to the various stable trees.
Will
PATCH v4 1/1 arm64: Rework valid_user_regs
In reply to Mark RutlandQuoted 17 lines · Mark Rutland
We validate pstate using
PSR_MODE32_BIT, which is part of the user-provided pstate (and cannot be trusted). Also, we conflate validation of AArch32 and AArch64 pstate values, making the code difficult to reason about.Instead, validate the pstate value based on the associated task. The task may or may not be current (e.g. when using ptrace), so this must be passed explicitly by callers. To avoid circular header dependencies via
sched.h,is_compat_taskis pulled out ofasm/ptrace.h.To make the code possible to reason about, the AArch64 and AArch32 validation is split into separate functions. Software must respect the RES0 policy for SPSR bits, and thus the kernel mirrors the hardware policy (RAZ/WI) for bits as-yet unallocated. When these acquire an architected meaning writes may be permitted (potentially with additional validation).
Quoted 6 lines
Signed-off-by Mark Rutland <[email protected]>Cc Catalin Marinas <[email protected]>Cc Dave Martin <[email protected]>Cc James Morse <[email protected]>Cc Peter Maydell <[email protected]>Cc Will Deacon <[email protected]>
Applied. Thanks.
(just remember, once it gets a bit more testing in -next and mainline, to send a back-port to stable)
Signature · 1 line
Catalin