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.
Unified · Split
| @@ -53,9 +53,6 @@ struct pt_regs · 52 unmodified lines | ||
| 53 | 53 | unsigned long esr; |
| 54 | 54 | }; |
| 55 | 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 | 58 | #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP) |
| @@ -57,6 +57,7 @@ struct thread_info { · 56 unmodified lines | ||
| 57 | 57 | #ifdef CONFIG_SMP |
| 58 | 58 | unsigned int cpu; |
| 59 | 59 | #endif |
| 60 | + unsigned long exit_flags; /* Exit Flags for entry/exit */ | |
| 60 | 61 | unsigned long syscall_work; /* SYSCALL_WORK_ flags */ |
| 61 | 62 | unsigned long local_flags; /* private flags for thread */ |
| 62 | 63 | #ifdef CONFIG_LIVEPATCH_64 |
| @@ -55,8 +55,6 @@ struct pt_regs · 54 unmodified lines | ||
| 55 | 55 | unsigned long dar; /* Fault registers */ |
| 56 | 56 | unsigned long dsisr; /* on 4xx/Book-E used for ESR */ |
| 57 | 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 | 60 | #endif /* __ASSEMBLER__ */ |
| @@ -116,12 +114,10 @@ struct pt_regs · 53 unmodified lines | ||
| 116 | 114 | #define PT_DAR 41 |
| 117 | 115 | #define PT_DSISR 42 |
| 118 | 116 | #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 | |
| 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 | 122 | #ifndef __powerpc64__ |
| 127 | 123 | |
| @@ -133,7 +129,7 @@ struct pt_regs · 5 unmodified lines | ||
| 133 | 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 | 133 | #define PT_VSCR (PT_VR0 + 32*2 + 1) |
| 138 | 134 | #define PT_VRSAVE (PT_VR0 + 33*2) |
| 139 | 135 | |
| @@ -141,7 +137,7 @@ struct pt_regs · 1 unmodified line | ||
| 141 | 137 | /* |
| 142 | 138 | * 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 | 141 | #define PT_VSR31 (PT_VSR0 + 2*31) |
| 146 | 142 | #endif /* __powerpc64__ */ |
| 147 | 143 | |
| @@ -89,15 +89,17 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 88 unmodified lines | ||
| 89 | 89 | long scv) |
| 90 | 90 | { |
| 91 | 91 | unsigned long ti_flags; |
| 92 | + unsigned long ret = 0; | |
| 92 | 93 | bool is_not_scv = !IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !scv; |
| 93 | 94 | |
| 94 | 95 | kuap_assert_locked(); |
| 95 | 96 | |
| 96 | 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 | 103 | if (unlikely(r3 >= (unsigned long)-MAX_ERRNO) && is_not_scv) { |
| 102 | 104 | if (likely(!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) { |
| 103 | 105 | r3 = -r3; |
| @@ -107,7 +109,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 3 unmodified lines | ||
| 107 | 109 | |
| 108 | 110 | if (unlikely(ti_flags & _TIF_PERSYSCALL_MASK)) { |
| 109 | 111 | if (ti_flags & _TIF_RESTOREALL) |
| 110 | − regs->exit_flags = _TIF_RESTOREALL; | |
| 112 | + ret = _TIF_RESTOREALL; | |
| 111 | 113 | else |
| 112 | 114 | regs->gpr[3] = r3; |
| 113 | 115 | clear_bits(_TIF_PERSYSCALL_MASK, ¤t_thread_info()->flags); |
| @@ -116,7 +118,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, · 2 unmodified lines | ||
| 116 | 118 | } |
| 117 | 119 | |
| 118 | 120 | if (unlikely(ti_flags & _TIF_SYSCALL_DOTRACE)) { |
| 119 | − regs->exit_flags |= _TIF_RESTOREALL; | |
| 121 | + ret |= _TIF_RESTOREALL; | |
| 120 | 122 | } |
| 121 | 123 | |
| 122 | 124 | syscall_exit_to_user_mode(regs); |
| @@ -132,17 +134,19 @@ again: · 9 unmodified lines | ||
| 132 | 134 | |
| 133 | 135 | /* Restore user access locks last */ |
| 134 | 136 | kuap_user_restore(regs); |
| 135 | − | |
| 137 | + ret |= current->thread_info.exit_flags; | |
| 136 | 138 | #ifdef CONFIG_PPC64 |
| 137 | − regs->exit_result = regs->exit_flags; | |
| 139 | + regs->exit_result = ret; | |
| 138 | 140 | #endif |
| 139 | 141 | |
| 140 | − return regs->exit_flags; | |
| 142 | + return ret; | |
| 141 | 143 | } |
| 142 | 144 | |
| 143 | 145 | #ifdef CONFIG_PPC64 |
| 144 | 146 | notrace unsigned long syscall_exit_restart(unsigned long r3, struct pt_regs *regs) |
| 145 | 147 | { |
| 148 | + unsigned long ret; | |
| 149 | + | |
| 146 | 150 | /* |
| 147 | 151 | * This is called when detecting a soft-pending interrupt as well as |
| 148 | 152 | * 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 | 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 | 180 | #endif |
| 175 | 181 | |
| @@ -186,8 +192,10 @@ notrace unsigned long interrupt_exit_user_prepare(struct pt_regs *regs) · 10 unmodified lines | ||
| 186 | 192 | */ |
| 187 | 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 | 198 | local_irq_disable(); |
| 190 | − regs->exit_flags = 0; | |
| 191 | 199 | again: |
| 192 | 200 | check_return_regs_valid(regs); |
| 193 | 201 | user_enter_irqoff(); |
| @@ -200,9 +208,7 @@ again: · 6 unmodified lines | ||
| 200 | 208 | |
| 201 | 209 | /* Restore user access locks last */ |
| 202 | 210 | kuap_user_restore(regs); |
| 203 | − | |
| 204 | − ret = regs->exit_flags; | |
| 205 | − | |
| 211 | + ret = current_thread_info()->exit_flags & _TIF_RESTOREALL; | |
| 206 | 212 | #ifdef CONFIG_PPC64 |
| 207 | 213 | regs->exit_result = ret; |
| 208 | 214 | #endif |
| @@ -291,7 +291,6 @@ void __init pt_regs_check(void) · 290 unmodified lines | ||
| 291 | 291 | CHECK_REG(PT_DAR, dar); |
| 292 | 292 | CHECK_REG(PT_DSISR, dsisr); |
| 293 | 293 | CHECK_REG(PT_RESULT, result); |
| 294 | − CHECK_REG(PT_EXIT_FLAGS, exit_flags); | |
| 295 | 294 | #undef CHECK_REG |
| 296 | 295 | |
| 297 | 296 | BUILD_BUG_ON(PT_REGS_COUNT != sizeof(struct user_pt_regs) / sizeof(unsigned long)); |
| @@ -356,6 +356,6 @@ void signal_fault(struct task_struct *tsk, struct pt_regs *regs, · 355 unmodified lines | ||
| 356 | 356 | void arch_do_signal_or_restart(struct pt_regs *regs) |
| 357 | 357 | { |
| 358 | 358 | BUG_ON(regs != current->thread.regs); |
| 359 | − regs->exit_flags |= _TIF_RESTOREALL; | |
| 359 | + current_thread_info()->exit_flags |= _TIF_RESTOREALL; | |
| 360 | 360 | do_signal(current); |
| 361 | 361 | } |