Acquire a lock when iterating gHandleList, as in EDK2

Signed-off-by: SergeySlice <sergey.slice@gmail.com>
This commit is contained in:
SergeySlice 2021-11-27 21:48:24 +03:00
parent fbd1d0fbc9
commit f6334a521c
8 changed files with 121 additions and 47 deletions

View File

@ -74,7 +74,9 @@ CoreConnectController (
// //
// Make sure ControllerHandle is valid // Make sure ControllerHandle is valid
// //
CoreAcquireProtocolLock ();
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
CoreReleaseProtocolLock ();
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; return Status;
} }
@ -159,7 +161,9 @@ CoreConnectController (
// //
// Make sure the DriverBindingHandle is valid // Make sure the DriverBindingHandle is valid
// //
CoreAcquireProtocolLock ();
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
CoreReleaseProtocolLock ();
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
// //
// Release the protocol lock on the handle database // Release the protocol lock on the handle database
@ -273,7 +277,9 @@ AddSortedDriverBindingProtocol (
// //
// Make sure the DriverBindingHandle is valid // Make sure the DriverBindingHandle is valid
// //
CoreAcquireProtocolLock ();
Status = CoreValidateHandle (DriverBindingHandle); Status = CoreValidateHandle (DriverBindingHandle);
CoreReleaseProtocolLock ();
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return; return;
} }
@ -782,8 +788,10 @@ CoreDisconnectController (
// //
// Make sure ControllerHandle is valid // Make sure ControllerHandle is valid
// //
CoreAcquireProtocolLock ();
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
CoreReleaseProtocolLock ();
return Status; return Status;
} }
@ -793,10 +801,12 @@ CoreDisconnectController (
if (ChildHandle != NULL) { if (ChildHandle != NULL) {
Status = CoreValidateHandle (ChildHandle); Status = CoreValidateHandle (ChildHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
CoreReleaseProtocolLock ();
return Status; return Status;
} }
} }
CoreReleaseProtocolLock ();
Handle = ControllerHandle; Handle = ControllerHandle;
// //

View File

@ -727,18 +727,24 @@ CoreUninstallProtocolInterface (
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
//
// Lock the protocol database
//
CoreAcquireProtocolLock ();
// //
// Check that UserHandle is a valid handle // Check that UserHandle is a valid handle
// //
Status = CoreValidateHandle (UserHandle); Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; // return Status;
goto Done;
} }
// //
// Lock the protocol database // Lock the protocol database
// //
CoreAcquireProtocolLock (); // CoreAcquireProtocolLock ();
// //
// Check that Protocol exists on UserHandle, and Interface matches the interface in the database // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
@ -1013,12 +1019,18 @@ CoreOpenProtocol (
} }
} }
//
// Lock the protocol database
//
CoreAcquireProtocolLock ();
// //
// Check for invalid UserHandle // Check for invalid UserHandle
// //
Status = CoreValidateHandle (UserHandle); Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; // return Status;
goto Done;
} }
// //
@ -1028,21 +1040,26 @@ CoreOpenProtocol (
case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER : case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER :
Status = CoreValidateHandle (ImageHandle); Status = CoreValidateHandle (ImageHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; // return Status;
goto Done;
} }
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; // return Status;
goto Done;
} }
if (UserHandle == ControllerHandle) { if (UserHandle == ControllerHandle) {
return EFI_INVALID_PARAMETER; // return EFI_INVALID_PARAMETER;
Status = EFI_INVALID_PARAMETER;
goto Done;
} }
break; break;
case EFI_OPEN_PROTOCOL_BY_DRIVER : case EFI_OPEN_PROTOCOL_BY_DRIVER :
case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE : case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE :
Status = CoreValidateHandle (ImageHandle); Status = CoreValidateHandle (ImageHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; // return Status;
goto Done;
} }
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
@ -1052,7 +1069,8 @@ CoreOpenProtocol (
case EFI_OPEN_PROTOCOL_EXCLUSIVE : case EFI_OPEN_PROTOCOL_EXCLUSIVE :
Status = CoreValidateHandle (ImageHandle); Status = CoreValidateHandle (ImageHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; // return Status;
goto Done;
} }
break; break;
case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL : case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL :
@ -1060,13 +1078,15 @@ CoreOpenProtocol (
case EFI_OPEN_PROTOCOL_TEST_PROTOCOL : case EFI_OPEN_PROTOCOL_TEST_PROTOCOL :
break; break;
default: default:
return EFI_INVALID_PARAMETER; // return EFI_INVALID_PARAMETER;
Status = EFI_INVALID_PARAMETER;
goto Done;
} }
// //
// Lock the protocol database // Lock the protocol database
// //
CoreAcquireProtocolLock (); //CoreAcquireProtocolLock ();
// //
// Look at each protocol interface for a match // Look at each protocol interface for a match
@ -1224,31 +1244,37 @@ CoreCloseProtocol (
LIST_ENTRY *Link; LIST_ENTRY *Link;
OPEN_PROTOCOL_DATA *OpenData; OPEN_PROTOCOL_DATA *OpenData;
//
// Lock the protocol database
//
CoreAcquireProtocolLock ();
// //
// Check for invalid parameters // Check for invalid parameters
// //
Status = CoreValidateHandle (UserHandle); Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
Status = CoreValidateHandle (AgentHandle); Status = CoreValidateHandle (AgentHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
if (ControllerHandle != NULL) { if (ControllerHandle != NULL) {
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
} }
if (Protocol == NULL) { if (Protocol == NULL) {
return EFI_INVALID_PARAMETER; Status = EFI_INVALID_PARAMETER;
goto Done;
} }
// //
// Lock the protocol database // Lock the protocol database
// //
CoreAcquireProtocolLock (); //CoreAcquireProtocolLock ();
// //
// Look at each protocol interface for a match // Look at each protocol interface for a match
@ -1442,6 +1468,14 @@ CoreProtocolsPerHandle (
CoreAcquireProtocolLock (); CoreAcquireProtocolLock ();
Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR (Status)) {
goto Done;
}
Handle = (IHANDLE *)UserHandle;
for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) { for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
ProtocolCount++; ProtocolCount++;
} }

View File

@ -248,6 +248,7 @@ CoreReleaseProtocolLock (
/** /**
Check whether a handle is a valid EFI_HANDLE Check whether a handle is a valid EFI_HANDLE
The gProtocolDatabaseLock must be owned
@param UserHandle The handle to check @param UserHandle The handle to check

View File

@ -193,22 +193,28 @@ CoreReinstallProtocolInterface (
PROTOCOL_INTERFACE *Prot; PROTOCOL_INTERFACE *Prot;
PROTOCOL_ENTRY *ProtEntry; PROTOCOL_ENTRY *ProtEntry;
Status = CoreValidateHandle (UserHandle); // Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) { // if (EFI_ERROR(Status)) {
return Status; // return Status;
} // }
if (Protocol == NULL) { if (Protocol == NULL) {
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
Handle = (IHANDLE *) UserHandle; // Handle = (IHANDLE *) UserHandle;
// //
// Lock the protocol database // Lock the protocol database
// //
CoreAcquireProtocolLock (); CoreAcquireProtocolLock ();
Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR (Status)) {
goto Done;
}
Handle = (IHANDLE *) UserHandle;
// //
// Check that Protocol exists on UserHandle, and Interface matches the interface in the database // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
// //

View File

@ -68,7 +68,9 @@ CoreConnectController (
// //
// Make sure ControllerHandle is valid // Make sure ControllerHandle is valid
// //
CoreAcquireProtocolLock ();
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
CoreReleaseProtocolLock ();
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; return Status;
} }
@ -268,7 +270,9 @@ AddSortedDriverBindingProtocol (
// //
// Make sure the DriverBindingHandle is valid // Make sure the DriverBindingHandle is valid
// //
CoreAcquireProtocolLock ();
Status = CoreValidateHandle (DriverBindingHandle); Status = CoreValidateHandle (DriverBindingHandle);
CoreReleaseProtocolLock ();
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return; return;
} }
@ -746,8 +750,10 @@ CoreDisconnectController (
// //
// Make sure ControllerHandle is valid // Make sure ControllerHandle is valid
// //
CoreAcquireProtocolLock ();
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
CoreReleaseProtocolLock ();
return Status; return Status;
} }
@ -757,10 +763,11 @@ CoreDisconnectController (
if (ChildHandle != NULL) { if (ChildHandle != NULL) {
Status = CoreValidateHandle (ChildHandle); Status = CoreValidateHandle (ChildHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
CoreReleaseProtocolLock ();
return Status; return Status;
} }
} }
CoreReleaseProtocolLock ();
Handle = ControllerHandle; Handle = ControllerHandle;
// //

View File

@ -1002,12 +1002,17 @@ CoreOpenProtocol (
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
//
// Lock the protocol database
//
CoreAcquireProtocolLock ();
// //
// Check for invalid UserHandle // Check for invalid UserHandle
// //
Status = CoreValidateHandle (UserHandle); Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
// //
@ -1017,31 +1022,32 @@ CoreOpenProtocol (
case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER : case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER :
Status = CoreValidateHandle (ImageHandle); Status = CoreValidateHandle (ImageHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
if (UserHandle == ControllerHandle) { if (UserHandle == ControllerHandle) {
return EFI_INVALID_PARAMETER; Status = EFI_INVALID_PARAMETER;
goto Done;
} }
break; break;
case EFI_OPEN_PROTOCOL_BY_DRIVER : case EFI_OPEN_PROTOCOL_BY_DRIVER :
case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE : case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE :
Status = CoreValidateHandle (ImageHandle); Status = CoreValidateHandle (ImageHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
break; break;
case EFI_OPEN_PROTOCOL_EXCLUSIVE : case EFI_OPEN_PROTOCOL_EXCLUSIVE :
Status = CoreValidateHandle (ImageHandle); Status = CoreValidateHandle (ImageHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
break; break;
case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL : case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL :
@ -1049,13 +1055,14 @@ CoreOpenProtocol (
case EFI_OPEN_PROTOCOL_TEST_PROTOCOL : case EFI_OPEN_PROTOCOL_TEST_PROTOCOL :
break; break;
default: default:
return EFI_INVALID_PARAMETER; Status = EFI_INVALID_PARAMETER;
goto Done;
} }
// //
// Lock the protocol database // Lock the protocol database
// //
CoreAcquireProtocolLock (); //CoreAcquireProtocolLock ();
// //
// Look at each protocol interface for a match // Look at each protocol interface for a match
@ -1238,31 +1245,37 @@ CoreCloseProtocol (
LIST_ENTRY *Link; LIST_ENTRY *Link;
OPEN_PROTOCOL_DATA *OpenData; OPEN_PROTOCOL_DATA *OpenData;
//
// Lock the protocol database
//
CoreAcquireProtocolLock ();
// //
// Check for invalid parameters // Check for invalid parameters
// //
Status = CoreValidateHandle (UserHandle); Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
Status = CoreValidateHandle (AgentHandle); Status = CoreValidateHandle (AgentHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
if (ControllerHandle != NULL) { if (ControllerHandle != NULL) {
Status = CoreValidateHandle (ControllerHandle); Status = CoreValidateHandle (ControllerHandle);
if (EFI_ERROR(Status)) { if (EFI_ERROR(Status)) {
return Status; goto Done;
} }
} }
if (Protocol == NULL) { if (Protocol == NULL) {
return EFI_INVALID_PARAMETER; Status = EFI_INVALID_PARAMETER;
goto Done;
} }
// //
// Lock the protocol database // Lock the protocol database
// //
CoreAcquireProtocolLock (); //CoreAcquireProtocolLock ();
// //
// Look at each protocol interface for a match // Look at each protocol interface for a match
@ -1435,12 +1448,6 @@ CoreProtocolsPerHandle (
UINTN ProtocolCount; UINTN ProtocolCount;
EFI_GUID **Buffer; EFI_GUID **Buffer;
Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) {
return Status;
}
Handle = (IHANDLE *)UserHandle;
if (ProtocolBuffer == NULL) { if (ProtocolBuffer == NULL) {
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
@ -1456,6 +1463,14 @@ CoreProtocolsPerHandle (
CoreAcquireProtocolLock (); CoreAcquireProtocolLock ();
Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) {
goto Done;
}
Handle = (IHANDLE *)UserHandle;
for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) { for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
ProtocolCount++; ProtocolCount++;
} }

View File

@ -242,6 +242,7 @@ CoreReleaseProtocolLock (
/** /**
Check whether a handle is a valid EFI_HANDLE Check whether a handle is a valid EFI_HANDLE
The gProtocolDatabaseLock must be owned
@param UserHandle The handle to check @param UserHandle The handle to check

View File

@ -188,22 +188,22 @@ CoreReinstallProtocolInterface (
PROTOCOL_INTERFACE *Prot; PROTOCOL_INTERFACE *Prot;
PROTOCOL_ENTRY *ProtEntry; PROTOCOL_ENTRY *ProtEntry;
Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR(Status)) {
return Status;
}
if (Protocol == NULL) { if (Protocol == NULL) {
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
Handle = (IHANDLE *) UserHandle;
// //
// Lock the protocol database // Lock the protocol database
// //
CoreAcquireProtocolLock (); CoreAcquireProtocolLock ();
Status = CoreValidateHandle (UserHandle);
if (EFI_ERROR (Status)) {
goto Done;
}
Handle = (IHANDLE *) UserHandle;
// //
// Check that Protocol exists on UserHandle, and Interface matches the interface in the database // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
// //