SlideShare a Scribd company logo
1 of 29
Download to read offline
Cinema time!
Andrey Labunets
Nikita Tarakanov
Hexacon
15th of October 2022
Paris, France
#WhoWeAre
• Nikita Tarakanov is an independent security researcher. He has
worked as a security researcher in Positive Technologies, Vupen
Security, Intel corporation and Huawei. He likes writing exploits,
especially for OS kernels. He won the PHDays Hack2Own contest
in 2011 and 2012. He has published a few papers about kernel
mode drivers and their exploitation. He is currently engaged in
reverse engineering research and vulnerability search automation.
• Andrey Labunets is a security researcher with more than a decade
of experience in vulnerability research and reverse engineering.
Agenda
• Video decoding subsystem overview
• AppleAVD internals and attack surface
• Previously disclosed vulnerabilities & Exploitation
• Fuzzing approach
• Results
• Discussion
• Q&A
Video decoding subsystem
macOS Monterey
AppleAVD kext
AppleVideoDecoder
VTDecoderXPCService
usermode
kernel
Video decoding subsystem
• Out of scope today - hardware components
• Main focus is AppleAVD kext internals on
macOS Monterey
• You can
fi
nd some info on AVD hardware here:
• https://twitter.com/rqou_/status/1577967077955993600
• https://github.com/rqou/m1-avd-reverse-engineering/blob/main/avd_emu.py
AppleAVD internals
Codebase overview
• AppleAVD - one of the largest kexts in
macOS
• ~120 KLOC in IDA decompiler
• Large part of this codebase are actual
decoders, which process parts of
media input in kernel space
AppleAVD internals
Codebase overview
• AppleAVD - one of the largest kexts in
macOS
• ~120 KLOC in IDA decompiler
• Large part of this codebase are actual
decoders, which process parts of
media input in kernel space
AppleAVD internals
Entry points (external methods)
• Accessed through AppleAVDUserClient
• Most interesting external methods:
• AppleAVDUserClient::createDecoder
• AppleAVDUserClient::decodeFrameFig
• Message passing between user / kernel
(media data, NAL units, etc)
• IOSurface
AppleAVD internals
Entry points (external methods)
• This set of external methods covers most of the kext functionality:
• AppleAVDUserClient::setCallback
• AppleAVDUserClient::createDecoder
• AppleAVDUserClient::setCryptSession
• AppleAVDUserClient::decodeFrameFig
AppleAVD internals
createDecoder
• Sets up one of 3 decoders of our choice
AppleAVD internals
decodeFrameFig
• Processes plaintext frames…
AppleAVD internals
decodeFrameFig -> decodeFrameFigHelper_DecryptFrame
• Processes plaintext frames…
• …and DRM content
• Likely it’s FairPlay Streaming
• FairPlay code obfuscation
complicates some analysis
AppleAVD internals
setCryptSession
• Allocates data bu
ff
ers for
(decrypted?) data, initializes
session parameters
• From AppleVideoDecoder:
AppleAVD attack surface
• AppleAVD media processing is performed with stateless decoders and
stateful logic
• Stateless components might be easier to attack remotely
• Attacking stateful logic might require more control over kernel objects,
achieved with local privilege escalation (LPE)
AppleAVD attack surface
• To
fi
nd or rule out the most straightforward remote vectors, we extracted
stateless components and tested them directly with a coverage-guided fuzzer
• To investigate the rest, we reviewed stateful parts by reconstructing the logic
of external methods and reviewing object initialization and interaction
between components
• We left all
fi
rmware and hardware vectors out of scope
Fuzzing decoders
• AppleAVD decoders process media data, such as Network Abstraction Layer
(NAL) units, parameter sets (SPS, PPS)
• This code is implemented in CAVDAvcDecoder, CAVDLghDecoder,
CAVDHevcDecoder
• Parsing is done inside virtual VAStartDecode and VADecodeFrame
Fuzzing decoders
Seed corpus generation
• First, we fuzzed
ff
mpeg with a small set of publicly available templates
•
ff
mpeg fuzzer generated a su
ffi
cient seed corpus for AppleAVD
• AppleAVD expects NAL units in a special format:
• <next_NAL_unit_o
ff
set><current_NAL_unit>…
Fuzzing decoders
Target code setup and fuzzing
• We build the target from extracted IDA decompiler pseudocode
• In our experiments with AppleAVD and other macOS subsystems, the
control
fl
ow does not di
ff
er from the original machine code
• A tiny shim handles IDA types, reimplements ARM intrinsics and selected
pieces of kernel library code
• Fuzzing ~3KLOC of CAVDAVCDecoder+AVC_RBSP with AFL++ resulted in
a single unexploitable crash (an artifact of fuzzing setup) and 96% coverage
Fuzzing decoders
Road not taken - alternative fuzzing setup
• Another approach by Junzhi Lu, Xindi
Wang, Ju Zhuto runs kexts in user
mode with a custom macho loader for
debugging, but could be useful for
fuzzing too.
• Our approach with code extraction
gives source code-level
fl
exibility to
fuzz selected code paths with debug
symbols.
https://github.com/pwn0rz/fairplay_research
Manual analysis
• Analysis of 3 external
methods revealed an issue
with inconsistent checks
• More details to come
AppleAVD attack surface
• Results:
• Fuzzed one of three decoders - 1 non-exploitable crash (fuzzing setup at fault), not
an issue
• Reviewed control
fl
ow and interaction between 3 external methods - 1 unveri
fi
ed
fi
nding, possibly an issue
• Limited results suggests interaction between stateful components in AVD is more
error-prone
Previously discovered vulnerabilities
• CVE-2018-4348 - memory corruption
• CVE-2020-9958 - out-of-bound write
• CVE-2022-22675 - overflow in parseHRD
• CVE-2022-32788 - overflow in parseSliceHeader
Overflow in parseHRD
• cpb_cnt_minus1 = Read_byte_from_stream();
• *HRD_data_in_spsList = cpb_cnt_minus1;
• HRD_arrays = HRD_data_in_spsList + 0x104;
• Index = 0
• do {
• *(_DWORD *)&HRD_arrays[4 * index - 0x100] = Read_dword_from_stream();
• *(_DWORD *)&HRD_arrays[4 * index - 0x80] = Read_dword_from_stream();
• HRD_arrays[index] = = Read_byte_from_stream();
• } while ( index++ < *HRD_data_in_spsList );
Overflow in parseHRD
• Size of array is 0x20 elements we can copy up-to 0x100 elements
• We can overflow adjacent element in spsList array
• We can overflow adjacent memory to spsList array (first element in ppsList)
Overflow in parseSliceHeaders
• counter_oob = 0;
• while ( 1 ){
• *((_BYTE *)SliceHeaderBuffer + counter_oob + 47) = Read_byte_from_stream();
• *((_DWORD *)SliceHeaderBuffer + counter_oob + 55) = Read_dword_from_stream();
• *((_DWORD *)SliceHeaderBuffer + counter_oob + 21) = Read_dword_from_stream();
• counter_oob++;
• If ( *((_BYTE *)SliceHeaderBuffer + counter_oob + 47) == 0x3 ) {
• break;
• }
• }
Overflow in parseSliceHeader
• Size of SliceHeader buffer is 0x480 located in CAVDAvcDecoder object
• We can overflow adjacent fields in CAVDAvcDecoder object
• CAVDAvcDecoder object is huge (0x8642B0 byes) in KHEAP_KEXT
• We can spray CAVDAvcDecoder objects and smash pointers in it
• Problem is that we have to win race between using vtable (PAC) and pointers
to other objects
Discussion
• AppleAVD runs about ~100KLOC of parsers in kernel on all incoming
media.
• Can this functionality be moved to user land instead or isolated?
• Some input validation is spread across multiple external methods,
processing is partially performed by an obfuscated FairplayKit.
• Can we even inspect the security of Apple media pipeline in any
meaningful way?
Thanks
• Thanks to Max Dmitriev (I_Greek) and to Berk Cem Göksel (@berkcgoksel)!
• Questions?
cinema_time_new.pdf

More Related Content

What's hot

Security Onion - Brief
Security Onion - BriefSecurity Onion - Brief
Security Onion - Brief
Ashley Deuble
 
Big Data in Real-Time at Twitter
Big Data in Real-Time at TwitterBig Data in Real-Time at Twitter
Big Data in Real-Time at Twitter
nkallen
 
Building a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDBBuilding a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDB
MongoDB
 
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Opersys inc.
 

What's hot (20)

Everything you need to know about GraalVM Native Image
Everything you need to know about GraalVM Native ImageEverything you need to know about GraalVM Native Image
Everything you need to know about GraalVM Native Image
 
HBase Storage Internals
HBase Storage InternalsHBase Storage Internals
HBase Storage Internals
 
Step-by-Step Introduction to Apache Flink
Step-by-Step Introduction to Apache Flink Step-by-Step Introduction to Apache Flink
Step-by-Step Introduction to Apache Flink
 
Netflix conductor
Netflix conductorNetflix conductor
Netflix conductor
 
Security Onion - Brief
Security Onion - BriefSecurity Onion - Brief
Security Onion - Brief
 
2022 OWASP AppSec USA Keynote
2022 OWASP AppSec USA Keynote2022 OWASP AppSec USA Keynote
2022 OWASP AppSec USA Keynote
 
Time-Series Apache HBase
Time-Series Apache HBaseTime-Series Apache HBase
Time-Series Apache HBase
 
10 tk3193-ids
10 tk3193-ids10 tk3193-ids
10 tk3193-ids
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Big Data in Real-Time at Twitter
Big Data in Real-Time at TwitterBig Data in Real-Time at Twitter
Big Data in Real-Time at Twitter
 
Learning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessLearning AOSP - Android Booting Process
Learning AOSP - Android Booting Process
 
Building a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDBBuilding a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDB
 
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
 
Introduction to Spring Cloud
Introduction to Spring Cloud           Introduction to Spring Cloud
Introduction to Spring Cloud
 
Understanding jvm gc advanced
Understanding jvm gc advancedUnderstanding jvm gc advanced
Understanding jvm gc advanced
 
Porting Android
Porting AndroidPorting Android
Porting Android
 
Introduction To Git
Introduction To GitIntroduction To Git
Introduction To Git
 
Trace kernel code tips
Trace kernel code tipsTrace kernel code tips
Trace kernel code tips
 
Pentesting GraphQL Applications
Pentesting GraphQL ApplicationsPentesting GraphQL Applications
Pentesting GraphQL Applications
 
Prerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrencyPrerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrency
 

Similar to cinema_time_new.pdf

Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
DefCamp
 
SIMD inside and outside oracle 12c
SIMD inside and outside oracle 12cSIMD inside and outside oracle 12c
SIMD inside and outside oracle 12c
Laurent Leturgez
 
Embedded c & working with avr studio
Embedded c & working with avr studioEmbedded c & working with avr studio
Embedded c & working with avr studio
Nitesh Singh
 
Bh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slidesBh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slides
Matt Kocubinski
 

Similar to cinema_time_new.pdf (20)

openioc_scan - IOC scanner for memory forensics
openioc_scan - IOC scanner for memory forensicsopenioc_scan - IOC scanner for memory forensics
openioc_scan - IOC scanner for memory forensics
 
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
 
VB2013 - Security Research and Development Framework
VB2013 - Security Research and Development FrameworkVB2013 - Security Research and Development Framework
VB2013 - Security Research and Development Framework
 
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
Captain Hook: Pirating AVs to Bypass Exploit MitigationsCaptain Hook: Pirating AVs to Bypass Exploit Mitigations
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
 
Piratng Avs to bypass exploit mitigation
Piratng Avs to bypass exploit mitigationPiratng Avs to bypass exploit mitigation
Piratng Avs to bypass exploit mitigation
 
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
 
SIMD inside and outside oracle 12c
SIMD inside and outside oracle 12cSIMD inside and outside oracle 12c
SIMD inside and outside oracle 12c
 
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
[若渴計畫] Challenges and Solutions of Window Remote Shellcode[若渴計畫] Challenges and Solutions of Window Remote Shellcode
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
 
Dmitriy D1g1 Evdokimov - DBI Intro
Dmitriy D1g1 Evdokimov - DBI IntroDmitriy D1g1 Evdokimov - DBI Intro
Dmitriy D1g1 Evdokimov - DBI Intro
 
Slide Reverse Engineering an APT Malware targeting Vietnamese
Slide Reverse Engineering an APT Malware targeting VietnameseSlide Reverse Engineering an APT Malware targeting Vietnamese
Slide Reverse Engineering an APT Malware targeting Vietnamese
 
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
 
Advanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCONAdvanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCON
 
Deep hooks
Deep hooksDeep hooks
Deep hooks
 
Embedded c & working with avr studio
Embedded c & working with avr studioEmbedded c & working with avr studio
Embedded c & working with avr studio
 
Decompiling Android
Decompiling AndroidDecompiling Android
Decompiling Android
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution Toolkit
 
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangPractical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
 
02 direct3 d_pipeline
02 direct3 d_pipeline02 direct3 d_pipeline
02 direct3 d_pipeline
 
Bh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slidesBh ad-12-stealing-from-thieves-saher-slides
Bh ad-12-stealing-from-thieves-saher-slides
 

Recently uploaded

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 

cinema_time_new.pdf

  • 1. Cinema time! Andrey Labunets Nikita Tarakanov Hexacon 15th of October 2022 Paris, France
  • 2. #WhoWeAre • Nikita Tarakanov is an independent security researcher. He has worked as a security researcher in Positive Technologies, Vupen Security, Intel corporation and Huawei. He likes writing exploits, especially for OS kernels. He won the PHDays Hack2Own contest in 2011 and 2012. He has published a few papers about kernel mode drivers and their exploitation. He is currently engaged in reverse engineering research and vulnerability search automation. • Andrey Labunets is a security researcher with more than a decade of experience in vulnerability research and reverse engineering.
  • 3. Agenda • Video decoding subsystem overview • AppleAVD internals and attack surface • Previously disclosed vulnerabilities & Exploitation • Fuzzing approach • Results • Discussion • Q&A
  • 4. Video decoding subsystem macOS Monterey AppleAVD kext AppleVideoDecoder VTDecoderXPCService usermode kernel
  • 5. Video decoding subsystem • Out of scope today - hardware components • Main focus is AppleAVD kext internals on macOS Monterey • You can fi nd some info on AVD hardware here: • https://twitter.com/rqou_/status/1577967077955993600 • https://github.com/rqou/m1-avd-reverse-engineering/blob/main/avd_emu.py
  • 6. AppleAVD internals Codebase overview • AppleAVD - one of the largest kexts in macOS • ~120 KLOC in IDA decompiler • Large part of this codebase are actual decoders, which process parts of media input in kernel space
  • 7. AppleAVD internals Codebase overview • AppleAVD - one of the largest kexts in macOS • ~120 KLOC in IDA decompiler • Large part of this codebase are actual decoders, which process parts of media input in kernel space
  • 8. AppleAVD internals Entry points (external methods) • Accessed through AppleAVDUserClient • Most interesting external methods: • AppleAVDUserClient::createDecoder • AppleAVDUserClient::decodeFrameFig • Message passing between user / kernel (media data, NAL units, etc) • IOSurface
  • 9. AppleAVD internals Entry points (external methods) • This set of external methods covers most of the kext functionality: • AppleAVDUserClient::setCallback • AppleAVDUserClient::createDecoder • AppleAVDUserClient::setCryptSession • AppleAVDUserClient::decodeFrameFig
  • 10. AppleAVD internals createDecoder • Sets up one of 3 decoders of our choice
  • 12. AppleAVD internals decodeFrameFig -> decodeFrameFigHelper_DecryptFrame • Processes plaintext frames… • …and DRM content • Likely it’s FairPlay Streaming • FairPlay code obfuscation complicates some analysis
  • 13. AppleAVD internals setCryptSession • Allocates data bu ff ers for (decrypted?) data, initializes session parameters • From AppleVideoDecoder:
  • 14. AppleAVD attack surface • AppleAVD media processing is performed with stateless decoders and stateful logic • Stateless components might be easier to attack remotely • Attacking stateful logic might require more control over kernel objects, achieved with local privilege escalation (LPE)
  • 15. AppleAVD attack surface • To fi nd or rule out the most straightforward remote vectors, we extracted stateless components and tested them directly with a coverage-guided fuzzer • To investigate the rest, we reviewed stateful parts by reconstructing the logic of external methods and reviewing object initialization and interaction between components • We left all fi rmware and hardware vectors out of scope
  • 16. Fuzzing decoders • AppleAVD decoders process media data, such as Network Abstraction Layer (NAL) units, parameter sets (SPS, PPS) • This code is implemented in CAVDAvcDecoder, CAVDLghDecoder, CAVDHevcDecoder • Parsing is done inside virtual VAStartDecode and VADecodeFrame
  • 17. Fuzzing decoders Seed corpus generation • First, we fuzzed ff mpeg with a small set of publicly available templates • ff mpeg fuzzer generated a su ffi cient seed corpus for AppleAVD • AppleAVD expects NAL units in a special format: • <next_NAL_unit_o ff set><current_NAL_unit>…
  • 18. Fuzzing decoders Target code setup and fuzzing • We build the target from extracted IDA decompiler pseudocode • In our experiments with AppleAVD and other macOS subsystems, the control fl ow does not di ff er from the original machine code • A tiny shim handles IDA types, reimplements ARM intrinsics and selected pieces of kernel library code • Fuzzing ~3KLOC of CAVDAVCDecoder+AVC_RBSP with AFL++ resulted in a single unexploitable crash (an artifact of fuzzing setup) and 96% coverage
  • 19. Fuzzing decoders Road not taken - alternative fuzzing setup • Another approach by Junzhi Lu, Xindi Wang, Ju Zhuto runs kexts in user mode with a custom macho loader for debugging, but could be useful for fuzzing too. • Our approach with code extraction gives source code-level fl exibility to fuzz selected code paths with debug symbols. https://github.com/pwn0rz/fairplay_research
  • 20. Manual analysis • Analysis of 3 external methods revealed an issue with inconsistent checks • More details to come
  • 21. AppleAVD attack surface • Results: • Fuzzed one of three decoders - 1 non-exploitable crash (fuzzing setup at fault), not an issue • Reviewed control fl ow and interaction between 3 external methods - 1 unveri fi ed fi nding, possibly an issue • Limited results suggests interaction between stateful components in AVD is more error-prone
  • 22. Previously discovered vulnerabilities • CVE-2018-4348 - memory corruption • CVE-2020-9958 - out-of-bound write • CVE-2022-22675 - overflow in parseHRD • CVE-2022-32788 - overflow in parseSliceHeader
  • 23. Overflow in parseHRD • cpb_cnt_minus1 = Read_byte_from_stream(); • *HRD_data_in_spsList = cpb_cnt_minus1; • HRD_arrays = HRD_data_in_spsList + 0x104; • Index = 0 • do { • *(_DWORD *)&HRD_arrays[4 * index - 0x100] = Read_dword_from_stream(); • *(_DWORD *)&HRD_arrays[4 * index - 0x80] = Read_dword_from_stream(); • HRD_arrays[index] = = Read_byte_from_stream(); • } while ( index++ < *HRD_data_in_spsList );
  • 24. Overflow in parseHRD • Size of array is 0x20 elements we can copy up-to 0x100 elements • We can overflow adjacent element in spsList array • We can overflow adjacent memory to spsList array (first element in ppsList)
  • 25. Overflow in parseSliceHeaders • counter_oob = 0; • while ( 1 ){ • *((_BYTE *)SliceHeaderBuffer + counter_oob + 47) = Read_byte_from_stream(); • *((_DWORD *)SliceHeaderBuffer + counter_oob + 55) = Read_dword_from_stream(); • *((_DWORD *)SliceHeaderBuffer + counter_oob + 21) = Read_dword_from_stream(); • counter_oob++; • If ( *((_BYTE *)SliceHeaderBuffer + counter_oob + 47) == 0x3 ) { • break; • } • }
  • 26. Overflow in parseSliceHeader • Size of SliceHeader buffer is 0x480 located in CAVDAvcDecoder object • We can overflow adjacent fields in CAVDAvcDecoder object • CAVDAvcDecoder object is huge (0x8642B0 byes) in KHEAP_KEXT • We can spray CAVDAvcDecoder objects and smash pointers in it • Problem is that we have to win race between using vtable (PAC) and pointers to other objects
  • 27. Discussion • AppleAVD runs about ~100KLOC of parsers in kernel on all incoming media. • Can this functionality be moved to user land instead or isolated? • Some input validation is spread across multiple external methods, processing is partially performed by an obfuscated FairplayKit. • Can we even inspect the security of Apple media pipeline in any meaningful way?
  • 28. Thanks • Thanks to Max Dmitriev (I_Greek) and to Berk Cem Göksel (@berkcgoksel)! • Questions?