From eb22dbd032a5a93d77d76505924116b5ccda5abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Wed, 20 Apr 2022 18:06:48 +0200 Subject: [PATCH] libroot: check timeout interval parameter in ppoll(), pselect() and select() * also introduce timeval_to_bigtime * time_private is c++ only Change-Id: I2bddbe5f95240735c7b42ccf429dcbb3f1e8bb1b Reviewed-on: https://review.haiku-os.org/c/haiku/+/5220 Tested-by: Commit checker robot Reviewed-by: waddlesplash Reviewed-by: Adrien Destugues --- headers/private/libroot/time_private.h | 8 ++++++++ src/system/kernel/lib/Jamfile | 4 ++-- src/system/libroot/posix/Jamfile | 2 +- src/system/libroot/posix/{poll.c => poll.cpp} | 7 ++++--- src/system/libroot/posix/sys/Jamfile | 2 +- .../posix/sys/{select.c => select.cpp} | 20 +++++++++---------- 6 files changed, 26 insertions(+), 17 deletions(-) rename src/system/libroot/posix/{poll.c => poll.cpp} (77%) rename src/system/libroot/posix/sys/{select.c => select.cpp} (79%) diff --git a/headers/private/libroot/time_private.h b/headers/private/libroot/time_private.h index 0cfd9faa6d..c01fde2ed2 100644 --- a/headers/private/libroot/time_private.h +++ b/headers/private/libroot/time_private.h @@ -67,6 +67,14 @@ timeval_to_timespec(const timeval& val, timespec& spec) } +static inline bool +timeval_to_bigtime(const timeval& val, bigtime_t& _time) +{ + timespec spec; + return timeval_to_timespec(val, spec) && timespec_to_bigtime(spec, _time); +} + + static inline void timespec_to_timeval(const timespec& spec, timeval& val) { diff --git a/src/system/kernel/lib/Jamfile b/src/system/kernel/lib/Jamfile index 408673f4a5..3591b3afa9 100644 --- a/src/system/kernel/lib/Jamfile +++ b/src/system/kernel/lib/Jamfile @@ -52,7 +52,7 @@ KernelMergeObject kernel_lib_posix.o : kernel_errno.cpp dirent.c fcntl.cpp - poll.c + poll.cpp utime.c # locale ctype.cpp @@ -77,7 +77,7 @@ KernelMergeObject kernel_lib_posix.o : chmod.c stat.c mkdir.c - select.c + select.cpp gettimeofday.c uio.c # time diff --git a/src/system/libroot/posix/Jamfile b/src/system/libroot/posix/Jamfile index ae4880dfcc..568e09073e 100644 --- a/src/system/libroot/posix/Jamfile +++ b/src/system/libroot/posix/Jamfile @@ -29,7 +29,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { inttypes.c libgen.cpp nftw.c - poll.c + poll.cpp $(PWD_BACKEND) scheduler.cpp semaphore.cpp diff --git a/src/system/libroot/posix/poll.c b/src/system/libroot/posix/poll.cpp similarity index 77% rename from src/system/libroot/posix/poll.c rename to src/system/libroot/posix/poll.cpp index 458683af47..687ad3c538 100644 --- a/src/system/libroot/posix/poll.c +++ b/src/system/libroot/posix/poll.cpp @@ -13,9 +13,10 @@ #include #include +#include -int __ppoll(struct pollfd *fds, nfds_t numfds, const struct timespec *tv, +extern "C" int __ppoll(struct pollfd *fds, nfds_t numfds, const struct timespec *tv, const sigset_t *sigMask); int @@ -32,8 +33,8 @@ __ppoll(struct pollfd *fds, nfds_t numfds, const struct timespec *tv, { int status; bigtime_t timeout = -1LL; - if (tv) - timeout = tv->tv_sec * 1000000LL + tv->tv_nsec / 1000LL; + if (tv != NULL && !timespec_to_bigtime(*tv, timeout)) + RETURN_AND_SET_ERRNO_TEST_CANCEL(EINVAL); status = _kern_poll(fds, numfds, timeout, sigMask); diff --git a/src/system/libroot/posix/sys/Jamfile b/src/system/libroot/posix/sys/Jamfile index d7f4bea3b5..cdf616ef18 100644 --- a/src/system/libroot/posix/sys/Jamfile +++ b/src/system/libroot/posix/sys/Jamfile @@ -23,7 +23,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { mman.cpp priority.c rlimit.c - select.c + select.cpp stat.c statvfs.c times.cpp diff --git a/src/system/libroot/posix/sys/select.c b/src/system/libroot/posix/sys/select.cpp similarity index 79% rename from src/system/libroot/posix/sys/select.c rename to src/system/libroot/posix/sys/select.cpp index 96948cebf2..2850268837 100644 --- a/src/system/libroot/posix/sys/select.c +++ b/src/system/libroot/posix/sys/select.cpp @@ -13,16 +13,16 @@ #include #include +#include #include #include - -#include +#include -int __pselect_beos(int numBits, struct fd_set *readBits, +extern "C" int __pselect_beos(int numBits, struct fd_set *readBits, struct fd_set *writeBits, struct fd_set *errorBits, const struct timespec *tv, const sigset_t *beosSignalMask); -int __pselect(int numBits, struct fd_set *readBits, struct fd_set *writeBits, +extern "C" int __pselect(int numBits, struct fd_set *readBits, struct fd_set *writeBits, struct fd_set *errorBits, const struct timespec *tv, const sigset_t *sigMask); @@ -35,8 +35,8 @@ __pselect_beos(int numBits, struct fd_set *readBits, struct fd_set *writeBits, int status; sigset_t signalMask; bigtime_t timeout = -1LL; - if (tv) - timeout = tv->tv_sec * 1000000LL + tv->tv_nsec / 1000LL; + if (tv != NULL && !timespec_to_bigtime(*tv, timeout)) + RETURN_AND_SET_ERRNO_TEST_CANCEL(EINVAL); if (beosSignalMask != NULL) signalMask = from_beos_sigset(*beosSignalMask); @@ -55,8 +55,8 @@ __pselect(int numBits, struct fd_set *readBits, struct fd_set *writeBits, { int status; bigtime_t timeout = -1LL; - if (tv) - timeout = tv->tv_sec * 1000000LL + tv->tv_nsec / 1000LL; + if (tv != NULL && !timespec_to_bigtime(*tv, timeout)) + RETURN_AND_SET_ERRNO_TEST_CANCEL(EINVAL); status = _kern_select(numBits, readBits, writeBits, errorBits, timeout, sigMask); @@ -71,8 +71,8 @@ select(int numBits, struct fd_set *readBits, struct fd_set *writeBits, { int status; bigtime_t timeout = -1LL; - if (tv) - timeout = tv->tv_sec * 1000000LL + tv->tv_usec; + if (tv != NULL && !timeval_to_bigtime(*tv, timeout)) + RETURN_AND_SET_ERRNO_TEST_CANCEL(EINVAL); status = _kern_select(numBits, readBits, writeBits, errorBits, timeout, NULL);