git: 91e7bdcdcf10 - main - Add timespecvalid_interval macro and use it.

From: Dmitry Chagin <dchagin_at_FreeBSD.org>
Date: Mon, 25 Apr 2022 07:23:41 UTC
The branch main has been updated by dchagin:

URL: https://cgit.FreeBSD.org/src/commit/?id=91e7bdcdcf10684098e213e311cdafcd7a0ac983

commit 91e7bdcdcf10684098e213e311cdafcd7a0ac983
Author:     Dmitry Chagin <dchagin@FreeBSD.org>
AuthorDate: 2022-04-25 07:20:54 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2022-04-25 07:20:54 +0000

    Add timespecvalid_interval macro and use it.
    
    Reviewed by:            jhb, imp (early rev)
    Differential revision:  https://reviews.freebsd.org/D34848
    MFC after:              2 weeks
---
 sys/compat/linux/linux_time.c |  4 ++--
 sys/kern/kern_event.c         |  3 +--
 sys/kern/kern_time.c          |  4 ++--
 sys/kern/kern_umtx.c          | 21 ++++++---------------
 sys/kern/sys_generic.c        |  4 +---
 sys/sys/time.h                |  2 ++
 6 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/sys/compat/linux/linux_time.c b/sys/compat/linux/linux_time.c
index f43e4d3985aa..739480342ad9 100644
--- a/sys/compat/linux/linux_time.c
+++ b/sys/compat/linux/linux_time.c
@@ -142,7 +142,7 @@ int
 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
 {
 
-	if (ltp->tv_sec < 0 || ltp->tv_nsec < 0 || ltp->tv_nsec > 999999999)
+	if (!timespecvalid_interval(ltp))
 		return (EINVAL);
 	ntp->tv_sec = ltp->tv_sec;
 	ntp->tv_nsec = ltp->tv_nsec;
@@ -165,7 +165,7 @@ int
 linux_to_native_timespec64(struct timespec *ntp, struct l_timespec64 *ltp64)
 {
 
-	if (ltp64->tv_sec < 0 || ltp64->tv_nsec < 0 || ltp64->tv_nsec > 999999999)
+	if (!timespecvalid_interval(ltp64))
 		return (EINVAL);
 	ntp->tv_sec = ltp64->tv_sec;
 	ntp->tv_nsec = ltp64->tv_nsec;
diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
index 0dc715f96670..a49762d9453c 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -1930,8 +1930,7 @@ kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
 
 	rsbt = 0;
 	if (tsp != NULL) {
-		if (tsp->tv_sec < 0 || tsp->tv_nsec < 0 ||
-		    tsp->tv_nsec >= 1000000000) {
+		if (!timespecvalid_interval(tsp)) {
 			error = EINVAL;
 			goto done_nl;
 		}
diff --git a/sys/kern/kern_time.c b/sys/kern/kern_time.c
index f052c4b6d698..de1b4ea7bc02 100644
--- a/sys/kern/kern_time.c
+++ b/sys/kern/kern_time.c
@@ -411,7 +411,7 @@ kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
 		return (error);
 	if (clock_id != CLOCK_REALTIME)
 		return (EINVAL);
-	if (ats->tv_nsec < 0 || ats->tv_nsec >= NS_PER_SEC || ats->tv_sec < 0)
+	if (!timespecvalid_interval(ats))
 		return (EINVAL);
 	if (!allow_insane_settime &&
 	    (ats->tv_sec > 8000ULL * 365 * 24 * 60 * 60 ||
@@ -1644,7 +1644,7 @@ static int
 itimespecfix(struct timespec *ts)
 {
 
-	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= NS_PER_SEC)
+	if (!timespecvalid_interval(ts))
 		return (EINVAL);
 	if ((UINT64_MAX - ts->tv_nsec) / NS_PER_SEC < ts->tv_sec)
 		return (EINVAL);
diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c
index 7f74ba68b59c..2f666bdcdc7b 100644
--- a/sys/kern/kern_umtx.c
+++ b/sys/kern/kern_umtx.c
@@ -3772,9 +3772,7 @@ umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
 
 	error = copyin(uaddr, tsp, sizeof(*tsp));
 	if (error == 0) {
-		if (tsp->tv_sec < 0 ||
-		    tsp->tv_nsec >= 1000000000 ||
-		    tsp->tv_nsec < 0)
+		if (!timespecvalid_interval(tsp))
 			error = EINVAL;
 	}
 	return (error);
@@ -3793,8 +3791,7 @@ umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
 		error = copyin(uaddr, tp, sizeof(*tp));
 	if (error != 0)
 		return (error);
-	if (tp->_timeout.tv_sec < 0 ||
-	    tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
+	if (!timespecvalid_interval(&tp->_timeout))
 		return (EINVAL);
 	return (0);
 }
@@ -4640,9 +4637,7 @@ umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
 
 	error = copyin(uaddr, &ts32, sizeof(ts32));
 	if (error == 0) {
-		if (ts32.tv_sec < 0 ||
-		    ts32.tv_nsec >= 1000000000 ||
-		    ts32.tv_nsec < 0)
+		if (!timespecvalid_interval(&ts32))
 			error = EINVAL;
 		else {
 			CP(ts32, *tsp, tv_sec);
@@ -4666,8 +4661,7 @@ umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
 		error = copyin(uaddr, &t32, sizeof(t32));
 	if (error != 0)
 		return (error);
-	if (t32._timeout.tv_sec < 0 ||
-	    t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
+	if (!timespecvalid_interval(&t32._timeout))
 		return (EINVAL);
 	TS_CP(t32, *tp, _timeout);
 	CP(t32, *tp, _flags);
@@ -4704,9 +4698,7 @@ umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
 
 	error = copyin(uaddr, &ts32, sizeof(ts32));
 	if (error == 0) {
-		if (ts32.tv_sec < 0 ||
-		    ts32.tv_nsec >= 1000000000 ||
-		    ts32.tv_nsec < 0)
+		if (!timespecvalid_interval(&ts32))
 			error = EINVAL;
 		else {
 			CP(ts32, *tsp, tv_sec);
@@ -4730,8 +4722,7 @@ umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
 		error = copyin(uaddr, &t32, sizeof(t32));
 	if (error != 0)
 		return (error);
-	if (t32._timeout.tv_sec < 0 ||
-	    t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
+	if (!timespecvalid_interval(&t32._timeout))
 		return (EINVAL);
 	TS_CP(t32, *tp, _timeout);
 	CP(t32, *tp, _flags);
diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c
index dd33d3dbe53e..b8cf1ff45610 100644
--- a/sys/kern/sys_generic.c
+++ b/sys/kern/sys_generic.c
@@ -1501,9 +1501,7 @@ kern_poll_kfds(struct thread *td, struct pollfd *kfds, u_int nfds,
 
 	precision = 0;
 	if (tsp != NULL) {
-		if (tsp->tv_sec < 0)
-			return (EINVAL);
-		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
+		if (!timespecvalid_interval(tsp))
 			return (EINVAL);
 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
 			sbt = 0;
diff --git a/sys/sys/time.h b/sys/sys/time.h
index f3a3bc99a0f2..7520478cf3ed 100644
--- a/sys/sys/time.h
+++ b/sys/sys/time.h
@@ -404,6 +404,8 @@ tvtosbt(struct timeval _tv)
 			(vsp)->tv_nsec += 1000000000L;			\
 		}							\
 	} while (0)
+#define	timespecvalid_interval(tsp)	((tsp)->tv_sec >= 0 &&		\
+	    (tsp)->tv_nsec >= 0 && (tsp)->tv_nsec < 1000000000L)
 
 #ifdef _KERNEL