FORTIFY_SOURCE: fortify read()

Change-Id: I3d7b4ec86d04efb865117ce7629a2e26917f3331
This commit is contained in:
Nick Kralevich
2013-10-09 13:44:38 -07:00
parent 777a4ee677
commit 65c99de2cb
4 changed files with 87 additions and 0 deletions

View File

@@ -202,6 +202,39 @@ extern int setdomainname(const char *, size_t);
} 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_ */