SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
death of the vmsize=0 dyld trick
(one more way to persist on your iPhone killed)
SyScan 2015 Bonus Slides
Stefan Esser <stefan.esser@sektioneins.de>
http://www.sektioneins.de
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
Who am I?
Stefan Esser
• from Cologne / Germany
• in information security since 1998
• invested in PHP security from 2001 to 20xx
• since 2010 focused on iPhone security (ASLR/jailbreak)
• founder of SektionEins GmbH
2
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
Introduction
• at SyScan 2015 I made a talk about
• how Apple failed to fix vulnerabilities used in iOS 678 jailbreaks over and over again
• how and why Chinese jailbreak teams took over the jb scene in 2014

• during the talk I discussed “Patient ALPHA” an incomplete code signing bug that
Apple failed to analyse correctly and therefore had to issue 4 security updates for
• during the talk I also promised to disclose another incomplete code signing
vulnerability that Apple closed by accident with their patches for “Patient ALPHA”
• we should be fair in information security and not only discuss fail, but also 

show how they killed a bug (even if they most probably did not know about it)
3
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
Incomplete Codesigning
• simplified:
• if a page is not executable a missing code sig is not a problem
• if a page is executable there must be a code sig on first access
• prior to iOS 5 therefore jailbreaks would use ALL DATA dylibs to
exploit dyld via various meta-data structures
• around the end of iOS 4 Apple added checks to dyld to enforce load
commands are in an executable segment
• therefore while header parsing (first access) code sig is required
4
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
mach-o dynamic library loading
• mach-o dynamic libraries loaded by dyld
• load commands describe i.a. layout of
segments in memory
• virtual address and 

virtual size of segments
• file position and 

file size of segment
5
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
Wait a second …
• actually it is not that simple
• mach-o header is first loaded into stack
• initial LC parse is performed to collect info
• this info is used to map the file into memory
• segments are touched to enforce code sig
• another LC parse is performed to make dyld
use the mach-o header from paged memory
• more and more parsing
6
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
TOCTOU Problems
• this chain of events has some TOCTOU
(Time of Check Time of Use) problems
• attacking the code flow between
sniffLoadCommands and
crashIfInvalidCodesignature
• tricking e.g. mapSegments
• evad3rs tricked that function first, but
they went after replacing segment
mappings or stripping the X flag
• however there was a 0-day that tricked
this whole logic in a different way
7
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
mapSegments
• mapSegments goes through the list of known
segments and maps them one by one
• count of segments is calculated inside the
sniffLoadCommands functions
• mapSegments fully relies on that count
• apparently there will be problems if
sniffLoadCommands is counting the segments
wrong
8
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
sniffLoadCommands and Segment Counting
• code inside sniffLoadCommands counts LC_SEGMENT_COMMAND commands
• but it ignores segments if their VMSIZE is 0
9
switch (cmd->cmd) {
case LC_DYLD_INFO:
case LC_DYLD_INFO_ONLY:
*compressed = true;
break;
case LC_SEGMENT_COMMAND:
segCmd = (struct macho_segment_command*)cmd;
// ignore zero-sized segments
if ( segCmd->vmsize != 0 )
*segCount += 1;
// <rdar://problem/7942521> all load commands must be in an executable segment
if ( context.codeSigningEnforced && (segCmd->fileoff < mh->sizeofcmds) && (segCmd->filesize != 0) ) {
if ( (segCmd->fileoff != 0) || (segCmd->filesize < (mh->sizeofcmds+sizeof(macho_header))) )
dyld::throwf("malformed mach-o image: segment %s does not span al…”, segCmd->segname);
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
ImageLoaderMachO::ImageLoaderMachO (I)
• inside the constructor of ImageLoaderMachO the addresses of the
LC_SEGMENT_COMMAND load commands are put into a cache
• this is done to easier traverse through the list of segments
• the code also ignores segments with a VMSIZE=0
• IMPORTANT: this means that if the segment containing load commands has
vmsize=0 it is not in list of segments and will not be traversed by later code
10
// construct SegmentMachO object for each LC_SEGMENT cmd using "placement new" to put
// each SegmentMachO object in array at end of ImageLoaderMachO object
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
const struct load_command* cmd = cmds;
for (uint32_t i = 0, segIndex=0; i < cmd_count; ++i) {
if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
const struct macho_segment_command* segCmd = (struct macho_segment_command*)cmd;
// ignore zero-sized segments
if ( segCmd->vmsize != 0 ) {
// record offset of load command
segOffsets[segIndex++] = (uint32_t)((uint8_t*)segCmd - fMachOData);
}
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
ImageLoaderMachO::ImageLoaderMachO (II)
• IMPORTANT: internally all accesses to the mach-o header go through fMachOData
• so initially it is set to the stack 

(which is not executable and therefore requires no code signature)
• later on it is supposed to be pointing to the mapped executable segment containing
the load commands
11
// construct SegmentMachO object for each LC_SEGMENT cmd using "placement new" to put
// each SegmentMachO object in array at end of ImageLoaderMachO object
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
const struct load_command* cmd = cmds;
for (uint32_t i = 0, segIndex=0; i < cmd_count; ++i) {
if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
const struct macho_segment_command* segCmd = (struct macho_segment_command*)cmd;
// ignore zero-sized segments
if ( segCmd->vmsize != 0 ) {
// record offset of load command
segOffsets[segIndex++] = (uint32_t)((uint8_t*)segCmd - fMachOData);
}
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
mapSegments mapping segments
• mapSegments traverses ONLY the cached list of LC_SEGMENT_COMMANDS
• any segment that had a vmsize=0 will never be mapped (because they are not in there)
12
// map in all segments
for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
vm_offset_t fileOffset = segFileOffset(i) + offsetInFat;
vm_size_t size = segFileSize(i);
uintptr_t requestedLoadAddress = segPreferredLoadAddress(i) + slide;
...
void* loadAddress = xmmap((void*)requestedLoadAddress, size, protection,
MAP_FIXED | MAP_PRIVATE, fd, fileOffset);
if ( loadAddress == ((void*)(-1)) ) {
dyld::throwf("mmap() error %d at address=0x%08lX, size=0x%08lX segment=%s in
Segment::map() mapping %s",
errno, requestedLoadAddress, (uintptr_t)size, segName(i), getPath());
}
}
…
}
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
crashIfInvalidCodeSignature
• because the executable segment with the load commands had vmsize=0 

it is never in the segment list and therefore never touched in here
• therefore there is no crash on invalid code signature
13
int ImageLoaderMachO::crashIfInvalidCodeSignature()
{
// Now that segments are mapped in, try reading from first executable segment.
// If code signing is enabled the kernel will validate the code signature
// when paging in, and kill the process if invalid.
for(unsigned int i=0; i < fSegmentsCount; ++i) {
if ( (segFileOffset(i) == 0) && (segFileSize(i) != 0) ) {
// return read value to ensure compiler does not optimize away load
int* p = (int*)segActualLoadAddress(i);
return *p;
}
}
return 0;
}
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
parseLoadCmds
• is called by instantiateFinish
• it should set the fMachOData pointer to the mapped executable segment
• but because our LC segment with vmsize=0 is never in that list this never happens
• this means fMachOData keeps pointing to the stack copy of mach-o header
• there will never be access to an 

executable segment = code signing bypassed = WIN !!!
14
void ImageLoaderMachO::parseLoadCmds()
{
// now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
for(unsigned int i=0; i < fSegmentsCount; ++i) {
...
// some segment always starts at beginning of file and contains mach_header and ..
if ( (segFileOffset(i) == 0) && (segFileSize(i) != 0) ) {
fMachOData = (uint8_t*)(segActualLoadAddress(i));
}
}
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
Conclusion
• it was trivial to bypass the dynamic linkers security checks
• the trick was to give the load command segment a vmsize=0
• Apple accidentally killed that trick by enforcing that vmsize > filesize
• one less way attackers can use to persist on iDevices to surveil you
• while this fix is most probably an accident Apple did good here :)
15
Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 • 
Questions
?
16

Weitere ähnliche Inhalte

Ähnlich wie SyScan 2015 Bonus Slides - death of the vmsize=0 dyld trick

Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...
Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...
Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...Priyanka Aash
 
Safe Clearing of Private Data
Safe Clearing of Private DataSafe Clearing of Private Data
Safe Clearing of Private DataPVS-Studio
 
SPU gameplay
SPU gameplaySPU gameplay
SPU gameplaySlide_N
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Vincenzo Iozzo
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3Raahul Raghavan
 
A Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and AllegroA Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and Allegrosnowfarthing
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeWim Godden
 
The Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmThe Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmAndrey Karpov
 
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019 Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019 Unity Technologies
 
Databricks spark-knowledge-base-1
Databricks spark-knowledge-base-1Databricks spark-knowledge-base-1
Databricks spark-knowledge-base-1Rahul Kumar
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeWim Godden
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source codePVS-Studio
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source codeAndrey Karpov
 
CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...
CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...
CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...PROIDEA
 
Behind the scenes with IOS security
Behind the scenes with IOS securityBehind the scenes with IOS security
Behind the scenes with IOS securityPriyanka Aash
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Andrey Karpov
 
Java Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoJava Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoValeriia Maliarenko
 

Ähnlich wie SyScan 2015 Bonus Slides - death of the vmsize=0 dyld trick (20)

Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...
Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...
Over-the-Air: How we Remotely Compromised the Gateway, BCM, and Autopilot ECU...
 
Safe Clearing of Private Data
Safe Clearing of Private DataSafe Clearing of Private Data
Safe Clearing of Private Data
 
Fedor Polyakov - Optimizing computer vision problems on mobile platforms
Fedor Polyakov - Optimizing computer vision problems on mobile platforms Fedor Polyakov - Optimizing computer vision problems on mobile platforms
Fedor Polyakov - Optimizing computer vision problems on mobile platforms
 
SPU gameplay
SPU gameplaySPU gameplay
SPU gameplay
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
 
A Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and AllegroA Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and Allegro
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
The Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmThe Unicorn's Travel to the Microcosm
The Unicorn's Travel to the Microcosm
 
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019 Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
 
Databricks spark-knowledge-base-1
Databricks spark-knowledge-base-1Databricks spark-knowledge-base-1
Databricks spark-knowledge-base-1
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...
CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...
CONFidence 2018: Intel ME: Security keys Genealogy, Obfuscation and other Mag...
 
Behind the scenes with IOS security
Behind the scenes with IOS securityBehind the scenes with IOS security
Behind the scenes with IOS security
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
 
Java Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoJava Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey Kovalenko
 
Quick Wins
Quick WinsQuick Wins
Quick Wins
 

Kürzlich hochgeladen

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Kürzlich hochgeladen (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

SyScan 2015 Bonus Slides - death of the vmsize=0 dyld trick

  • 1. death of the vmsize=0 dyld trick (one more way to persist on your iPhone killed) SyScan 2015 Bonus Slides Stefan Esser <stefan.esser@sektioneins.de> http://www.sektioneins.de
  • 2. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  Who am I? Stefan Esser • from Cologne / Germany • in information security since 1998 • invested in PHP security from 2001 to 20xx • since 2010 focused on iPhone security (ASLR/jailbreak) • founder of SektionEins GmbH 2
  • 3. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  Introduction • at SyScan 2015 I made a talk about • how Apple failed to fix vulnerabilities used in iOS 678 jailbreaks over and over again • how and why Chinese jailbreak teams took over the jb scene in 2014
 • during the talk I discussed “Patient ALPHA” an incomplete code signing bug that Apple failed to analyse correctly and therefore had to issue 4 security updates for • during the talk I also promised to disclose another incomplete code signing vulnerability that Apple closed by accident with their patches for “Patient ALPHA” • we should be fair in information security and not only discuss fail, but also 
 show how they killed a bug (even if they most probably did not know about it) 3
  • 4. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  Incomplete Codesigning • simplified: • if a page is not executable a missing code sig is not a problem • if a page is executable there must be a code sig on first access • prior to iOS 5 therefore jailbreaks would use ALL DATA dylibs to exploit dyld via various meta-data structures • around the end of iOS 4 Apple added checks to dyld to enforce load commands are in an executable segment • therefore while header parsing (first access) code sig is required 4
  • 5. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  mach-o dynamic library loading • mach-o dynamic libraries loaded by dyld • load commands describe i.a. layout of segments in memory • virtual address and 
 virtual size of segments • file position and 
 file size of segment 5
  • 6. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  Wait a second … • actually it is not that simple • mach-o header is first loaded into stack • initial LC parse is performed to collect info • this info is used to map the file into memory • segments are touched to enforce code sig • another LC parse is performed to make dyld use the mach-o header from paged memory • more and more parsing 6
  • 7. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  TOCTOU Problems • this chain of events has some TOCTOU (Time of Check Time of Use) problems • attacking the code flow between sniffLoadCommands and crashIfInvalidCodesignature • tricking e.g. mapSegments • evad3rs tricked that function first, but they went after replacing segment mappings or stripping the X flag • however there was a 0-day that tricked this whole logic in a different way 7
  • 8. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  mapSegments • mapSegments goes through the list of known segments and maps them one by one • count of segments is calculated inside the sniffLoadCommands functions • mapSegments fully relies on that count • apparently there will be problems if sniffLoadCommands is counting the segments wrong 8
  • 9. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  sniffLoadCommands and Segment Counting • code inside sniffLoadCommands counts LC_SEGMENT_COMMAND commands • but it ignores segments if their VMSIZE is 0 9 switch (cmd->cmd) { case LC_DYLD_INFO: case LC_DYLD_INFO_ONLY: *compressed = true; break; case LC_SEGMENT_COMMAND: segCmd = (struct macho_segment_command*)cmd; // ignore zero-sized segments if ( segCmd->vmsize != 0 ) *segCount += 1; // <rdar://problem/7942521> all load commands must be in an executable segment if ( context.codeSigningEnforced && (segCmd->fileoff < mh->sizeofcmds) && (segCmd->filesize != 0) ) { if ( (segCmd->fileoff != 0) || (segCmd->filesize < (mh->sizeofcmds+sizeof(macho_header))) ) dyld::throwf("malformed mach-o image: segment %s does not span al…”, segCmd->segname);
  • 10. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  ImageLoaderMachO::ImageLoaderMachO (I) • inside the constructor of ImageLoaderMachO the addresses of the LC_SEGMENT_COMMAND load commands are put into a cache • this is done to easier traverse through the list of segments • the code also ignores segments with a VMSIZE=0 • IMPORTANT: this means that if the segment containing load commands has vmsize=0 it is not in list of segments and will not be traversed by later code 10 // construct SegmentMachO object for each LC_SEGMENT cmd using "placement new" to put // each SegmentMachO object in array at end of ImageLoaderMachO object const uint32_t cmd_count = mh->ncmds; const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; const struct load_command* cmd = cmds; for (uint32_t i = 0, segIndex=0; i < cmd_count; ++i) { if ( cmd->cmd == LC_SEGMENT_COMMAND ) { const struct macho_segment_command* segCmd = (struct macho_segment_command*)cmd; // ignore zero-sized segments if ( segCmd->vmsize != 0 ) { // record offset of load command segOffsets[segIndex++] = (uint32_t)((uint8_t*)segCmd - fMachOData); } } cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); }
  • 11. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  ImageLoaderMachO::ImageLoaderMachO (II) • IMPORTANT: internally all accesses to the mach-o header go through fMachOData • so initially it is set to the stack 
 (which is not executable and therefore requires no code signature) • later on it is supposed to be pointing to the mapped executable segment containing the load commands 11 // construct SegmentMachO object for each LC_SEGMENT cmd using "placement new" to put // each SegmentMachO object in array at end of ImageLoaderMachO object const uint32_t cmd_count = mh->ncmds; const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)]; const struct load_command* cmd = cmds; for (uint32_t i = 0, segIndex=0; i < cmd_count; ++i) { if ( cmd->cmd == LC_SEGMENT_COMMAND ) { const struct macho_segment_command* segCmd = (struct macho_segment_command*)cmd; // ignore zero-sized segments if ( segCmd->vmsize != 0 ) { // record offset of load command segOffsets[segIndex++] = (uint32_t)((uint8_t*)segCmd - fMachOData); } } cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); }
  • 12. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  mapSegments mapping segments • mapSegments traverses ONLY the cached list of LC_SEGMENT_COMMANDS • any segment that had a vmsize=0 will never be mapped (because they are not in there) 12 // map in all segments for(unsigned int i=0, e=segmentCount(); i < e; ++i) { vm_offset_t fileOffset = segFileOffset(i) + offsetInFat; vm_size_t size = segFileSize(i); uintptr_t requestedLoadAddress = segPreferredLoadAddress(i) + slide; ... void* loadAddress = xmmap((void*)requestedLoadAddress, size, protection, MAP_FIXED | MAP_PRIVATE, fd, fileOffset); if ( loadAddress == ((void*)(-1)) ) { dyld::throwf("mmap() error %d at address=0x%08lX, size=0x%08lX segment=%s in Segment::map() mapping %s", errno, requestedLoadAddress, (uintptr_t)size, segName(i), getPath()); } } … }
  • 13. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  crashIfInvalidCodeSignature • because the executable segment with the load commands had vmsize=0 
 it is never in the segment list and therefore never touched in here • therefore there is no crash on invalid code signature 13 int ImageLoaderMachO::crashIfInvalidCodeSignature() { // Now that segments are mapped in, try reading from first executable segment. // If code signing is enabled the kernel will validate the code signature // when paging in, and kill the process if invalid. for(unsigned int i=0; i < fSegmentsCount; ++i) { if ( (segFileOffset(i) == 0) && (segFileSize(i) != 0) ) { // return read value to ensure compiler does not optimize away load int* p = (int*)segActualLoadAddress(i); return *p; } } return 0; }
  • 14. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  parseLoadCmds • is called by instantiateFinish • it should set the fMachOData pointer to the mapped executable segment • but because our LC segment with vmsize=0 is never in that list this never happens • this means fMachOData keeps pointing to the stack copy of mach-o header • there will never be access to an 
 executable segment = code signing bypassed = WIN !!! 14 void ImageLoaderMachO::parseLoadCmds() { // now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide for(unsigned int i=0; i < fSegmentsCount; ++i) { ... // some segment always starts at beginning of file and contains mach_header and .. if ( (segFileOffset(i) == 0) && (segFileSize(i) != 0) ) { fMachOData = (uint8_t*)(segActualLoadAddress(i)); } }
  • 15. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  Conclusion • it was trivial to bypass the dynamic linkers security checks • the trick was to give the load command segment a vmsize=0 • Apple accidentally killed that trick by enforcing that vmsize > filesize • one less way attackers can use to persist on iDevices to surveil you • while this fix is most probably an accident Apple did good here :) 15
  • 16. Stefan Esser • death of the vmsize=0 trick - SyScan Bonus Slides • March 2015 •  Questions ? 16