Skip to content

Commit 2b3f8e8

Browse files
mikeyozbenh
authored andcommitted
powerpc/tm: Fix userspace stack corruption on signal delivery for active transactions
When in an active transaction that takes a signal, we need to be careful with the stack. It's possible that the stack has moved back up after the tbegin. The obvious case here is when the tbegin is called inside a function that returns before a tend. In this case, the stack is part of the checkpointed transactional memory state. If we write over this non transactionally or in suspend, we are in trouble because if we get a tm abort, the program counter and stack pointer will be back at the tbegin but our in memory stack won't be valid anymore. To avoid this, when taking a signal in an active transaction, we need to use the stack pointer from the checkpointed state, rather than the speculated state. This ensures that the signal context (written tm suspended) will be written below the stack required for the rollback. The transaction is aborted becuase of the treclaim, so any memory written between the tbegin and the signal will be rolled back anyway. For signals taken in non-TM or suspended mode, we use the normal/non-checkpointed stack pointer. Tested with 64 and 32 bit signals Signed-off-by: Michael Neuling <mikey@neuling.org> Cc: <stable@vger.kernel.org> # v3.9 Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
1 parent b75c100 commit 2b3f8e8

7 files changed

Lines changed: 74 additions & 36 deletions

File tree

‎Documentation/powerpc/transactional_memory.txt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ Example signal handler:
147147
fix_the_problem(ucp->dar);
148148
}
149149

150+
When in an active transaction that takes a signal, we need to be careful with
151+
the stack. It's possible that the stack has moved back up after the tbegin.
152+
The obvious case here is when the tbegin is called inside a function that
153+
returns before a tend. In this case, the stack is part of the checkpointed
154+
transactional memory state. If we write over this non transactionally or in
155+
suspend, we are in trouble because if we get a tm abort, the program counter and
156+
stack pointer will be back at the tbegin but our in memory stack won't be valid
157+
anymore.
158+
159+
To avoid this, when taking a signal in an active transaction, we need to use
160+
the stack pointer from the checkpointed state, rather than the speculated
161+
state. This ensures that the signal context (written tm suspended) will be
162+
written below the stack required for the rollback. The transaction is aborted
163+
becuase of the treclaim, so any memory written between the tbegin and the
164+
signal will be rolled back anyway.
165+
166+
For signals taken in non-TM or suspended mode, we use the
167+
normal/non-checkpointed stack pointer.
168+
150169

151170
Failure cause codes used by kernel
152171
==================================

‎arch/powerpc/include/asm/processor.h‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,16 @@ static inline void prefetchw(const void *x)
409409
#endif
410410

411411
#ifdef CONFIG_PPC64
412-
static inline unsigned long get_clean_sp(struct pt_regs *regs, int is_32)
412+
static inline unsigned long get_clean_sp(unsigned long sp, int is_32)
413413
{
414-
unsigned long sp;
415-
416414
if (is_32)
417-
sp = regs->gpr[1] & 0x0ffffffffUL;
418-
else
419-
sp = regs->gpr[1];
420-
415+
return sp & 0x0ffffffffUL;
421416
return sp;
422417
}
423418
#else
424-
static inline unsigned long get_clean_sp(struct pt_regs *regs, int is_32)
419+
static inline unsigned long get_clean_sp(unsigned long sp, int is_32)
425420
{
426-
return regs->gpr[1];
421+
return sp;
427422
}
428423
#endif
429424

‎arch/powerpc/include/asm/signal.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33

44
#define __ARCH_HAS_SA_RESTORER
55
#include <uapi/asm/signal.h>
6+
#include <uapi/asm/ptrace.h>
7+
8+
extern unsigned long get_tm_stackpointer(struct pt_regs *regs);
69

710
#endif /* _ASM_POWERPC_SIGNAL_H */

‎arch/powerpc/kernel/signal.c‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <asm/uaccess.h>
1919
#include <asm/unistd.h>
2020
#include <asm/debug.h>
21+
#include <asm/tm.h>
2122

2223
#include "signal.h"
2324

@@ -30,13 +31,13 @@ int show_unhandled_signals = 1;
3031
/*
3132
* Allocate space for the signal frame
3233
*/
33-
void __user * get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
34+
void __user * get_sigframe(struct k_sigaction *ka, unsigned long sp,
3435
size_t frame_size, int is_32)
3536
{
3637
unsigned long oldsp, newsp;
3738

3839
/* Default to using normal stack */
39-
oldsp = get_clean_sp(regs, is_32);
40+
oldsp = get_clean_sp(sp, is_32);
4041

4142
/* Check for alt stack */
4243
if ((ka->sa.sa_flags & SA_ONSTACK) &&
@@ -175,3 +176,38 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
175176

176177
user_enter();
177178
}
179+
180+
unsigned long get_tm_stackpointer(struct pt_regs *regs)
181+
{
182+
/* When in an active transaction that takes a signal, we need to be
183+
* careful with the stack. It's possible that the stack has moved back
184+
* up after the tbegin. The obvious case here is when the tbegin is
185+
* called inside a function that returns before a tend. In this case,
186+
* the stack is part of the checkpointed transactional memory state.
187+
* If we write over this non transactionally or in suspend, we are in
188+
* trouble because if we get a tm abort, the program counter and stack
189+
* pointer will be back at the tbegin but our in memory stack won't be
190+
* valid anymore.
191+
*
192+
* To avoid this, when taking a signal in an active transaction, we
193+
* need to use the stack pointer from the checkpointed state, rather
194+
* than the speculated state. This ensures that the signal context
195+
* (written tm suspended) will be written below the stack required for
196+
* the rollback. The transaction is aborted becuase of the treclaim,
197+
* so any memory written between the tbegin and the signal will be
198+
* rolled back anyway.
199+
*
200+
* For signals taken in non-TM or suspended mode, we use the
201+
* normal/non-checkpointed stack pointer.
202+
*/
203+
204+
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
205+
if (MSR_TM_ACTIVE(regs->msr)) {
206+
tm_enable();
207+
tm_reclaim(&current->thread, regs->msr, TM_CAUSE_SIGNAL);
208+
if (MSR_TM_TRANSACTIONAL(regs->msr))
209+
return current->thread.ckpt_regs.gpr[1];
210+
}
211+
#endif
212+
return regs->gpr[1];
213+
}

‎arch/powerpc/kernel/signal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
extern void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags);
1414

15-
extern void __user * get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
15+
extern void __user * get_sigframe(struct k_sigaction *ka, unsigned long sp,
1616
size_t frame_size, int is_32);
1717

1818
extern int handle_signal32(unsigned long sig, struct k_sigaction *ka,

‎arch/powerpc/kernel/signal_32.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,6 @@ static int save_tm_user_regs(struct pt_regs *regs,
503503
{
504504
unsigned long msr = regs->msr;
505505

506-
/* tm_reclaim rolls back all reg states, updating thread.ckpt_regs,
507-
* thread.transact_fpr[], thread.transact_vr[], etc.
508-
*/
509-
tm_enable();
510-
tm_reclaim(&current->thread, msr, TM_CAUSE_SIGNAL);
511-
512506
/* Make sure floating point registers are stored in regs */
513507
flush_fp_to_thread(current);
514508

@@ -965,7 +959,7 @@ int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,
965959

966960
/* Set up Signal Frame */
967961
/* Put a Real Time Context onto stack */
968-
rt_sf = get_sigframe(ka, regs, sizeof(*rt_sf), 1);
962+
rt_sf = get_sigframe(ka, get_tm_stackpointer(regs), sizeof(*rt_sf), 1);
969963
addr = rt_sf;
970964
if (unlikely(rt_sf == NULL))
971965
goto badframe;
@@ -1403,7 +1397,7 @@ int handle_signal32(unsigned long sig, struct k_sigaction *ka,
14031397
unsigned long tramp;
14041398

14051399
/* Set up Signal Frame */
1406-
frame = get_sigframe(ka, regs, sizeof(*frame), 1);
1400+
frame = get_sigframe(ka, get_tm_stackpointer(regs), sizeof(*frame), 1);
14071401
if (unlikely(frame == NULL))
14081402
goto badframe;
14091403
sc = (struct sigcontext __user *) &frame->sctx;

‎arch/powerpc/kernel/signal_64.c‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,12 @@ static long setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
154154
* As above, but Transactional Memory is in use, so deliver sigcontexts
155155
* containing checkpointed and transactional register states.
156156
*
157-
* To do this, we treclaim to gather both sets of registers and set up the
158-
* 'normal' sigcontext registers with rolled-back register values such that a
159-
* simple signal handler sees a correct checkpointed register state.
160-
* If interested, a TM-aware sighandler can examine the transactional registers
161-
* in the 2nd sigcontext to determine the real origin of the signal.
157+
* To do this, we treclaim (done before entering here) to gather both sets of
158+
* registers and set up the 'normal' sigcontext registers with rolled-back
159+
* register values such that a simple signal handler sees a correct
160+
* checkpointed register state. If interested, a TM-aware sighandler can
161+
* examine the transactional registers in the 2nd sigcontext to determine the
162+
* real origin of the signal.
162163
*/
163164
static long setup_tm_sigcontexts(struct sigcontext __user *sc,
164165
struct sigcontext __user *tm_sc,
@@ -184,16 +185,6 @@ static long setup_tm_sigcontexts(struct sigcontext __user *sc,
184185

185186
BUG_ON(!MSR_TM_ACTIVE(regs->msr));
186187

187-
/* tm_reclaim rolls back all reg states, saving checkpointed (older)
188-
* GPRs to thread.ckpt_regs and (if used) FPRs to (newer)
189-
* thread.transact_fp and/or VRs to (newer) thread.transact_vr.
190-
* THEN we save out FP/VRs, if necessary, to the checkpointed (older)
191-
* thread.fr[]/vr[]s. The transactional (newer) GPRs are on the
192-
* stack, in *regs.
193-
*/
194-
tm_enable();
195-
tm_reclaim(&current->thread, msr, TM_CAUSE_SIGNAL);
196-
197188
flush_fp_to_thread(current);
198189

199190
#ifdef CONFIG_ALTIVEC
@@ -711,7 +702,7 @@ int handle_rt_signal64(int signr, struct k_sigaction *ka, siginfo_t *info,
711702
unsigned long newsp = 0;
712703
long err = 0;
713704

714-
frame = get_sigframe(ka, regs, sizeof(*frame), 0);
705+
frame = get_sigframe(ka, get_tm_stackpointer(regs), sizeof(*frame), 0);
715706
if (unlikely(frame == NULL))
716707
goto badframe;
717708

0 commit comments

Comments
 (0)