From: Stefan Metzmacher Date: Fri, 8 May 2020 11:06:54 +0000 (+0200) Subject: lib: util: Add sys_valid_io_range() X-Git-Tag: ldb-2.2.0~549 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e02cbd5c3ea6903d2b7b43c3193b8662d029ecdd;p=thirdparty%2Fsamba.git lib: util: Add sys_valid_io_range() This implements the contraints of [MS-FSA] 2.1.5.2 Server Requests a Read. The special handling of [MS-FSA] 2.1.5.3 Server Requests a Write with offset < 0, should be handled by higher layers! Which means the check can also be used for writes. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14361 Signed-off-by: Stefan Metzmacher Reviewed-by: Jeremy Allison --- diff --git a/lib/util/sys_rw.c b/lib/util/sys_rw.c index 9a6cdcaa606..6fa7ca57365 100644 --- a/lib/util/sys_rw.c +++ b/lib/util/sys_rw.c @@ -24,6 +24,30 @@ #include "system/filesys.h" #include "lib/util/sys_rw.h" +bool sys_valid_io_range(off_t offset, size_t length) +{ + uint64_t last_byte_ofs; + + if (offset < 0) { + return false; + } + + if (offset > INT64_MAX) { + return false; + } + + if (length > UINT32_MAX) { + return false; + } + + last_byte_ofs = (uint64_t)offset + (uint64_t)length; + if (last_byte_ofs > INT64_MAX) { + return false; + } + + return true; +} + /******************************************************************* A read wrapper that will deal with EINTR/EWOULDBLOCK ********************************************************************/ diff --git a/lib/util/sys_rw.h b/lib/util/sys_rw.h index ab456d87b22..70864cb2b74 100644 --- a/lib/util/sys_rw.h +++ b/lib/util/sys_rw.h @@ -27,6 +27,7 @@ struct iovec; +bool sys_valid_io_range(off_t offset, size_t length); ssize_t sys_read(int fd, void *buf, size_t count); void sys_read_v(int fd, void *buf, size_t count); ssize_t sys_write(int fd, const void *buf, size_t count);