Skip to content

Commit f35d2f2

Browse files
npigginmpe
authored andcommitted
powerpc/64s: Fix copy-paste data exposure into newly created tasks
copy-paste contains implicit "copy buffer" state that can contain arbitrary user data (if the user process executes a copy instruction). This could be snooped by another process if a context switch hits while the state is live. So cp_abort is executed on context switch to clear out possible sensitive data and prevent the leak. cp_abort is done after the low level _switch(), which means it is never reached by newly created tasks, so they could snoop on this buffer between their first and second context switch. Fix this by doing the cp_abort before calling _switch. Add some comments which should make the issue harder to miss. Fixes: 07d2a62 ("powerpc/64s: Avoid cpabort in context switch when possible") Signed-off-by: Nicholas Piggin <npiggin@gmail.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20210622053036.474678-1-npiggin@gmail.com
1 parent a27755d commit f35d2f2

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

‎arch/powerpc/kernel/process.c‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,19 @@ struct task_struct *__switch_to(struct task_struct *prev,
12231223
__flush_tlb_pending(batch);
12241224
batch->active = 0;
12251225
}
1226+
1227+
/*
1228+
* On POWER9 the copy-paste buffer can only paste into
1229+
* foreign real addresses, so unprivileged processes can not
1230+
* see the data or use it in any way unless they have
1231+
* foreign real mappings. If the new process has the foreign
1232+
* real address mappings, we must issue a cp_abort to clear
1233+
* any state and prevent snooping, corruption or a covert
1234+
* channel. ISA v3.1 supports paste into local memory.
1235+
*/
1236+
if (new->mm && (cpu_has_feature(CPU_FTR_ARCH_31) ||
1237+
atomic_read(&new->mm->context.vas_windows)))
1238+
asm volatile(PPC_CP_ABORT);
12261239
#endif /* CONFIG_PPC_BOOK3S_64 */
12271240

12281241
#ifdef CONFIG_PPC_ADV_DEBUG_REGS
@@ -1273,30 +1286,33 @@ struct task_struct *__switch_to(struct task_struct *prev,
12731286
#endif
12741287
last = _switch(old_thread, new_thread);
12751288

1289+
/*
1290+
* Nothing after _switch will be run for newly created tasks,
1291+
* because they switch directly to ret_from_fork/ret_from_kernel_thread
1292+
* etc. Code added here should have a comment explaining why that is
1293+
* okay.
1294+
*/
1295+
12761296
#ifdef CONFIG_PPC_BOOK3S_64
1297+
/*
1298+
* This applies to a process that was context switched while inside
1299+
* arch_enter_lazy_mmu_mode(), to re-activate the batch that was
1300+
* deactivated above, before _switch(). This will never be the case
1301+
* for new tasks.
1302+
*/
12771303
if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
12781304
current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
12791305
batch = this_cpu_ptr(&ppc64_tlb_batch);
12801306
batch->active = 1;
12811307
}
12821308

1283-
if (current->thread.regs) {
1309+
/*
1310+
* Math facilities are masked out of the child MSR in copy_thread.
1311+
* A new task does not need to restore_math because it will
1312+
* demand fault them.
1313+
*/
1314+
if (current->thread.regs)
12841315
restore_math(current->thread.regs);
1285-
1286-
/*
1287-
* On POWER9 the copy-paste buffer can only paste into
1288-
* foreign real addresses, so unprivileged processes can not
1289-
* see the data or use it in any way unless they have
1290-
* foreign real mappings. If the new process has the foreign
1291-
* real address mappings, we must issue a cp_abort to clear
1292-
* any state and prevent snooping, corruption or a covert
1293-
* channel. ISA v3.1 supports paste into local memory.
1294-
*/
1295-
if (current->mm &&
1296-
(cpu_has_feature(CPU_FTR_ARCH_31) ||
1297-
atomic_read(&current->mm->context.vas_windows)))
1298-
asm volatile(PPC_CP_ABORT);
1299-
}
13001316
#endif /* CONFIG_PPC_BOOK3S_64 */
13011317

13021318
return last;

0 commit comments

Comments
 (0)