Skip to content

Commit 873c3e8

Browse files
committed
arm64: Kill 32-bit applications scheduled on 64-bit-only CPUs
Scheduling a 32-bit application on a 64-bit-only CPU is a bad idea. Ensure that 32-bit applications always take the slow-path when returning to userspace on a system with mismatched support at EL0, so that we can avoid trying to run on a 64-bit-only CPU and force a SIGKILL instead. Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Link: https://lore.kernel.org/r/20210608180313.11502-5-will@kernel.org Signed-off-by: Will Deacon <will@kernel.org>
1 parent 2f6a49b commit 873c3e8

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

arch/arm64/kernel/process.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ static void erratum_1418040_thread_switch(struct task_struct *prev,
527527
write_sysreg(val, cntkctl_el1);
528528
}
529529

530+
static void compat_thread_switch(struct task_struct *next)
531+
{
532+
if (!is_compat_thread(task_thread_info(next)))
533+
return;
534+
535+
if (static_branch_unlikely(&arm64_mismatched_32bit_el0))
536+
set_tsk_thread_flag(next, TIF_NOTIFY_RESUME);
537+
}
538+
530539
static void update_sctlr_el1(u64 sctlr)
531540
{
532541
/*
@@ -568,6 +577,7 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
568577
ssbs_thread_switch(next);
569578
erratum_1418040_thread_switch(prev, next);
570579
ptrauth_thread_switch_user(next);
580+
compat_thread_switch(next);
571581

572582
/*
573583
* Complete any pending TLB or cache maintenance on this CPU in case
@@ -633,8 +643,15 @@ unsigned long arch_align_stack(unsigned long sp)
633643
*/
634644
void arch_setup_new_exec(void)
635645
{
636-
current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0;
646+
unsigned long mmflags = 0;
647+
648+
if (is_compat_task()) {
649+
mmflags = MMCF_AARCH32;
650+
if (static_branch_unlikely(&arm64_mismatched_32bit_el0))
651+
set_tsk_thread_flag(current, TIF_NOTIFY_RESUME);
652+
}
637653

654+
current->mm->context.flags = mmflags;
638655
ptrauth_thread_init_user();
639656
mte_thread_init_user();
640657

arch/arm64/kernel/signal.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,19 @@ static void do_signal(struct pt_regs *regs)
911911
restore_saved_sigmask();
912912
}
913913

914+
static bool cpu_affinity_invalid(struct pt_regs *regs)
915+
{
916+
if (!compat_user_mode(regs))
917+
return false;
918+
919+
/*
920+
* We're preemptible, but a reschedule will cause us to check the
921+
* affinity again.
922+
*/
923+
return !cpumask_test_cpu(raw_smp_processor_id(),
924+
system_32bit_el0_cpumask());
925+
}
926+
914927
asmlinkage void do_notify_resume(struct pt_regs *regs,
915928
unsigned long thread_flags)
916929
{
@@ -938,6 +951,19 @@ asmlinkage void do_notify_resume(struct pt_regs *regs,
938951
if (thread_flags & _TIF_NOTIFY_RESUME) {
939952
tracehook_notify_resume(regs);
940953
rseq_handle_notify_resume(NULL, regs);
954+
955+
/*
956+
* If we reschedule after checking the affinity
957+
* then we must ensure that TIF_NOTIFY_RESUME
958+
* is set so that we check the affinity again.
959+
* Since tracehook_notify_resume() clears the
960+
* flag, ensure that the compiler doesn't move
961+
* it after the affinity check.
962+
*/
963+
barrier();
964+
965+
if (cpu_affinity_invalid(regs))
966+
force_sig(SIGKILL);
941967
}
942968

943969
if (thread_flags & _TIF_FOREIGN_FPSTATE)

0 commit comments

Comments
 (0)