From nobody Tue Nov 17 01:09:40 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 4jT68P0xm8z71Q1w for ; Tue, 17 Nov 2026 01:09:41 +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 4jT68N5QpGz3CBm for ; Tue, 17 Nov 2026 01:09:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794877780; 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=EcuMgOHmIrLGtLrSjySSYRuopaiyfoI72FyKmyuEiyM=; b=P2kukKJblYls9HNA8OxQuVOFAo9ZKQLL7/wPxP66EVvsXLxihImwDuGtTvSIy/sXpE+RIe +0k6A6f4V06PmDikOXpYjnE+FZXloT46MEeU5yDnAlljBzxMWSld5Uz2LtrCctlZNCwj9M IQ/Qi+A3poTS/lxJlHTUBfgA5CQD2d2H4+9NvHdD/sHNX5aypo9EjSpEirkQcllIRPpRqZ HWCAhBCNxzKNCZVs2MPeqadNgrL/lLcnra2hUHJBEigKj1b9UEj/jUKkaZcM5ezaE/eahY npRdG62kBXlv+kLJ27rl4Y3em3qMrR+iGt5T5lowgm8QXpJGGp5rVwfK/dVQkg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794877780; b=UGsAocdDIscsQs7bmdhihceBokEYIk1LHApU+qrKoJGLJqDzW4cnLoJf4d0i2RbWwsZl14 IH+F3q5Tuf6PB3wfMJ71jzfjJCEaOyX+ht5cobmCyJ1Ua7r0o5eO/WAzXOQkxksw4X5jTt 3aDkUyYwv20rm0HkcgXHqfo8xcXC4Q9pPKShXt3ow8uvRH7ikUMjpztbGRnNpBAxaBuMzu i701HcbmczBVjLtWKEjwKqFCnFe1GK6U+PLV5vplWnK9paeJIQYq9k5IZfq/imIiRNAYlG DtjzzsbdLDNjrzVERprJFr3Hq2pt7X5xli44HvNnDxBSFqaLK1RNSgRO+A008w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794877780; 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=EcuMgOHmIrLGtLrSjySSYRuopaiyfoI72FyKmyuEiyM=; b=VLwMdt+P0htip5XSQqGOGxs4dN1g9pm+Y/BXiCoYTAoB4llt4zhN5YdeOpV8fS6WuCQrtx 19P35aA1HD5vl2qy47aEcRuybJxvQH0pfNS0voYLunQIGO7RBbUTyvJFi/TrnBsdH6pg3D RSKLF+z4daUw00mQ8Zx2ljz3312bHDv6ka3VGicdlmcTNg93vTQFwYhmcmtQe6uyFT2+pk 2cCt6k+fv1SM+C55084PTL8pDBahPdveYkhQD4qpbz/OwriVwPxV7vp+lCwY9JdRIS5o0C EtGZa0wdSH2rGyWn1YJ0zWR2J+bWGEqb7ebYOq+xEnLjiyR7logDxuHGYEN5Fw== 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 4jT68N4XSRzJYn for ; Tue, 17 Nov 2026 01:09:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1925 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 17 Nov 2026 01:09:40 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 2efa9b03392e - stable/15 - 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 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/stable/15 X-Git-Reftype: branch X-Git-Commit: 2efa9b03392eb3c5ee41647981d30ed4cfaf0daa Auto-Submitted: auto-generated Date: Tue, 17 Nov 2026 01:09:40 +0000 Message-Id: <6afba954.1925.127dbf31@gitrepo.freebsd.org> The branch stable/15 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=2efa9b03392eb3c5ee41647981d30ed4cfaf0daa commit 2efa9b03392eb3c5ee41647981d30ed4cfaf0daa Author: Konstantin Belousov AuthorDate: 2026-11-05 20:21:14 +0000 Commit: Konstantin Belousov CommitDate: 2026-11-17 00:24:59 +0000 kern_umtx.c: validate clockid used for _umtx_op(2) timeouts (cherry picked from commit 524f1bf7f9fd8d5d0eee48346d6c1adb0413f95c) --- 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 77ce0f97bb4c..6ae344809a51 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 @@ -2241,6 +2299,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; @@ -2249,9 +2311,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) { @@ -2538,6 +2597,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 ? @@ -2545,9 +2608,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; @@ -3081,6 +3141,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) @@ -3089,9 +3153,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; @@ -3269,6 +3330,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) @@ -3277,9 +3342,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); @@ -3563,6 +3625,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) @@ -3571,9 +3637,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); @@ -3674,9 +3737,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) @@ -4134,6 +4197,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) { @@ -4152,8 +4216,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; @@ -4166,13 +4232,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);