NetworkPkg/DnsDxe: Fix the potential memory leak issue.
Cc: Ye Ting <ting.ye@intel.com> Cc: Fu Siyuan <siyuan.fu@intel.com> Cc: Wang Fan <fan.wang@intel.com> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Wu Jiaxin <jiaxin.wu@intel.com> Reviewed-by: Fu Siyuan <siyuan.fu@intel.com>
This commit is contained in:
		| @@ -792,6 +792,10 @@ UpdateDns4Cache ( | |||||||
|         // Delete matching DNS Cache entry |         // Delete matching DNS Cache entry | ||||||
|         // |         // | ||||||
|         RemoveEntryList (&Item->AllCacheLink); |         RemoveEntryList (&Item->AllCacheLink); | ||||||
|  |  | ||||||
|  |         FreePool (Item->DnsCache.HostName); | ||||||
|  |         FreePool (Item->DnsCache.IpAddress); | ||||||
|  |         FreePool (Item); | ||||||
|          |          | ||||||
|         return EFI_SUCCESS; |         return EFI_SUCCESS; | ||||||
|       } else if (Override) { |       } else if (Override) { | ||||||
| @@ -819,13 +823,16 @@ UpdateDns4Cache ( | |||||||
|     |     | ||||||
|   NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName)); |   NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName)); | ||||||
|   if (NewDnsCache->DnsCache.HostName == NULL) {  |   if (NewDnsCache->DnsCache.HostName == NULL) {  | ||||||
|  |     FreePool (NewDnsCache); | ||||||
|     return EFI_OUT_OF_RESOURCES; |     return EFI_OUT_OF_RESOURCES; | ||||||
|   } |   } | ||||||
|    |    | ||||||
|   CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName)); |   CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName)); | ||||||
|  |  | ||||||
|   NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv4_ADDRESS)); |   NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv4_ADDRESS)); | ||||||
|   if (NewDnsCache->DnsCache.IpAddress == NULL) {  |   if (NewDnsCache->DnsCache.IpAddress == NULL) { | ||||||
|  |     FreePool (NewDnsCache->DnsCache.HostName); | ||||||
|  |     FreePool (NewDnsCache); | ||||||
|     return EFI_OUT_OF_RESOURCES; |     return EFI_OUT_OF_RESOURCES; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -884,6 +891,10 @@ UpdateDns6Cache ( | |||||||
|         // |         // | ||||||
|         RemoveEntryList (&Item->AllCacheLink); |         RemoveEntryList (&Item->AllCacheLink); | ||||||
|          |          | ||||||
|  |         FreePool (Item->DnsCache.HostName); | ||||||
|  |         FreePool (Item->DnsCache.IpAddress); | ||||||
|  |         FreePool (Item); | ||||||
|  |          | ||||||
|         return EFI_SUCCESS; |         return EFI_SUCCESS; | ||||||
|       } else if (Override) { |       } else if (Override) { | ||||||
|         // |         // | ||||||
| @@ -910,13 +921,16 @@ UpdateDns6Cache ( | |||||||
|     |     | ||||||
|   NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName)); |   NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName)); | ||||||
|   if (NewDnsCache->DnsCache.HostName == NULL) {  |   if (NewDnsCache->DnsCache.HostName == NULL) {  | ||||||
|  |     FreePool (NewDnsCache); | ||||||
|     return EFI_OUT_OF_RESOURCES; |     return EFI_OUT_OF_RESOURCES; | ||||||
|   } |   } | ||||||
|    |    | ||||||
|   CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName)); |   CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName)); | ||||||
|  |  | ||||||
|   NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv6_ADDRESS)); |   NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv6_ADDRESS)); | ||||||
|   if (NewDnsCache->DnsCache.IpAddress == NULL) {  |   if (NewDnsCache->DnsCache.IpAddress == NULL) { | ||||||
|  |     FreePool (NewDnsCache->DnsCache.HostName); | ||||||
|  |     FreePool (NewDnsCache); | ||||||
|     return EFI_OUT_OF_RESOURCES; |     return EFI_OUT_OF_RESOURCES; | ||||||
|   } |   } | ||||||
|    |    | ||||||
| @@ -1256,12 +1270,12 @@ ParseDnsResponse ( | |||||||
|       // |       // | ||||||
|       // It's the GeneralLookUp querying. |       // It's the GeneralLookUp querying. | ||||||
|       // |       // | ||||||
|       Dns4TokenEntry->Token->RspData.GLookupData = AllocatePool (sizeof (DNS_RESOURCE_RECORD)); |       Dns4TokenEntry->Token->RspData.GLookupData = AllocateZeroPool (sizeof (DNS_RESOURCE_RECORD)); | ||||||
|       if (Dns4TokenEntry->Token->RspData.GLookupData == NULL) { |       if (Dns4TokenEntry->Token->RspData.GLookupData == NULL) { | ||||||
|         Status = EFI_OUT_OF_RESOURCES; |         Status = EFI_OUT_OF_RESOURCES; | ||||||
|         goto ON_EXIT; |         goto ON_EXIT; | ||||||
|       } |       } | ||||||
|       Dns4TokenEntry->Token->RspData.GLookupData->RRList = AllocatePool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD)); |       Dns4TokenEntry->Token->RspData.GLookupData->RRList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD)); | ||||||
|       if (Dns4TokenEntry->Token->RspData.GLookupData->RRList == NULL) { |       if (Dns4TokenEntry->Token->RspData.GLookupData->RRList == NULL) { | ||||||
|         Status = EFI_OUT_OF_RESOURCES; |         Status = EFI_OUT_OF_RESOURCES; | ||||||
|         goto ON_EXIT; |         goto ON_EXIT; | ||||||
| @@ -1271,12 +1285,12 @@ ParseDnsResponse ( | |||||||
|       // It's not the GeneralLookUp querying. Check the Query type. |       // It's not the GeneralLookUp querying. Check the Query type. | ||||||
|       // |       // | ||||||
|       if (QuerySection->Type == DNS_TYPE_A) { |       if (QuerySection->Type == DNS_TYPE_A) { | ||||||
|         Dns4TokenEntry->Token->RspData.H2AData = AllocatePool (sizeof (DNS_HOST_TO_ADDR_DATA)); |         Dns4TokenEntry->Token->RspData.H2AData = AllocateZeroPool (sizeof (DNS_HOST_TO_ADDR_DATA)); | ||||||
|         if (Dns4TokenEntry->Token->RspData.H2AData == NULL) { |         if (Dns4TokenEntry->Token->RspData.H2AData == NULL) { | ||||||
|           Status = EFI_OUT_OF_RESOURCES; |           Status = EFI_OUT_OF_RESOURCES; | ||||||
|           goto ON_EXIT; |           goto ON_EXIT; | ||||||
|         } |         } | ||||||
|         Dns4TokenEntry->Token->RspData.H2AData->IpList = AllocatePool (DnsHeader->AnswersNum * sizeof (EFI_IPv4_ADDRESS)); |         Dns4TokenEntry->Token->RspData.H2AData->IpList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (EFI_IPv4_ADDRESS)); | ||||||
|         if (Dns4TokenEntry->Token->RspData.H2AData->IpList == NULL) { |         if (Dns4TokenEntry->Token->RspData.H2AData->IpList == NULL) { | ||||||
|           Status = EFI_OUT_OF_RESOURCES; |           Status = EFI_OUT_OF_RESOURCES; | ||||||
|           goto ON_EXIT; |           goto ON_EXIT; | ||||||
| @@ -1293,12 +1307,12 @@ ParseDnsResponse ( | |||||||
|       // |       // | ||||||
|       // It's the GeneralLookUp querying. |       // It's the GeneralLookUp querying. | ||||||
|       // |       // | ||||||
|       Dns6TokenEntry->Token->RspData.GLookupData = AllocatePool (sizeof (DNS_RESOURCE_RECORD)); |       Dns6TokenEntry->Token->RspData.GLookupData = AllocateZeroPool (sizeof (DNS_RESOURCE_RECORD)); | ||||||
|       if (Dns6TokenEntry->Token->RspData.GLookupData == NULL) { |       if (Dns6TokenEntry->Token->RspData.GLookupData == NULL) { | ||||||
|         Status = EFI_OUT_OF_RESOURCES; |         Status = EFI_OUT_OF_RESOURCES; | ||||||
|         goto ON_EXIT; |         goto ON_EXIT; | ||||||
|       } |       } | ||||||
|       Dns6TokenEntry->Token->RspData.GLookupData->RRList = AllocatePool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD)); |       Dns6TokenEntry->Token->RspData.GLookupData->RRList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD)); | ||||||
|       if (Dns6TokenEntry->Token->RspData.GLookupData->RRList == NULL) { |       if (Dns6TokenEntry->Token->RspData.GLookupData->RRList == NULL) { | ||||||
|         Status = EFI_OUT_OF_RESOURCES; |         Status = EFI_OUT_OF_RESOURCES; | ||||||
|         goto ON_EXIT; |         goto ON_EXIT; | ||||||
| @@ -1308,12 +1322,12 @@ ParseDnsResponse ( | |||||||
|       // It's not the GeneralLookUp querying. Check the Query type. |       // It's not the GeneralLookUp querying. Check the Query type. | ||||||
|       // |       // | ||||||
|       if (QuerySection->Type == DNS_TYPE_AAAA) { |       if (QuerySection->Type == DNS_TYPE_AAAA) { | ||||||
|         Dns6TokenEntry->Token->RspData.H2AData = AllocatePool (sizeof (DNS6_HOST_TO_ADDR_DATA)); |         Dns6TokenEntry->Token->RspData.H2AData = AllocateZeroPool (sizeof (DNS6_HOST_TO_ADDR_DATA)); | ||||||
|         if (Dns6TokenEntry->Token->RspData.H2AData == NULL) { |         if (Dns6TokenEntry->Token->RspData.H2AData == NULL) { | ||||||
|           Status = EFI_OUT_OF_RESOURCES; |           Status = EFI_OUT_OF_RESOURCES; | ||||||
|           goto ON_EXIT; |           goto ON_EXIT; | ||||||
|         } |         } | ||||||
|         Dns6TokenEntry->Token->RspData.H2AData->IpList = AllocatePool (DnsHeader->AnswersNum * sizeof (EFI_IPv6_ADDRESS)); |         Dns6TokenEntry->Token->RspData.H2AData->IpList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (EFI_IPv6_ADDRESS)); | ||||||
|         if (Dns6TokenEntry->Token->RspData.H2AData->IpList == NULL) { |         if (Dns6TokenEntry->Token->RspData.H2AData->IpList == NULL) { | ||||||
|           Status = EFI_OUT_OF_RESOURCES; |           Status = EFI_OUT_OF_RESOURCES; | ||||||
|           goto ON_EXIT; |           goto ON_EXIT; | ||||||
| @@ -1603,6 +1617,97 @@ ON_COMPLETE: | |||||||
|   } |   } | ||||||
|  |  | ||||||
| ON_EXIT: | ON_EXIT: | ||||||
|  |   // | ||||||
|  |   // Free the allocated buffer if error happen. | ||||||
|  |   // | ||||||
|  |   if (EFI_ERROR (Status)) { | ||||||
|  |     if (Dns4TokenEntry != NULL) { | ||||||
|  |       if (Dns4TokenEntry->GeneralLookUp) { | ||||||
|  |         if (Dns4TokenEntry->Token->RspData.GLookupData != NULL) { | ||||||
|  |           if (Dns4TokenEntry->Token->RspData.GLookupData->RRList != NULL) { | ||||||
|  |             while (RRCount != 0) { | ||||||
|  |               RRCount --; | ||||||
|  |               if (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName != NULL) { | ||||||
|  |                 FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName); | ||||||
|  |               } | ||||||
|  |  | ||||||
|  |               if (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData != NULL) { | ||||||
|  |                 FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData); | ||||||
|  |               } | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList); | ||||||
|  |           } | ||||||
|  |            | ||||||
|  |           FreePool (Dns4TokenEntry->Token->RspData.GLookupData); | ||||||
|  |         } | ||||||
|  |       } else { | ||||||
|  |         if (QuerySection->Type == DNS_TYPE_A && Dns4TokenEntry->Token->RspData.H2AData != NULL) { | ||||||
|  |           if (Dns4TokenEntry->Token->RspData.H2AData->IpList != NULL) { | ||||||
|  |             FreePool (Dns4TokenEntry->Token->RspData.H2AData->IpList); | ||||||
|  |           } | ||||||
|  |            | ||||||
|  |           FreePool (Dns4TokenEntry->Token->RspData.H2AData); | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (Dns6TokenEntry != NULL) { | ||||||
|  |       if (Dns6TokenEntry->GeneralLookUp) { | ||||||
|  |         if (Dns6TokenEntry->Token->RspData.GLookupData != NULL) { | ||||||
|  |           if (Dns6TokenEntry->Token->RspData.GLookupData->RRList != NULL) { | ||||||
|  |             while (RRCount != 0) { | ||||||
|  |               RRCount --; | ||||||
|  |               if (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName != NULL) { | ||||||
|  |                 FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName); | ||||||
|  |               } | ||||||
|  |  | ||||||
|  |               if (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData != NULL) { | ||||||
|  |                 FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData); | ||||||
|  |               } | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList); | ||||||
|  |           } | ||||||
|  |            | ||||||
|  |           FreePool (Dns6TokenEntry->Token->RspData.GLookupData); | ||||||
|  |         } | ||||||
|  |       } else { | ||||||
|  |         if (QuerySection->Type == DNS_TYPE_AAAA && Dns6TokenEntry->Token->RspData.H2AData != NULL) { | ||||||
|  |           if (Dns6TokenEntry->Token->RspData.H2AData->IpList != NULL) { | ||||||
|  |             FreePool (Dns6TokenEntry->Token->RspData.H2AData->IpList); | ||||||
|  |           } | ||||||
|  |            | ||||||
|  |           FreePool (Dns6TokenEntry->Token->RspData.H2AData); | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (Dns4CacheEntry != NULL) { | ||||||
|  |       if (Dns4CacheEntry->HostName != NULL) { | ||||||
|  |         FreePool (Dns4CacheEntry->HostName); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       if (Dns4CacheEntry->IpAddress != NULL) { | ||||||
|  |         FreePool (Dns4CacheEntry->IpAddress); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       FreePool (Dns4CacheEntry); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (Dns6CacheEntry != NULL) { | ||||||
|  |       if (Dns6CacheEntry->HostName != NULL) { | ||||||
|  |         FreePool (Dns6CacheEntry->HostName); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       if (Dns6CacheEntry->IpAddress != NULL) { | ||||||
|  |         FreePool (Dns6CacheEntry->IpAddress); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       FreePool (Dns6CacheEntry); | ||||||
|  |     }     | ||||||
|  |   } | ||||||
|  |    | ||||||
|   gBS->RestoreTPL (OldTpl); |   gBS->RestoreTPL (OldTpl); | ||||||
|   return Status; |   return Status; | ||||||
| } | } | ||||||
| @@ -2053,6 +2158,9 @@ DnsOnTimerUpdate ( | |||||||
|     Item4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); |     Item4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); | ||||||
|     if (Item4->DnsCache.Timeout == 0) { |     if (Item4->DnsCache.Timeout == 0) { | ||||||
|       RemoveEntryList (&Item4->AllCacheLink); |       RemoveEntryList (&Item4->AllCacheLink); | ||||||
|  |       FreePool (Item4->DnsCache.HostName); | ||||||
|  |       FreePool (Item4->DnsCache.IpAddress); | ||||||
|  |       FreePool (Item4); | ||||||
|       Entry = mDriverData->Dns4CacheList.ForwardLink; |       Entry = mDriverData->Dns4CacheList.ForwardLink; | ||||||
|     } else { |     } else { | ||||||
|       Entry = Entry->ForwardLink; |       Entry = Entry->ForwardLink; | ||||||
| @@ -2072,6 +2180,9 @@ DnsOnTimerUpdate ( | |||||||
|     Item6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); |     Item6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); | ||||||
|     if (Item6->DnsCache.Timeout == 0) { |     if (Item6->DnsCache.Timeout == 0) { | ||||||
|       RemoveEntryList (&Item6->AllCacheLink); |       RemoveEntryList (&Item6->AllCacheLink); | ||||||
|  |       FreePool (Item6->DnsCache.HostName); | ||||||
|  |       FreePool (Item6->DnsCache.IpAddress); | ||||||
|  |       FreePool (Item6); | ||||||
|       Entry = mDriverData->Dns6CacheList.ForwardLink; |       Entry = mDriverData->Dns6CacheList.ForwardLink; | ||||||
|     } else { |     } else { | ||||||
|       Entry = Entry->ForwardLink; |       Entry = Entry->ForwardLink; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user