FORTIFY_SOURCE: fortify read()
Change-Id: Ic7de163fe121db13e00560adb257331bc709814d
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 60964f0..fb1f663 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -202,6 +202,39 @@
} while (_rc == -1 && errno == EINTR); \
_rc; })
+#if defined(__BIONIC_FORTIFY)
+extern ssize_t __read_chk(int, void*, size_t, size_t);
+__errordecl(__read_dest_size_error, "read called with size bigger than destination");
+__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
+extern ssize_t __read_real(int, void*, size_t)
+ __asm__(__USER_LABEL_PREFIX__ "read");
+
+__BIONIC_FORTIFY_INLINE
+ssize_t read(int fd, void* buf, size_t count) {
+ size_t bos = __bos0(buf);
+
+#if !defined(__clang__)
+ if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
+ __read_count_toobig_error();
+ }
+
+ if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
+ return __read_real(fd, buf, count);
+ }
+
+ if (__builtin_constant_p(count) && (count > bos)) {
+ __read_dest_size_error();
+ }
+
+ if (__builtin_constant_p(count) && (count <= bos)) {
+ return __read_real(fd, buf, count);
+ }
+#endif
+
+ return __read_chk(fd, buf, count, bos);
+}
+#endif /* defined(__BIONIC_FORTIFY) */
+
__END_DECLS
#endif /* _UNISTD_H_ */