History

powerpc: Fix exit_flags field placement in pt_regs for ptrace

Commit d7a6797e0bc1 ("powerpc: add exit_flags field in pt_regs") added the exit_flags field to struct pt_regs to pass internal exit control flags (e.g. _TIF_RESTOREALL) from syscall_exit_prepare() to the low-level assembly exit path. However, the field was placed in a way that was visible to userspace tools such as strace via PTRACE_GETREGS, or caused a struct layout or size regression observable through ptrace. The field is purely kernel-internal and must not be exposed beyond the user_pt_regs boundary. Move exit_flags into struct thread_info where it is only accessible to the kernel, and keep it out of the ptrace-visible register window entirely.

Mukesh Kumar Chaurasiya (IBM) committed 3 months ago

Commit 263e5151 parent bddf7546 files · +26 −27Browse files

Unified · Split

6 files
  1. arch/powerpc/include/asm/ptrace.h +0 −3
  2. arch/powerpc/include/asm/thread_info.h +1 −0
  3. arch/powerpc/include/uapi/asm/ptrace.h +5 −9
  4. arch/powerpc/kernel/interrupt.c +19 −13
  5. arch/powerpc/kernel/ptrace/ptrace.c +0 −1
  6. arch/powerpc/kernel/signal.c +1 −1
arch/powerpc/include/asm/ptrace.h+0−3
@@ -53,9 +53,6 @@ struct pt_regs · 52 unmodified lines
5353 unsigned long esr;
5454 };
5555 unsigned long result;
56− unsigned long exit_flags;
57− /* Maintain 16 byte interrupt stack alignment */
58− unsigned long __pt_regs_pad[3];
5956 };
6057 };
6158 #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP)
arch/powerpc/include/asm/thread_info.h+1−0
@@ -57,6 +57,7 @@ struct thread_info { · 56 unmodified lines
5757 #ifdef CONFIG_SMP
5858 unsigned int cpu;
5959 #endif
60+ unsigned long exit_flags; /* Exit Flags for entry/exit */
6061 unsigned long syscall_work; /* SYSCALL_WORK_ flags */
6162 unsigned long local_flags; /* private flags for thread */
6263 #ifdef CONFIG_LIVEPATCH_64
arch/powerpc/include/uapi/asm/ptrace.h+5−9
@@ -55,8 +55,6 @@ struct pt_regs · 54 unmodified lines
5555 unsigned long dar; /* Fault registers */
5656 unsigned long dsisr; /* on 4xx/Book-E used for ESR */
5757 unsigned long result; /* Result of a system call */
58− unsigned long exit_flags; /* System call exit flags */
59− unsigned long __pt_regs_pad[3]; /* Maintain 16 byte interrupt stack alignment */
6058 };
6159
6260 #endif /* __ASSEMBLER__ */
@@ -116,12 +114,10 @@ struct pt_regs · 53 unmodified lines
116114 #define PT_DAR 41
117115 #define PT_DSISR 42
118116 #define PT_RESULT 43
119−#define PT_EXIT_FLAGS 44
120−#define PT_PAD 47 /* 3 times */
121−#define PT_DSCR 48
122−#define PT_REGS_COUNT 48
117+#define PT_DSCR 44
118+#define PT_REGS_COUNT 44
123119
124−#define PT_FPR0 (PT_REGS_COUNT + 4) /* each FP reg occupies 2 slots in this space */
120+#define PT_FPR0 48 /* each FP reg occupies 2 slots in this space */
125121
126122 #ifndef __powerpc64__
127123
@@ -133,7 +129,7 @@ struct pt_regs · 5 unmodified lines
133129 #define PT_FPSCR (PT_FPR0 + 32) /* each FP reg occupies 1 slot in 64-bit space */
134130
135131
136−#define PT_VR0 (PT_FPSCR + 2) /* <82> each Vector reg occupies 2 slots in 64-bit */
132+#define PT_VR0 82 /* each Vector reg occupies 2 slots in 64-bit */
137133 #define PT_VSCR (PT_VR0 + 32*2 + 1)
138134 #define PT_VRSAVE (PT_VR0 + 33*2)
139135
@@ -141,7 +137,7 @@ struct pt_regs · 1 unmodified line
141137 /*
142138 * Only store first 32 VSRs here. The second 32 VSRs in VR0-31
143139 */
144−#define PT_VSR0 (PT_VRSAVE + 2) /* each VSR reg occupies 2 slots in 64-bit */
140+#define PT_VSR0 150 /* each VSR reg occupies 2 slots in 64-bit */
145141 #define PT_VSR31 (PT_VSR0 + 2*31)
146142 #endif /* __powerpc64__ */
147143
arch/powerpc/kernel/interrupt.c+19−13
@@ -89,15 +89,17 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 88 unmodified lines
8989 long scv)
9090 {
9191 unsigned long ti_flags;
92+ unsigned long ret = 0;
9293 bool is_not_scv = !IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !scv;
9394
9495 kuap_assert_locked();
9596
9697 regs->result = r3;
97− regs->exit_flags = 0;
9898
99− ti_flags = read_thread_flags();
99+ /* Clear exit_flags so only flags set during this exit are visible */
100+ current->thread_info.exit_flags = 0;
100101
102+ ti_flags = read_thread_flags();
101103 if (unlikely(r3 >= (unsigned long)-MAX_ERRNO) && is_not_scv) {
102104 if (likely(!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) {
103105 r3 = -r3;
@@ -107,7 +109,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 3 unmodified lines
107109
108110 if (unlikely(ti_flags & _TIF_PERSYSCALL_MASK)) {
109111 if (ti_flags & _TIF_RESTOREALL)
110− regs->exit_flags = _TIF_RESTOREALL;
112+ ret = _TIF_RESTOREALL;
111113 else
112114 regs->gpr[3] = r3;
113115 clear_bits(_TIF_PERSYSCALL_MASK, &current_thread_info()->flags);
@@ -116,7 +118,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 2 unmodified lines
116118 }
117119
118120 if (unlikely(ti_flags & _TIF_SYSCALL_DOTRACE)) {
119− regs->exit_flags |= _TIF_RESTOREALL;
121+ ret |= _TIF_RESTOREALL;
120122 }
121123
122124 syscall_exit_to_user_mode(regs);
@@ -132,17 +134,19 @@ again: · 9 unmodified lines
132134
133135 /* Restore user access locks last */
134136 kuap_user_restore(regs);
135−
137+ ret |= current->thread_info.exit_flags;
136138 #ifdef CONFIG_PPC64
137− regs->exit_result = regs->exit_flags;
139+ regs->exit_result = ret;
138140 #endif
139141
140− return regs->exit_flags;
142+ return ret;
141143 }
142144
143145 #ifdef CONFIG_PPC64
144146 notrace unsigned long syscall_exit_restart(unsigned long r3, struct pt_regs *regs)
145147 {
148+ unsigned long ret;
149+
146150 /*
147151 * This is called when detecting a soft-pending interrupt as well as
148152 * an alternate-return interrupt. So we can't just have the alternate
@@ -167,9 +171,11 @@ again: · 18 unmodified lines
167171 }
168172
169173 kuap_user_restore(regs);
170− regs->exit_result |= regs->exit_flags;
174+ ret = current_thread_info()->exit_flags & _TIF_RESTOREALL;
175+ current_thread_info()->exit_flags &= ~_TIF_RESTOREALL;
176+ regs->exit_result |= ret;
171177
172− return regs->exit_result;
178+ return ret;
173179 }
174180 #endif
175181
@@ -186,8 +192,10 @@ notrace unsigned long interrupt_exit_user_prepare(struct pt_regs *regs) · 10 unmodified lines
186192 */
187193 kuap_assert_locked();
188194
195+ /* Clear exit_flags so only flags set during this exit are visible */
196+ current_thread_info()->exit_flags = 0;
197+
189198 local_irq_disable();
190− regs->exit_flags = 0;
191199 again:
192200 check_return_regs_valid(regs);
193201 user_enter_irqoff();
@@ -200,9 +208,7 @@ again: · 6 unmodified lines
200208
201209 /* Restore user access locks last */
202210 kuap_user_restore(regs);
203−
204− ret = regs->exit_flags;
205−
211+ ret = current_thread_info()->exit_flags & _TIF_RESTOREALL;
206212 #ifdef CONFIG_PPC64
207213 regs->exit_result = ret;
208214 #endif
arch/powerpc/kernel/ptrace/ptrace.c+0−1
@@ -291,7 +291,6 @@ void __init pt_regs_check(void) · 290 unmodified lines
291291 CHECK_REG(PT_DAR, dar);
292292 CHECK_REG(PT_DSISR, dsisr);
293293 CHECK_REG(PT_RESULT, result);
294− CHECK_REG(PT_EXIT_FLAGS, exit_flags);
295294 #undef CHECK_REG
296295
297296 BUILD_BUG_ON(PT_REGS_COUNT != sizeof(struct user_pt_regs) / sizeof(unsigned long));
arch/powerpc/kernel/signal.c+1−1
@@ -356,6 +356,6 @@ void signal_fault(struct task_struct *tsk, struct pt_regs *regs, · 355 unmodified lines
356356 void arch_do_signal_or_restart(struct pt_regs *regs)
357357 {
358358 BUG_ON(regs != current->thread.regs);
359− regs->exit_flags |= _TIF_RESTOREALL;
359+ current_thread_info()->exit_flags |= _TIF_RESTOREALL;
360360 do_signal(current);
361361 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t