I believe most of the programmers (unix C programmers) are aware of the application of LD_PRELOAD env variable. Basically it can be used to override any functionality of libc with the in house implementation. For ex: 'getaddrinfo' may have different implementation for an organization and cannot replaced in libc code itself (due to GPL restrictions). The organization may not be wanting to expose their internal implementation to public domain to preserve intellectual stuffs. Can't they write their own implementation? That is crux of the matter. They may be using third party application and want to modify certain functionalities for their use. Instead of modifying glibc code, they replace with their own implementation. Aha! how is this irony :-). Forget it! The usual method is to implement same implementation with prototype matching libc and create a dynamic shared library. Later set the LD_PRELOAD environment variable to this dynamic shared object. This makes the program loader to preload the created .so before libc gets loaded. The loader marks all the symbols used by executable based on the order of libs loaded. In above case, 'getaddrinfo' will be linked to created library than libc. Even though I knew this information, never attempted to write a program. Finally I was tempted to write and here it is. This guy overrides malloc implementation
The library for malloc -- Buggy!!!
my_lib.c
#include <stdio.h>
char *virus_data_segment = (char*)(0x1234567890)
void* malloc(size_t size)
{
printf("Get lost! I do not have even a penny to give ;-)\n");
return virus_data_segment;
}
created shared library: gcc -shared -o libmy_lib.so -fPIC my_lib.c
Now the actual fun starts :D
set LD_PRELOAD
>>LD_PRELOAD=./libmy_lib.so
execute ls command
>> ls
Get lost! I do not have even a penny to give ;-)
Segmentation fault
LOL! The malloc has been overridden and 'ls' cannot run (may be 'ls' is using malloc). Why segmentation fault? Because malloc returned an address region not belonging to process address space. Here is output when we return NULL.
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
ls: memory exhausted
Strange is it not! Either it is retrying or multiple places trying to allocate.
Yes, LD_PRELOAD is well known :-) but scribbled here in case for anyone can provide more insight. I will try to gather some more information if possible and post them.
The library for malloc -- Buggy!!!
my_lib.c
#include <stdio.h>
char *virus_data_segment = (char*)(0x1234567890)
void* malloc(size_t size)
{
printf("Get lost! I do not have even a penny to give ;-)\n");
return virus_data_segment;
}
created shared library: gcc -shared -o libmy_lib.so -fPIC my_lib.c
Now the actual fun starts :D
set LD_PRELOAD
>>LD_PRELOAD=./libmy_lib.so
execute ls command
>> ls
Get lost! I do not have even a penny to give ;-)
Segmentation fault
LOL! The malloc has been overridden and 'ls' cannot run (may be 'ls' is using malloc). Why segmentation fault? Because malloc returned an address region not belonging to process address space. Here is output when we return NULL.
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
Get lost! I do not have even a penny to give ;-)
ls: memory exhausted
Strange is it not! Either it is retrying or multiple places trying to allocate.
Yes, LD_PRELOAD is well known :-) but scribbled here in case for anyone can provide more insight. I will try to gather some more information if possible and post them.
No comments:
Post a Comment