allow kext patches length will be less then find

Signed-off-by: Sergey Isakov <isakov-sl@bk.ru>
This commit is contained in:
Sergey Isakov 2020-04-24 12:01:28 +03:00
parent b3318cc1a4
commit 21f4bc6cb4
3 changed files with 37 additions and 25 deletions

View File

@ -22,7 +22,7 @@ set EDK2_BUILD_OPTIONS=-D NO_GRUB_DRIVERS
set VBIOSPATCHCLOVEREFI=0 set VBIOSPATCHCLOVEREFI=0
set ONLY_SATA_0=0 set ONLY_SATA_0=0
set USE_BIOS_BLOCKIO=0 set USE_BIOS_BLOCKIO=0
set USE_LOW_EBDA=1 set USE_LOW_EBDA=0
set DISABLE_USB_SUPPORT=0 set DISABLE_USB_SUPPORT=0
set GENPAGE=0 set GENPAGE=0
set MSG= set MSG=
@ -386,7 +386,7 @@ rem # drop compiled files to EFI folder
echo "ENABLE_SECURE_BOOT" doesnt work ATM ... echo "ENABLE_SECURE_BOOT" doesnt work ATM ...
echo. echo.
) )
goto noboot rem goto noboot
call:createDir %DEST_BOOTLOADERS%\%TARGETARCH% call:createDir %DEST_BOOTLOADERS%\%TARGETARCH%
echo Compressing DUETEFIMainFv.FV ^(%TARGETARCH%^) ... echo Compressing DUETEFIMainFv.FV ^(%TARGETARCH%^) ...

View File

@ -1136,7 +1136,7 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches,
TmpData = GetDataSetting (Prop2, "Find", &FindLen); TmpData = GetDataSetting (Prop2, "Find", &FindLen);
TmpPatch = GetDataSetting (Prop2, "Replace", &ReplaceLen); TmpPatch = GetDataSetting (Prop2, "Replace", &ReplaceLen);
if (!FindLen || !ReplaceLen || (FindLen != ReplaceLen)) { if (!FindLen || !ReplaceLen) {
DBG(" - invalid Find/Replace data - skipping!\n"); DBG(" - invalid Find/Replace data - skipping!\n");
continue; continue;
} }
@ -1155,17 +1155,20 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches,
CopyMem(Patches->KextPatches[Patches->NrKexts].MaskFind, TmpData, MaskLen); CopyMem(Patches->KextPatches[Patches->NrKexts].MaskFind, TmpData, MaskLen);
} }
FreePool(TmpData); 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); FreePool(TmpPatch);
MaskLen = 0; MaskLen = 0;
TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen); TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen);
MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; MaskLen = MIN(ReplaceLen, MaskLen);
if (TmpData == NULL || MaskLen == 0) { if (TmpData == NULL || MaskLen == 0) {
Patches->KextPatches[Patches->NrKexts].MaskReplace = NULL; Patches->KextPatches[Patches->NrKexts].MaskReplace = NULL;
} else { } else {
Patches->KextPatches[Patches->NrKexts].MaskReplace = (__typeof__(Patches->KextPatches[Patches->NrKexts].MaskReplace))AllocateZeroPool (FindLen); 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); FreePool(TmpData);
Patches->KextPatches[Patches->NrKexts].MatchOS = NULL; 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)); KERNEL_PATCH *newPatches = (__typeof__(newPatches))AllocateZeroPool (Count * sizeof(KERNEL_PATCH));
Patches->KernelPatches = newPatches; Patches->KernelPatches = newPatches;
DBG("KernelToPatch: %lld requested\n", Count); DBG("KernelToPatch: %lld requested\n", Count);
for (i = 0; i < Count; i++) { for (i = 0; i < Count; i++) {
CHAR8 *KernelPatchesLabel; CHAR8 *KernelPatchesLabel;
UINTN FindLen = 0, ReplaceLen = 0, MaskLen = 0; UINTN FindLen = 0, ReplaceLen = 0, MaskLen = 0;
UINT8 *TmpData, *TmpPatch; UINT8 *TmpData, *TmpPatch;
EFI_STATUS Status = GetElement (Prop, i, &Prop2); EFI_STATUS Status = GetElement (Prop, i, &Prop2);
if (EFI_ERROR(Status)) { 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; continue;
} }
if (Prop2 == NULL) { if (Prop2 == NULL) {
break; break;
} }
DBG(" - [%02lld]:", i); DBG(" - [%02lld]:", i);
Dict = GetProperty(Prop2, "Comment"); Dict = GetProperty(Prop2, "Comment");
if (Dict != NULL) { if (Dict != NULL) {
@ -1299,11 +1302,13 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches,
} }
FreePool(TmpData); FreePool(TmpData);
// this is "Replace" string len of ReplaceLen // this is "Replace" string len of ReplaceLen
ReplaceLen = MIN(ReplaceLen, FindLen);
Patches->KernelPatches[Patches->NrKernels].Patch = (__typeof__(Patches->KernelPatches[Patches->NrKernels].Patch))AllocateZeroPool(FindLen); Patches->KernelPatches[Patches->NrKernels].Patch = (__typeof__(Patches->KernelPatches[Patches->NrKernels].Patch))AllocateZeroPool(FindLen);
CopyMem(Patches->KernelPatches[Patches->NrKernels].Patch, TmpPatch, ReplaceLen); CopyMem(Patches->KernelPatches[Patches->NrKernels].Patch, TmpPatch, ReplaceLen);
FreePool(TmpPatch); FreePool(TmpPatch);
MaskLen = 0;
TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen); //reuse MaskLen TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen); //reuse MaskLen
MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; MaskLen = MIN(ReplaceLen, MaskLen);
if (TmpData == NULL || MaskLen == 0) { if (TmpData == NULL || MaskLen == 0) {
Patches->KernelPatches[Patches->NrKernels].MaskReplace = NULL; Patches->KernelPatches[Patches->NrKernels].MaskReplace = NULL;
} else { } else {
@ -1377,20 +1382,20 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches,
KERNEL_PATCH *newPatches = (__typeof__(newPatches))AllocateZeroPool (Count * sizeof(KERNEL_PATCH)); KERNEL_PATCH *newPatches = (__typeof__(newPatches))AllocateZeroPool (Count * sizeof(KERNEL_PATCH));
Patches->BootPatches = newPatches; Patches->BootPatches = newPatches;
DBG("BootPatches: %lld requested\n", Count); DBG("BootPatches: %lld requested\n", Count);
for (i = 0; i < Count; i++) { for (i = 0; i < Count; i++) {
CHAR8 *BootPatchesLabel; CHAR8 *BootPatchesLabel;
UINTN FindLen = 0, ReplaceLen = 0, MaskLen = 0; UINTN FindLen = 0, ReplaceLen = 0, MaskLen = 0;
UINT8 *TmpData, *TmpPatch; UINT8 *TmpData, *TmpPatch;
EFI_STATUS Status = GetElement (Prop, i, &Prop2); EFI_STATUS Status = GetElement (Prop, i, &Prop2);
if (EFI_ERROR(Status)) { 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; continue;
} }
if (Prop2 == NULL) { if (Prop2 == NULL) {
break; break;
} }
DBG(" - [%02lld]:", i); DBG(" - [%02lld]:", i);
Dict = GetProperty(Prop2, "Comment"); Dict = GetProperty(Prop2, "Comment");
if (Dict != NULL) { if (Dict != NULL) {
@ -1407,16 +1412,17 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches,
TmpData = GetDataSetting (Prop2, "Find", &FindLen); TmpData = GetDataSetting (Prop2, "Find", &FindLen);
TmpPatch = GetDataSetting (Prop2, "Replace", &ReplaceLen); TmpPatch = GetDataSetting (Prop2, "Replace", &ReplaceLen);
if (!FindLen || !ReplaceLen || (FindLen != ReplaceLen)) { if (!FindLen || !ReplaceLen) {
DBG(" :: invalid Find/Replace data - skipping!\n"); DBG(" :: invalid Find/Replace data - skipping!\n");
continue; continue;
} }
ReplaceLen = MIN(ReplaceLen, FindLen);
Patches->BootPatches[Patches->NrBoots].Data = (__typeof__(Patches->BootPatches[Patches->NrBoots].Data))AllocateCopyPool (FindLen, TmpData); Patches->BootPatches[Patches->NrBoots].Data = (__typeof__(Patches->BootPatches[Patches->NrBoots].Data))AllocateCopyPool (FindLen, TmpData);
Patches->BootPatches[Patches->NrBoots].DataLen = FindLen; Patches->BootPatches[Patches->NrBoots].DataLen = FindLen;
FreePool(TmpData); FreePool(TmpData);
MaskLen = 0;
TmpData = GetDataSetting (Prop2, "MaskFind", &MaskLen); TmpData = GetDataSetting (Prop2, "MaskFind", &MaskLen);
MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; MaskLen = MIN(FindLen, MaskLen);
if (TmpData == NULL || MaskLen == 0) { if (TmpData == NULL || MaskLen == 0) {
Patches->BootPatches[Patches->NrBoots].MaskFind = NULL; Patches->BootPatches[Patches->NrBoots].MaskFind = NULL;
} else { } else {
@ -1425,12 +1431,14 @@ FillinKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Patches,
CopyMem(Patches->BootPatches[Patches->NrBoots].MaskFind, TmpData, MaskLen); CopyMem(Patches->BootPatches[Patches->NrBoots].MaskFind, TmpData, MaskLen);
} }
FreePool(TmpData); 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); FreePool(TmpPatch);
MaskLen = 0;
TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen); TmpData = GetDataSetting (Prop2, "MaskReplace", &MaskLen);
MaskLen = (MaskLen > FindLen)? FindLen : MaskLen; MaskLen = MIN(ReplaceLen, MaskLen);
if (TmpData == NULL || MaskLen == 0) { if (TmpData == NULL || MaskLen == 0) {
Patches->BootPatches[Patches->NrBoots].MaskReplace = NULL; Patches->BootPatches[Patches->NrBoots].MaskReplace = NULL; //this is old behavior
} else { } else {
Patches->BootPatches[Patches->NrBoots].MaskReplace = (__typeof__(Patches->BootPatches[Patches->NrBoots].MaskReplace))AllocateZeroPool (FindLen); Patches->BootPatches[Patches->NrBoots].MaskReplace = (__typeof__(Patches->BootPatches[Patches->NrBoots].MaskReplace))AllocateZeroPool (FindLen);
CopyMem(Patches->BootPatches[Patches->NrBoots].MaskReplace, TmpData, MaskLen); CopyMem(Patches->BootPatches[Patches->NrBoots].MaskReplace, TmpData, MaskLen);

View File

@ -77,11 +77,11 @@ UINTN SearchAndReplace(UINT8 *Source, UINT64 SourceSize, UINT8 *Search, UINTN Se
BOOLEAN CompareMemMask(UINT8 *Source, UINT8 *Search, UINT8 *Mask, UINTN SearchSize) BOOLEAN CompareMemMask(UINT8 *Source, UINT8 *Search, UINT8 *Mask, UINTN SearchSize)
{ {
UINT8 M; UINT8 M;
UINTN Ind;
if (!Mask) { if (!Mask) {
return !CompareMem(Source, Search, SearchSize); return !CompareMem(Source, Search, SearchSize);
} }
for (Ind = 0; Ind < SearchSize; Ind++) { for (UINTN Ind = 0; Ind < SearchSize; Ind++) {
M = *Mask++; M = *Mask++;
if ((*Source++ & M) != (*Search++ & M)) { if ((*Source++ & M) != (*Search++ & M)) {
return FALSE; 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) VOID CopyMemMask(UINT8 *Dest, UINT8 *Replace, UINT8 *Mask, UINTN SearchSize)
{ {
UINT8 M, D; UINT8 M, D;
UINTN Ind; // the procedure is called from SearchAndReplaceMask with own check but for future it is better to check twice
if (!Mask) { if (!Dest || !Replace) {
CopyMem(Dest, Replace, SearchSize);
return; 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++; M = *Mask++;
D = *Dest; D = *Dest;
*Dest++ = ((D ^ *Replace++) & M) ^ D; *Dest++ = ((D ^ *Replace++) & M) ^ D;