From nobody Sat May 18 20:15:34 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+81270+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+81270+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1632899638; cv=none; d=zohomail.com; s=zohoarc; b=ARYfKJccy903TD6PLbn901XUm5SeLoAz9Uehoc6ng7ppOCq6Km6K2Q8cvsUay7XgrlC+nt5BllMYO1SWwioWr5YRFCpqEv+OoOnvqZnw7icBVydnk6HU3sPWNga8GET3mOiA53Z3uj0Gg+SU2nvIAJRavhCShPxwBq+vDV1n8CY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1632899638; 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=rQH9WgF8X2zp0nuSvrdUf86spYNa06JAj1b+24S3V6s=; b=P+Zg784DU5ICOkNSAJhfN0HhUsKmiauv6VXNN26JuP/n+U94FPdESQMPh1PaQBet2Jv4OlECb2kw5b8jo96FMl3R7iJ4Wp560RUySRWIXXYUPwW+NX/1e+elC9S1/zEpjlGG0DNNzVC1eixujmtxNOVLKdwoXjJAEA1pQDe6cL0= 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+81270+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 1632899638552751.9433040514493; Wed, 29 Sep 2021 00:13:58 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id hJsNYY1788612xNAmObGHTeV; Wed, 29 Sep 2021 00:13:58 -0700 X-Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web08.5085.1632894589932332633 for ; Tue, 28 Sep 2021 22:49:50 -0700 X-IronPort-AV: E=McAfee;i="6200,9189,10121"; a="285880910" X-IronPort-AV: E=Sophos;i="5.85,331,1624345200"; d="scan'208";a="285880910" X-Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2021 22:49:49 -0700 X-IronPort-AV: E=Sophos;i="5.85,331,1624345200"; d="scan'208";a="554490893" X-Received: from huama-mobl1.ccr.corp.intel.com ([10.239.15.137]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2021 22:49:48 -0700 From: "Ma, Hua" To: devel@edk2.groups.io Cc: Hua Ma , Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH] MdeModulePkg/Core/Dxe: Add lock protection in CoreLocateHandleBuffer() Date: Wed, 29 Sep 2021 13:49:27 +0800 Message-Id: <33349c0e687ed186cd13db413fa708a8b761ffea.1632894508.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: 1LOkwbG5sO92o0uGtA4hXpQ5x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1632899638; bh=Zx9ikc3lYQKkIzIyczSGM7NTbrdTW7zzWrmpa0DKrd8=; h=Cc:Date:From:Reply-To:Subject:To; b=nHaAmwMP+zKlHW55fBpqB15YQluKSqQEttCChz6iOJBVaGgPSB4bre4Yj4fjWCS0KB8 U3qlnEEycHACEuQGV5HBsFWG38Xpo+lW3Ep76WSp/muqg1sSA0xtdufK6N0oWihV2tRQm fpVn3L9p1QlcL+0XyLwVaMwBLEOU7AVYIx8= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1632899639877100001 Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3666 Currently, CoreLocateHandleBuffer() follows three steps: 1) get the size of protocol database firstly 2) allocate the buffer based on the size 3) get the protocol database into the buffer There is no lock protection for the whole three steps. If a new protocol added in step 2) by other task, e.g. (event timer handle USB device hotplug). The size of protocol database may be increased and cannot fit into the previous buffer in step 3). The protocol database cannot be returned successfully, EFI_BUFFER_TOO_SMALL error will be returned. This patch adds the lock to protect the whole three steps. It can make sure the correct protocol database be returned. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Hua Ma Reviewed-by: Dandan Bi Reviewed-by: Liming Gao --- MdeModulePkg/Core/Dxe/Hand/Locate.c | 64 +++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Hand/Locate.c b/MdeModulePkg/Core/Dxe/Ha= nd/Locate.c index be17f4cbc3..4987c046c6 100644 --- a/MdeModulePkg/Core/Dxe/Hand/Locate.c +++ b/MdeModulePkg/Core/Dxe/Hand/Locate.c @@ -86,7 +86,8 @@ CoreGetNextLocateByProtocol ( =20 =20 /** - Locates the requested handle(s) and returns them in Buffer. + Internal function for locating the requested handle(s) and returns them = in Buffer. + The caller should already have acquired the ProtocolLock. =20 @param SearchType The type of search to perform to locate t= he handles @@ -104,8 +105,7 @@ CoreGetNextLocateByProtocol ( =20 **/ EFI_STATUS -EFIAPI -CoreLocateHandle ( +InternalCoreLocateHandle ( IN EFI_LOCATE_SEARCH_TYPE SearchType, IN EFI_GUID *Protocol OPTIONAL, IN VOID *SearchKey OPTIONAL, @@ -143,11 +143,6 @@ CoreLocateHandle ( ResultBuffer =3D (IHANDLE **) Buffer; Status =3D EFI_SUCCESS; =20 - // - // Lock the protocol database - // - CoreAcquireProtocolLock (); - // // Get the search function based on type // @@ -190,7 +185,6 @@ CoreLocateHandle ( } =20 if (EFI_ERROR(Status)) { - CoreReleaseProtocolLock (); return Status; } =20 @@ -247,10 +241,47 @@ CoreLocateHandle ( } } =20 - CoreReleaseProtocolLock (); return Status; } =20 +/** + Locates the requested handle(s) and returns them in Buffer. + + @param SearchType The type of search to perform to locate t= he + handles + @param Protocol The protocol to search for + @param SearchKey Dependant on SearchType + @param BufferSize On input the size of Buffer. On output t= he + size of data returned. + @param Buffer The buffer to return the results in + + @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size is + returned in BufferSize. + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_SUCCESS Successfully found the requested handle(s= ) and + returns them in Buffer. + +**/ +EFI_STATUS +EFIAPI +CoreLocateHandle ( + IN EFI_LOCATE_SEARCH_TYPE SearchType, + IN EFI_GUID *Protocol OPTIONAL, + IN VOID *SearchKey OPTIONAL, + IN OUT UINTN *BufferSize, + OUT EFI_HANDLE *Buffer + ) +{ + EFI_STATUS Status; + + // + // Lock the protocol database + // + CoreAcquireProtocolLock (); + Status =3D InternalCoreLocateHandle(SearchType, Protocol, SearchKey, Buf= ferSize, Buffer); + CoreReleaseProtocolLock (); + return Status; +} =20 =20 /** @@ -610,7 +641,6 @@ Done: return Status; } =20 - /** Function returns an array of handles that support the requested protocol in a buffer allocated from pool. This is a version of CoreLocateHandle() @@ -657,7 +687,12 @@ CoreLocateHandleBuffer ( BufferSize =3D 0; *NumberHandles =3D 0; *Buffer =3D NULL; - Status =3D CoreLocateHandle ( + + // + // Lock the protocol database + // + CoreAcquireProtocolLock(); + Status =3D InternalCoreLocateHandle ( SearchType, Protocol, SearchKey, @@ -674,15 +709,17 @@ CoreLocateHandleBuffer ( if (Status !=3D EFI_INVALID_PARAMETER) { Status =3D EFI_NOT_FOUND; } + CoreReleaseProtocolLock (); return Status; } =20 *Buffer =3D AllocatePool (BufferSize); if (*Buffer =3D=3D NULL) { + CoreReleaseProtocolLock (); return EFI_OUT_OF_RESOURCES; } =20 - Status =3D CoreLocateHandle ( + Status =3D InternalCoreLocateHandle ( SearchType, Protocol, SearchKey, @@ -695,6 +732,7 @@ CoreLocateHandleBuffer ( *NumberHandles =3D 0; } =20 + CoreReleaseProtocolLock (); return Status; } =20 --=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 (#81270): https://edk2.groups.io/g/devel/message/81270 Mute This Topic: https://groups.io/mt/85943365/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-