MdeModulePkg/DxeCore: Avoid accessing non-owned memory

The patch fixes two kinds of bugs in DxeCore that accesses memory
which might be freed or owned by other modules.
The two 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 <ruiyu.ni@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
This commit is contained in:
Ruiyu Ni 2017-07-05 18:45:56 +08:00
parent 357c48258e
commit 5fc3889bb3
1 changed files with 27 additions and 27 deletions

View File

@ -72,15 +72,20 @@ CoreValidateHandle (
) )
{ {
IHANDLE *Handle; IHANDLE *Handle;
LIST_ENTRY *Link;
Handle = (IHANDLE *)UserHandle; if (UserHandle == NULL) {
if (Handle == NULL) {
return EFI_INVALID_PARAMETER;
}
if (Handle->Signature != EFI_HANDLE_SIGNATURE) {
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
for (Link = gHandleList.BackLink; Link != &gHandleList; Link = Link->BackLink) {
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 { do {
ItemFound = FALSE; ItemFound = FALSE;
for ( Link = Prot->OpenList.ForwardLink; for (Link = Prot->OpenList.ForwardLink; Link != &Prot->OpenList; Link = Link->ForwardLink) {
(Link != &Prot->OpenList) && !ItemFound;
Link = Link->ForwardLink ) {
OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE); OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) { if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {
ItemFound = TRUE;
CoreReleaseProtocolLock (); CoreReleaseProtocolLock ();
Status = CoreDisconnectController (UserHandle, OpenData->AgentHandle, NULL); Status = CoreDisconnectController (UserHandle, OpenData->AgentHandle, NULL);
CoreAcquireProtocolLock (); CoreAcquireProtocolLock ();
if (EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
ItemFound = FALSE; ItemFound = TRUE;
break;
} }
break;
} }
} }
} while (ItemFound); } while (ItemFound);
@ -664,21 +666,17 @@ CoreDisconnectControllersUsingProtocolInterface (
// //
// Attempt to remove BY_HANDLE_PROTOOCL and GET_PROTOCOL and TEST_PROTOCOL Open List items // Attempt to remove BY_HANDLE_PROTOOCL and GET_PROTOCOL and TEST_PROTOCOL Open List items
// //
do { for (Link = Prot->OpenList.ForwardLink; Link != &Prot->OpenList;) {
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); OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
if ((OpenData->Attributes & if ((OpenData->Attributes &
(EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL | EFI_OPEN_PROTOCOL_GET_PROTOCOL | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) != 0) { (EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL | EFI_OPEN_PROTOCOL_GET_PROTOCOL | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) != 0) {
ItemFound = TRUE; Link = RemoveEntryList (&OpenData->Link);
RemoveEntryList (&OpenData->Link);
Prot->OpenListCount--; Prot->OpenListCount--;
CoreFreePool (OpenData); CoreFreePool (OpenData);
} else {
Link = Link->ForwardLink;
} }
} }
} while (ItemFound);
} }
// //
@ -1132,7 +1130,7 @@ CoreOpenProtocol (
if (ByDriver) { if (ByDriver) {
do { do {
Disconnect = FALSE; 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); OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) { if ((OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {
Disconnect = TRUE; Disconnect = TRUE;
@ -1142,6 +1140,8 @@ CoreOpenProtocol (
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
Status = EFI_ACCESS_DENIED; Status = EFI_ACCESS_DENIED;
goto Done; goto Done;
} else {
break;
} }
} }
} }