lib: util: Add sys_valid_io_range()
authorStefan Metzmacher <metze@samba.org>
Fri, 8 May 2020 11:06:54 +0000 (13:06 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 12 May 2020 19:53:43 +0000 (19:53 +0000)
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 <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/sys_rw.c
lib/util/sys_rw.h

index 9a6cdcaa606343845616e1673622a2736d64425c..6fa7ca57365423aac7a752dae3994496b3d50ea5 100644 (file)
 #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
 ********************************************************************/
index ab456d87b22e9747dce2e8d9d94c82e799ffbaec..70864cb2b74a16466b7ec0d9c305a815596ee752 100644 (file)
@@ -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);