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
53 unsigned long esr;53 unsigned long esr;
54 };54 };
55 unsigned long result;55 unsigned long result;
56 unsigned long exit_flags;
57 /* Maintain 16 byte interrupt stack alignment */
58 unsigned long __pt_regs_pad[3];
59 };56 };
60 };57 };
61#if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP)58#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
57#ifdef CONFIG_SMP57#ifdef CONFIG_SMP
58 unsigned int cpu;58 unsigned int cpu;
59#endif59#endif
60 unsigned long exit_flags; /* Exit Flags for entry/exit */
60 unsigned long syscall_work; /* SYSCALL_WORK_ flags */61 unsigned long syscall_work; /* SYSCALL_WORK_ flags */
61 unsigned long local_flags; /* private flags for thread */62 unsigned long local_flags; /* private flags for thread */
62#ifdef CONFIG_LIVEPATCH_6463#ifdef CONFIG_LIVEPATCH_64
arch/powerpc/include/uapi/asm/ptrace.h+5−9
@@ -55,8 +55,6 @@ struct pt_regs · 54 unmodified lines
55 unsigned long dar; /* Fault registers */55 unsigned long dar; /* Fault registers */
56 unsigned long dsisr; /* on 4xx/Book-E used for ESR */56 unsigned long dsisr; /* on 4xx/Book-E used for ESR */
57 unsigned long result; /* Result of a system call */57 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 */
60};58};
61 59
62#endif /* __ASSEMBLER__ */60#endif /* __ASSEMBLER__ */
@@ -116,12 +114,10 @@ struct pt_regs · 53 unmodified lines
116#define PT_DAR 41114#define PT_DAR 41
117#define PT_DSISR 42115#define PT_DSISR 42
118#define PT_RESULT 43116#define PT_RESULT 43
119#define PT_EXIT_FLAGS 44117#define PT_DSCR 44
120#define PT_PAD 47 /* 3 times */118#define PT_REGS_COUNT 44
121#define PT_DSCR 48
122#define PT_REGS_COUNT 48
123 119
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 */
125 121
126#ifndef __powerpc64__122#ifndef __powerpc64__
127 123
@@ -133,7 +129,7 @@ struct pt_regs · 5 unmodified lines
133#define PT_FPSCR (PT_FPR0 + 32) /* each FP reg occupies 1 slot in 64-bit space */129#define PT_FPSCR (PT_FPR0 + 32) /* each FP reg occupies 1 slot in 64-bit space */
134 130
135 131
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 */
137#define PT_VSCR (PT_VR0 + 32*2 + 1)133#define PT_VSCR (PT_VR0 + 32*2 + 1)
138#define PT_VRSAVE (PT_VR0 + 33*2)134#define PT_VRSAVE (PT_VR0 + 33*2)
139 135
@@ -141,7 +137,7 @@ struct pt_regs · 1 unmodified line
141/*137/*
142 * Only store first 32 VSRs here. The second 32 VSRs in VR0-31138 * Only store first 32 VSRs here. The second 32 VSRs in VR0-31
143 */139 */
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 */
145#define PT_VSR31 (PT_VSR0 + 2*31)141#define PT_VSR31 (PT_VSR0 + 2*31)
146#endif /* __powerpc64__ */142#endif /* __powerpc64__ */
147 143
arch/powerpc/kernel/interrupt.c+19−13
@@ -89,15 +89,17 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 88 unmodified lines
89 long scv)89 long scv)
90{90{
91 unsigned long ti_flags;91 unsigned long ti_flags;
92 unsigned long ret = 0;
92 bool is_not_scv = !IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !scv;93 bool is_not_scv = !IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !scv;
93 94
94 kuap_assert_locked();95 kuap_assert_locked();
95 96
96 regs->result = r3;97 regs->result = r3;
97 regs->exit_flags = 0;
98 98
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;
100 101
102 ti_flags = read_thread_flags();
101 if (unlikely(r3 >= (unsigned long)-MAX_ERRNO) && is_not_scv) {103 if (unlikely(r3 >= (unsigned long)-MAX_ERRNO) && is_not_scv) {
102 if (likely(!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) {104 if (likely(!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) {
103 r3 = -r3;105 r3 = -r3;
@@ -107,7 +109,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 3 unmodified lines
107 109
108 if (unlikely(ti_flags & _TIF_PERSYSCALL_MASK)) {110 if (unlikely(ti_flags & _TIF_PERSYSCALL_MASK)) {
109 if (ti_flags & _TIF_RESTOREALL)111 if (ti_flags & _TIF_RESTOREALL)
110 regs->exit_flags = _TIF_RESTOREALL;112 ret = _TIF_RESTOREALL;
111 else113 else
112 regs->gpr[3] = r3;114 regs->gpr[3] = r3;
113 clear_bits(_TIF_PERSYSCALL_MASK, &current_thread_info()->flags);115 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
116 }118 }
117 119
118 if (unlikely(ti_flags & _TIF_SYSCALL_DOTRACE)) {120 if (unlikely(ti_flags & _TIF_SYSCALL_DOTRACE)) {
119 regs->exit_flags |= _TIF_RESTOREALL;121 ret |= _TIF_RESTOREALL;
120 }122 }
121 123
122 syscall_exit_to_user_mode(regs);124 syscall_exit_to_user_mode(regs);
@@ -132,17 +134,19 @@ again: · 9 unmodified lines
132 134
133 /* Restore user access locks last */135 /* Restore user access locks last */
134 kuap_user_restore(regs);136 kuap_user_restore(regs);
135 137 ret |= current->thread_info.exit_flags;
136#ifdef CONFIG_PPC64138#ifdef CONFIG_PPC64
137 regs->exit_result = regs->exit_flags;139 regs->exit_result = ret;
138#endif140#endif
139 141
140 return regs->exit_flags;142 return ret;
141}143}
142 144
143#ifdef CONFIG_PPC64145#ifdef CONFIG_PPC64
144notrace unsigned long syscall_exit_restart(unsigned long r3, struct pt_regs *regs)146notrace unsigned long syscall_exit_restart(unsigned long r3, struct pt_regs *regs)
145{147{
148 unsigned long ret;
149
146 /*150 /*
147 * This is called when detecting a soft-pending interrupt as well as151 * This is called when detecting a soft-pending interrupt as well as
148 * an alternate-return interrupt. So we can't just have the alternate152 * an alternate-return interrupt. So we can't just have the alternate
@@ -167,9 +171,11 @@ again: · 18 unmodified lines
167 }171 }
168 172
169 kuap_user_restore(regs);173 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;
171 177
172 return regs->exit_result;178 return ret;
173}179}
174#endif180#endif
175 181
@@ -186,8 +192,10 @@ notrace unsigned long interrupt_exit_user_prepare(struct pt_regs *regs) · 10 unmodified lines
186 */192 */
187 kuap_assert_locked();193 kuap_assert_locked();
188 194
195 /* Clear exit_flags so only flags set during this exit are visible */
196 current_thread_info()->exit_flags = 0;
197
189 local_irq_disable();198 local_irq_disable();
190 regs->exit_flags = 0;
191again:199again:
192 check_return_regs_valid(regs);200 check_return_regs_valid(regs);
193 user_enter_irqoff();201 user_enter_irqoff();
@@ -200,9 +208,7 @@ again: · 6 unmodified lines
200 208
201 /* Restore user access locks last */209 /* Restore user access locks last */
202 kuap_user_restore(regs);210 kuap_user_restore(regs);
203 211 ret = current_thread_info()->exit_flags & _TIF_RESTOREALL;
204 ret = regs->exit_flags;
205
206#ifdef CONFIG_PPC64212#ifdef CONFIG_PPC64
207 regs->exit_result = ret;213 regs->exit_result = ret;
208#endif214#endif
arch/powerpc/kernel/ptrace/ptrace.c+0−1
@@ -291,7 +291,6 @@ void __init pt_regs_check(void) · 290 unmodified lines
291 CHECK_REG(PT_DAR, dar);291 CHECK_REG(PT_DAR, dar);
292 CHECK_REG(PT_DSISR, dsisr);292 CHECK_REG(PT_DSISR, dsisr);
293 CHECK_REG(PT_RESULT, result);293 CHECK_REG(PT_RESULT, result);
294 CHECK_REG(PT_EXIT_FLAGS, exit_flags);
295 #undef CHECK_REG294 #undef CHECK_REG
296 295
297 BUILD_BUG_ON(PT_REGS_COUNT != sizeof(struct user_pt_regs) / sizeof(unsigned long));296 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
356void arch_do_signal_or_restart(struct pt_regs *regs)356void arch_do_signal_or_restart(struct pt_regs *regs)
357{357{
358 BUG_ON(regs != current->thread.regs);358 BUG_ON(regs != current->thread.regs);
359 regs->exit_flags |= _TIF_RESTOREALL;359 current_thread_info()->exit_flags |= _TIF_RESTOREALL;
360 do_signal(current);360 do_signal(current);
361}361}

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