Skip to content

Commit

Permalink
OcAppleKernelLib: Make more progress with KC context init
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed Jun 28, 2020
1 parent 83b77ff commit 44081eb
Show file tree
Hide file tree
Showing 9 changed files with 20,875 additions and 56 deletions.
16 changes: 16 additions & 0 deletions Include/Acidanthera/Library/OcAppleKernelLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ typedef struct {
//
MACH_SECTION_64 *PrelinkedTextSection;
//
// Mach-O context for inner prelinkedkernel (for KC mode).
//
OC_MACHO_CONTEXT InnerMachContext;
//
// Pointer to PRELINK_INFO_SEGMENT in the inner prelinkedkernel.
//
MACH_SEGMENT_COMMAND_64 *InnerInfoSegment;
//
// Pointer to PRELINK_INFO_SECTION in the inner prelinkedkernel.
//
MACH_SECTION_64 *InnerInfoSection;
//
// Copy of prelinkedkernel PRELINK_INFO_SECTION used for XML_DOCUMENT.
// Freed upon context destruction.
//
Expand Down Expand Up @@ -138,6 +150,10 @@ typedef struct {
// This is a sublist of PrelinkedKexts.
//
LIST_ENTRY InjectedKexts;
//
// Whether this kernel is a kernel collection (used by macOS 11.0+).
//
BOOLEAN IsKernelCollection;
} PRELINKED_CONTEXT;

//
Expand Down
6 changes: 3 additions & 3 deletions Library/OcAppleKernelLib/KernelCollection.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ InternalKcWriteCommandHeaders (
// Write 8-byte aligned fileset command.
//
Command.FilesetEntry->CommandType = MACH_LOAD_COMMAND_FILESET_ENTRY;
Command.FilesetEntry->CommandSize = ALIGN_VALUE (sizeof (MACH_FILESET_ENTRY_COMMAND) + StringSize, 8);
Command.FilesetEntry->CommandSize = (UINT32) ALIGN_VALUE (sizeof (MACH_FILESET_ENTRY_COMMAND) + StringSize, 8);
Command.FilesetEntry->VirtualAddress = PrelinkedKext->Context.VirtualBase;
Segment = MachoGetNextSegment64 (&PrelinkedKext->Context.MachContext, NULL);
ASSERT (Segment != NULL);
Expand Down Expand Up @@ -294,7 +294,7 @@ InternalKcInitSegmentFixupChains (
SegChain->PointerFormat = MACH_DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE;
SegChain->SegmentOffset = Segment->VirtualAddress;
SegChain->MaxValidPointer = 0;
SegChain->PageCount = Segment->Size / MACHO_PAGE_SIZE;
SegChain->PageCount = (UINT16) (Segment->Size / MACHO_PAGE_SIZE);
//
// Initialise all pages with no associated fixups.
//
Expand Down Expand Up @@ -413,7 +413,7 @@ InternalKcConvertRelocToFixup (
IterFixupData = SegmentPageData + IterFixupPageOffset;

CopyMem (&IterFixup, IterFixupData, sizeof (IterFixup));
NextIterFixupPageOffset = IterFixupPageOffset + IterFixup.Next;
NextIterFixupPageOffset = (UINT16) (IterFixupPageOffset + IterFixup.Next);
} while (NextIterFixupPageOffset < NewFixupPageOffset && IterFixup.Next != 0);

FixupDelta = NewFixupPageOffset - IterFixupPageOffset;
Expand Down
181 changes: 137 additions & 44 deletions Library/OcAppleKernelLib/PrelinkedContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,40 @@ PrelinkedFindKmodAddress (
return TRUE;
}

STATIC
EFI_STATUS
PrelinkedGetSegmentsFromMacho (
IN OC_MACHO_CONTEXT *MachoContext,
OUT MACH_SEGMENT_COMMAND_64 **PrelinkedInfoSegment,
OUT MACH_SECTION_64 **PrelinkedInfoSection
)
{
*PrelinkedInfoSegment = MachoGetSegmentByName64 (
MachoContext,
PRELINK_INFO_SEGMENT
);
if (*PrelinkedInfoSegment == NULL) {
return EFI_NOT_FOUND;
}
if ((*PrelinkedInfoSegment)->FileOffset > MAX_UINT32) {
return EFI_UNSUPPORTED;
}

*PrelinkedInfoSection = MachoGetSectionByName64 (
MachoContext,
*PrelinkedInfoSegment,
PRELINK_INFO_SECTION
);
if (*PrelinkedInfoSection == NULL) {
return EFI_NOT_FOUND;
}
if ((*PrelinkedInfoSection)->Size > MAX_UINT32) {
return EFI_UNSUPPORTED;
}

return EFI_SUCCESS;
}

EFI_STATUS
PrelinkedContextInit (
IN OUT PRELINKED_CONTEXT *Context,
Expand All @@ -153,10 +187,14 @@ PrelinkedContextInit (
IN UINT32 PrelinkedAllocSize
)
{
XML_NODE *PrelinkedInfoRoot;
CONST CHAR8 *PrelinkedInfoRootKey;
UINT32 PrelinkedInfoRootIndex;
UINT32 PrelinkedInfoRootCount;
EFI_STATUS Status;
MACH_HEADER_64 *Header;
MACH_SEGMENT_COMMAND_64 *Segment;
XML_NODE *DocumentRoot;
XML_NODE *PrelinkedInfoRoot;
CONST CHAR8 *PrelinkedInfoRootKey;
UINT32 PrelinkedInfoRootIndex;
UINT32 PrelinkedInfoRootCount;

ASSERT (Context != NULL);
ASSERT (Prelinked != NULL);
Expand All @@ -169,15 +207,6 @@ PrelinkedContextInit (
Context->PrelinkedSize = MACHO_ALIGN (PrelinkedSize);
Context->PrelinkedAllocSize = PrelinkedAllocSize;

//
// Initialize kext list with kernel pseudo kext.
//
InitializeListHead (&Context->PrelinkedKexts);
InitializeListHead (&Context->InjectedKexts);
if (InternalCachedPrelinkedKernel (Context) == NULL) {
return EFI_INVALID_PARAMETER;
}

//
// Ensure that PrelinkedSize is always aligned.
//
Expand All @@ -189,36 +218,63 @@ PrelinkedContextInit (
ZeroMem (&Prelinked[PrelinkedSize], Context->PrelinkedSize - PrelinkedSize);
}

//
// Initialise primary context.
//
if (!MachoInitializeContext (&Context->PrelinkedMachContext, Prelinked, PrelinkedSize)) {
return EFI_INVALID_PARAMETER;
}

Context->PrelinkedLastAddress = MACHO_ALIGN (MachoGetLastAddress64 (&Context->PrelinkedMachContext));
if (Context->PrelinkedLastAddress == 0) {
return EFI_INVALID_PARAMETER;
//
// Detect kernel type.
//
Header = MachoGetMachHeader64 (&Context->PrelinkedMachContext);
Context->IsKernelCollection = Header->FileType == MachHeaderFileTypeFileSet;

//
// When dealing with the kernel collections the actual kernel is pointed by one of the segments.
//
if (Context->IsKernelCollection) {
Segment = MachoGetSegmentByName64 (
&Context->PrelinkedMachContext,
"__TEXT_EXEC"
);
if (Segment == NULL || Segment->VirtualAddress < Segment->FileOffset) {
return EFI_INVALID_PARAMETER;
}

if (!MachoInitializeContext (
&Context->InnerMachContext,
&Context->Prelinked[Segment->FileOffset],
Context->PrelinkedSize - Segment->FileOffset)) {
return EFI_INVALID_PARAMETER;
}
}

Context->PrelinkedInfoSegment = MachoGetSegmentByName64 (
&Context->PrelinkedMachContext,
PRELINK_INFO_SEGMENT
);
if (Context->PrelinkedInfoSegment == NULL) {
return EFI_NOT_FOUND;
//
// Initialize kext list with kernel pseudo kext.
//
InitializeListHead (&Context->PrelinkedKexts);
InitializeListHead (&Context->InjectedKexts);
if (InternalCachedPrelinkedKernel (Context) == NULL) {
return EFI_INVALID_PARAMETER;
}
if (Context->PrelinkedInfoSegment->FileOffset > MAX_UINT32) {
return EFI_UNSUPPORTED;

Context->PrelinkedLastAddress = MACHO_ALIGN (MachoGetLastAddress64 (&Context->PrelinkedMachContext));
if (Context->PrelinkedLastAddress == 0) {
return EFI_INVALID_PARAMETER;
}

Context->PrelinkedInfoSection = MachoGetSectionByName64 (
//
// Register normal segment entries.
//
Status = PrelinkedGetSegmentsFromMacho (
&Context->PrelinkedMachContext,
Context->PrelinkedInfoSegment,
PRELINK_INFO_SECTION
&Context->PrelinkedInfoSegment,
&Context->PrelinkedInfoSection
);
if (Context->PrelinkedInfoSection == NULL) {
return EFI_NOT_FOUND;
}
if (Context->PrelinkedInfoSection->Size > MAX_UINT32) {
return EFI_UNSUPPORTED;
if (EFI_ERROR (Status)) {
return Status;
}

Context->PrelinkedTextSegment = MachoGetSegmentByName64 (
Expand All @@ -238,6 +294,20 @@ PrelinkedContextInit (
return EFI_NOT_FOUND;
}

//
// Additionally process inner entries for KC.
//
if (Context->IsKernelCollection) {
Status = PrelinkedGetSegmentsFromMacho (
&Context->InnerMachContext,
&Context->InnerInfoSegment,
&Context->InnerInfoSection
);
if (EFI_ERROR (Status)) {
return Status;
}
}

Context->PrelinkedInfo = AllocateCopyPool (
(UINTN)Context->PrelinkedInfoSection->Size,
&Context->Prelinked[Context->PrelinkedInfoSection->Offset]
Expand All @@ -252,7 +322,17 @@ PrelinkedContextInit (
return EFI_INVALID_PARAMETER;
}

PrelinkedInfoRoot = PlistNodeCast (XmlDocumentRoot (Context->PrelinkedInfoDocument), PLIST_NODE_TYPE_DICT);
//
// For a kernel collection the this is a full plist, while for legacy prelinked format
// it starts with a <dict> node.
//
if (Context->IsKernelCollection) {
DocumentRoot = PlistDocumentRoot (Context->PrelinkedInfoDocument);
} else {
DocumentRoot = XmlDocumentRoot (Context->PrelinkedInfoDocument);
}

PrelinkedInfoRoot = PlistNodeCast (DocumentRoot, PLIST_NODE_TYPE_DICT);
if (PrelinkedInfoRoot == NULL) {
PrelinkedContextFree (Context);
return EFI_INVALID_PARAMETER;
Expand Down Expand Up @@ -368,9 +448,31 @@ PrelinkedInjectPrepare (
{
UINT64 SegmentEndOffset;

Context->PrelinkedInfoSegment->VirtualAddress = 0;
Context->PrelinkedInfoSegment->Size = 0;
Context->PrelinkedInfoSegment->FileOffset = 0;
Context->PrelinkedInfoSegment->FileSize = 0;
Context->PrelinkedInfoSection->Address = 0;
Context->PrelinkedInfoSection->Size = 0;
Context->PrelinkedInfoSection->Offset = 0;

if (Context->IsKernelCollection) {
Context->InnerInfoSegment->VirtualAddress = 0;
Context->InnerInfoSegment->Size = 0;
Context->InnerInfoSegment->FileOffset = 0;
Context->InnerInfoSegment->FileSize = 0;
Context->InnerInfoSection->Address = 0;
Context->InnerInfoSection->Size = 0;
Context->InnerInfoSection->Offset = 0;

return EFI_SUCCESS;
}

//
// Plist info is normally the last segment, so we may potentially save
// some data by removing it and then appending new kexts over.
// For older variant of the prelinkedkernel plist info is normally
// the last segment, so we may potentially save some data by removing
// it and then appending new kexts over. This is different for KC,
// where plist info is in the middle of the file.
//

SegmentEndOffset = Context->PrelinkedInfoSegment->FileOffset + Context->PrelinkedInfoSegment->FileSize;
Expand All @@ -379,14 +481,6 @@ PrelinkedInjectPrepare (
Context->PrelinkedSize = (UINT32) MACHO_ALIGN (Context->PrelinkedInfoSegment->FileOffset);
}

Context->PrelinkedInfoSegment->VirtualAddress = 0;
Context->PrelinkedInfoSegment->Size = 0;
Context->PrelinkedInfoSegment->FileOffset = 0;
Context->PrelinkedInfoSegment->FileSize = 0;
Context->PrelinkedInfoSection->Address = 0;
Context->PrelinkedInfoSection->Size = 0;
Context->PrelinkedInfoSection->Offset = 0;

Context->PrelinkedLastAddress = MACHO_ALIGN (MachoGetLastAddress64 (&Context->PrelinkedMachContext));
if (Context->PrelinkedLastAddress == 0) {
return EFI_INVALID_PARAMETER;
Expand All @@ -395,7 +489,6 @@ PrelinkedInjectPrepare (
//
// Prior to plist there usually is prelinked text.
//

SegmentEndOffset = Context->PrelinkedTextSegment->FileOffset + Context->PrelinkedTextSegment->FileSize;

if (MACHO_ALIGN (SegmentEndOffset) != Context->PrelinkedSize) {
Expand Down
29 changes: 26 additions & 3 deletions Library/OcAppleKernelLib/PrelinkedKext.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,32 @@ InternalCachedPrelinkedKernel (
ASSERT (Prelinked->Prelinked != NULL);
ASSERT (Prelinked->PrelinkedSize > 0);

if (!MachoInitializeContext (&NewKext->Context.MachContext, &Prelinked->Prelinked[0], Prelinked->PrelinkedSize)) {
FreePool (NewKext);
return NULL;
//
// When dealing with the kernel collections the actual kernel is pointed by one of the segments.
//
if (Prelinked->IsKernelCollection) {
Segment = MachoGetSegmentByName64 (
&Prelinked->PrelinkedMachContext,
"__TEXT_EXEC"
);
if (Segment == NULL || Segment->VirtualAddress < Segment->FileOffset) {
FreePool (NewKext);
return NULL;
}

if (!MachoInitializeContext (
&NewKext->Context.MachContext,
&Prelinked->Prelinked[Segment->FileOffset],
Prelinked->PrelinkedSize - Segment->FileOffset)) {
FreePool (NewKext);
return NULL;
}
} else {
CopyMem (
&NewKext->Context.MachContext,
&Prelinked->PrelinkedMachContext,
sizeof (NewKext->Context.MachContext)
);
}

Segment = MachoGetSegmentByName64 (
Expand Down
Binary file modified Utilities/TestKernelCollection/KernelCollection
Binary file not shown.
Loading

0 comments on commit 44081eb

Please sign in to comment.