everyone!
Image that i have a program(usemalloc) like this:
#include <stdio.h>
#include <stdlib.h>
#define USER_BYTES_SIZE 100
int main(void){
char* userbytes = (char*)malloc(USER_BYTES_SIZE*sizeof(char));
if(!userbytes)
return 1;
for(int i = 0; i <= USER_BYTES_SIZE; i++){ // "i <= USER_BYTES_SIZE" leads to an off-by-one memory overrun.
userbytes[i] = 0;
}
return 0;
}
As you see that, there is an off-by-one bug which leads to a memory overflow. I want to detect such bugs at runtime. LD_PRELOADed libraries are proper to do my work. I have manufactured a library named libhijack.so to hijack the call to real malloc and replace it with the call to my own customed malloc which call the real malloc and add red zones at ends of the memory strips allocted by the real malloc. The libhijack.so's code like this:
void* (*real_malloc) (size_t size);
void* malloc(size_t size){
real_malloc = ((void*)(*)(size_t))dlsym(RTLD_NEXT, "malloc");
void* allocbytes = (void*)real_malloc(size + 4); //put 2 bytes at each end, call them red zones
return (allocbytes + 2);
}
I run the main program with the library using this command:
LD_PRELOAD=./libhijack.so ./usemalloc
Then if there are access to memory in red zones, I will detect them and deem them as memory overflow bugs.
This LD_PRELOAD solution works well when the main process contains calls to malloc but fails when forked child process does that.
For example, we change the "usemalloc" as follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // +
#define USER_BYTES_SIZE 100
int main(void){
pid_t child = fork();
if(child < 0)
exit(1);
if(child == 0){ //child process
char* userbytes = (char*)malloc(USER_BYTES_SIZE*sizeof(char));
if(!userbytes)
return 1;
for(int i = 0; i <= USER_BYTES_SIZE; i++){ // "i <= USER_BYTES_SIZE" leads to an off-by-one memory overrun.
userbytes[i] = 0;
}
}
else { //the current process
wait(NULL);
}
return 0;
}
The overflow bug occured in child process will not be detect by LD_PRELOADed library.
So my questions are: how can I detect the overflow bug in the child process using LD_PRELOADed libraries? Is that(using LD_PRELOADed libraries) possible? If not, any alternatives? Any suggestions ar eappreciated!!!