hidl_memory: fail on transfer if size > SIZE_MAX

hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
size in size_t. If size is over SIZE_MAX, mapMemory could succeed
but the mapped memory's actual size will be smaller than the reported size.

This CL makes sure that if hidl_memory enters from another process
and then is used in other functions (but not mapMemory), it will
still trigger this bug.

Bug: 79376389
Test: hidl_test
Merged-In: Id5c8682627fdb1701e95b049d198bbcd0e3513d3
Change-Id: Id5c8682627fdb1701e95b049d198bbcd0e3513d3
diff --git a/transport/HidlBinderSupport.cpp b/transport/HidlBinderSupport.cpp
index f421953..142720a 100644
--- a/transport/HidlBinderSupport.cpp
+++ b/transport/HidlBinderSupport.cpp
@@ -19,6 +19,7 @@
 #include <hidl/HidlBinderSupport.h>
 
 // C includes
+#include <inttypes.h>
 #include <unistd.h>
 
 // C++ includes
@@ -49,6 +50,15 @@
                 parentOffset + hidl_memory::kOffsetOfName);
     }
 
+    // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
+    // size in size_t. If size is over SIZE_MAX, mapMemory could succeed
+    // but the mapped memory's actual size will be smaller than the reported size.
+    if (memory.size() > SIZE_MAX) {
+        ALOGE("Cannot use memory with %" PRId64 " bytes because it is too large.", memory.size());
+        android_errorWriteLog(0x534e4554, "79376389");
+        return BAD_VALUE;
+    }
+
     return _hidl_err;
 }