From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.20; helo=mga02.intel.com; envelope-from=jiaxin.wu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id E795E20356268 for ; Mon, 4 Dec 2017 22:55:06 -0800 (PST) Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Dec 2017 22:59:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,363,1508828400"; d="scan'208";a="157599" Received: from jiaxinwu-mobl2.ccr.corp.intel.com ([10.239.196.96]) by orsmga008.jf.intel.com with ESMTP; 04 Dec 2017 22:59:36 -0800 From: Jiaxin Wu To: edk2-devel@lists.01.org Cc: Ye Ting , Fu Siyuan , Wang Fan , Wu Jiaxin Date: Tue, 5 Dec 2017 14:59:22 +0800 Message-Id: <1512457162-9296-5-git-send-email-jiaxin.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.1 In-Reply-To: <1512457162-9296-1-git-send-email-jiaxin.wu@intel.com> References: <1512457162-9296-1-git-send-email-jiaxin.wu@intel.com> Subject: [Patch 4/4] NetworkPkg/DnsDxe: Avoid to access the freed memory buffer. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Dec 2017 06:55:07 -0000 The HostNameToIp() is a asynchronous function, so the caller may free the HostName buffer immediately once HostNameToIp() is returned. Then DNS driver may access the freed memory buffer later. This patch is to fix above issue. Cc: Ye Ting Cc: Fu Siyuan Cc: Wang Fan Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Wu Jiaxin --- NetworkPkg/DnsDxe/DnsProtocol.c | 69 +++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/NetworkPkg/DnsDxe/DnsProtocol.c b/NetworkPkg/DnsDxe/DnsProtocol.c index df737dc..1fcaabd 100644 --- a/NetworkPkg/DnsDxe/DnsProtocol.c +++ b/NetworkPkg/DnsDxe/DnsProtocol.c @@ -462,13 +462,19 @@ Dns4HostNameToIp ( Status = EFI_OUT_OF_RESOURCES; goto ON_EXIT; } TokenEntry->PacketToLive = Token->RetryInterval; - TokenEntry->QueryHostName = HostName; TokenEntry->Token = Token; - + TokenEntry->QueryHostName = AllocateZeroPool (StrSize (HostName)); + if (TokenEntry->QueryHostName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (TokenEntry->QueryHostName, HostName, StrSize (HostName)); + // // Construct QName. // QueryName = NetLibCreateDnsQName (TokenEntry->QueryHostName); if (QueryName == NULL) { @@ -478,49 +484,48 @@ Dns4HostNameToIp ( // // Construct DNS Query Packet. // Status = ConstructDNSQuery (Instance, QueryName, DNS_TYPE_A, DNS_CLASS_INET, &Packet); - if (EFI_ERROR (Status)) { - if (TokenEntry != NULL) { - FreePool (TokenEntry); - } - + if (EFI_ERROR (Status)) { goto ON_EXIT; } ASSERT (Packet != NULL); // // Save the token into the Dns4TxTokens map. // Status = NetMapInsertTail (&Instance->Dns4TxTokens, TokenEntry, Packet); if (EFI_ERROR (Status)) { - if (TokenEntry != NULL) { - FreePool (TokenEntry); - } - - NetbufFree (Packet); - goto ON_EXIT; } // // Dns Query Ip // Status = DoDnsQuery (Instance, Packet); if (EFI_ERROR (Status)) { Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, TokenEntry); + } + +ON_EXIT: + if (EFI_ERROR (Status)) { if (TokenEntry != NULL) { + if (TokenEntry->QueryHostName != NULL) { + FreePool (TokenEntry->QueryHostName); + } + FreePool (TokenEntry); } - NetbufFree (Packet); + if (Packet != NULL) { + NetbufFree (Packet); + } } -ON_EXIT: if (QueryName != NULL) { FreePool (QueryName); } gBS->RestoreTPL (OldTpl); @@ -1299,13 +1304,18 @@ Dns6HostNameToIp ( Status = EFI_OUT_OF_RESOURCES; goto ON_EXIT; } TokenEntry->PacketToLive = Token->RetryInterval; - TokenEntry->QueryHostName = HostName; TokenEntry->Token = Token; - + TokenEntry->QueryHostName = AllocateZeroPool (StrSize (HostName)); + if (TokenEntry->QueryHostName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (TokenEntry->QueryHostName, HostName, StrSize (HostName)); // // Construct QName. // QueryName = NetLibCreateDnsQName (TokenEntry->QueryHostName); @@ -1317,48 +1327,47 @@ Dns6HostNameToIp ( // // Construct DNS Query Packet. // Status = ConstructDNSQuery (Instance, QueryName, DNS_TYPE_AAAA, DNS_CLASS_INET, &Packet); if (EFI_ERROR (Status)) { - if (TokenEntry != NULL) { - FreePool (TokenEntry); - } - goto ON_EXIT; } ASSERT (Packet != NULL); // // Save the token into the Dns6TxTokens map. // Status = NetMapInsertTail (&Instance->Dns6TxTokens, TokenEntry, Packet); if (EFI_ERROR (Status)) { - if (TokenEntry != NULL) { - FreePool (TokenEntry); - } - - NetbufFree (Packet); - goto ON_EXIT; } // // Dns Query Ip // Status = DoDnsQuery (Instance, Packet); if (EFI_ERROR (Status)) { Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, TokenEntry); - + } + +ON_EXIT: + + if (EFI_ERROR (Status)) { if (TokenEntry != NULL) { + if (TokenEntry->QueryHostName != NULL) { + FreePool (TokenEntry->QueryHostName); + } + FreePool (TokenEntry); } - NetbufFree (Packet); + if (Packet != NULL) { + NetbufFree (Packet); + } } -ON_EXIT: if (QueryName != NULL) { FreePool (QueryName); } gBS->RestoreTPL (OldTpl); -- 1.9.5.msysgit.1