Skip to content

OcAppleKernelLib: fix unchecked overflow and align-before-check in PrelinkedContext.c - #614

Closed
albert-mueller wants to merge 1 commit into
acidanthera:masterfrom
albert-mueller:PrelinkedContext.c-Fehler-beheben
Closed

OcAppleKernelLib: fix unchecked overflow and align-before-check in PrelinkedContext.c#614
albert-mueller wants to merge 1 commit into
acidanthera:masterfrom
albert-mueller:PrelinkedContext.c-Fehler-beheben

Conversation

@albert-mueller

Copy link
Copy Markdown
Contributor
  • PrelinkedContextInit: guard the KC __LINKEDIT SegmentEndOffset and PrelinkedLastLoadAddress additions with BaseOverflowAddU64, matching the overflow-checked style already used elsewhere in this file (e.g. PrelinkedFindLastLoadAddress).
  • PrelinkedInjectPrepare: compute AlignedExpansion before validating it against PrelinkedAllocSize, instead of validating the unaligned LinkedExpansion and then zeroing/committing the larger aligned value.

…elinkedContext.c

- PrelinkedContextInit: guard the KC __LINKEDIT SegmentEndOffset and PrelinkedLastLoadAddress additions with BaseOverflowAddU64, matching the overflow-checked style already used elsewhere in this file (e.g. PrelinkedFindLastLoadAddress).
- PrelinkedInjectPrepare: compute AlignedExpansion before validating it against PrelinkedAllocSize, instead of validating the unaligned LinkedExpansion and then zeroing/committing the larger aligned value.

@vit9696 vit9696 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the patch. I am not fully positive all the changes are needed. Replied inline.

// In KC mode last load address is the __LINKEDIT address.
//
SegmentEndOffset = Context->LinkEditSegment->Segment64.FileOffset + Context->LinkEditSegment->Segment64.FileSize;
if (BaseOverflowAddU64 (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Context->PrelinkedLastLoadAddress = Context->LinkEditSegment->Segment64.VirtualAddress + Context->LinkEditSegment->Segment64.Size;
if (BaseOverflowAddU64 (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// zeroed and committed below -- checking the unaligned LinkedExpansion here
// would let AlignedExpansion (rounded up) write past the validated bound.
//
AlignedExpansion = MACHO_ALIGN (LinkedExpansion);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you find an actual path to violate this? I am not yet sure this change is needed, but it will help if you confirm it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is needed, just tested on my T2 MacBook Pro and for the first time on a T2 Mac it boots Tahoe via OpenCore Legacy Patcher T2 and now it is able to boot Tahoe without triggering Activation Lock or causing Needs authenticator panics. Just uploading a video showing my MacBook Pro 2020 booting OpenCore with a spoofed SMBIOS, with adding the latest changes I did in my fork on top:
https://youtu.be/_ACpaSJEt2Q?is=rXZPrSjH3SAsbL3_ - this video demonstrates my MacBook Pro 2020 4 thunderbolt 3 ports being able to boot via OpenCorePkg spoofed as MacBook Pro 2019 16 inch without much patches except the most obvious kexts and does get to the desktop.
On a Hackintosh system or a non-T2 Mac, you don’t have a T2 chip and as such this change only improves performance. But on T2 Macs trying to boot Tahoe via OpenCorePkg supported or not, this change is must-have to even boot.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, hmm.

  1. The first two changes do nothing, they are definitely checked prior to the code you changed.
  2. The third change will abort booting, if the check you modified triggers. In fact, if the check triggers, you have corrupted kernel collection supplied by Apple, which has never been the case so far.

The fact your T2 system boots is dependent on something else, and these changes cannot affect it directly, I am afraid.

What is the origin of the changes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, and thanks for checking — you're right on all three points.

Origin: these came out of an AI-assisted read-through of this file. It picked up on the overflow-checked style used elsewhere in PrelinkedContext.c and pattern-matched the same treatment onto these two additions, without tracing far enough to see they're already covered.

On 1 & 2: confirmed against MachoGetSegmentByName() → MachoGetNextSegment() directly. Context->LinkEditSegment can only ever come from there, and that path already checks FileOffset + FileSize (overflow, bounded against Context->FileSize) and VirtualAddress + Size (overflow) before a segment is ever returned. So both BaseOverflowAddU64 additions are unreachable, as you said.

On 3: I don't have a concrete path that violates the existing LinkedExpansion check, and since PrelinkedAllocSize is reserved with its own margin by the caller, I agree this is theoretical at best.

And I should retract the T2 boot claim — that's not explained by any of this. Whatever actually fixed that boot is unrelated to this file. Sorry for the noise, closing this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thank you. Since 3 might happen, though very very unlikely, I made the change in master to make things cleaner anyway.

@albert-mueller
albert-mueller deleted the PrelinkedContext.c-Fehler-beheben branch August 16, 2026 08:54
vit9696 added a commit that referenced this pull request Aug 16, 2026
Might happen if aligning LinkedExpansion exceeds PrelinkedAllocSize.

Thanks @albert-mueller
closes #614
@albert-mueller

albert-mueller commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

I’ve found that this error is still occurring mostly on unsupported T2 Macs: albert-mueller/OpenCore-Legacy-Patcher-T2#130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants