From nobody Sat May 4 11:20:17 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+97090+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+97090+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1670429572; cv=none; d=zohomail.com; s=zohoarc; b=A8LkJfhP2y1X88kOmjD9UV4JB467C3IEg0K53FnRQPjMfvdpX3vGJsUcyJL3RTsHg/kNpqFI11JUfrbUKDTHKwlRQNngtNCcL09G8dzXOcAIW6J7xwQ7maQZ3nTtcNdE64GSU9/ujGBDUrPy5VNN8MPSASdNT/dJuq5u2bgngd0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670429572; 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=xxnFhXI0+xdwQQuH6xhkhzWYL8RSDSik1DRx3DTuj44=; b=Bpg4omUFlYM+r/Nprqj1ZxPakuUW9I5uigZ8EHSFfQVneFAZ0QA1XnMMlqidaNYynKL6OIkRzNeX0Cv2S95eW7QvcWEsgFnr7aOlQ8gTsrtD+AEMKWs8EpmpVztpa0yHK1NR/tcsdoJ4oUNUmPrAU5nfhGcF4tHRNnP/Xdge0p8= 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+97090+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 1670429572589856.3334580712047; Wed, 7 Dec 2022 08:12:52 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id GnQ4YY1788612xnKSeqLVSZn; Wed, 07 Dec 2022 08:12:52 -0800 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.19555.1670429571694375239 for ; Wed, 07 Dec 2022 08:12:51 -0800 X-Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EE67161AC4; Wed, 7 Dec 2022 16:12:50 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6059DC433C1; Wed, 7 Dec 2022 16:12:49 +0000 (UTC) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: ray.ni@intel.com, zhichao.gao@intel.com, Ard Biesheuvel Subject: [edk2-devel] [PATCH] ShellPkg: Avoid corrupting installed device path protocols Date: Wed, 7 Dec 2022 17:12:45 +0100 Message-Id: <20221207161245.2554193-1-ardb@kernel.org> 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,ardb@kernel.org X-Gm-Message-State: KHNUiq2FtOSbdtQEaDtwOUdHx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1670429572; bh=J8ePf6wXHi7wSYjba0vwfmvUBYaO+IE2sYeXP3gbKg0=; h=Cc:Date:From:Reply-To:Subject:To; b=p9ngDNwU35uDryQKrvYxr3tf1PyfdrF5JNMKln0HSfSk13MRqcXe9S+UcNN+K71ryG8 fO67Mni6fGiG6MOkB8YuA4e1kKRgipLquOcs2SAO/AK4MNQNFL76w0CptTwP/CDHDfTSG yywG5kCjjX7xRxAEQrD7S7DdukwdbXZ340Y= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1670429573806100002 Content-Type: text/plain; charset="utf-8" The Shell locates device path protocol instances from the database and happily passes them to destructive device path operations, resulting in the original protocol to get corrupted as well. So take a copy instead, and discard it once we no longer need it. Signed-off-by: Ard Biesheuvel --- ShellPkg/Application/Shell/ShellProtocol.c | 10 +++- .../Library/UefiShellLevel2CommandsLib/Map.c | 47 +++++++++++-------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/ShellPkg/Application/Shell/ShellProtocol.c b/ShellPkg/Applicat= ion/Shell/ShellProtocol.c index 509eb60e40f4..6dbf344520d0 100644 --- a/ShellPkg/Application/Shell/ShellProtocol.c +++ b/ShellPkg/Application/Shell/ShellProtocol.c @@ -838,7 +838,9 @@ EfiShellOpenRootByHandle ( EFI_STATUS Status; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem; EFI_FILE_PROTOCOL *RealFileHandle; - EFI_DEVICE_PATH_PROTOCOL *DevPath; + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath; + VOID *DevPathBuf; + EFI_DEVICE_PATH_PROTOCOL *DevPathCopy; =20 // // get the simple file system interface @@ -875,7 +877,11 @@ EfiShellOpenRootByHandle ( return Status; } =20 - *FileHandle =3D ConvertEfiFileProtocolToShellHandle (RealFileHandle, Efi= ShellGetMapFromDevicePath (&DevPath)); + DevPathCopy =3D DevPathBuf =3D DuplicateDevicePath (DevPath); + *FileHandle =3D ConvertEfiFileProtocolToShellHandle (RealFileHandle, + EfiShellGetMapFromDev= icePath (&DevPathCopy) + ); + SHELL_FREE_NON_NULL (DevPathBuf); return (EFI_SUCCESS); } =20 diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c b/ShellPkg/L= ibrary/UefiShellLevel2CommandsLib/Map.c index f3c888edd48c..094e08eab4a5 100644 --- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c +++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c @@ -134,7 +134,7 @@ SearchList ( **/ CHAR16 * GetDeviceMediaType ( - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { ACPI_HID_DEVICE_PATH *Acpi; @@ -179,7 +179,7 @@ GetDeviceMediaType ( **/ BOOLEAN IsRemoveableDevice ( - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { if (NULL =3D=3D DevicePath) { @@ -307,24 +307,29 @@ PerformSingleMappingDisplay ( IN CONST EFI_HANDLE Handle ) { - EFI_DEVICE_PATH_PROTOCOL *DevPath; - EFI_DEVICE_PATH_PROTOCOL *DevPathCopy; - CONST CHAR16 *MapList; - CHAR16 *CurrentName; - CHAR16 *MediaType; - CHAR16 *DevPathString; - CHAR16 *TempSpot; - CHAR16 *Alias; - UINTN TempLen; - BOOLEAN Removable; - CONST CHAR16 *TempSpot2; + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath; + VOID *DevPathBuf; + EFI_DEVICE_PATH_PROTOCOL *DevPathCopy; + CONST CHAR16 *MapList; + CHAR16 *CurrentName; + CHAR16 *MediaType; + CHAR16 *DevPathString; + CHAR16 *TempSpot; + CHAR16 *Alias; + UINTN TempLen; + BOOLEAN Removable; + CONST CHAR16 *TempSpot2; =20 Alias =3D NULL; TempSpot2 =3D NULL; CurrentName =3D NULL; DevPath =3D DevicePathFromHandle (Handle); - DevPathCopy =3D DevPath; + DevPathBuf =3D DuplicateDevicePath (DevPath); + DevPathCopy =3D DevPathBuf; MapList =3D gEfiShellProtocol->GetMapFromDevicePath (&DevPathCopy); + + SHELL_FREE_NON_NULL (DevPathBuf); + if (MapList =3D=3D NULL) { return EFI_NOT_FOUND; } @@ -485,16 +490,20 @@ PerformSingleMappingDelete ( IN CONST EFI_HANDLE Handle ) { - EFI_DEVICE_PATH_PROTOCOL *DevPath; - EFI_DEVICE_PATH_PROTOCOL *DevPathCopy; - CONST CHAR16 *MapList; - CHAR16 *CurrentName; + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath; + VOID *DevPathBuf; + EFI_DEVICE_PATH_PROTOCOL *DevPathCopy; + CONST CHAR16 *MapList; + CHAR16 *CurrentName; =20 DevPath =3D DevicePathFromHandle (Handle); - DevPathCopy =3D DevPath; + DevPathBuf =3D DuplicateDevicePath (DevPath); + DevPathCopy =3D DevPathBuf; MapList =3D gEfiShellProtocol->GetMapFromDevicePath (&DevPathCopy); CurrentName =3D NULL; =20 + SHELL_FREE_NON_NULL (DevPathBuf); + if (MapList =3D=3D NULL) { return (EFI_NOT_FOUND); } --=20 2.35.1 -=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 (#97090): https://edk2.groups.io/g/devel/message/97090 Mute This Topic: https://groups.io/mt/95518373/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-