Consider the following function that is supposed to validate passwords:
char *systemkey = ...... ;
int validate(char* key) {
char* k = malloc(16);
char* sk = malloc(16);
strcpy(sk,systemkey);
strcpy(k,key);
return (strncmp(k,sk,16) ==0);
}
If k
and sk
are allocated consecutively, that it's easy to break the function by supplying 2 identical blocks of 16 bytes each.
If I'm the one writing the compiler/malloc/free/OS, is there any way I can identify MOST of these type of hacks and prevent them?
EDIT
One possible solution is to put some sort of canary word between each two different allocations. Is there another way?