From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (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 D35AF21A07AB5 for ; Wed, 5 Jul 2017 03:44:39 -0700 (PDT) Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP; 05 Jul 2017 03:46:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.40,311,1496127600"; d="scan'208";a="104624252" Received: from ray-dev.ccr.corp.intel.com ([10.239.9.12]) by orsmga004.jf.intel.com with ESMTP; 05 Jul 2017 03:46:17 -0700 From: Ruiyu Ni To: edk2-devel@lists.01.org Cc: Michael D Kinney , Liming Gao , Star Zeng , Hao A Wu Date: Wed, 5 Jul 2017 18:46:14 +0800 Message-Id: <20170705104614.295620-1-ruiyu.ni@intel.com> X-Mailer: git-send-email 2.12.2.windows.2 Subject: [PATCH] MdeModulePkg/DxeCore: Avoid accessing non-owned memory 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: Wed, 05 Jul 2017 10:44:40 -0000 The patch fixes two kinds of issues in DxeCore that accesses memory which might be freed or owned by other modules. The existing bugs don't cause functionality issue. 1. CoreValidateHandle() checks whether the handle is valid by validating its signature. The proper way is to check whether the handle is in the handle database. 2. CoreDisconnectControllersUsingProtocolInterface() and CoreOpenProtocol() de-reference Link pointer which is already freed. The proper way is to not de-reference the pointer. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ruiyu Ni Cc: Michael D Kinney Cc: Liming Gao Cc: Star Zeng Cc: Hao A Wu --- MdeModulePkg/Core/Dxe/Hand/Handle.c | 54 ++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Hand/Handle.c b/MdeModulePkg/Core/Dxe/Hand/Handle.c index 59b89148c8..8e2f361805 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Handle.c +++ b/MdeModulePkg/Core/Dxe/Hand/Handle.c @@ -72,15 +72,20 @@ CoreValidateHandle ( ) { IHANDLE *Handle; + LIST_ENTRY *Link; - Handle = (IHANDLE *)UserHandle; - if (Handle == NULL) { + if (UserHandle == NULL) { return EFI_INVALID_PARAMETER; } - if (Handle->Signature != EFI_HANDLE_SIGNATURE) { - return EFI_INVALID_PARAMETER; + + for (Link = gHandleList.ForwardLink; Link != &gHandleList; Link = Link->ForwardLink) { + Handle = CR (Link, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE); + if (Handle == (IHANDLE *) UserHandle) { + return EFI_SUCCESS; + } } - return EFI_SUCCESS; + + return EFI_INVALID_PARAMETER; } @@ -643,19 +648,16 @@ CoreDisconnectControllersUsingProtocolInterface ( // do { ItemFound = FALSE; - for ( Link = Prot->OpenList.ForwardLink; - (Link != &Prot->OpenList) && !ItemFound; - Link = Link->ForwardLink ) { + for (Link = Prot->OpenList.ForwardLink; Link != &Prot->OpenList; Link = Link->ForwardLink) { OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE); if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) { - ItemFound = TRUE; CoreReleaseProtocolLock (); Status = CoreDisconnectController (UserHandle, OpenData->AgentHandle, NULL); CoreAcquireProtocolLock (); - if (EFI_ERROR (Status)) { - ItemFound = FALSE; - break; + if (!EFI_ERROR (Status)) { + ItemFound = TRUE; } + break; } } } while (ItemFound); @@ -664,21 +666,17 @@ CoreDisconnectControllersUsingProtocolInterface ( // // Attempt to remove BY_HANDLE_PROTOOCL and GET_PROTOCOL and TEST_PROTOCOL Open List items // - do { - ItemFound = FALSE; - for ( Link = Prot->OpenList.ForwardLink; - (Link != &Prot->OpenList) && !ItemFound; - Link = Link->ForwardLink ) { - OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE); - if ((OpenData->Attributes & - (EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL | EFI_OPEN_PROTOCOL_GET_PROTOCOL | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) != 0) { - ItemFound = TRUE; - RemoveEntryList (&OpenData->Link); - Prot->OpenListCount--; - CoreFreePool (OpenData); - } + for (Link = Prot->OpenList.ForwardLink; Link != &Prot->OpenList;) { + OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE); + if ((OpenData->Attributes & + (EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL | EFI_OPEN_PROTOCOL_GET_PROTOCOL | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) != 0) { + Link = RemoveEntryList (&OpenData->Link); + Prot->OpenListCount--; + CoreFreePool (OpenData); + } else { + Link = Link->ForwardLink; } - } while (ItemFound); + } } // @@ -1132,7 +1130,7 @@ CoreOpenProtocol ( if (ByDriver) { do { Disconnect = FALSE; - for ( Link = Prot->OpenList.ForwardLink; (Link != &Prot->OpenList) && (!Disconnect); Link = Link->ForwardLink) { + for (Link = Prot->OpenList.ForwardLink; Link != &Prot->OpenList; Link = Link->ForwardLink) { OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE); if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) { Disconnect = TRUE; @@ -1142,6 +1140,8 @@ CoreOpenProtocol ( if (EFI_ERROR (Status)) { Status = EFI_ACCESS_DENIED; goto Done; + } else { + break; } } } -- 2.12.2.windows.2