Correct some mistakes with kernel/kext patcher

This commit is contained in:
asava 2020-05-03 10:03:06 +03:00
parent d7d3c960a9
commit ccfbc5b558
3 changed files with 31 additions and 20 deletions

View File

@ -831,8 +831,8 @@ CopyKernelAndKextPatches (IN OUT KERNEL_AND_KEXT_PATCHES *Dst,
Dst->KernelPatches[Dst->NrKernels].StartPattern = NULL;
Dst->KernelPatches[Dst->NrKernels].StartMask = NULL;
}
Dst->KernelPatches[Dst->NrKernels].StartPatternLen = Src->KernelPatches[Dst->NrKernels].StartPatternLen;
Dst->KernelPatches[Dst->NrKernels].SearchLen = Src->KernelPatches[Dst->NrKernels].SearchLen;
Dst->KernelPatches[Dst->NrKernels].StartPatternLen = Src->KernelPatches[i].StartPatternLen;
Dst->KernelPatches[Dst->NrKernels].SearchLen = Src->KernelPatches[i].SearchLen;
if (Src->KernelPatches[i].ProcedureName != NULL) {
INTN len = strlen(Src->KernelPatches[i].ProcedureName);
Dst->KernelPatches[Dst->NrKernels].ProcedureName = (__typeof__(Dst->KernelPatches[Dst->NrKernels].ProcedureName))AllocateCopyPool(len, Src->KernelPatches[i].ProcedureName);

View File

@ -1883,6 +1883,10 @@ BOOLEAN
LOADER_ENTRY::KernelUserPatch(IN UINT8 *UKernelData)
{
INTN Num, i = 0, y = 0;
// if we modify directly KernelAndKextPatches->KernelPatches[i].SearchLen, it will wrong for next driver
UINTN SearchLen = KernelAndKextPatches->KernelPatches[i].SearchLen;
// old confuse
// We are using KernelAndKextPatches as set by Custom Entries.
// while config patches go to gSettings.KernelAndKextPatches
@ -1899,14 +1903,14 @@ LOADER_ENTRY::KernelUserPatch(IN UINT8 *UKernelData)
UINTN procLen = 0;
UINTN procAddr = searchProc(UKernelData, KernelAndKextPatches->KernelPatches[i].ProcedureName, &procLen);
DBG_RT("procedure %s found at 0x%llx\n", KernelAndKextPatches->KernelPatches[i].ProcedureName, procAddr);
if (KernelAndKextPatches->KernelPatches[i].SearchLen == 0) {
KernelAndKextPatches->KernelPatches[i].SearchLen = KERNEL_MAX_SIZE;
if (SearchLen == 0) {
SearchLen = KERNEL_MAX_SIZE;
if (procLen > KERNEL_MAX_SIZE) {
procLen = KERNEL_MAX_SIZE - procAddr;
once = true;
}
} else {
procLen = KernelAndKextPatches->KernelPatches[i].SearchLen;
procLen = SearchLen;
}
UINT8 * curs = &UKernelData[procAddr];
UINTN j = 0;
@ -1930,8 +1934,8 @@ LOADER_ENTRY::KernelUserPatch(IN UINT8 *UKernelData)
if (Num) {
y++;
curs += KernelAndKextPatches->KernelPatches[i].SearchLen - 1;
j += KernelAndKextPatches->KernelPatches[i].SearchLen - 1;
curs += SearchLen - 1;
j += SearchLen - 1;
}
DBG_RT( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);
if (once ||
@ -1954,8 +1958,12 @@ BOOLEAN
LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
{
INTN Num, i = 0, y = 0;
if (!KernelAndKextPatches->BootPatches[i].SearchLen) {
KernelAndKextPatches->BootPatches[i].SearchLen = BooterSize;
// if we modify directly KernelAndKextPatches-BootPatches[i].SearchLen, it will wrong for next driver
UINTN SearchLen = KernelAndKextPatches->KernelPatches[i].SearchLen;
if (!SearchLen) {
SearchLen = BooterSize;
}
for (; i < KernelAndKextPatches->NrBoots; ++i) {
DBG_RT( "Patch[%lld]: %s\n", i, KernelAndKextPatches->BootPatches[i].Label);
@ -1975,7 +1983,7 @@ LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
DBG_RT( " StartPattern found\n");
Num = SearchAndReplaceMask(BooterData,
KernelAndKextPatches->BootPatches[i].SearchLen,
SearchLen,
(const UINT8*)KernelAndKextPatches->BootPatches[i].Data,
(const UINT8*)KernelAndKextPatches->BootPatches[i].MaskFind,
KernelAndKextPatches->BootPatches[i].DataLen,
@ -1985,8 +1993,8 @@ LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
);
if (Num) {
y++;
curs += KernelAndKextPatches->KernelPatches[i].SearchLen - 1;
j += KernelAndKextPatches->KernelPatches[i].SearchLen - 1;
curs += SearchLen - 1;
j += SearchLen - 1;
}
DBG_RT( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);

View File

@ -999,6 +999,9 @@ VOID LOADER_ENTRY::AnyKextPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPli
{
UINTN Num = 0;
INTN Ind;
// if we modify value directly at KernelAndKextPatches->KextPatches[N].SearchLen, it will be wrong for next driver
UINTN SearchLen = KernelAndKextPatches->KextPatches[N].SearchLen;
DBG_RT("\nAnyKextPatch %d: driverAddr = %s, driverSize = %x\nAnyKext = %s\n",
N, Driver, DriverSize, KernelAndKextPatches->KextPatches[N].Label);
@ -1008,9 +1011,9 @@ VOID LOADER_ENTRY::AnyKextPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPli
return;
}
if (!KernelAndKextPatches->KextPatches[N].SearchLen ||
(KernelAndKextPatches->KextPatches[N].SearchLen > DriverSize)) {
KernelAndKextPatches->KextPatches[N].SearchLen = DriverSize;
if (!SearchLen ||
(SearchLen > DriverSize)) {
SearchLen = DriverSize;
}
if (KernelAndKextPatches->KPDebug) {
@ -1026,14 +1029,14 @@ VOID LOADER_ENTRY::AnyKextPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPli
UINTN procLen = 0;
UINTN procAddr = searchProc(Driver, KernelAndKextPatches->KextPatches[N].ProcedureName, &procLen);
if (KernelAndKextPatches->KextPatches[N].SearchLen == 0) {
KernelAndKextPatches->KextPatches[N].SearchLen = DriverSize;
if (SearchLen == 0) {
SearchLen = DriverSize;
if (procLen > DriverSize) {
procLen = DriverSize - procAddr;
once = true;
}
} else {
procLen = KernelAndKextPatches->KextPatches[N].SearchLen;
procLen = SearchLen;
}
const UINT8 * curs = &Driver[procAddr];
UINTN j = 0;
@ -1055,8 +1058,8 @@ VOID LOADER_ENTRY::AnyKextPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPli
(const UINT8*)KernelAndKextPatches->KextPatches[N].MaskReplace,
-1);
if (Num) {
curs += KernelAndKextPatches->KextPatches[N].SearchLen - 1;
j += KernelAndKextPatches->KextPatches[N].SearchLen - 1;
curs += SearchLen - 1;
j += SearchLen - 1;
}
}
if (once ||