From 21f4bc6cb4b7eb28b2fb7b60647c044b479a2f6a Mon Sep 17 00:00:00 2001 From: Sergey Isakov Date: Fri, 24 Apr 2020 12:01:28 +0300 Subject: [PATCH] allow kext patches length will be less then find Signed-off-by: Sergey Isakov --- cbuild.bat | 4 +-- rEFIt_UEFI/Platform/Settings.cpp | 42 +++++++++++++++++----------- rEFIt_UEFI/Platform/kext_patcher.cpp | 16 +++++++---- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/cbuild.bat b/cbuild.bat index bc666e3ef..3ea58c974 100644 --- a/cbuild.bat +++ b/cbuild.bat @@ -22,7 +22,7 @@ set EDK2_BUILD_OPTIONS=-D NO_GRUB_DRIVERS set VBIOSPATCHCLOVEREFI=0 set ONLY_SATA_0=0 set USE_BIOS_BLOCKIO=0 -set USE_LOW_EBDA=1 +set USE_LOW_EBDA=0 set DISABLE_USB_SUPPORT=0 set GENPAGE=0 set MSG= @@ -386,7 +386,7 @@ rem # drop compiled files to EFI folder echo "ENABLE_SECURE_BOOT" doesnt work ATM ... echo. ) - goto noboot + rem goto noboot call:createDir %DEST_BOOTLOADERS%\%TARGETARCH% echo Compressing DUETEFIMainFv.FV ^(%TARGETARCH%^) ... diff --git a/rEFIt_UEFI/Platform/Settings.cpp b/rEFIt_UEFI/Platform/Settings.cpp index 787af1742..ecea0db46 100644 --- a/rEFIt_UEFI/Platform/Settings.cpp +++ b/rEFIt_UEFI/Platform/Settings.cpp @@ -1136,7 +1136,7 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches, TmpData = GetDataSetting (Prop2, "Find", &FindLen); TmpPatch = GetDataSetting (Prop2, "Replace", &ReplaceLen); - if (!FindLen || !ReplaceLen || (FindLen != ReplaceLen)) { + if (!FindLen || !ReplaceLen) { DBG(" - invalid Find/Replace data - skipping!\n"); continue; } @@ -1155,17 +1155,20 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches, CopyMem(Patches->KextPatches[Patches->NrKexts].MaskFind, TmpData, MaskLen); } FreePool(TmpData); - Patches->KextPatches[Patches->NrKexts].Patch = (__typeof__(Patches->KextPatches[Patches->NrKexts].Patch))AllocateCopyPool (FindLen, TmpPatch); + // take into account a possibility to set ReplaceLen < FindLen. In this case assumes MaskReplace = 0 for the rest of bytes + Patches->KextPatches[Patches->NrKexts].Patch = (__typeof__(Patches->KextPatches[Patches->NrKexts].Patch))AllocateZeroPool (FindLen); + ReplaceLen = MIN(ReplaceLen, FindLen); + CopyMem(Patches->KextPatches[Patches->NrKexts].Patch, TmpPatch, ReplaceLen); FreePool(TmpPatch); MaskLen = 0; TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen); - MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; + MaskLen = MIN(ReplaceLen, MaskLen); if (TmpData == NULL || MaskLen == 0) { Patches->KextPatches[Patches->NrKexts].MaskReplace = NULL; } else { Patches->KextPatches[Patches->NrKexts].MaskReplace = (__typeof__(Patches->KextPatches[Patches->NrKexts].MaskReplace))AllocateZeroPool (FindLen); - CopyMem(Patches->KextPatches[Patches->NrKexts].MaskReplace, TmpData, MaskLen); + CopyMem(Patches->KextPatches[Patches->NrKexts].MaskReplace, TmpData, MaskLen); //other bytes are zeros, means no replace } FreePool(TmpData); Patches->KextPatches[Patches->NrKexts].MatchOS = NULL; @@ -1250,21 +1253,21 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches, KERNEL_PATCH *newPatches = (__typeof__(newPatches))AllocateZeroPool (Count * sizeof(KERNEL_PATCH)); Patches->KernelPatches = newPatches; - DBG("KernelToPatch: %lld requested\n", Count); + DBG("KernelToPatch: %lld requested\n", Count); for (i = 0; i < Count; i++) { CHAR8 *KernelPatchesLabel; UINTN FindLen = 0, ReplaceLen = 0, MaskLen = 0; UINT8 *TmpData, *TmpPatch; EFI_STATUS Status = GetElement (Prop, i, &Prop2); if (EFI_ERROR(Status)) { - DBG(" - [%02lld]: Patches error %s getting next element\n", i, strerror(Status)); + DBG(" - [%02lld]: Patches error %s getting next element\n", i, strerror(Status)); continue; } if (Prop2 == NULL) { break; } - DBG(" - [%02lld]:", i); + DBG(" - [%02lld]:", i); Dict = GetProperty(Prop2, "Comment"); if (Dict != NULL) { @@ -1299,11 +1302,13 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches, } FreePool(TmpData); // this is "Replace" string len of ReplaceLen + ReplaceLen = MIN(ReplaceLen, FindLen); Patches->KernelPatches[Patches->NrKernels].Patch = (__typeof__(Patches->KernelPatches[Patches->NrKernels].Patch))AllocateZeroPool(FindLen); CopyMem(Patches->KernelPatches[Patches->NrKernels].Patch, TmpPatch, ReplaceLen); FreePool(TmpPatch); + MaskLen = 0; TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen); //reuse MaskLen - MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; + MaskLen = MIN(ReplaceLen, MaskLen); if (TmpData == NULL || MaskLen == 0) { Patches->KernelPatches[Patches->NrKernels].MaskReplace = NULL; } else { @@ -1377,20 +1382,20 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches, KERNEL_PATCH *newPatches = (__typeof__(newPatches))AllocateZeroPool (Count * sizeof(KERNEL_PATCH)); Patches->BootPatches = newPatches; - DBG("BootPatches: %lld requested\n", Count); + DBG("BootPatches: %lld requested\n", Count); for (i = 0; i < Count; i++) { CHAR8 *BootPatchesLabel; UINTN FindLen = 0, ReplaceLen = 0, MaskLen = 0; UINT8 *TmpData, *TmpPatch; EFI_STATUS Status = GetElement (Prop, i, &Prop2); if (EFI_ERROR(Status)) { - DBG(" - [%02lld]: error %s getting next element\n", i, strerror(Status)); + DBG(" - [%02lld]: error %s getting next element\n", i, strerror(Status)); continue; } if (Prop2 == NULL) { break; } - DBG(" - [%02lld]:", i); + DBG(" - [%02lld]:", i); Dict = GetProperty(Prop2, "Comment"); if (Dict != NULL) { @@ -1407,16 +1412,17 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches, TmpData = GetDataSetting (Prop2, "Find", &FindLen); TmpPatch = GetDataSetting (Prop2, "Replace", &ReplaceLen); - if (!FindLen || !ReplaceLen || (FindLen != ReplaceLen)) { + if (!FindLen || !ReplaceLen) { DBG(" :: invalid Find/Replace data - skipping!\n"); continue; } - + ReplaceLen = MIN(ReplaceLen, FindLen); Patches->BootPatches[Patches->NrBoots].Data = (__typeof__(Patches->BootPatches[Patches->NrBoots].Data))AllocateCopyPool (FindLen, TmpData); Patches->BootPatches[Patches->NrBoots].DataLen = FindLen; FreePool(TmpData); + MaskLen = 0; TmpData = GetDataSetting (Prop2, "MaskFind", &MaskLen); - MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; + MaskLen = MIN(FindLen, MaskLen); if (TmpData == NULL || MaskLen == 0) { Patches->BootPatches[Patches->NrBoots].MaskFind = NULL; } else { @@ -1425,12 +1431,14 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches, CopyMem(Patches->BootPatches[Patches->NrBoots].MaskFind, TmpData, MaskLen); } FreePool(TmpData); - Patches->BootPatches[Patches->NrBoots].Patch = (__typeof__(Patches->BootPatches[Patches->NrBoots].Patch))AllocateCopyPool (FindLen, TmpPatch); + Patches->BootPatches[Patches->NrBoots].Patch = (__typeof__(Patches->BootPatches[Patches->NrBoots].Patch))AllocateZeroPool (FindLen); + CopyMem(Patches->BootPatches[Patches->NrBoots].Patch, TmpPatch, ReplaceLen); FreePool(TmpPatch); + MaskLen = 0; TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen); - MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; + MaskLen = MIN(ReplaceLen, MaskLen); if (TmpData == NULL || MaskLen == 0) { - Patches->BootPatches[Patches->NrBoots].MaskReplace = NULL; + Patches->BootPatches[Patches->NrBoots].MaskReplace = NULL; //this is old behavior } else { Patches->BootPatches[Patches->NrBoots].MaskReplace = (__typeof__(Patches->BootPatches[Patches->NrBoots].MaskReplace))AllocateZeroPool (FindLen); CopyMem(Patches->BootPatches[Patches->NrBoots].MaskReplace, TmpData, MaskLen); diff --git a/rEFIt_UEFI/Platform/kext_patcher.cpp b/rEFIt_UEFI/Platform/kext_patcher.cpp index 92ec699b2..70c6a6c95 100644 --- a/rEFIt_UEFI/Platform/kext_patcher.cpp +++ b/rEFIt_UEFI/Platform/kext_patcher.cpp @@ -77,11 +77,11 @@ UINTN SearchAndReplace(UINT8 *Source, UINT64 SourceSize, UINT8 *Search, UINTN Se BOOLEAN CompareMemMask(UINT8 *Source, UINT8 *Search, UINT8 *Mask, UINTN SearchSize) { UINT8 M; - UINTN Ind; + if (!Mask) { return !CompareMem(Source, Search, SearchSize); } - for (Ind = 0; Ind < SearchSize; Ind++) { + for (UINTN Ind = 0; Ind < SearchSize; Ind++) { M = *Mask++; if ((*Source++ & M) != (*Search++ & M)) { return FALSE; @@ -93,12 +93,16 @@ BOOLEAN CompareMemMask(UINT8 *Source, UINT8 *Search, UINT8 *Mask, UINTN SearchSi VOID CopyMemMask(UINT8 *Dest, UINT8 *Replace, UINT8 *Mask, UINTN SearchSize) { UINT8 M, D; - UINTN Ind; - if (!Mask) { - CopyMem(Dest, Replace, SearchSize); + // the procedure is called from SearchAndReplaceMask with own check but for future it is better to check twice + if (!Dest || !Replace) { return; } - for (Ind = 0; Ind < SearchSize; Ind++) { + + if (!Mask) { + CopyMem(Dest, Replace, SearchSize); //old behavior + return; + } + for (UINTN Ind = 0; Ind < SearchSize; Ind++) { M = *Mask++; D = *Dest; *Dest++ = ((D ^ *Replace++) & M) ^ D;