Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 18th, 2012  |  syntax: None  |  size: 1.90 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2.  
  3. #include <dlfcn.h>
  4.  
  5. #include <sys/socket.h>
  6. #include <cutils/sockets.h>
  7.  
  8.  
  9. #define VOLD_NAMESPACE "vold"
  10. #define SHARED_LIBRARY "/system/lib/libcutils.so"
  11. #define SHARED_LIB_FUNC "socket"
  12.  
  13. #define LOAD_SUCCESS 1
  14. #define LOAD_FAILURE 0
  15.  
  16.  
  17.  int (*ptr_socket_local_client)(const char*, int, int);
  18.  
  19.  
  20.  
  21.  
  22. void*
  23. try_find_export(void *handle, const char *name)
  24. {
  25.         if(handle == NULL || name == NULL){
  26.                 return NULL;
  27.         }
  28.        
  29.         void* func = dlsym(handle, name);
  30.         if (func == NULL) {
  31.                 return NULL;
  32.         }
  33.         else {
  34.                 return func;
  35.         }
  36.         return NULL;   
  37. }
  38.  
  39. int
  40. try_load_libs(void)
  41. {
  42.         void *library = dlopen(SHARED_LIBRARY, RTLD_NOW);
  43.        
  44.         if (library == NULL) {
  45.                 printf("Error while getting file handle.\n");
  46.         }
  47.        
  48.         socket_local_client = (ptr_socket_local_client)try_find_export(library, SHARED_LIB_FUNC);
  49.        
  50.         if (socket_local_client == NULL) {
  51.                 printf("Dying. Couldn't locate socket functions.\n");
  52.                 return 0;
  53.         }
  54.        
  55.         else {
  56.                 printf("All libraries accounted for\n");
  57.                 return 1;
  58.         }
  59.        
  60.         return 0;
  61.        
  62. }
  63.  
  64. int
  65. main (int argc, const char * argv[])
  66. {
  67.     // insert code here...
  68.         int sock = -1;
  69.         printf("\n==============================================================\n");
  70.         printf("Vold2 Proof of concept.\nozzeh@manykdepressive.com\n");
  71.         printf("==============================================================\n");    
  72.         printf("Trying to assemble the shared libraries.\n");
  73.        
  74.         if(try_load_libs() != LOAD_SUCCESS) {
  75.                 printf("Couldn't load required shared objects for this POC.\n");
  76.                 //return 0;
  77.         }
  78.         else {
  79.                 printf("Shared objects loaded, attempting to open a client socket\n");
  80.         }
  81.  
  82.         //sock = socket(AF_INET, SOCK_STREAM, 0);
  83.         socket = socket_local_client(VOLD_NAMESPACE,
  84.                                                                 1,
  85.                                                                  SOCK_STREAM);
  86.        
  87.         printf("socket returned: %d\n", sock);
  88.        
  89.         if (socket > 0) {
  90.                 printf("Socket accepted!\n");
  91.         }
  92.         else {
  93.                 printf("Socket creation failed.\n");
  94.         }
  95.        
  96.         return 0;
  97. }