SlideShare a Scribd company logo
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

More Related Content

Similar to 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 Data
PVS-Studio
 
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
Eastern European Computer Vision Conference
 
SPU gameplay
SPU gameplaySPU gameplay
SPU gameplay
Slide_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_3
Raahul 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 Allegro
snowfarthing
 
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
Wim 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 Microcosm
Andrey 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-1
Rahul 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 code
Wim Godden
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
PVS-Studio
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
Andrey 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 security
Priyanka 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 Kovalenko
Valeriia Maliarenko
 

Similar to 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
 

Recently uploaded

By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 

Recently uploaded (20)

By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 

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