sched/cache: Fix use-after-free of the mm replaced by exec
Subsystems (4)
PATCH v2 1/1 sched/cache: Fix use-after-free of the mm replaced by exec
When the waker cannot use the wakelist, ttwu_queue() takes the target rq lock and goes down into update_curr(). If the target rq belongs to another CPU, the task handed to account_mm_sched() is the one running on that CPU, not the task being woken.
account_mm_sched() reads p->mm and updates mm->sc_stat. Nothing keeps that mm alive. The rq lock and rq->cpu_epoch_lock it holds have nothing to do with the lifetime of the mm.
If that task happens to be in execve(), exec_mmap() points tsk->mm and tsk->active_mm at the new mm, and exec_mm_put_old() from setup_new_exec() drops the old one. free_bprm() does the same when exec fails. On the way from mmput() down to __mmdrop(), mm_destroy_sched() calls free_percpu() on sc_stat.pcpu_sched and free_mm() returns the mm_struct.
Whoever already read the old pointer keeps using it. It adds to runtime in the freed per-cpu area and reads sc_stat in the freed mm_struct. Depending on the condition it also writes sc_stat.cpu. That is a use-after-free.
Commit 9f23469401b0 ("sched/cache: Fix potential NULL mm pointer access") changed the remaining p->mm dereference to the local variable, and said the active_mm reference keeps the structure allocated. That holds for the other paths that detach an mm, since they take an mmgrab_lazy_tlb() reference. exec reassigns active_mm to the new mm as well, so that reference is gone. What is left is the mm_users reference in bprm->old_mm, and dropping it is the free.
CPU0 CPU1
write(pipe)
try_to_wake_up()
ttwu_queue() // takes rq0 lock
enqueue_task_fair()
update_curr()
update_se()
account_mm_sched()
mm = rq0->curr->mm
// old mm
execve()
exec_mmap() // tsk->mm = new mm
setup_new_exec()
exec_mm_put_old()
mmput() -> ... -> __mmdrop()
mm_destroy_sched() // free_percpu()
free_mm()
read mm->sc_stat.epoch
// use-after-freeKASAN log:
BUG: KASAN: slab-use-after-free in update_se+0xe6e/0xf70 Read of size 8 at addr ffff8880093cad10 by task sc-direct-set/80 ... Call Trace: update_se+0xe6e/0xf70 update_curr.isra.0+0x28/0x380 enqueue_task_fair+0xb58/0x3560 enqueue_task+0x70/0x170 ttwu_do_activate+0xeb/0x590 try_to_wake_up+0x79b/0x14f0 autoremove_wake_function+0x16/0x150 __wake_up_common+0xed/0x160 __wake_up_sync_key+0x36/0x50 anon_pipe_write+0xa62/0x1830 vfs_write+0xa4f/0xcf0 ksys_write+0x17c/0x1c0 do_syscall_64+0xdd/0x4a0 entry_SYSCALL_64_after_hwframe+0x77/0x7f ... Freed by task 1: kmem_cache_free+0xba/0x3b0 setup_new_exec+0x2c4/0x3d0 load_elf_binary+0x435/0x4740 bprm_execve+0x6d2/0x1290 do_execveat_common.isra.0+0x3a3/0x580 __x64_sys_execve+0x8e/0xc0 ... The buggy address belongs to the object at ffff8880093cab80 which belongs to the cache mm_struct of size 1688 The buggy address is located 400 bytes inside of freed 1688-byte region [ffff8880093cab80, ffff8880093cb218)
Wait for an RCU grace period before the old mm is dropped. account_mm_sched() runs with the rq lock held, so preemption is disabled there and that section is an RCU read-side critical section. A reader that already holds the old pointer finishes before the grace period ends, and the store to tsk->mm precedes the wait, so a reader that starts afterwards observes the new mm.
---
Changes in v2:
- Drop an unrelated hunk that slipped into v1.
- Use synchronize_rcu() instead of cycling the rq lock, which covers every reader that runs with preemption disabled.
- v1: https://lore.kernel.org/all/apPb-Dr4nPYuHQOK@v4bel/
--- fs/exec.c | 1 + include/linux/sched.h | 4 ++++ kernel/sched/fair.c | 12 ++++++++++++ 3 files changed, 17 insertions(+)
fs/exec.c +1 -0
@@ -916,6 +916,7 @@ static void exec_mm_put_old(struct mm_struct *old_mm) {setmax_mm_hiwater_rss (¤t->signal->maxrss, old_mm);mm_update_next_owner (old_mm); +sched_cache_exec_done ();mmput (old_mm); }
include/linux/sched.h +4 -0
@@ -2415,10 +2415,14 @@ struct sched_cache_stat { int cpu; }____cacheline_aligned_in_smp ; +void sched_cache_exec_done (void ); +#else struct sched_cache_stat { }; +static inline void sched_cache_exec_done (void ) { } +#endif #ifndef MODULE
kernel/sched/fair.c +12 -0
@@ -1989,6 +1989,18 @@ void init_sched_mm(struct task_struct *p) p->preferred_llc = -1; } +/* exec() has switched to the new mm and is about to drop the old one. */ +void sched_cache_exec_done (void ) +{ +/* +* account_mm_sched() dereferences rq->curr->mm with the rq lock held, +* so a remote CPU can still be using the old mm. That section runs with +* preemption disabled and is therefore an RCU read-side critical +* section, so wait for a grace period before the mm goes away. +*/ +synchronize_rcu (); +} +#else /* CONFIG_SCHED_CACHE */ static inline void account_mm_sched(struct rq *rq, struct task_struct *p,
Signature · 1 line
2.43.0