From nobody Mon May 13 20:34:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+81812+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+81812+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1634027684; cv=none; d=zohomail.com; s=zohoarc; b=nHkW95w68eaHMQOf+6AG9qgZvQEd3X8X0CtvYn4h0eX+0WMSEnAmeEqjn1OHZ8Rdtha/ER0mUNO9SknqgyclOiHh5saCMySc0XWVUBamm7t5uUG1HNeilT2kI4taY1TQZWHmm33bdVV2Nvozl7Qzpm9bpShy9T7eSSxJAtgcI+U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634027684; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=pLTQbCHqhhM3x9oqjkqGDly52J/6I7vUYNjDdnbAtAg=; b=RELYcqd8xJcbWOKWuaHBw+J511pRtVxNUwNdJ5ZWp3bTqNTVk7nss/nWnsLX+MCpAOEY7zwZ7u6OWZ923T2r+Cp5489IYz/JM/v7MGwC6NvuMO0Pct774lLQF7WYGbpkt21Vs2AmpcN7QHyf129guLg4xi/aozAUPmlFWHQLdJs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+81812+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1634027684156328.86097739195645; Tue, 12 Oct 2021 01:34:44 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 68d5YY1788612xuctRXwUJTy; Tue, 12 Oct 2021 01:34:43 -0700 X-Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web10.10899.1634027682879124692 for ; Tue, 12 Oct 2021 01:34:43 -0700 X-IronPort-AV: E=McAfee;i="6200,9189,10134"; a="224502586" X-IronPort-AV: E=Sophos;i="5.85,367,1624345200"; d="scan'208";a="224502586" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2021 01:34:42 -0700 X-IronPort-AV: E=Sophos;i="5.85,367,1624345200"; d="scan'208";a="490870733" X-Received: from huama-mobl1.ccr.corp.intel.com ([10.239.15.137]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2021 01:34:40 -0700 From: "Ma, Hua" To: devel@edk2.groups.io Cc: Hua Ma , Jian J Wang , Liming Gao , Dandan Bi , Ray Ni Subject: [edk2-devel] [PATCH v2] MdeModulePkg/Core/Dxe: Acquire a lock when iterating gHandleList Date: Tue, 12 Oct 2021 16:34:06 +0800 Message-Id: MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,hua.ma@intel.com X-Gm-Message-State: ZZZ9V5ESEm4eO88ocX9VWWhqx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1634027683; bh=eC6upIdInLbPIf+mIVjcwPNHgVW2UqEKen+eRRv+LBw=; h=Cc:Date:From:Reply-To:Subject:To; b=ITLicAynO6cxkf6c2T2n5a+rGcWrMskgUtVFWIAAJZiQqj/P2Gmfk2pv25lWL/b5Hls Hfwe/ISguM2JN3MWZ88aOP4QeHP0Gqb8ZVrgl1v2qw/DF1XiMt6/5y1suDy2juuraarqs GfVOHcSQN/9C6B98JbE/i2jf4++GHMTdFzQ= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1634027685699100001 Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3680 This patch fixes the following issue: The global variable gHandleList is a linked list. This list is locked when a entry is added or removed from the list, but there is no lock when iterating this list in function CoreValidateHandle(). It can lead to "Handle.c (76): CR has Bad Signature" assertion if the iterated entry in the list is just removed by other task during iterating. Locking the list when iterating can fix this issue. v2 changes: - Add lock check and comments in CoreGetProtocolInterface() before calling CoreValidateHandle() - Update the comments in CoreValidateHandle() header file v1: https://edk2.groups.io/g/devel/topic/86233569 Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Cc: Ray Ni Signed-off-by: Hua Ma Reviewed-by: Dandan Bi --- MdeModulePkg/Core/Dxe/Hand/DriverSupport.c | 10 ++-- MdeModulePkg/Core/Dxe/Hand/Handle.c | 56 +++++++++++++++------- MdeModulePkg/Core/Dxe/Hand/Handle.h | 5 +- MdeModulePkg/Core/Dxe/Hand/Notify.c | 2 +- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Hand/DriverSupport.c b/MdeModulePkg/Core= /Dxe/Hand/DriverSupport.c index feabf12faf..eb8a765d2c 100644 --- a/MdeModulePkg/Core/Dxe/Hand/DriverSupport.c +++ b/MdeModulePkg/Core/Dxe/Hand/DriverSupport.c @@ -68,7 +68,7 @@ CoreConnectController ( // // Make sure ControllerHandle is valid // - Status =3D CoreValidateHandle (ControllerHandle); + Status =3D CoreValidateHandle (ControllerHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } @@ -154,7 +154,7 @@ CoreConnectController ( // // Make sure the DriverBindingHandle is valid // - Status =3D CoreValidateHandle (ControllerHandle); + Status =3D CoreValidateHandle (ControllerHandle, TRUE); if (EFI_ERROR (Status)) { // // Release the protocol lock on the handle database @@ -268,7 +268,7 @@ AddSortedDriverBindingProtocol ( // // Make sure the DriverBindingHandle is valid // - Status =3D CoreValidateHandle (DriverBindingHandle); + Status =3D CoreValidateHandle (DriverBindingHandle, FALSE); if (EFI_ERROR (Status)) { return; } @@ -746,7 +746,7 @@ CoreDisconnectController ( // // Make sure ControllerHandle is valid // - Status =3D CoreValidateHandle (ControllerHandle); + Status =3D CoreValidateHandle (ControllerHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } @@ -755,7 +755,7 @@ CoreDisconnectController ( // Make sure ChildHandle is valid if it is not NULL // if (ChildHandle !=3D NULL) { - Status =3D CoreValidateHandle (ChildHandle); + Status =3D CoreValidateHandle (ChildHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } diff --git a/MdeModulePkg/Core/Dxe/Hand/Handle.c b/MdeModulePkg/Core/Dxe/Ha= nd/Handle.c index 6eccb41ecb..46f67d3d6a 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Handle.c +++ b/MdeModulePkg/Core/Dxe/Hand/Handle.c @@ -55,31 +55,46 @@ CoreReleaseProtocolLock ( Check whether a handle is a valid EFI_HANDLE =20 @param UserHandle The handle to check + @param IsLocked The protocol lock is acquried or not =20 @retval EFI_INVALID_PARAMETER The handle is NULL or not a valid EFI_HAN= DLE. + @retval EFI_NOT_FOUND The handle is not found in the handle dat= abase. @retval EFI_SUCCESS The handle is valid EFI_HANDLE. =20 **/ EFI_STATUS CoreValidateHandle ( - IN EFI_HANDLE UserHandle + IN EFI_HANDLE UserHandle, + IN BOOLEAN IsLocked ) { IHANDLE *Handle; LIST_ENTRY *Link; + EFI_STATUS Status; =20 if (UserHandle =3D=3D NULL) { return EFI_INVALID_PARAMETER; } =20 + if (IsLocked) { + ASSERT_LOCKED(&gProtocolDatabaseLock); + } else { + CoreAcquireProtocolLock (); + } + + Status =3D EFI_NOT_FOUND; for (Link =3D gHandleList.BackLink; Link !=3D &gHandleList; Link =3D Lin= k->BackLink) { Handle =3D CR (Link, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE); if (Handle =3D=3D (IHANDLE *) UserHandle) { - return EFI_SUCCESS; + Status =3D EFI_SUCCESS; + break; } } =20 - return EFI_INVALID_PARAMETER; + if (!IsLocked) { + CoreReleaseProtocolLock (); + } + return Status; } =20 =20 @@ -428,7 +443,7 @@ CoreInstallProtocolInterfaceNotify ( // InsertTailList (&gHandleList, &Handle->AllHandles); } else { - Status =3D CoreValidateHandle (Handle); + Status =3D CoreValidateHandle (Handle, TRUE); if (EFI_ERROR (Status)) { DEBUG((DEBUG_ERROR, "InstallProtocolInterface: input handle at 0x%x = is invalid\n", Handle)); goto Done; @@ -723,7 +738,7 @@ CoreUninstallProtocolInterface ( // // Check that UserHandle is a valid handle // - Status =3D CoreValidateHandle (UserHandle); + Status =3D CoreValidateHandle (UserHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } @@ -899,7 +914,16 @@ CoreGetProtocolInterface ( IHANDLE *Handle; LIST_ENTRY *Link; =20 - Status =3D CoreValidateHandle (UserHandle); + // + // The gProtocolDatabaseLock must be owned + // + ASSERT_LOCKED(&gProtocolDatabaseLock); + + // + // The gProtocolDatabaseLock is owned + // Call CoreValidateHandle () with IsLocked =3D=3D TRUE + // + Status =3D CoreValidateHandle (UserHandle, TRUE); if (EFI_ERROR (Status)) { return NULL; } @@ -1013,7 +1037,7 @@ CoreOpenProtocol ( // // Check for invalid UserHandle // - Status =3D CoreValidateHandle (UserHandle); + Status =3D CoreValidateHandle (UserHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } @@ -1023,11 +1047,11 @@ CoreOpenProtocol ( // switch (Attributes) { case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER : - Status =3D CoreValidateHandle (ImageHandle); + Status =3D CoreValidateHandle (ImageHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } - Status =3D CoreValidateHandle (ControllerHandle); + Status =3D CoreValidateHandle (ControllerHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } @@ -1037,17 +1061,17 @@ CoreOpenProtocol ( break; case EFI_OPEN_PROTOCOL_BY_DRIVER : case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE : - Status =3D CoreValidateHandle (ImageHandle); + Status =3D CoreValidateHandle (ImageHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } - Status =3D CoreValidateHandle (ControllerHandle); + Status =3D CoreValidateHandle (ControllerHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } break; case EFI_OPEN_PROTOCOL_EXCLUSIVE : - Status =3D CoreValidateHandle (ImageHandle); + Status =3D CoreValidateHandle (ImageHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } @@ -1249,16 +1273,16 @@ CoreCloseProtocol ( // // Check for invalid parameters // - Status =3D CoreValidateHandle (UserHandle); + Status =3D CoreValidateHandle (UserHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } - Status =3D CoreValidateHandle (AgentHandle); + Status =3D CoreValidateHandle (AgentHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } if (ControllerHandle !=3D NULL) { - Status =3D CoreValidateHandle (ControllerHandle); + Status =3D CoreValidateHandle (ControllerHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } @@ -1443,7 +1467,7 @@ CoreProtocolsPerHandle ( UINTN ProtocolCount; EFI_GUID **Buffer; =20 - Status =3D CoreValidateHandle (UserHandle); + Status =3D CoreValidateHandle (UserHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } diff --git a/MdeModulePkg/Core/Dxe/Hand/Handle.h b/MdeModulePkg/Core/Dxe/Ha= nd/Handle.h index 83eb2b9f3a..20d886beca 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Handle.h +++ b/MdeModulePkg/Core/Dxe/Hand/Handle.h @@ -244,14 +244,17 @@ CoreReleaseProtocolLock ( Check whether a handle is a valid EFI_HANDLE =20 @param UserHandle The handle to check + @param IsLocked The protocol lock is acquried or not =20 @retval EFI_INVALID_PARAMETER The handle is NULL or not a valid EFI_HAN= DLE. + @retval EFI_NOT_FOUND The handle is not found in the handle dat= abase. @retval EFI_SUCCESS The handle is valid EFI_HANDLE. =20 **/ EFI_STATUS CoreValidateHandle ( - IN EFI_HANDLE UserHandle + IN EFI_HANDLE UserHandle, + IN BOOLEAN IsLocked ); =20 // diff --git a/MdeModulePkg/Core/Dxe/Hand/Notify.c b/MdeModulePkg/Core/Dxe/Ha= nd/Notify.c index 553413a350..f4e7c01e96 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Notify.c +++ b/MdeModulePkg/Core/Dxe/Hand/Notify.c @@ -188,7 +188,7 @@ CoreReinstallProtocolInterface ( PROTOCOL_INTERFACE *Prot; PROTOCOL_ENTRY *ProtEntry; =20 - Status =3D CoreValidateHandle (UserHandle); + Status =3D CoreValidateHandle (UserHandle, FALSE); if (EFI_ERROR (Status)) { return Status; } --=20 2.32.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#81812): https://edk2.groups.io/g/devel/message/81812 Mute This Topic: https://groups.io/mt/86257717/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-