[PATCH] posix-cpu-timers: Cleanup CPU timers before freeing them during exec
authorThadeu Lima de Souza Cascardo <cascardo@canonical.com>
Fri, 22 Jul 2022 09:52:11 +0000 (06:52 -0300)
committerSalvatore Bonaccorso <carnil@debian.org>
Wed, 10 Aug 2022 18:11:48 +0000 (19:11 +0100)
Commit 55e8c8eb2c7b ("posix-cpu-timers: Store a reference to a pid not a
task") started looking up tasks by PID when deleting a CPU timer.

When a non-leader thread calls execve, it will switch PIDs with the leader
process. Then, as it calls exit_itimers, posix_cpu_timer_del cannot find
the task because the timer still points out to the old PID.

That means that armed timers won't be disarmed, that is, they won't be
removed from the timerqueue_list. exit_itimers will still release their
memory, and when that list is later processed, it leads to a
use-after-free.

Clean up the timers from the de-threaded task before freeing them. This
prevents a reported use-after-free.

Fixes: 55e8c8eb2c7b ("posix-cpu-timers: Store a reference to a pid not a task")
Reported-by: "An independent security researcher working with SSD Secure Disclosure"
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
Gbp-Pq: Topic bugfix/all
Gbp-Pq: Name posix-cpu-timers-Cleanup-CPU-timers-before-freeing-t.patch

fs/exec.c

index 5a75e92b1a0a372f0f71e4d184788b14e5fb8c07..a9f5acf8f0ecb8888b7828735e8e70da64ff1239 100644 (file)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1297,6 +1297,9 @@ int begin_new_exec(struct linux_binprm * bprm)
        bprm->mm = NULL;
 
 #ifdef CONFIG_POSIX_TIMERS
+       spin_lock_irq(&me->sighand->siglock);
+       posix_cpu_timers_exit(me);
+       spin_unlock_irq(&me->sighand->siglock);
        exit_itimers(me);
        flush_itimer_signals();
 #endif