Merge "FORTIFY_SOURCE: enhanced memcpy protections."
This commit is contained in:
commit
88bfc28ac4
3 changed files with 58 additions and 7 deletions
|
@ -87,9 +87,42 @@ extern size_t strxfrm(char *, const char *, size_t);
|
|||
|
||||
#if defined(__BIONIC_FORTIFY_INLINE)
|
||||
|
||||
extern void __memcpy_dest_size_error()
|
||||
__attribute__((__error__("memcpy called with size bigger than destination")));
|
||||
extern void __memcpy_src_size_error()
|
||||
__attribute__((__error__("memcpy called with size bigger than source")));
|
||||
extern void __memcpy_overlap_error()
|
||||
__attribute__((__error__("memcpy called with overlapping regions")));
|
||||
extern void *__memcpy_real(void *, const void *, size_t)
|
||||
__asm__(__USER_LABEL_PREFIX__ "memcpy");
|
||||
extern void *__memcpy_chk2(void *, const void *, size_t, size_t, size_t);
|
||||
|
||||
|
||||
__BIONIC_FORTIFY_INLINE
|
||||
void *memcpy (void *dest, const void *src, size_t len) {
|
||||
return __builtin___memcpy_chk(dest, src, len, __builtin_object_size (dest, 0));
|
||||
void *memcpy (void *dest, const void *src, size_t copy_amount) {
|
||||
char *d = (char *) dest;
|
||||
const char *s = (const char *) src;
|
||||
size_t s_len = __builtin_object_size(s, 0);
|
||||
size_t d_len = __builtin_object_size(d, 0);
|
||||
|
||||
if (__builtin_constant_p(copy_amount) && (copy_amount > d_len)) {
|
||||
__memcpy_dest_size_error();
|
||||
}
|
||||
|
||||
if (__builtin_constant_p(copy_amount) && (copy_amount > s_len)) {
|
||||
__memcpy_src_size_error();
|
||||
}
|
||||
|
||||
if (__builtin_constant_p(d - s) && __builtin_constant_p(copy_amount)
|
||||
&& (((size_t)(d - s) < copy_amount) || ((size_t)(s - d) < copy_amount))) {
|
||||
__memcpy_overlap_error();
|
||||
}
|
||||
|
||||
if (__builtin_constant_p(copy_amount) && __builtin_constant_p(d - s)) {
|
||||
return __memcpy_real(dest, src, copy_amount);
|
||||
}
|
||||
|
||||
return __memcpy_chk2(dest, src, copy_amount, d_len, s_len);
|
||||
}
|
||||
|
||||
__BIONIC_FORTIFY_INLINE
|
||||
|
|
|
@ -26,12 +26,13 @@
|
|||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#undef _FORTIFY_SOURCE
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <private/logd.h>
|
||||
|
||||
/*
|
||||
* Runtime implementation of __builtin____memcpy_chk.
|
||||
* Runtime implementation of __memcpy_chk2.
|
||||
*
|
||||
* See
|
||||
* http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
|
||||
|
@ -41,15 +42,31 @@
|
|||
* This memcpy check is called if _FORTIFY_SOURCE is defined and
|
||||
* greater than 0.
|
||||
*/
|
||||
void *__memcpy_chk (void *dest, const void *src,
|
||||
size_t len, size_t dest_len)
|
||||
void *__memcpy_chk2(void *dest, const void *src,
|
||||
size_t copy_amount, size_t dest_len, size_t src_len)
|
||||
{
|
||||
if (len > dest_len) {
|
||||
char *d = (char *) dest;
|
||||
const char *s = (const char *) src;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return memcpy(dest, src, len);
|
||||
if (__builtin_expect(copy_amount > src_len, 0)) {
|
||||
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
|
||||
"*** memcpy read overflow detected ***\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
if (__builtin_expect(((d <= s) && ((size_t)(s - d) < copy_amount))
|
||||
|| ((d >= s) && ((size_t)(d - s) < copy_amount)), 0)) {
|
||||
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
|
||||
"*** memcpy memory overlap detected ***\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
return memcpy(dest, src, copy_amount);
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
#undef _FORTIFY_SOURCE
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
|
|
Loading…
Reference in a new issue