bprm_execve() makes the task leave the CID management of its mm at the start of execve(). The task joins again at the end: the new mm on success, the old one on failure. sched_mm_cid_fork() handles the join. It is written for a fork() child, which does not run yet. In per CPU mode the child gets no CID there. It takes the CID of its CPU when it is scheduled in. When the join switches the mode, the forking task runs the fixups. These are mm_cid_fixup_tasks_to_cpus() and mm_cid_fixup_cpus_to_tasks(). A successful execve() fits this design. The task is the first user of a new mm, and the first user gets its CID at once. A failed execve() does not fit. The task rejoins its old mm while it runs. A failure before de_thread() leaves the other threads alive. The CPUs can also still own CIDs of the old mm. This has three effects: 1. In per CPU mode, the task returns to user space with no CID. It reads an mm_cid of 0x80000000 until its next context switch. A program that indexes per-CID data with it would read out of bounds. mm_cid_execve_test of this series found it. 2. Per CPU mode can last down to one user. This happens with two or three possible CPUs, or when the mm counts one allowed CPU. The mm can count one allowed CPU while its task runs on other CPUs. mm_update_cpus_allowed() ignores an affinity change while the mm has no user, as inside a failed execve(). When that last user leaves, sched_mm_cid_exit() frees only the CID of its own CPU. Other CPUs keep theirs. The rejoin then takes a new CID, and a CID leaks. Case 2 of mm_cid_execve_test of this series shows it: a single thread reads mm_cid 1. The comment above run_child_widened() in that test has the sequence as a diagram. On an earlier tip, a guest with two CPUs locked up after such a leak. This was not reproduced on tip sched/urgent. 3. The rejoin can switch the mm to per CPU mode. If the other users hold every CID, only the fixups can free one. The task runs them itself, and it can be preempted before them. A and B are two threads on CPUs 0 and 1 of a machine with two possible CPUs. The mm is in per task mode. C is the vfork() child of B: CPU 0 CPU 1 A: execve() sched_mm_cid_before_execve(A) users 2 -> 1, frees the CID of A B: vfork() sched_mm_cid_fork(C) users 1 -> 2, C takes a CID B and C hold both CIDs A: execve() fails sched_mm_cid_after_execve(A) sched_mm_cid_fork(A) users 2 -> 3, switch to per CPU mode A: is preempted before mm_cid_fixup_tasks_to_cpus() A: is scheduled in again mm_cid_schedin(A) no CID on A or on CPU 0 mm_get_cid() loops A model in the Spin model checker found this. A reproducer makes a SCHED_FIFO task wake every 50us to preempt A. On tip sched/urgent, it hung each of four guests with two CPUs. On a fifth guest, the QEMU monitor showed a CPU in mm_get_cid() under __schedule(). Fix this by moving both hooks into exec_mmap(). The task leaves the old mm after mm_exit_exec_release(). By then, de_thread() has killed the other threads. Another process may still share the old mm: a vfork() parent, or a task created with CLONE_VM and without CLONE_THREAD. The leave is then an ordinary exit of one user. The task joins the new mm once it is installed. In between, only down_write_killable() and mmap_read_lock_killable() can fail, and only with a pending SIGKILL. The task then dies. A tracer cannot cancel a SIGKILL. It could cancel the SIGSEGV which bprm_execve() forces on other failures past the point of no return. So a task never rejoins its old mm. The last user no longer needs to free the CID of its CPU in sched_mm_cid_exit(). Karl Mehltretter found that earlier versions of this change broke under ptrace and with vfork(). He proposed the placement in exec_mmap(). With this change, mm_cid_execve_test, mm_cid_transition_test and mm_cid_window_test no longer read 0x80000000 on guests with 2 to 64 CPUs. Case 2 of mm_cid_execve_test no longer leaks a CID. The SCHED_FIFO reproducer completed 20000 attempts on each of four guests with two CPUs. Fixes: 653fda7ae73d ("sched/mmcid: Switch over to the new mechanism") Suggested-by: Thomas Gleixner Suggested-by: Karl Mehltretter Signed-off-by: Mathieu Desnoyers Cc: stable@vger.kernel.org Link: https://lore.kernel.org/all/awyrCYBrJS-CuBE8@gmail.com/ --- fs/exec.c | 13 ++++++++++--- kernel/sched/core.c | 12 ++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 8d2b14e1d455..d188f014871d 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -858,6 +858,15 @@ static int exec_mmap(struct linux_binprm *bprm) /* Clean up futexes and release the mm */ mm_exit_exec_release(tsk, old_mm); + /* + * The below lock acquisitions can only fail due to a SIGKILL. In + * that case the task will die and even PTRACE won't be able to + * resurrect it. So this is the safest place to leave MM CID + * management as there is no way that this will go back to user + * space. + */ + sched_mm_cid_before_execve(current); + ret = down_write_killable(&tsk->signal->exec_update_lock); if (ret) return ret; @@ -908,6 +917,7 @@ static int exec_mmap(struct linux_binprm *bprm) } else { mmdrop_lazy_tlb(active_mm); } + sched_mm_cid_after_execve(current); futex_exec_done(tsk); return 0; } @@ -1853,7 +1863,6 @@ static int bprm_execve(struct linux_binprm *bprm) */ check_unsafe_exec(bprm); current->in_execve = 1; - sched_mm_cid_before_execve(current); sched_exec(); @@ -1866,7 +1875,6 @@ static int bprm_execve(struct linux_binprm *bprm) if (retval < 0) goto out; - sched_mm_cid_after_execve(current); rseq_execve(current); /* execve succeeded */ current->in_execve = 0; @@ -1885,7 +1893,6 @@ static int bprm_execve(struct linux_binprm *bprm) if (bprm->point_of_no_return && !fatal_signal_pending(current)) force_fatal_sig(SIGSEGV); - sched_mm_cid_after_execve(current); rseq_force_update(); current->in_execve = 0; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 07ec834d54fd..3ad27670d67f 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -11060,7 +11060,7 @@ static void sched_mm_cid_fork(struct task_struct *t) if (!mm->mm_cid.users) { sched_mm_cid_add_user(t, mm); t->mm_cid.cid = mm_get_cid(mm); - /* Required for execve() */ + /* Required for execve() as the task is running */ pcp->cid = t->mm_cid.cid; return; } @@ -11158,10 +11158,10 @@ void sched_mm_cid_exit(struct task_struct *t) } /* Last user */ scoped_guard(raw_spinlock_irq, &mm->mm_cid.lock) { - /* Required across execve() */ - if (t == current) - mm_cid_transit_to_task(t, this_cpu_ptr(mm->mm_cid.pcpu)); - /* Ignore mode change. There is nothing to do. */ + /* + * Ignore mode change. There is nothing to do as the MM + * is going away in the common case. + */ sched_mm_cid_remove_user(t); } } @@ -11178,7 +11178,7 @@ void sched_mm_cid_exit(struct task_struct *t) cancel_work_sync(&mm->mm_cid.work); } -/* Deactivate MM CID allocation across execve() */ +/* Deactivate MM CID allocation across execve() switching the MM */ void sched_mm_cid_before_execve(struct task_struct *t) { sched_mm_cid_exit(t); -- 2.47.3