From nobody Wed May 15 17:39:08 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+81864+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+81864+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1634111167; cv=none; d=zohomail.com; s=zohoarc; b=ToYe/v5Y3RoGjG8Svvu88P/gVRhogXPTmR5uTkmK0NZ83KUah4nB9q28CZb0mmjtg5dAzqbmJ9jKmUpzLF6WwQT27HEttPsqeH2Z0oDuQp0BE5bSBZnjalxw2xSlga2ZbNEqvGtos0yOeBDNVwvn14zILgIn6bAChUZeb6L65Vs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634111167; 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=Ni3wQ8osGITWwsCqJ0T+cb4G+nBdWEqWIIXGYEYdq6Y=; b=eoGIm+/ujGYVG5xZk8itsg2IJHxhsI1UEZN7OCeqUahzC38yZPNoVFvU9x/OsKcd7Do7WJPkYkvCib0VbHWnbf/bhEG8WBH4buJcLddfMTJNmT4N5EJD41X1tzNIWSV1uThfdleUXJEHjxXhI1VMJA0DLkIKXoJTfcu9egsixck= 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+81864+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 1634111166787535.3813064524008; Wed, 13 Oct 2021 00:46:06 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id xnIAYY1788612xfZDJO1Cgtm; Wed, 13 Oct 2021 00:46:06 -0700 X-Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web09.4936.1634111164823985013 for ; Wed, 13 Oct 2021 00:46:05 -0700 X-IronPort-AV: E=McAfee;i="6200,9189,10135"; a="290864729" X-IronPort-AV: E=Sophos;i="5.85,369,1624345200"; d="scan'208";a="290864729" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Oct 2021 00:46:03 -0700 X-IronPort-AV: E=Sophos;i="5.85,369,1624345200"; d="scan'208";a="491343540" 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; 13 Oct 2021 00:46:02 -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 v3] MdeModulePkg/Core/Dxe: Acquire a lock when iterating gHandleList Date: Wed, 13 Oct 2021 15:45:14 +0800 Message-Id: <837b6a9e5919e51d47fabc133e4b860389e99a15.1634109861.git.hua.ma@intel.com> 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: ttlLXGGC54XYe9FRTmEz3i9ax1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1634111166; bh=u39AnDe7orEZT8U85q23rlv0abU1oVOUzpA8wxL1WYY=; h=Cc:Date:From:Reply-To:Subject:To; b=u3RI+wTCT9NJ7NhkX5oM1IUA3RwIlNH5CgY375TOeNNKquyG51IRggPK1TR8rgbixws mu0WT9KNUCQeYCZ6llB4dICWrOi85t4KlJzBp7vZmO856+57xv+9Z9C4bmf1LR/IzTxto rVs7C2YzRVMBTnja3mlr+046VAs0rNvnB7M= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1634111169321100001 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. Currently some caller functions of CoreValidateHandle() have CoreAcquireProtocolLock(), but some caller functions of CoreValidateHandle() do not CoreAcquireProtocolLock(). Add CoreAcquireProtocolLock() always when CoreValidateHandle() is called, Also, A lock check is added in the CoreValidateHandle(). v3 changes: - keep ASSERT_LOCKED(&gProtocolDatabaseLock) in CoreValidateHandle() - Call CoreAcquireProtocolLock() before any calling of CoreValidateHandle() and CoreReleaseProtocolLock() afterwards - Update the commit message 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 Reviewed-by: Jian J Wang --- MdeModulePkg/Core/Dxe/Hand/DriverSupport.c | 16 +++++ MdeModulePkg/Core/Dxe/Hand/Handle.c | 75 ++++++++++++---------- MdeModulePkg/Core/Dxe/Hand/Handle.h | 1 + MdeModulePkg/Core/Dxe/Hand/Notify.c | 13 ++-- 4 files changed, 64 insertions(+), 41 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Hand/DriverSupport.c b/MdeModulePkg/Core= /Dxe/Hand/DriverSupport.c index feabf12faf..12a202417c 100644 --- a/MdeModulePkg/Core/Dxe/Hand/DriverSupport.c +++ b/MdeModulePkg/Core/Dxe/Hand/DriverSupport.c @@ -68,7 +68,12 @@ CoreConnectController ( // // Make sure ControllerHandle is valid // + CoreAcquireProtocolLock (); + Status =3D CoreValidateHandle (ControllerHandle); + + CoreReleaseProtocolLock (); + if (EFI_ERROR (Status)) { return Status; } @@ -268,7 +273,12 @@ AddSortedDriverBindingProtocol ( // // Make sure the DriverBindingHandle is valid // + CoreAcquireProtocolLock (); + Status =3D CoreValidateHandle (DriverBindingHandle); + + CoreReleaseProtocolLock (); + if (EFI_ERROR (Status)) { return; } @@ -746,8 +756,11 @@ CoreDisconnectController ( // // Make sure ControllerHandle is valid // + CoreAcquireProtocolLock (); + Status =3D CoreValidateHandle (ControllerHandle); if (EFI_ERROR (Status)) { + CoreReleaseProtocolLock (); return Status; } =20 @@ -757,10 +770,13 @@ CoreDisconnectController ( if (ChildHandle !=3D NULL) { Status =3D CoreValidateHandle (ChildHandle); if (EFI_ERROR (Status)) { + CoreReleaseProtocolLock (); return Status; } } =20 + CoreReleaseProtocolLock (); + Handle =3D ControllerHandle; =20 // diff --git a/MdeModulePkg/Core/Dxe/Hand/Handle.c b/MdeModulePkg/Core/Dxe/Ha= nd/Handle.c index 6eccb41ecb..92979281b7 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Handle.c +++ b/MdeModulePkg/Core/Dxe/Hand/Handle.c @@ -53,6 +53,7 @@ CoreReleaseProtocolLock ( =20 /** Check whether a handle is a valid EFI_HANDLE + The gProtocolDatabaseLock must be owned =20 @param UserHandle The handle to check =20 @@ -72,6 +73,8 @@ CoreValidateHandle ( return EFI_INVALID_PARAMETER; } =20 + ASSERT_LOCKED(&gProtocolDatabaseLock); + 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) { @@ -720,19 +723,19 @@ CoreUninstallProtocolInterface ( return EFI_INVALID_PARAMETER; } =20 + // + // Lock the protocol database + // + CoreAcquireProtocolLock (); + // // Check that UserHandle is a valid handle // Status =3D CoreValidateHandle (UserHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } =20 - // - // Lock the protocol database - // - CoreAcquireProtocolLock (); - // // Check that Protocol exists on UserHandle, and Interface matches the i= nterface in the database // @@ -1010,12 +1013,17 @@ CoreOpenProtocol ( return EFI_INVALID_PARAMETER; } =20 + // + // Lock the protocol database + // + CoreAcquireProtocolLock (); + // // Check for invalid UserHandle // Status =3D CoreValidateHandle (UserHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } =20 // @@ -1025,31 +1033,32 @@ CoreOpenProtocol ( case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER : Status =3D CoreValidateHandle (ImageHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } Status =3D CoreValidateHandle (ControllerHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } if (UserHandle =3D=3D ControllerHandle) { - return EFI_INVALID_PARAMETER; + Status =3D EFI_INVALID_PARAMETER; + goto Done; } break; case EFI_OPEN_PROTOCOL_BY_DRIVER : case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE : Status =3D CoreValidateHandle (ImageHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } Status =3D CoreValidateHandle (ControllerHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } break; case EFI_OPEN_PROTOCOL_EXCLUSIVE : Status =3D CoreValidateHandle (ImageHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } break; case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL : @@ -1057,13 +1066,10 @@ CoreOpenProtocol ( case EFI_OPEN_PROTOCOL_TEST_PROTOCOL : break; default: - return EFI_INVALID_PARAMETER; + Status =3D EFI_INVALID_PARAMETER; + goto Done; } =20 - // - // Lock the protocol database - // - CoreAcquireProtocolLock (); =20 // // Look at each protocol interface for a match @@ -1246,32 +1252,33 @@ CoreCloseProtocol ( LIST_ENTRY *Link; OPEN_PROTOCOL_DATA *OpenData; =20 + // + // Lock the protocol database + // + CoreAcquireProtocolLock (); + // // Check for invalid parameters // Status =3D CoreValidateHandle (UserHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } Status =3D CoreValidateHandle (AgentHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } if (ControllerHandle !=3D NULL) { Status =3D CoreValidateHandle (ControllerHandle); if (EFI_ERROR (Status)) { - return Status; + goto Done; } } if (Protocol =3D=3D NULL) { - return EFI_INVALID_PARAMETER; + Status =3D EFI_INVALID_PARAMETER; + goto Done; } =20 - // - // Lock the protocol database - // - CoreAcquireProtocolLock (); - // // Look at each protocol interface for a match // @@ -1443,13 +1450,6 @@ CoreProtocolsPerHandle ( UINTN ProtocolCount; EFI_GUID **Buffer; =20 - Status =3D CoreValidateHandle (UserHandle); - if (EFI_ERROR (Status)) { - return Status; - } - - Handle =3D (IHANDLE *)UserHandle; - if (ProtocolBuffer =3D=3D NULL) { return EFI_INVALID_PARAMETER; } @@ -1464,6 +1464,13 @@ CoreProtocolsPerHandle ( =20 CoreAcquireProtocolLock (); =20 + Status =3D CoreValidateHandle (UserHandle); + if (EFI_ERROR (Status)) { + goto Done; + } + + Handle =3D (IHANDLE *)UserHandle; + for (Link =3D Handle->Protocols.ForwardLink; Link !=3D &Handle->Protocol= s; Link =3D Link->ForwardLink) { ProtocolCount++; } diff --git a/MdeModulePkg/Core/Dxe/Hand/Handle.h b/MdeModulePkg/Core/Dxe/Ha= nd/Handle.h index 83eb2b9f3a..3f83e3af15 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Handle.h +++ b/MdeModulePkg/Core/Dxe/Hand/Handle.h @@ -242,6 +242,7 @@ CoreReleaseProtocolLock ( =20 /** Check whether a handle is a valid EFI_HANDLE + The gProtocolDatabaseLock must be owned =20 @param UserHandle The handle to check =20 diff --git a/MdeModulePkg/Core/Dxe/Hand/Notify.c b/MdeModulePkg/Core/Dxe/Ha= nd/Notify.c index 553413a350..d05f95207f 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Notify.c +++ b/MdeModulePkg/Core/Dxe/Hand/Notify.c @@ -188,22 +188,21 @@ CoreReinstallProtocolInterface ( PROTOCOL_INTERFACE *Prot; PROTOCOL_ENTRY *ProtEntry; =20 - Status =3D CoreValidateHandle (UserHandle); - if (EFI_ERROR (Status)) { - return Status; - } - if (Protocol =3D=3D NULL) { return EFI_INVALID_PARAMETER; } =20 - Handle =3D (IHANDLE *) UserHandle; - // // Lock the protocol database // CoreAcquireProtocolLock (); =20 + Status =3D CoreValidateHandle (UserHandle); + if (EFI_ERROR (Status)) { + goto Done; + } + + Handle =3D (IHANDLE *) UserHandle; // // Check that Protocol exists on UserHandle, and Interface matches the i= nterface in the database // --=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 (#81864): https://edk2.groups.io/g/devel/message/81864 Mute This Topic: https://groups.io/mt/86282804/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-