Skip to contentSkip to conversation

valid_user_regs() backport

stable:PATCH James Morse 3 participants 6 messages Jul 18, 2016 – Oct 8, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi all,

These three copies of the same patch backport dbd4d7ca563f ("arm64: Rework valid_user_regs") to the stable kernels listed on kernel.org as far back as v3.16.

This patch fixed a userspace triggerable oops when userspace tries to switch to a mode not supported by the kernel:

[  286.628508] Bad mode in Synchronous Abort handler detected, code 0x46000000 -- SVC (AArch32)
[  286.639625] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.648391] Hardware name: ARM Juno development board (r1) (DT)
[ ... ]
[  286.716784] Bad mode in Synchronous Abort handler detected, code 0x86000007 -- IABT (current EL)
[  286.725483] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.731513] Hardware name: ARM Juno development board (r1) (DT)
[ ... ]
[  286.840815] Internal error: Oops - bad mode: 0 [#1] PREEMPT SMP
[  286.846673] Modules linked in:
[  286.849699] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.855728] Hardware name: ARM Juno development board (r1) (DT)

Thanks,

James

 arch/arm64/include/asm/ptrace.h | 34 +++---------------
 arch/arm64/kernel/ptrace.c      | 80 +++++++++++++++++++++++++++++++++++++++--
 arch/arm64/kernel/signal.c      |  4 +--
 arch/arm64/kernel/signal32.c    |  2 +-
 4 files changed, 86 insertions(+), 34 deletions(-)
Signature · 1 line
2.8.0.rc3
#1 lore raw

[stable:PATCH] arm64: Rework valid_user_regs (v4.1+)

From: Mark Rutland <[email protected]>

commit dbd4d7ca563fd0a8949718d35ce197e5642d5d9d upstream.

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).

[ rebased for v4.1+
This avoids a user-triggerable Oops() if a task is switched to a mode not supported by the kernel (e.g. switching a 64-bit task to AArch32).
]

---
For example, switching a task to 32bit mode, when the kernel was not built with support for this will cause:

[  286.628508] Bad mode in Synchronous Abort handler detected, code 0x46000000 -- SVC (AArch32)
[  286.639625] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.648391] Hardware name: ARM Juno development board (r1) (DT)
[ ... ]
[  286.716784] Bad mode in Synchronous Abort handler detected, code 0x86000007 -- IABT (current EL)
[  286.725483] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.731513] Hardware name: ARM Juno development board (r1) (DT)
[ ... ]
[  286.840815] Internal error: Oops - bad mode: 0 [#1] PREEMPT SMP
[  286.846673] Modules linked in:
[  286.849699] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.855728] Hardware name: ARM Juno development board (r1) (DT)
 arch/arm64/include/asm/ptrace.h | 33 ++---------------
 arch/arm64/kernel/ptrace.c      | 81 ++++++++++++++++++++++++++++++++++++++++-
 arch/arm64/kernel/signal.c      |  4 +-
 arch/arm64/kernel/signal32.c    |  2 +-
 4 files changed, 86 insertions(+), 34 deletions(-)
arch/arm64/include/asm/ptrace.h +4 -29
index e9e5467e0bf4..a307eb6e7fa8 100644
@@ -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 +79 -2

102 lines not on this page · raw message

arch/arm64/kernel/signal.c +2 -2
index e18c48cb6db1..a8eafdbc7cb8 100644
@@ -115,7 +115,7 @@ static int restore_sigframe(struct pt_regs *regs,
 	 */
 	regs->syscallno = ~0UL;
 
-	err |= !valid_user_regs(&regs->user_regs);
+	err |= !valid_user_regs(&regs->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(&regs->user_regs);
+	ret |= !valid_user_regs(&regs->user_regs, current);
 
 	/*
 	 * Fast forward the stepping logic so we step into the signal
arch/arm64/kernel/signal32.c +1 -1
index 71ef6dc89ae5..107335637390 100644
@@ -356,7 +356,7 @@ static int compat_restore_sigframe(struct pt_regs *regs,
 	 */
 	regs->syscallno = ~0UL;
 
-	err |= !valid_user_regs(&regs->user_regs);
+	err |= !valid_user_regs(&regs->user_regs, current);
 
 	aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
 	if (err == 0)
Signature · 1 line
2.8.0.rc3
#2 lore raw

[stable:PATCH] arm64: Rework valid_user_regs (v3.18)

In reply to James Morse

From: Mark Rutland <[email protected]>

commit dbd4d7ca563fd0a8949718d35ce197e5642d5d9d upstream.

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).

[ rebased for v3.18
This avoids a user-triggerable Oops() if a task is switched to a mode not supported by the kernel (e.g. switching a 64-bit task to AArch32).

v3.18 does not support SETEND, support for this was added by 2d888f48e056 ("arm64: Emulate SETEND for AArch32 tasks") in v3.20 This backport forces the kernel endianness on userspace.
]

---
For example, switching a task to 32bit mode, when the kernel was not built with support for this will cause:

[   58.637566] Bad mode in Synchronous Abort handler detected, code 0x46000000
[   58.646842] CPU: 0 PID: 1802 Comm: inter Tainted: G        W      3.18.36 #4519
[ ... ]
[   58.719758] Bad mode in Synchronous Abort handler detected, code 0x86000007
[   58.726651] CPU: 0 PID: 1802 Comm: inter Tainted: G        W      3.18.36 #4519
[ ... ]
[   58.837320] Internal error: Oops - bad mode: 0 [#1] PREEMPT SMP
[   58.843177] Modules linked in:
[   58.846203] CPU: 0 PID: 1802 Comm: inter Tainted: G        W      3.18.36 #4519
 arch/arm64/include/asm/ptrace.h | 34 +++---------------
 arch/arm64/kernel/ptrace.c      | 79 +++++++++++++++++++++++++++++++++++++++--
 arch/arm64/kernel/signal.c      |  4 +--
 arch/arm64/kernel/signal32.c    |  2 +-
 4 files changed, 85 insertions(+), 34 deletions(-)
arch/arm64/include/asm/ptrace.h +5 -29
index 41ed9e13795e..0d07fdb3fe59 100644
@@ -58,6 +58,8 @@
 #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
+
 /*
  * These are 'magic' values for PTRACE_PEEKUSR that return info about where a
  * process is located in memory.
@@ -144,35 +146,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 +77 -2

94 lines not on this page · raw message

arch/arm64/kernel/signal.c +2 -2
index 6fa792137eda..ae65430f5fb7 100644
@@ -115,7 +115,7 @@ static int restore_sigframe(struct pt_regs *regs,
 	 */
 	regs->syscallno = ~0UL;
 
-	err |= !valid_user_regs(&regs->user_regs);
+	err |= !valid_user_regs(&regs->user_regs, current);
 
 	if (err == 0) {
 		struct fpsimd_context *fpsimd_ctx =
@@ -314,7 +314,7 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 	/*
 	 * Check that the resulting registers are actually sane.
 	 */
-	ret |= !valid_user_regs(&regs->user_regs);
+	ret |= !valid_user_regs(&regs->user_regs, current);
 
 	/*
 	 * Fast forward the stepping logic so we step into the signal
arch/arm64/kernel/signal32.c +1 -1
index 15dd021b0025..1714e25bf85b 100644
@@ -350,7 +350,7 @@ static int compat_restore_sigframe(struct pt_regs *regs,
 	 */
 	regs->syscallno = ~0UL;
 
-	err |= !valid_user_regs(&regs->user_regs);
+	err |= !valid_user_regs(&regs->user_regs, current);
 
 	aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
 	if (err == 0)
Signature · 1 line
2.8.0.rc3
#3 lore raw

[stable:PATCH] arm64: Rework valid_user_regs (v3.16)

In reply to James Morse

From: Mark Rutland <[email protected]>

commit dbd4d7ca563fd0a8949718d35ce197e5642d5d9d upstream.

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).

[ rebased for v3.16
This avoids a user-triggerable Oops() if a task is switched to a mode not supported by the kernel (e.g. switching a 64-bit task to AArch32).

v3.16 does not support SETEND, support for this was added by 2d888f48e056 ("arm64: Emulate SETEND for AArch32 tasks") in v3.20 This backport forces the kernel endianness on userspace.

Added a DBG_SPSR_SS define hidden by #ifdefs to avoid conflicts with other backports.
]

---
For example, switching a task to 32bit mode, when the kernel was not built with support for this will cause:

[   39.634111] Bad mode in Synchronous Abort handler detected, code 0x46000000
[   39.643381] CPU: 0 PID: 1712 Comm: inter Not tainted 3.16.36 #4514
[ ... ]
[   39.709287] Bad mode in Synchronous Abort handler detected, code 0x86000007
[   39.716180] CPU: 0 PID: 1712 Comm: inter Not tainted 3.16.36 #4514
[ ... ]
[   39.825750] Internal error: Oops - bad mode: 0 [#1] PREEMPT SMP
[   39.831608] Modules linked in:
[   39.834634] CPU: 0 PID: 1712 Comm: inter Not tainted 3.16.36 #4514
 arch/arm64/include/asm/ptrace.h | 34 +++--------------
 arch/arm64/kernel/ptrace.c      | 83 ++++++++++++++++++++++++++++++++++++++++-
 arch/arm64/kernel/signal.c      |  4 +-
 arch/arm64/kernel/signal32.c    |  2 +-
 4 files changed, 89 insertions(+), 34 deletions(-)
arch/arm64/include/asm/ptrace.h +5 -29
index 88d6e2436808..300a382ed88f 100644
@@ -58,6 +58,8 @@
 #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
+
 /*
  * These are 'magic' values for PTRACE_PEEKUSR that return info about where a
  * process is located in memory.
@@ -144,35 +146,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 +81 -2

104 lines not on this page · raw message

arch/arm64/kernel/signal.c +2 -2
index 6357b9c6c90e..d1ae7c217ba9 100644
@@ -115,7 +115,7 @@ static int restore_sigframe(struct pt_regs *regs,
 	 */
 	regs->syscallno = ~0UL;
 
-	err |= !valid_user_regs(&regs->user_regs);
+	err |= !valid_user_regs(&regs->user_regs, current);
 
 	if (err == 0) {
 		struct fpsimd_context *fpsimd_ctx =
@@ -322,7 +322,7 @@ static void handle_signal(unsigned long sig, struct k_sigaction *ka,
 	/*
 	 * Check that the resulting registers are actually sane.
 	 */
-	ret |= !valid_user_regs(&regs->user_regs);
+	ret |= !valid_user_regs(&regs->user_regs, current);
 
 	if (ret != 0) {
 		force_sigsegv(sig, tsk);
arch/arm64/kernel/signal32.c +1 -1
index e3ac8f0c0fc7..03520c650701 100644
@@ -350,7 +350,7 @@ static int compat_restore_sigframe(struct pt_regs *regs,
 	 */
 	regs->syscallno = ~0UL;
 
-	err |= !valid_user_regs(&regs->user_regs);
+	err |= !valid_user_regs(&regs->user_regs, current);
 
 	aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
 	if (err == 0)
Signature · 1 line
2.8.0.rc3
reviewer Reviewed-by
#5 lore raw
In reply to James Morse
Quoted 1 line · James Morse

Hi all,

Hi James,

Quoted 6 lines

These three copies of the same patch backport dbd4d7ca563f ("arm64: Rework valid_user_regs") to the stable kernels listed on kernel.org as far back as v3.16.

This patch fixed a userspace triggerable oops when userspace tries to switch to a mode not supported by the kernel:

Quoted 12 lines
[  286.628508] Bad mode in Synchronous Abort handler detected, code 0x46000000 -- SVC (AArch32)
[  286.639625] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.648391] Hardware name: ARM Juno development board (r1) (DT)
[ ... ]
[  286.716784] Bad mode in Synchronous Abort handler detected, code 0x86000007 -- IABT (current EL)
[  286.725483] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.731513] Hardware name: ARM Juno development board (r1) (DT)
[ ... ]
[  286.840815] Internal error: Oops - bad mode: 0 [#1] PREEMPT SMP
[  286.846673] Modules linked in:
[  286.849699] CPU: 3 PID: 2072 Comm: inter Not tainted 4.4.14 #4504
[  286.855728] Hardware name: ARM Juno development board (r1) (DT)

Thanks for doing this.

Having looked over them, I believe that the fixups for all three backport patches are correct. So FWIW, for all the patches:

Thanks,
Mark.

#4 lore raw

[stable:PATCH] arm64: Rework valid_user_regs (v3.16)

In reply to James Morse
Quoted 31 lines · James Morse

From: Mark Rutland <[email protected]>

commit dbd4d7ca563fd0a8949718d35ce197e5642d5d9d upstream.

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).

[ rebased for v3.16
This avoids a user-triggerable Oops() if a task is switched to a mode not supported by the kernel (e.g. switching a 64-bit task to AArch32).

v3.16 does not support SETEND, support for this was added by 2d888f48e056 ("arm64: Emulate SETEND for AArch32 tasks") in v3.20 This backport forces the kernel endianness on userspace.

Quoted 5 lines

Added a DBG_SPSR_SS define hidden by #ifdefs to avoid conflicts with other backports.
]

[...]

Belatedly queued this up for 3.16.

Ben.

Signature · 10 lines
Ben Hutchings
compatible: Gracefully accepts erroneous data from any source

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20171008/2cd3880d/attachment.sig>