Advertisement
Guest User

Untitled

a guest
May 26th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. uintptr_t LocateDataNullZone( const char* moduleName, const unsigned int numVMs )
  2. {
  3. static std::vector<dl_phdr_info> libraries;
  4. if ( libraries.empty() ) {
  5. dl_iterate_phdr( []( struct dl_phdr_info* info, size_t, void* ) {
  6.  
  7. libraries.push_back( *info );
  8.  
  9. return 0;
  10. }, nullptr );
  11. }
  12.  
  13. size_t page = sysconf(_SC_PAGESIZE); // ~4096
  14. /* Locate Data section/s for this module */
  15. for ( const dl_phdr_info& current : libraries ) {
  16. if ( !current.dlpi_name )
  17. continue;
  18. if ( !strcasestr( current.dlpi_name, moduleName ) )
  19. continue;
  20.  
  21. for( int i = 0; i < current.dlpi_phnum; i++ ){
  22. if( !( current.dlpi_phdr[i].p_flags & ( PF_R | PF_W ) ) ) // not Readable and writable? Keep going...
  23. continue;
  24.  
  25. uintptr_t address = current.dlpi_addr + current.dlpi_phdr[i].p_vaddr;
  26. size_t len = current.dlpi_phdr[i].p_memsz;
  27.  
  28. /* Start at beginning of page */
  29. if( address % page ){
  30. len += address % page;
  31. address -= address %page;
  32. }
  33.  
  34. /* Make sure to get the extra bits at the end. */
  35. if( len % page ){
  36. len += page - (len % page);
  37. }
  38.  
  39. if( !current.dlpi_phdr[i].p_vaddr )
  40. continue;
  41.  
  42. uintptr_t endAddress = address + len - sizeof(uintptr_t);
  43.  
  44. LogLocal("Found a .data segment at: %p-%p\n", (void*)address, (void*)endAddress);
  45.  
  46. for( uintptr_t currentAddr = address; currentAddr < endAddress; currentAddr += sizeof(uintptr_t) ) {
  47. if( *reinterpret_cast<uintptr_t*>( currentAddr ) == 0 ){
  48. bool isNullZone = true;
  49. size_t ptrCount = 0;
  50.  
  51. for( ; ptrCount < numVMs && isNullZone; ptrCount += sizeof(uintptr_t) ){
  52. if( *reinterpret_cast<uintptr_t*>( currentAddr + ptrCount ) != 0 )
  53. isNullZone = false;
  54. }
  55.  
  56. if( isNullZone && ptrCount >= numVMs )
  57. return currentAddr;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement