evilsocket / arminject

An application to dynamically inject a shared object into a running process on ARM architectures.
Other
447 stars 160 forks source link

Remote memory write `corruption` bug #8

Closed scottishdeveloper closed 8 years ago

scottishdeveloper commented 8 years ago
/*
You copy 26 bytes, the remaining alignment bytes bytes are junk (not \0\0),
PTRACE_POKETEXT will write those 2 junk bytes from malloc(28)!
*/
memcpy(ptr, buf, blen);

Solution:

Use calloc() instead of malloc() (https://github.com/evilsocket/arminject/blob/master/jni/injector/traced.hpp#L144)

char *ptr = (char *)calloc(blen + blen % sizeof(size_t),1);

PS. Sometimes you'll get real lucky and malloc actually returns 0 initialized bytes in the alignment area, masking this elusive issue! ;)

Cheers!

Martin Alexander

scottishdeveloper commented 8 years ago

Or alternatively:

write( mem, (unsigned char *)s, strlen(s) + 1); //+1 to capture the \0