diff options
| author | Mark Johnston <markj@FreeBSD.org> | 2026-11-12 19:16:53 +0000 |
|---|---|---|
| committer | Mark Johnston <markj@FreeBSD.org> | 2026-11-16 21:17:12 +0000 |
| commit | 6afa595f1f1666c3cd39f12cd4271f358549062f (patch) | |
| tree | f9d10ff0b9ab552155fbb42eefb398161f43a73f | |
| parent | 198357a7298a42469dd8db612f60b0711807646f (diff) | |
kqueue: Fix a race when detaching a timer knote
We currently maintain a per-process list of stopped timer knotes, used
when the owning process receives SIGSTOP. Upon receiving SIGCONT,
queued timers are re-armed.
When detaching a timer knote, we drain the callout and remove it from
the queue, then check to see if the callout was re-armed by SIGCONT; if
so, then we loop. However, the callout timer may fire before this
check, and softclock_call_cc() clears the pending bit without the
associated callout lock held, in which case filt_timerdetach() fails to
observe that the callout is still running and doesn't drain.
The callout handler then races with a free of the knote, and the
resulting memory corruption can be exploited to obtain root privileges.
Fix the problem by using an explicit flag that prevents re-arming a
timer whose knote is in the process of being detached.
Approved by: so
Security: FreeBSD-SA-26:85.kqueue
Security: CVE-2026-101320
Reported by: OpenAI Security Research Team
Fixes: 2fd1ffefaa4d ("Stop arming kqueue timers on knote owner suspend or terminate")
Sponsored by: The FreeBSD Foundation
Reviewed by: kib
Differential Revision: https://reviews.freebsd.org/D61359
| -rw-r--r-- | sys/kern/kern_event.c | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index a735dfaa97df..69fdfa121c50 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -700,7 +700,8 @@ struct kq_timer_cb_data { sbintime_t to; /* precalculated timer period, 0 for abs */ }; -#define KQ_TIMER_CB_ENQUEUED 0x01 +#define KQ_TIMER_CB_ENQUEUED 0x01 /* in queue of stopped timers */ +#define KQ_TIMER_CB_DETACHING 0x02 /* detach started, don't requeue */ static void kqtimer_sched_callout(struct kq_timer_cb_data *kc) @@ -768,7 +769,8 @@ filt_timerexpire_l(struct knote *kn, bool proc_locked) if (!proc_locked) PROC_LOCK(p); if (P_SHOULDSTOP(p) || P_KILLED(p)) { - if ((kc->flags & KQ_TIMER_CB_ENQUEUED) == 0) { + if ((kc->flags & (KQ_TIMER_CB_ENQUEUED | + KQ_TIMER_CB_DETACHING)) == 0) { /* * Insert into head so that * kqtimer_proc_continue() does not @@ -881,24 +883,16 @@ filt_timerdetach(struct knote *kn) { struct kq_timer_cb_data *kc; unsigned int old __unused; - bool pending; kc = kn->kn_ptr.p_v; - do { - callout_drain(&kc->c); - - /* - * kqtimer_proc_continue() might have rescheduled this callout. - * Double-check, using the process mutex as an interlock. - */ - PROC_LOCK(kc->p); - if ((kc->flags & KQ_TIMER_CB_ENQUEUED) != 0) { - kc->flags &= ~KQ_TIMER_CB_ENQUEUED; - TAILQ_REMOVE(&kc->p->p_kqtim_stop, kc, link); - } - pending = callout_pending(&kc->c); - PROC_UNLOCK(kc->p); - } while (pending); + PROC_LOCK(kc->p); + kc->flags |= KQ_TIMER_CB_DETACHING; + if ((kc->flags & KQ_TIMER_CB_ENQUEUED) != 0) { + kc->flags &= ~KQ_TIMER_CB_ENQUEUED; + TAILQ_REMOVE(&kc->p->p_kqtim_stop, kc, link); + } + PROC_UNLOCK(kc->p); + callout_drain(&kc->c); free(kc, M_KQUEUE); old = atomic_fetchadd_int(&kq_ncallouts, -1); KASSERT(old > 0, ("Number of callouts cannot become negative")); |
