aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Johnston <markj@FreeBSD.org>2026-11-16 13:46:51 +0000
committerMark Johnston <markj@FreeBSD.org>2026-11-16 21:25:02 +0000
commit701478cfd69762403b9c38c7204ec5beb5962313 (patch)
tree04b5afd6855980fb877cd0a87ec854a14aece73e
parent0c7ff24b1eb246f648caeec99ea5cd6de87b3f85 (diff)
kqueue: Fix a race between kqueue_fork_copy_list() and kqueue_expand()
While kqueue_fork_copy_list() is iterating over the knote lists, the backing array may change while the kqueue lock is dropped. Since each list is a singly-linked list, kqueue_fork_copy_list() must start iterating from the list head in order to remove the marker, but that list head pointer is invalid after kqueue_expand() runs. Add a bit of indirection to re-compute the correct list head each time the kqueue lock is dropped. Avoid changing the layout of struct knote since I want to backport this to releng branches. Approved by: so Security: FreeBSD-SA-26:85.kqueue Security: CVE-2026-127750 Reported by: Dennys Kaluzny Pereira <dennyskaluzny@gmail.com> Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D61608
-rw-r--r--sys/kern/kern_event.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
index 937a5c87cb5f..476e48b072ae 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -3138,15 +3138,26 @@ kqueue_fork_copy_knote(struct kqueue *kq, struct kqueue *kq1, struct knote *kn,
KQ_UNLOCK(kq1);
}
+enum klist_which {
+ KLIST_FD,
+ KLIST_HASH,
+};
+
+static struct klist *
+kqueue_list(struct kqueue *kq, enum klist_which which, int i)
+{
+ return (which == KLIST_FD ? &kq->kq_knlist[i] : &kq->kq_knhash[i]);
+}
+
static void
-kqueue_fork_copy_list(struct klist *knlist, struct knote *marker,
+kqueue_fork_copy_list(enum klist_which which, int i, struct knote *marker,
struct kqueue *kq, struct kqueue *kq1, struct proc *p1,
struct filedesc *fdp)
{
struct knote *kn;
KQ_OWNED(kq);
- kn = SLIST_FIRST(knlist);
+ kn = SLIST_FIRST(kqueue_list(kq, which, i));
while (kn != NULL) {
MPASS(kn->kn_kq == kq);
if ((kn->kn_status & (KN_DETACHED | KN_MARKER)) != 0 ||
@@ -3162,7 +3173,7 @@ kqueue_fork_copy_list(struct klist *knlist, struct knote *marker,
kn_leave_flux(kn);
kn = SLIST_NEXT(marker, kn_link);
/* XXXKIB switch kn_link to LIST? */
- SLIST_REMOVE(knlist, marker, knote, kn_link);
+ SLIST_REMOVE(kqueue_list(kq, which, i), marker, knote, kn_link);
}
}
@@ -3186,13 +3197,12 @@ kqueue_fork_copy(struct filedesc *fdp, struct file *fp, struct file *fp1,
KQ_LOCK(kq);
for (i = 0; i < kq->kq_knlistsize; i++) {
- kqueue_fork_copy_list(&kq->kq_knlist[i], marker, kq, kq1,
- p1, fdp);
+ kqueue_fork_copy_list(KLIST_FD, i, marker, kq, kq1, p1, fdp);
}
if (kq->kq_knhashmask != 0) {
for (i = 0; i <= kq->kq_knhashmask; i++) {
- kqueue_fork_copy_list(&kq->kq_knhash[i], marker, kq,
- kq1, p1, fdp);
+ kqueue_fork_copy_list(KLIST_HASH, i, marker, kq, kq1,
+ p1, fdp);
}
}
kqueue_release(kq, 1);