clean up FORTIFY_SOURCE handling.
Avoid duplicating huge chunks of code.
Change-Id: Id6145cdfce781c5ffba2abaaa79681d25a7ab28f
diff --git a/libc/bionic/__fgets_chk.cpp b/libc/bionic/__fgets_chk.cpp
index 780cf16..6ae97cc 100644
--- a/libc/bionic/__fgets_chk.cpp
+++ b/libc/bionic/__fgets_chk.cpp
@@ -45,15 +45,11 @@
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);
diff --git a/libc/bionic/__memcpy_chk.cpp b/libc/bionic/__memcpy_chk.cpp
index 991ff02..7a98cb7 100644
--- a/libc/bionic/__memcpy_chk.cpp
+++ b/libc/bionic/__memcpy_chk.cpp
@@ -46,10 +46,8 @@
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);
diff --git a/libc/bionic/__memmove_chk.cpp b/libc/bionic/__memmove_chk.cpp
index 1867d71..51f2e1c 100644
--- a/libc/bionic/__memmove_chk.cpp
+++ b/libc/bionic/__memmove_chk.cpp
@@ -45,10 +45,8 @@
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);
diff --git a/libc/bionic/__memset_chk.cpp b/libc/bionic/__memset_chk.cpp
index 97c5c38..99a12ad 100644
--- a/libc/bionic/__memset_chk.cpp
+++ b/libc/bionic/__memset_chk.cpp
@@ -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);
diff --git a/libc/bionic/__strcat_chk.cpp b/libc/bionic/__strcat_chk.cpp
index ec194fc..2450da6 100644
--- a/libc/bionic/__strcat_chk.cpp
+++ b/libc/bionic/__strcat_chk.cpp
@@ -50,17 +50,13 @@
// 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);
diff --git a/libc/bionic/__strcpy_chk.cpp b/libc/bionic/__strcpy_chk.cpp
index 1d45ea2..74ceda1 100644
--- a/libc/bionic/__strcpy_chk.cpp
+++ b/libc/bionic/__strcpy_chk.cpp
@@ -45,10 +45,8 @@
// 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);
diff --git a/libc/bionic/__strlcat_chk.cpp b/libc/bionic/__strlcat_chk.cpp
index 05b7d7d..12676f4 100644
--- a/libc/bionic/__strlcat_chk.cpp
+++ b/libc/bionic/__strlcat_chk.cpp
@@ -46,9 +46,7 @@
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);
diff --git a/libc/bionic/__strlcpy_chk.cpp b/libc/bionic/__strlcpy_chk.cpp
index bf98037..62fa14b 100644
--- a/libc/bionic/__strlcpy_chk.cpp
+++ b/libc/bionic/__strlcpy_chk.cpp
@@ -46,9 +46,7 @@
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);
diff --git a/libc/bionic/__strlen_chk.cpp b/libc/bionic/__strlen_chk.cpp
index 67410d4..5cc052e 100644
--- a/libc/bionic/__strlen_chk.cpp
+++ b/libc/bionic/__strlen_chk.cpp
@@ -57,9 +57,7 @@
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;
diff --git a/libc/bionic/__strncat_chk.cpp b/libc/bionic/__strncat_chk.cpp
index 2ba8550..32a3962 100644
--- a/libc/bionic/__strncat_chk.cpp
+++ b/libc/bionic/__strncat_chk.cpp
@@ -55,17 +55,13 @@
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);
diff --git a/libc/bionic/__strncpy_chk.cpp b/libc/bionic/__strncpy_chk.cpp
index 875d092..c9676ed 100644
--- a/libc/bionic/__strncpy_chk.cpp
+++ b/libc/bionic/__strncpy_chk.cpp
@@ -45,10 +45,8 @@
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);
diff --git a/libc/bionic/__umask_chk.cpp b/libc/bionic/__umask_chk.cpp
index df066b2..e1bc96d 100644
--- a/libc/bionic/__umask_chk.cpp
+++ b/libc/bionic/__umask_chk.cpp
@@ -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);
diff --git a/libc/bionic/__vsnprintf_chk.cpp b/libc/bionic/__vsnprintf_chk.cpp
index b4f534b..95d4915 100644
--- a/libc/bionic/__vsnprintf_chk.cpp
+++ b/libc/bionic/__vsnprintf_chk.cpp
@@ -51,9 +51,7 @@
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);
diff --git a/libc/bionic/__vsprintf_chk.cpp b/libc/bionic/__vsprintf_chk.cpp
index 00010cf..e1d10f5 100644
--- a/libc/bionic/__vsprintf_chk.cpp
+++ b/libc/bionic/__vsprintf_chk.cpp
@@ -52,9 +52,7 @@
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;
diff --git a/libc/bionic/logd_write.c b/libc/bionic/logd_write.c
index ac71689..71a6f8e 100644
--- a/libc/bionic/logd_write.c
+++ b/libc/bionic/logd_write.c
@@ -247,3 +247,14 @@
{
__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();
+}
diff --git a/libc/private/logd.h b/libc/private/logd.h
index 26878ba..a2828ec 100644
--- a/libc/private/logd.h
+++ b/libc/private/logd.h
@@ -71,6 +71,8 @@
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
diff --git a/libc/string/strchr.c b/libc/string/strchr.c
index 44516ef..564ea80 100644
--- a/libc/string/strchr.c
+++ b/libc/string/strchr.c
@@ -35,11 +35,8 @@
__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)
diff --git a/libc/string/strrchr.c b/libc/string/strrchr.c
index fc3dc4e..5d0415e 100644
--- a/libc/string/strrchr.c
+++ b/libc/string/strrchr.c
@@ -37,11 +37,8 @@
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)