Skip to content

Commit

Permalink
OcAppleKernelLib: Fix KPI handling in 10.6 prelinkedkernel
Browse files Browse the repository at this point in the history
Resolves issues injecting AppleIntelE1000e.kext referencing IOLockLock
  • Loading branch information
vit9696 committed Aug 26, 2020
1 parent d2230ec commit 520d27d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
7 changes: 6 additions & 1 deletion Library/OcAppleKernelLib/KxldState.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,13 @@ InternalKxldStateBuildLinkedVtables (
&NumVtables
);

//
// Some KPIs may not have vtables (e.g. BSD).
//
if (KxldVtables == NULL) {
return EFI_UNSUPPORTED;
Kext->LinkedVtables = NULL;
Kext->NumberOfVtables = 0;
return EFI_SUCCESS;
}

NumEntries = 0;
Expand Down
10 changes: 9 additions & 1 deletion Library/OcAppleKernelLib/Link.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,11 +1418,19 @@ InternalPrelinkKext64 (
if (!Result) {
DEBUG ((
DEBUG_INFO,
"OCAK: Symbol %s was unresolved for kext %a\n",
"OCAK: Symbol %a was unresolved for kext %a\n",
MachoGetSymbolName64 (MachoContext, Symbol),
Kext->Identifier
));
return EFI_LOAD_ERROR;
} else {
DEBUG ((
DEBUG_VERBOSE,
"OCAK: Symbol %a was resolved for kext %a to %Lx\n",
MachoGetSymbolName64 (MachoContext, Symbol),
Kext->Identifier,
Symbol->Value
));
}
}
//
Expand Down
36 changes: 26 additions & 10 deletions Library/OcAppleKernelLib/PrelinkedKext.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ InternalCreatePrelinkedKext (
UINT32 KxldStateSize;
UINT32 ContainerOffset;
BOOLEAN Found;
BOOLEAN HasExe;
BOOLEAN IsKpi;

KextIdentifier = NULL;
BundleLibraries = NULL;
Expand Down Expand Up @@ -154,17 +156,22 @@ InternalCreatePrelinkedKext (
//
// BundleLibraries, CompatibleVersion, and KmodInfo are optional and thus not checked.
//
if (!Found
|| KextIdentifier == NULL
|| SourceBase < VirtualBase
|| (Prelinked != NULL
&& (VirtualBase == 0
|| SourceBase == 0
|| SourceSize == 0
|| SourceSize > MAX_UINT32))) {
if (!Found || KextIdentifier == NULL || SourceBase < VirtualBase) {
return NULL;
}

//
// KPIs on 10.6.8 may not have executables, but for all other types they are required.
//
if (Prelinked != NULL) {
HasExe = VirtualBase != 0 && SourceBase != 0 && SourceSize != 0 && SourceSize <= MAX_UINT32;
IsKpi = VirtualBase == 0 && SourceBase == 0 && SourceSize == 0
&& KxldState != 0 && KxldStateSize != 0 && !Prelinked->IsKernelCollection;
if (!IsKpi && !HasExe) {
return NULL;
}
}

if (Prelinked != NULL && Prelinked->IsKernelCollection) {
CalculatedSourceSize = KcGetKextSize (Prelinked, SourceBase);
if (CalculatedSourceSize < MAX_UINT32 && CalculatedSourceSize > SourceSize) {
Expand All @@ -179,7 +186,7 @@ InternalCreatePrelinkedKext (
}
}

if (Prelinked != NULL) {
if (Prelinked != NULL && HasExe) {
if (Prelinked->IsKernelCollection) {
BaseSegment = Prelinked->RegionSegment;
} else {
Expand Down Expand Up @@ -208,6 +215,7 @@ InternalCreatePrelinkedKext (
}

if (Prelinked != NULL
&& HasExe
&& !MachoInitializeContext (&NewKext->Context.MachContext, &Prelinked->Prelinked[SourceBase], (UINT32)SourceSize, ContainerOffset)) {
FreePool (NewKext);
return NULL;
Expand Down Expand Up @@ -236,6 +244,14 @@ InternalCreatePrelinkedKext (
}
}

DEBUG ((
DEBUG_VERBOSE,
"OCAK: %a got KXLD %p %u\n",
NewKext->Identifier,
NewKext->Context.KxldState,
NewKext->Context.KxldStateSize
));

return NewKext;
}

Expand Down Expand Up @@ -887,7 +903,7 @@ InternalScanPrelinkedKext (
// _PrelinkExecutableLoadAddr / _PrelinkExecutableSourceAddr values equal to MAX_INT64.
// Skip them early to improve performance.
//
if ((Context->IsKernelCollection || Context->PrelinkedStateSegment != NULL)
if (Context->IsKernelCollection
&& AsciiStrnCmp (DependencyId, "com.apple.kpi.", L_STR_LEN ("com.apple.kpi.")) == 0) {
DEBUG ((DEBUG_VERBOSE, "OCAK: Ignoring KPI %a for kext %a in KC/state mode\n", DependencyId, Kext->Identifier));
continue;
Expand Down

0 comments on commit 520d27d

Please sign in to comment.