From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NrzJ/g4f; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) 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) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6059DC433C1; Wed, 7 Dec 2022 16:12:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1670429570; bh=IO7TIVvXvv4CyOwfMjL4oE660yslHPMzoUgJEWqZmuU=; h=From:To:Cc:Subject:Date:From; b=NrzJ/g4fFp0N5SHTb/dYtbRAy47UCkt7XLNsqwa/GM/ivM19oHQyFTIyJAokdYDHE NgzYiH9kmpXQY+iAz8ktRF29ub4CpeMPxXxF8mMwdIpawRQBCBSEPl8YdJw2NimVuD iF3SEfsAAwVPF266UNOiK+KjTja2qXffu50a3cocjvOikMOeGVwowRIkUOC27BJq4H h76Fa0+LYRQ06kLRRtiIluFxkIukyx9UBHjJ0A1yE+N+2Bh+1qi3kzXHrrKs0wj3Tq TIAY1X6Muhdvlv+BNzG/AtnowDOwe4Eqw0e8yF1xMLtxsWl0hn8lGegALwTFurubpC kKf+eqKNIfgAg== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: ray.ni@intel.com, zhichao.gao@intel.com, Ard Biesheuvel Subject: [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> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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;=0D EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;=0D EFI_FILE_PROTOCOL *RealFileHandle;=0D - EFI_DEVICE_PATH_PROTOCOL *DevPath;=0D + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;=0D + VOID *DevPathBuf;=0D + EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;=0D =0D //=0D // get the simple file system interface=0D @@ -875,7 +877,11 @@ EfiShellOpenRootByHandle ( return Status;=0D }=0D =0D - *FileHandle =3D ConvertEfiFileProtocolToShellHandle (RealFileHandle, Efi= ShellGetMapFromDevicePath (&DevPath));=0D + DevPathCopy =3D DevPathBuf =3D DuplicateDevicePath (DevPath);=0D + *FileHandle =3D ConvertEfiFileProtocolToShellHandle (RealFileHandle,=0D + EfiShellGetMapFromDev= icePath (&DevPathCopy)=0D + );=0D + SHELL_FREE_NON_NULL (DevPathBuf);=0D return (EFI_SUCCESS);=0D }=0D =0D 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 ( **/=0D CHAR16 *=0D GetDeviceMediaType (=0D - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath=0D + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath=0D )=0D {=0D ACPI_HID_DEVICE_PATH *Acpi;=0D @@ -179,7 +179,7 @@ GetDeviceMediaType ( **/=0D BOOLEAN=0D IsRemoveableDevice (=0D - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath=0D + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath=0D )=0D {=0D if (NULL =3D=3D DevicePath) {=0D @@ -307,24 +307,29 @@ PerformSingleMappingDisplay ( IN CONST EFI_HANDLE Handle=0D )=0D {=0D - EFI_DEVICE_PATH_PROTOCOL *DevPath;=0D - EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;=0D - CONST CHAR16 *MapList;=0D - CHAR16 *CurrentName;=0D - CHAR16 *MediaType;=0D - CHAR16 *DevPathString;=0D - CHAR16 *TempSpot;=0D - CHAR16 *Alias;=0D - UINTN TempLen;=0D - BOOLEAN Removable;=0D - CONST CHAR16 *TempSpot2;=0D + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;=0D + VOID *DevPathBuf;=0D + EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;=0D + CONST CHAR16 *MapList;=0D + CHAR16 *CurrentName;=0D + CHAR16 *MediaType;=0D + CHAR16 *DevPathString;=0D + CHAR16 *TempSpot;=0D + CHAR16 *Alias;=0D + UINTN TempLen;=0D + BOOLEAN Removable;=0D + CONST CHAR16 *TempSpot2;=0D =0D Alias =3D NULL;=0D TempSpot2 =3D NULL;=0D CurrentName =3D NULL;=0D DevPath =3D DevicePathFromHandle (Handle);=0D - DevPathCopy =3D DevPath;=0D + DevPathBuf =3D DuplicateDevicePath (DevPath);=0D + DevPathCopy =3D DevPathBuf;=0D MapList =3D gEfiShellProtocol->GetMapFromDevicePath (&DevPathCopy);= =0D +=0D + SHELL_FREE_NON_NULL (DevPathBuf);=0D +=0D if (MapList =3D=3D NULL) {=0D return EFI_NOT_FOUND;=0D }=0D @@ -485,16 +490,20 @@ PerformSingleMappingDelete ( IN CONST EFI_HANDLE Handle=0D )=0D {=0D - EFI_DEVICE_PATH_PROTOCOL *DevPath;=0D - EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;=0D - CONST CHAR16 *MapList;=0D - CHAR16 *CurrentName;=0D + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;=0D + VOID *DevPathBuf;=0D + EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;=0D + CONST CHAR16 *MapList;=0D + CHAR16 *CurrentName;=0D =0D DevPath =3D DevicePathFromHandle (Handle);=0D - DevPathCopy =3D DevPath;=0D + DevPathBuf =3D DuplicateDevicePath (DevPath);=0D + DevPathCopy =3D DevPathBuf;=0D MapList =3D gEfiShellProtocol->GetMapFromDevicePath (&DevPathCopy);= =0D CurrentName =3D NULL;=0D =0D + SHELL_FREE_NON_NULL (DevPathBuf);=0D +=0D if (MapList =3D=3D NULL) {=0D return (EFI_NOT_FOUND);=0D }=0D --=20 2.35.1