Advertisement
Guest User

GetPerTcpConnectionEStats potential buffer bug

a guest
Feb 24th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1.  
  2. void f()
  3. {
  4.     std::unique_ptr<MIB_TCPTABLE_OWNER_PID> buffer;
  5.     DWORD ret = 0;
  6.     DWORD dwSize = 0;
  7.  
  8.     do
  9.     {
  10.         buffer = std::unique_ptr<MIB_TCPTABLE_OWNER_PID>(new MIB_TCPTABLE_OWNER_PID[dwSize]());
  11.  
  12.         ret = GetExtendedTcpTable(buffer.get(), &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
  13.    
  14.     } while (ret == ERROR_INSUFFICIENT_BUFFER);
  15.    
  16.     PMIB_TCPTABLE_OWNER_PID ptTable;
  17.  
  18.     if (ret == ERROR_SUCCESS)
  19.     {
  20.         ptTable = (PMIB_TCPTABLE_OWNER_PID)buffer.get();
  21.     }
  22.     else
  23.     {
  24.         return;
  25.     }
  26.  
  27.     for (DWORD i = 0; i < ptTable->dwNumEntries; i++)
  28.     {
  29.         MIB_TCPROW row;
  30.         row.dwLocalAddr = ptTable->table[i].dwLocalAddr;
  31.         row.dwLocalPort = ptTable->table[i].dwLocalPort;
  32.         row.dwRemoteAddr = ptTable->table[i].dwRemoteAddr;
  33.         row.dwRemotePort = ptTable->table[i].dwRemotePort;
  34.         row.dwState = ptTable->table[i].dwState;
  35.  
  36.         if (row.dwRemoteAddr != 0)
  37.         {
  38.             ULONG rosSize = 0, rodSize = 0;
  39.             ULONG winStatus;
  40.             PUCHAR ros = NULL;// , rod = NULL;
  41.             std::shared_ptr<UCHAR> rod;
  42.             rodSize = sizeof(TCP_ESTATS_DATA_ROD_v0);
  43.             PTCP_ESTATS_DATA_ROD_v0 dataRod = { 0 };
  44.  
  45.             if (rosSize != 0)
  46.             {
  47.                 ros = (PUCHAR)malloc(rosSize);
  48.                 if (ros == NULL)
  49.                 {
  50.                     wprintf(L"\nOut of memory");
  51.                     return;
  52.                 }
  53.                 else
  54.                     memset(ros, 0, rosSize); // zero the buffer
  55.             }
  56.             if (rodSize != 0)
  57.             {
  58.                 rod = std::shared_ptr<UCHAR>(new UCHAR[rodSize]());
  59.                 if (rod == NULL)
  60.                 {
  61.                     free(ros);
  62.                     wprintf(L"\nOut of memory");
  63.                     return;
  64.                 }
  65.             }
  66.  
  67.             winStatus = GetPerTcpConnectionEStats((PMIB_TCPROW)&row, TcpConnectionEstatsData, NULL, 0, 0, ros, 0, rosSize, rod.get(), 0, rodSize);
  68.  
  69.             dataRod = (PTCP_ESTATS_DATA_ROD_v0)rod.get();
  70.  
  71.             PTCP_ESTATS_BANDWIDTH_ROD_v0 bandwidthRod = { 0 };
  72.  
  73.             rodSize = sizeof(TCP_ESTATS_BANDWIDTH_ROD_v0);
  74.             if (rodSize != 0)
  75.             {
  76.                 rod = std::shared_ptr<UCHAR>(new UCHAR[rodSize]());
  77.             }
  78.  
  79.             winStatus = GetPerTcpConnectionEStats((PMIB_TCPROW)&row, TcpConnectionEstatsBandwidth, NULL, 0, 0, ros, 0, rosSize, rod.get(), 0, rodSize);
  80.  
  81.             bandwidthRod = (PTCP_ESTATS_BANDWIDTH_ROD_v0)rod.get();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement