Merge "clean up FORTIFY_SOURCE handling."

This commit is contained in:
Nick Kralevich 2012-12-04 15:44:22 -08:00 committed by Gerrit Code Review
commit 7a34ed2bb3
18 changed files with 43 additions and 70 deletions

View file

@ -45,15 +45,11 @@ extern "C" char *__fgets_chk(char *dest, int supplied_size,
FILE *stream, size_t dest_len_from_compiler)
{
if (supplied_size < 0) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** fgets buffer size less than 0 ***\n");
abort();
__fortify_chk_fail("fgets buffer size less than 0", 0);
}
if (((size_t) supplied_size) > dest_len_from_compiler) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** fgets buffer overflow detected ***\n");
abort();
__fortify_chk_fail("fgets buffer overflow", 0);
}
return fgets(dest, supplied_size, stream);

View file

@ -46,10 +46,8 @@ extern "C" void *__memcpy_chk(void *dest, const void *src,
size_t copy_amount, size_t dest_len)
{
if (__builtin_expect(copy_amount > dest_len, 0)) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** memcpy buffer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW);
abort();
__fortify_chk_fail("memcpy buffer overflow",
BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW);
}
return memcpy(dest, src, copy_amount);

View file

@ -45,10 +45,8 @@ extern "C" void *__memmove_chk (void *dest, const void *src,
size_t len, size_t dest_len)
{
if (len > dest_len) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** memmove buffer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_MEMMOVE_BUFFER_OVERFLOW);
abort();
__fortify_chk_fail("memmove buffer overflow",
BIONIC_EVENT_MEMMOVE_BUFFER_OVERFLOW);
}
return memmove(dest, src, len);

View file

@ -43,10 +43,8 @@
*/
extern "C" void *__memset_chk (void *dest, int c, size_t n, size_t dest_len) {
if (n > dest_len) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** memset buffer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW);
abort();
__fortify_chk_fail("memset buffer overflow",
BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW);
}
return memset(dest, c, n);

View file

@ -50,17 +50,13 @@ extern "C" char *__strcat_chk (char *dest, const char *src, size_t dest_buf_size
// sum = src_len + dest_len + 1 (with overflow protection)
if (!safe_add3(&sum, src_len, dest_len, 1U)) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strcat integer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_STRCAT_INTEGER_OVERFLOW);
abort();
__fortify_chk_fail("strcat integer overflow",
BIONIC_EVENT_STRCAT_INTEGER_OVERFLOW);
}
if (sum > dest_buf_size) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strcat buffer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_STRNCAT_BUFFER_OVERFLOW);
abort();
__fortify_chk_fail("strcat buffer overflow",
BIONIC_EVENT_STRCAT_BUFFER_OVERFLOW);
}
return strcat(dest, src);

View file

@ -45,10 +45,8 @@ extern "C" char *__strcpy_chk (char *dest, const char *src, size_t dest_len) {
// TODO: optimize so we don't scan src twice.
size_t src_len = strlen(src) + 1;
if (src_len > dest_len) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strcpy buffer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_STRCPY_BUFFER_OVERFLOW);
abort();
__fortify_chk_fail("strcpy buffer overflow",
BIONIC_EVENT_STRCPY_BUFFER_OVERFLOW);
}
return strcpy(dest, src);

View file

@ -46,9 +46,7 @@ extern "C" size_t __strlcat_chk(char *dest, const char *src,
size_t supplied_size, size_t dest_len_from_compiler)
{
if (supplied_size > dest_len_from_compiler) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strlcat buffer overflow detected ***\n");
abort();
__fortify_chk_fail("strlcat buffer overflow", 0);
}
return strlcat(dest, src, supplied_size);

View file

@ -46,9 +46,7 @@ extern "C" size_t __strlcpy_chk(char *dest, const char *src,
size_t supplied_size, size_t dest_len_from_compiler)
{
if (supplied_size > dest_len_from_compiler) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strlcpy buffer overflow detected ***\n");
abort();
__fortify_chk_fail("strlcpy buffer overflow", 0);
}
return strlcpy(dest, src, supplied_size);

View file

@ -57,9 +57,7 @@ extern "C" size_t __strlen_chk(const char *s, size_t s_len) {
size_t ret = strlen(s);
if (__builtin_expect(ret >= s_len, 0)) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strlen read overflow detected ***\n");
abort();
__fortify_chk_fail("strlen read overflow", 0);
}
return ret;

View file

@ -55,17 +55,13 @@ extern "C" char *__strncat_chk (char *dest, const char *src,
size_t sum;
// sum = src_len + dest_len + 1 (with overflow protection)
if (!safe_add3(&sum, src_len, dest_len, 1U)) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strncat integer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_STRNCAT_INTEGER_OVERFLOW);
abort();
__fortify_chk_fail("strncat integer overflow",
BIONIC_EVENT_STRNCAT_INTEGER_OVERFLOW);
}
if (sum > dest_buf_size) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strncat buffer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_STRNCAT_BUFFER_OVERFLOW);
abort();
__fortify_chk_fail("strncat buffer overflow",
BIONIC_EVENT_STRNCAT_BUFFER_OVERFLOW);
}
return strncat(dest, src, len);

View file

@ -45,10 +45,8 @@ extern "C" char *__strncpy_chk (char *dest, const char *src,
size_t len, size_t dest_len)
{
if (len > dest_len) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** strncpy buffer overflow detected ***\n");
__libc_android_log_event_uid(BIONIC_EVENT_STRNCPY_BUFFER_OVERFLOW);
abort();
__fortify_chk_fail("strncpy buffer overflow",
BIONIC_EVENT_STRNCPY_BUFFER_OVERFLOW);
}
return strncpy(dest, src, len);

View file

@ -43,9 +43,7 @@
*/
extern "C" mode_t __umask_chk(mode_t mode) {
if ((mode & 0777) != mode) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** FORTIFY_SOURCE: umask called with invalid mask ***\n");
abort();
__fortify_chk_fail("umask called with invalid mask", 0);
}
return umask(mode);

View file

@ -51,9 +51,7 @@ extern "C" int __vsnprintf_chk(
va_list va)
{
if (supplied_size > dest_len_from_compiler) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** vsnprintf buffer overflow detected ***\n");
abort();
__fortify_chk_fail("vsnprintf buffer overflow", 0);
}
return vsnprintf(dest, supplied_size, format, va);

View file

@ -52,9 +52,7 @@ extern "C" int __vsprintf_chk(
int ret = vsnprintf(dest, dest_len_from_compiler, format, va);
if ((size_t) ret >= dest_len_from_compiler) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** vsprintf buffer overflow detected ***\n");
abort();
__fortify_chk_fail("vsprintf buffer overflow", 0);
}
return ret;

View file

@ -247,3 +247,14 @@ void __libc_android_log_event_uid(int32_t tag)
{
__libc_android_log_event_int(tag, getuid());
}
__LIBC_HIDDEN__
void __fortify_chk_fail(const char *msg, uint32_t tag) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"FORTIFY_SOURCE: %s. Calling abort().\n",
msg);
if (tag != 0) {
__libc_android_log_event_uid(tag);
}
abort();
}

View file

@ -71,6 +71,8 @@ int __libc_android_log_vprint(int prio, const char *tag, const char *fmt, va_lis
void __libc_android_log_event_int(int32_t tag, int value);
void __libc_android_log_event_uid(int32_t tag);
__noreturn extern void __fortify_chk_fail(const char *, uint32_t);
#ifdef __cplusplus
};
#endif

View file

@ -35,11 +35,8 @@ char *
__strchr_chk(const char *p, int ch, size_t s_len)
{
for (;; ++p, s_len--) {
if (s_len == 0) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** FORTIFY_SOURCE strchr read beyond buffer ***\n");
abort();
}
if (s_len == 0)
__fortify_chk_fail("strchr read beyond buffer", 0);
if (*p == (char) ch)
return((char *)p);
if (!*p)

View file

@ -37,11 +37,8 @@ __strrchr_chk(const char *p, int ch, size_t s_len)
char *save;
for (save = NULL;; ++p, s_len--) {
if (s_len == 0) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** FORTIFY_SOURCE strrchr read beyond buffer ***\n");
abort();
}
if (s_len == 0)
__fortify_chk_fail("strrchr read beyond buffer", 0);
if (*p == (char) ch)
save = (char *)p;
if (!*p)