From nobody Tue Nov 10 00:22:02 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jNnQf6qdmz70l7F for ; Tue, 10 Nov 2026 00:22:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jNnQf62Hrz3QbS for ; Tue, 10 Nov 2026 00:22:02 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794270122; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SN3tDMpyYE91iubZain2W6I/7Tl4fAs9zZth+5/cShE=; b=FzHRUAoe+qLqvrYC+ihHmFV5p9LnN3E6xDzamUzQ0+GJF7ypbFqKb/O6YTLyPyJkDXnaV+ tMgxXV2zHQjS8jUBd4w97tV4tIo0u16eRS6BLW9Y+JpaMjjpuobNcRaGL1Bi3C04OVQ8Rv eqnxTQaKr02Rp+eEhnAhnYtYhBG4o8M1Rjsre4Y1sDxjOwrKdOwhKI5hxQ+2oTbnNltO3i 2XlP6XjsEFWYrE6p8Zf2cDpUgw3jxuigkV0BocUBkTUofz3xF+jp0Mwhc/wCnymLhIZqoR Oo7VrtS824hZrGaLKrwbibbkVyfkSnqPfzCsfshZj9L1aOX86XAWjqznUXRUdA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794270122; b=Izki6G/GOu2rd2AFQg5OrqoXrz9SpYbM5g3Cre9UURSEDiJQLG4P6w1DFSraLNd33lW+K0 4L6Tl9Rmqpyf/Pog90GuI9x2ywSP/NRDn2bGYpD/niZdJHOFS2Du4xoux4yrQ/muaKRe/t T+FNADAvBWBIKUnkcOCFdY05BnDpGp/juV2QMdYfhX1NWPNIXBS6cMMIiom9MhTQu38DrN Zvc5w8B3E9mlJ/KWzaSEY4kHA6kFXyUvPBDLreVm6pFSZ4sC8M37jjPW31k1JOlEzO1iYi hpZSIgXUEgEkaMUmRTLUoJ0jaPgCYzAJP5Xww9nXLpuDeQRvrHEMQI2aosMeqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794270122; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SN3tDMpyYE91iubZain2W6I/7Tl4fAs9zZth+5/cShE=; b=kUIollz/zfszA/EUlKg1BuEmjFN1R/fqVkOzIDKW8bAZvu4+e07ocjLLK5wRFYvnSzsutf SmLBJrF92Cpc3AnjRS7DSpMXXpvJKmfh6ZRqOmdK/FfBsr+qNWSCXbakSUYFiAJmimnCfW fLAm8KfycuACpIfs1cXOdbbJndILFxs01rK1MFSGZzTAazJj+I0r/iNajxw0I82fR+dGT0 yEMsrHP4Li7EtVm6kfNmaipx4zEPACHSplmFcaohpDDXbyqDnAAAngMhxWB3r1db5UR/De Rqz/MUQmdnYn3fgnCNrwpYTTlpdpSYMT/YAXOSl7caLAzplEPFVEY7f7MdwrDQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jNnQf5hTcz14Jv for ; Tue, 10 Nov 2026 00:22:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id d646 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 10 Nov 2026 00:22:02 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 524f1bf7f9fd - main - kern_umtx.c: validate clockid used for _umtx_op(2) timeouts List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 524f1bf7f9fd8d5d0eee48346d6c1adb0413f95c Auto-Submitted: auto-generated Date: Tue, 10 Nov 2026 00:22:02 +0000 Message-Id: <6af263aa.d646.61b7c23@gitrepo.freebsd.org> The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=524f1bf7f9fd8d5d0eee48346d6c1adb0413f95c commit 524f1bf7f9fd8d5d0eee48346d6c1adb0413f95c Author: Konstantin Belousov AuthorDate: 2026-11-05 20:21:14 +0000 Commit: Konstantin Belousov CommitDate: 2026-11-10 00:21:18 +0000 kern_umtx.c: validate clockid used for _umtx_op(2) timeouts Only allow clocks that make sense for the umtx_sleep(). While there, do not ignore errors from kern_clock_gettime(). The invalid clock ids are filtered, but if the clock can't be read (CLOCK_TAI before the TAI-UTC offset is known), the functions operate on uninitialized memory. Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D61253 --- sys/kern/kern_umtx.c | 149 ++++++++++++++++++++++++++++++++++++--------------- sys/sys/umtxvar.h | 2 +- 2 files changed, 107 insertions(+), 44 deletions(-) diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c index 55ef536d5b98..9182b13cba5a 100644 --- a/sys/kern/kern_umtx.c +++ b/sys/kern/kern_umtx.c @@ -171,7 +171,7 @@ static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0 "umtx chain stats"); #endif -static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo, +static inline int umtx_abs_timeout_init2(struct umtx_abs_timeout *timo, const struct _umtx_time *umtxtime); static void umtx_shm_init(void); @@ -678,15 +678,58 @@ tstohz(const struct timespec *tsp) return tvtohz(&tv); } -void +/* + * Clocks that umtx_abs_timeout_getsbt() can wait on. + * + * The process CPU-time clocks CLOCK_VIRTUAL, CLOCK_PROF, and + * CLOCK_PROCESS_CPUTIME_ID advance while other threads of the process + * run. CLOCK_THREAD_CPUTIME_ID is not accepted because the CPU time + * of the sleeping thread does not advance. + * + * The clock ids from clock_getcpuclockid(3) and + * pthread_getcpuclockid(3) are not accepted, at least for now. + * Reading the CPU time of another process takes sleepable locks, + * while the timeout is checked with the umtx chain lock held. + */ +static int +umtx_check_clockid(clockid_t clockid) +{ + switch (clockid) { + case CLOCK_REALTIME: + case CLOCK_REALTIME_PRECISE: + case CLOCK_REALTIME_FAST: + case CLOCK_MONOTONIC: + case CLOCK_MONOTONIC_PRECISE: + case CLOCK_MONOTONIC_FAST: + case CLOCK_UPTIME: + case CLOCK_UPTIME_PRECISE: + case CLOCK_UPTIME_FAST: + case CLOCK_SECOND: + case CLOCK_VIRTUAL: + case CLOCK_PROF: + case CLOCK_PROCESS_CPUTIME_ID: + case CLOCK_TAI: + return (0); + default: + return (EINVAL); + } +} + +int umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid, int absolute, const struct timespec *timeout) { + int error; + error = umtx_check_clockid(clockid); + if (error != 0) + return (error); + error = kern_clock_gettime(curthread, clockid, &timo->cur); + if (error != 0) + return (error); timo->clockid = clockid; if (!absolute) { timo->is_abs_real = false; - kern_clock_gettime(curthread, timo->clockid, &timo->cur); timespecadd(&timo->cur, timeout, &timo->end); } else { timo->end = *timeout; @@ -696,15 +739,17 @@ umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid, clockid == CLOCK_TAI || clockid == CLOCK_SECOND; } + return (0); } -static void +static int umtx_abs_timeout_init2(struct umtx_abs_timeout *timo, const struct _umtx_time *umtxtime) { - - umtx_abs_timeout_init(timo, umtxtime->_clockid, - (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout); + if (umtxtime == NULL) + return (0); + return (umtx_abs_timeout_init(timo, umtxtime->_clockid, + (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout)); } static void @@ -727,6 +772,7 @@ umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt, struct bintime bt, bbt; struct timespec tts; sbintime_t rem; + int error; switch (timo->clockid) { @@ -790,7 +836,10 @@ umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt, case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_TAI: /* Boot time is not necessarily stable in TAI */ default: - kern_clock_gettime(curthread, timo->clockid, &timo->cur); + error = kern_clock_gettime(curthread, timo->clockid, + &timo->cur); + if (error != 0) + return (error); if (timespeccmp(&timo->end, &timo->cur, <=)) return (ETIMEDOUT); timespecsub(&timo->end, &timo->cur, &tts); @@ -927,8 +976,12 @@ do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id, int error = 0; uq = td->td_umtxq; - if (timeout != NULL) - umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout); + if (timeout != NULL) { + error = umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, + timeout); + if (error != 0) + return (error); + } /* * Care must be exercised when dealing with umtx structure. It @@ -1109,8 +1162,12 @@ do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id, uq = td->td_umtxq; - if (timeout != NULL) - umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout); + if (timeout != NULL) { + error = umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, + timeout); + if (error != 0) + return (error); + } /* * Care must be exercised when dealing with umtx structure. It @@ -1288,14 +1345,15 @@ do_wait(struct thread *td, void *addr, u_long id, uint32_t tmp32; int error = 0; + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); + uq = td->td_umtxq; if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT, is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0) return (error); - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); - umtxq_lock(&uq->uq_key); umtxq_insert(uq); umtxq_unlock(&uq->uq_key); @@ -1362,9 +1420,9 @@ do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, id = td->td_tid; uq = td->td_umtxq; - error = 0; - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); /* * Care must be exercised when dealing with umtx structure. It @@ -2250,6 +2308,10 @@ do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags, uint32_t id, old_owner, owner, old; int error, rv; + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); + id = td->td_tid; uq = td->td_umtxq; @@ -2258,9 +2320,6 @@ do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags, &uq->uq_key)) != 0) return (error); - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); - umtxq_lock(&uq->uq_key); pi = umtx_pi_lookup(&uq->uq_key); if (pi == NULL) { @@ -2547,6 +2606,10 @@ do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags, int error, pri, old_inherited_pri, new_pri, rv; bool su; + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); + id = td->td_tid; uq = td->td_umtxq; if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ? @@ -2554,9 +2617,6 @@ do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags, &uq->uq_key)) != 0) return (error); - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); - su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0); for (;;) { old_inherited_pri = uq->uq_inherited_pri; @@ -3090,6 +3150,10 @@ do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, int32_t blocked_readers; int error, error1, rv; + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); + uq = td->td_umtxq; error = fueword32(&rwlock->rw_flags, &flags); if (error == -1) @@ -3098,9 +3162,6 @@ do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, if (error != 0) return (error); - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); - wrflags = URWLOCK_WRITE_OWNER; if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER)) wrflags |= URWLOCK_WRITE_WAITERS; @@ -3278,6 +3339,10 @@ do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeo int32_t blocked_readers; int error, error1, rv; + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); + uq = td->td_umtxq; error = fueword32(&rwlock->rw_flags, &flags); if (error == -1) @@ -3286,9 +3351,6 @@ do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeo if (error != 0) return (error); - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); - blocked_readers = 0; for (;;) { rv = fueword32(&rwlock->rw_state, &state); @@ -3572,6 +3634,10 @@ do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout) uint32_t flags, count, count1; int error, rv, rv1; + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); + uq = td->td_umtxq; error = fueword32(&sem->_flags, &flags); if (error == -1) @@ -3580,9 +3646,6 @@ do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout) if (error != 0) return (error); - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); - again: umtxq_lock(&uq->uq_key); umtxq_busy(&uq->uq_key); @@ -3683,9 +3746,9 @@ do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout) uq = td->td_umtxq; flags = fuword32(&sem->_flags); - if (timeout != NULL) - umtx_abs_timeout_init2(&timo, timeout); - + error = umtx_abs_timeout_init2(&timo, timeout); + if (error != 0) + return (error); again: error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key); if (error != 0) @@ -4143,6 +4206,7 @@ __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap, (wflags & CVWAIT_UMTX_TIME) != 0)) return (EINVAL); + error = 0; if ((wflags & CVWAIT_UMTX_TIME) == 0) { /* Allow a null timespec (wait forever). */ if (uap->uaddr2 == NULL) { @@ -4161,8 +4225,10 @@ __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap, clockid = CLOCK_REALTIME; } if (ts != NULL) { - umtx_abs_timeout_init(&timo, clockid, + error = umtx_abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0, ts); + if (error != 0) + return (error); timop = &timo; } else { timop = NULL; @@ -4175,13 +4241,10 @@ __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap, if (error != 0) return (error); timop = &timo; - umtx_abs_timeout_init2(timop, &umtime); + error = umtx_abs_timeout_init2(timop, &umtime); + if (error != 0) + return (error); } - /* only HW clock id will work. */ - if (timop != NULL && (timop->clockid < CLOCK_REALTIME || - timop->clockid >= CLOCK_THREAD_CPUTIME_ID) && - timop->clockid != CLOCK_TAI) - return (EINVAL); return (do_cv_wait(td, cv, uap->uaddr1, timop, wflags)); } diff --git a/sys/sys/umtxvar.h b/sys/sys/umtxvar.h index 6165d37d9195..8f443cd48b7a 100644 --- a/sys/sys/umtxvar.h +++ b/sys/sys/umtxvar.h @@ -198,7 +198,7 @@ umtx_key_match(const struct umtx_key *k1, const struct umtx_key *k2) k1->info.both.b == k2->info.both.b); } -void umtx_abs_timeout_init(struct umtx_abs_timeout *, int, int, +int umtx_abs_timeout_init(struct umtx_abs_timeout *, int, int, const struct timespec *); int umtx_copyin_timeout(const void *, struct timespec *); void umtx_exec(struct proc *p);