SlideShare a Scribd company logo
FINDING MEMORY
BUGS WITH THE
BSIDES DELHI 2018
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
$ WHOAMI
▸Siddharth Muralee (R3x)
▸Third year CSE @ Amrita Vishwa Vidyapeetham
▸CTF player - Team bi0s
▸Core organising team @ InCTF and InCTFj
▸Reverse Engineering/Exploit Development
▸GSoC ’18 with NetBSD
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
AGENDA
▸What are Sanitizers ?
▸What is an Address Sanitizer(ASan)?
▸How does the Address Sanitizer work?
▸The Kernel Address Sanitizer(KASan)
▸Bug report examples
▸Address Sanitizer vs other tools
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
WHAT IS A
SANITIZER?
▸ Programming tool to detect
computer program errors.
▸ Compiler Instrumented
▸ The various Sanitizers are :
▸ Address Sanitizer
▸ Memory Sanitizer
▸ Thread Sanitizer
▸ Undefined Behaviour Sanitizer
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
ADDRESS SANITIZER (ASAN)
▸Open source tool developed by Google.
▸Detects memory corruption bugs - Overflows and UAFs
▸Implemented in Clang, GCC and Xcode.
▸Easy to use just compile with -fsanitize=address.
▸Run-time library
▸A pretty Amazing Fuzzer aide
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
ADDRESS SANITIZER - PREREQUISITE
KNOWLEDGE
▸Shadow Memory
▸Storing metadata corresponding to each piece of
application data.
▸Each Address Mapped to a Shadow memory offset
▸Compiler Instrumentation
▸Compiler adds instructions during compilation which
allows some information
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
ADDRESS
SANITIZER
WORKING
▸Each Memory access is modified using compiler
instrumentation. Prevents unintended memory read/write.
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
ADDRESS SANITIZER WORKING
▸Run time library replaces malloc and free.
▸Memory around chunks (memory blocks allocated using
malloc) are poisoned. Prevents Heap overflow bugs.
▸Freed memory placed in quarantine list . Prevents Use-
after-free bugs from being missed.
▸Parts of the stack are also poisoned. This is to avoid Stack
overflow bugs
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
SHADOW MEMORY
▸ ASan maps 1 byte of Application data to 1 bit of shadow memory.
▸ Shadow bit
▸ 1 - Unaddressable
▸ 0 - Addressable
▸ Total Shadow region size = Application size / 8
▸ Each byte needs to be converted to the corresponding shadow
memory address.
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
SHADOW MEMORY
▸ Shadow Address = (Addr >> 3) +
Shadow offset.
▸ Shadow offset = 0x20000000
Address Addr >> 3
Shadow
Address
0xffffffff 0x1fffffff 0x3fffffff
0x00000000 0x00000000 0x20000000
DEMO
OFF BY ONE BUG
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
KERNEL ADDRESS SANITIZER (KASAN)
▸Gcc and Clang come with a build in option -fsanitize=kernel-
address
▸You can build the entire kernel with the Address Sanitizer
using the kernel config file.
▸Provides an API which the kernel has to implement.
▸Is implemented as a feature in the Linux Kernel, OS X and
now NetBSD !!!
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
IMPLEMENTING KASAN
▸Allocation and population of the Shadow buffer during boot
▸Need to write interceptors for kernel allocator functions to
update shadow memory. (Multiple allocators)
▸Kernel VA space needs to be properly managed.
▸Implement Quarantine lists in page allocation
▸Bug Reporting infrastructure needs to be written
KASAN WORKING
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES
ptr - points to 0xdeadbeef
shadow_ptr - shadow address
kmem_to_shadow - converts
kernel address to shadow offset.
NETBSD KASAN -VA SPACE MAPPING
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
Fix buffer overflow, detected by kASan.
ifconfig gif0 create
ifconfig gif0 up
[ 50.682919] kASan: Unauthorized Access In 0xffffffff80f22655: 
Addr 0xffffffff81b997a0 [8 bytes, read]
[ 50.682919] #0 0xffffffff8021ce6a in kasan_memcpy <netbsd>
[ 50.692999] #1 0xffffffff80f22655 in m_copyback_internal
<netbsd>
[ 50.692999] #2 0xffffffff80f22e81 in m_copyback <netbsd>
[ 50.692999] #3 0xffffffff8103109a in rt_msg1 <netbsd>
[ 50.692999] #4 0xffffffff8159109a in compat_70_rt_newaddrmsg1 <n
[ 50.692999] #5 0xffffffff81031b0f in rt_newaddrmsg <netbsd>
[ 50.692999] #6 0xffffffff8102c35e in rt_ifa_addlocal <netbsd>
[ 50.692999] #7 0xffffffff80a5287c in in6_update_ifa1 <netbsd>
[ 50.692999] #8 0xffffffff80a54149 in in6_update_ifa <netbsd>
[ 50.692999] #9 0xffffffff80a59176 in in6_ifattach <netbsd>
[ 50.692999] #10 0xffffffff80a56dd4 in in6_if_up <netbsd>
[ 50.692999] #11 0xffffffff80fc5cb8 in if_up_locked <netbsd>
[ 50.703622] #12 0xffffffff80fcc4c1 in ifioctl_common <netbsd>
[ 50.703622] #13 0xffffffff80fde694 in gif_ioctl <netbsd>
[ 50.703622] #14 0xffffffff80fcdb1f in doifioctl <netbsd>
FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER
BSIDES DELHI
ASAN VS OTHER TOOLS
ASan Memcheck(Valgrind) Mudflap
Technique
Compiler
Instrumentation
Dynamic Binary
Instrumentation
Compiler
Instrumentation
Slowdown 2x 20x 2x - 20x
Types of Bugs
Heap and Stack
overflows, UAF, UAR
UMR, Heap
overflows and UAFs
Heap overflows
and UAFs
UMR - Uninitialised Memory Reads
UAF - Use After Free
UAR - Use After Return
QUESTION
S?

More Related Content

What's hot

Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
Adrian Huang
 
Ext4 write barrier
Ext4 write barrierExt4 write barrier
Ext4 write barrier
Somdutta Roy
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernel
Adrian Huang
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
Adrian Huang
 
plProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancerplProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancer
elliando dias
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
艾鍗科技
 
Oracle Client Failover - Under The Hood
Oracle Client Failover - Under The HoodOracle Client Failover - Under The Hood
Oracle Client Failover - Under The Hood
Ludovico Caldara
 
ClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and PrivacyClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
Altinity Ltd
 
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of FacebookTech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
The Hive
 
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
Ali Haider
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
Carlos Sierra
 
RocksDB detail
RocksDB detailRocksDB detail
RocksDB detail
MIJIN AN
 
RocksDB compaction
RocksDB compactionRocksDB compaction
RocksDB compaction
MIJIN AN
 
Linux commands
Linux commandsLinux commands
Linux commands
Mannu Khani
 
Oracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLONOracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLON
Markus Michalewicz
 
Do we need Unsafe in Java?
Do we need Unsafe in Java?Do we need Unsafe in Java?
Do we need Unsafe in Java?
Andrei Pangin
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
mukul bhardwaj
 
MySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialMySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete Tutorial
Sveta Smirnova
 
File system
File systemFile system
File system
Mohd Arif
 
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder
 

What's hot (20)

Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
 
Ext4 write barrier
Ext4 write barrierExt4 write barrier
Ext4 write barrier
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernel
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
plProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancerplProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancer
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
Oracle Client Failover - Under The Hood
Oracle Client Failover - Under The HoodOracle Client Failover - Under The Hood
Oracle Client Failover - Under The Hood
 
ClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and PrivacyClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
 
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of FacebookTech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
 
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
 
RocksDB detail
RocksDB detailRocksDB detail
RocksDB detail
 
RocksDB compaction
RocksDB compactionRocksDB compaction
RocksDB compaction
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Oracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLONOracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLON
 
Do we need Unsafe in Java?
Do we need Unsafe in Java?Do we need Unsafe in Java?
Do we need Unsafe in Java?
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
MySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialMySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete Tutorial
 
File system
File systemFile system
File system
 
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
 

Similar to BSidesDelhi 2018: Finding Memory Bugs with the Address Sanitizer

ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
Altinity Ltd
 
Beyond porting
Beyond portingBeyond porting
Beyond porting
Cass Everitt
 
AI On the Edge: Model Compression
AI On the Edge: Model CompressionAI On the Edge: Model Compression
AI On the Edge: Model Compression
Apache MXNet
 
Data Grids with Oracle Coherence
Data Grids with Oracle CoherenceData Grids with Oracle Coherence
Data Grids with Oracle Coherence
Ben Stopford
 
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
DataStax
 
Scylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS Insight
Scylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS InsightScylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS Insight
Scylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS Insight
ScyllaDB
 
A miało być tak... bez wycieków
A miało być tak... bez wyciekówA miało być tak... bez wycieków
A miało być tak... bez wycieków
Konrad Kokosa
 
GS-4108, Direct Compute in Gaming, by Bill Bilodeau
GS-4108, Direct Compute in Gaming, by Bill BilodeauGS-4108, Direct Compute in Gaming, by Bill Bilodeau
GS-4108, Direct Compute in Gaming, by Bill Bilodeau
AMD Developer Central
 
Nsd, il tuo compagno di viaggio quando Domino va in crash
Nsd, il tuo compagno di viaggio quando Domino va in crashNsd, il tuo compagno di viaggio quando Domino va in crash
Nsd, il tuo compagno di viaggio quando Domino va in crash
Fabio Pignatti
 
Distributing Data The Aerospike Way
Distributing Data The Aerospike WayDistributing Data The Aerospike Way
Distributing Data The Aerospike Way
Aerospike, Inc.
 
Python at Ordnance Survey
Python at Ordnance SurveyPython at Ordnance Survey
Python at Ordnance Survey
OliviaWilson3
 
Week-13-Memory Managementggvgjjjbbbb.ppt
Week-13-Memory Managementggvgjjjbbbb.pptWeek-13-Memory Managementggvgjjjbbbb.ppt
Week-13-Memory Managementggvgjjjbbbb.ppt
TanyaSharma662971
 
Build an affordable Cloud Stroage
Build an affordable Cloud StroageBuild an affordable Cloud Stroage
Build an affordable Cloud Stroage
Alex Lau
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
Jyaasa Technologies
 
Data Privacy with Apache Spark: Defensive and Offensive Approaches
Data Privacy with Apache Spark: Defensive and Offensive ApproachesData Privacy with Apache Spark: Defensive and Offensive Approaches
Data Privacy with Apache Spark: Defensive and Offensive Approaches
Databricks
 
Scaling IO-bound microservices
Scaling IO-bound microservicesScaling IO-bound microservices
Scaling IO-bound microservices
Salo Shp
 
Virtual training optimizing the tick stack
Virtual training  optimizing the tick stackVirtual training  optimizing the tick stack
Virtual training optimizing the tick stack
InfluxData
 
[2017.03.18] hst binary training part 1
[2017.03.18] hst binary training   part 1[2017.03.18] hst binary training   part 1
[2017.03.18] hst binary training part 1
Chia-Hao Tsai
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic Continues
Altinity Ltd
 
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
 

Similar to BSidesDelhi 2018: Finding Memory Bugs with the Address Sanitizer (20)

ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
 
Beyond porting
Beyond portingBeyond porting
Beyond porting
 
AI On the Edge: Model Compression
AI On the Edge: Model CompressionAI On the Edge: Model Compression
AI On the Edge: Model Compression
 
Data Grids with Oracle Coherence
Data Grids with Oracle CoherenceData Grids with Oracle Coherence
Data Grids with Oracle Coherence
 
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
 
Scylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS Insight
Scylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS InsightScylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS Insight
Scylla Summit 2018: From SAP to Scylla - Tracking the Fleet at GPS Insight
 
A miało być tak... bez wycieków
A miało być tak... bez wyciekówA miało być tak... bez wycieków
A miało być tak... bez wycieków
 
GS-4108, Direct Compute in Gaming, by Bill Bilodeau
GS-4108, Direct Compute in Gaming, by Bill BilodeauGS-4108, Direct Compute in Gaming, by Bill Bilodeau
GS-4108, Direct Compute in Gaming, by Bill Bilodeau
 
Nsd, il tuo compagno di viaggio quando Domino va in crash
Nsd, il tuo compagno di viaggio quando Domino va in crashNsd, il tuo compagno di viaggio quando Domino va in crash
Nsd, il tuo compagno di viaggio quando Domino va in crash
 
Distributing Data The Aerospike Way
Distributing Data The Aerospike WayDistributing Data The Aerospike Way
Distributing Data The Aerospike Way
 
Python at Ordnance Survey
Python at Ordnance SurveyPython at Ordnance Survey
Python at Ordnance Survey
 
Week-13-Memory Managementggvgjjjbbbb.ppt
Week-13-Memory Managementggvgjjjbbbb.pptWeek-13-Memory Managementggvgjjjbbbb.ppt
Week-13-Memory Managementggvgjjjbbbb.ppt
 
Build an affordable Cloud Stroage
Build an affordable Cloud StroageBuild an affordable Cloud Stroage
Build an affordable Cloud Stroage
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Data Privacy with Apache Spark: Defensive and Offensive Approaches
Data Privacy with Apache Spark: Defensive and Offensive ApproachesData Privacy with Apache Spark: Defensive and Offensive Approaches
Data Privacy with Apache Spark: Defensive and Offensive Approaches
 
Scaling IO-bound microservices
Scaling IO-bound microservicesScaling IO-bound microservices
Scaling IO-bound microservices
 
Virtual training optimizing the tick stack
Virtual training  optimizing the tick stackVirtual training  optimizing the tick stack
Virtual training optimizing the tick stack
 
[2017.03.18] hst binary training part 1
[2017.03.18] hst binary training   part 1[2017.03.18] hst binary training   part 1
[2017.03.18] hst binary training part 1
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic Continues
 
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
 

Recently uploaded

Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 

Recently uploaded (20)

Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 

BSidesDelhi 2018: Finding Memory Bugs with the Address Sanitizer

  • 1. FINDING MEMORY BUGS WITH THE BSIDES DELHI 2018
  • 2. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI $ WHOAMI ▸Siddharth Muralee (R3x) ▸Third year CSE @ Amrita Vishwa Vidyapeetham ▸CTF player - Team bi0s ▸Core organising team @ InCTF and InCTFj ▸Reverse Engineering/Exploit Development ▸GSoC ’18 with NetBSD
  • 3. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI AGENDA ▸What are Sanitizers ? ▸What is an Address Sanitizer(ASan)? ▸How does the Address Sanitizer work? ▸The Kernel Address Sanitizer(KASan) ▸Bug report examples ▸Address Sanitizer vs other tools
  • 4. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI WHAT IS A SANITIZER? ▸ Programming tool to detect computer program errors. ▸ Compiler Instrumented ▸ The various Sanitizers are : ▸ Address Sanitizer ▸ Memory Sanitizer ▸ Thread Sanitizer ▸ Undefined Behaviour Sanitizer
  • 5. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI ADDRESS SANITIZER (ASAN) ▸Open source tool developed by Google. ▸Detects memory corruption bugs - Overflows and UAFs ▸Implemented in Clang, GCC and Xcode. ▸Easy to use just compile with -fsanitize=address. ▸Run-time library ▸A pretty Amazing Fuzzer aide
  • 6. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI ADDRESS SANITIZER - PREREQUISITE KNOWLEDGE ▸Shadow Memory ▸Storing metadata corresponding to each piece of application data. ▸Each Address Mapped to a Shadow memory offset ▸Compiler Instrumentation ▸Compiler adds instructions during compilation which allows some information
  • 7. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI ADDRESS SANITIZER WORKING ▸Each Memory access is modified using compiler instrumentation. Prevents unintended memory read/write.
  • 8. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI ADDRESS SANITIZER WORKING ▸Run time library replaces malloc and free. ▸Memory around chunks (memory blocks allocated using malloc) are poisoned. Prevents Heap overflow bugs. ▸Freed memory placed in quarantine list . Prevents Use- after-free bugs from being missed. ▸Parts of the stack are also poisoned. This is to avoid Stack overflow bugs
  • 9. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI SHADOW MEMORY ▸ ASan maps 1 byte of Application data to 1 bit of shadow memory. ▸ Shadow bit ▸ 1 - Unaddressable ▸ 0 - Addressable ▸ Total Shadow region size = Application size / 8 ▸ Each byte needs to be converted to the corresponding shadow memory address.
  • 10. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI SHADOW MEMORY ▸ Shadow Address = (Addr >> 3) + Shadow offset. ▸ Shadow offset = 0x20000000 Address Addr >> 3 Shadow Address 0xffffffff 0x1fffffff 0x3fffffff 0x00000000 0x00000000 0x20000000
  • 12. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI KERNEL ADDRESS SANITIZER (KASAN) ▸Gcc and Clang come with a build in option -fsanitize=kernel- address ▸You can build the entire kernel with the Address Sanitizer using the kernel config file. ▸Provides an API which the kernel has to implement. ▸Is implemented as a feature in the Linux Kernel, OS X and now NetBSD !!!
  • 13. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI IMPLEMENTING KASAN ▸Allocation and population of the Shadow buffer during boot ▸Need to write interceptors for kernel allocator functions to update shadow memory. (Multiple allocators) ▸Kernel VA space needs to be properly managed. ▸Implement Quarantine lists in page allocation ▸Bug Reporting infrastructure needs to be written
  • 14. KASAN WORKING FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES ptr - points to 0xdeadbeef shadow_ptr - shadow address kmem_to_shadow - converts kernel address to shadow offset.
  • 15. NETBSD KASAN -VA SPACE MAPPING FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES
  • 16. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI Fix buffer overflow, detected by kASan. ifconfig gif0 create ifconfig gif0 up [ 50.682919] kASan: Unauthorized Access In 0xffffffff80f22655: Addr 0xffffffff81b997a0 [8 bytes, read] [ 50.682919] #0 0xffffffff8021ce6a in kasan_memcpy <netbsd> [ 50.692999] #1 0xffffffff80f22655 in m_copyback_internal <netbsd> [ 50.692999] #2 0xffffffff80f22e81 in m_copyback <netbsd> [ 50.692999] #3 0xffffffff8103109a in rt_msg1 <netbsd> [ 50.692999] #4 0xffffffff8159109a in compat_70_rt_newaddrmsg1 <n [ 50.692999] #5 0xffffffff81031b0f in rt_newaddrmsg <netbsd> [ 50.692999] #6 0xffffffff8102c35e in rt_ifa_addlocal <netbsd> [ 50.692999] #7 0xffffffff80a5287c in in6_update_ifa1 <netbsd> [ 50.692999] #8 0xffffffff80a54149 in in6_update_ifa <netbsd> [ 50.692999] #9 0xffffffff80a59176 in in6_ifattach <netbsd> [ 50.692999] #10 0xffffffff80a56dd4 in in6_if_up <netbsd> [ 50.692999] #11 0xffffffff80fc5cb8 in if_up_locked <netbsd> [ 50.703622] #12 0xffffffff80fcc4c1 in ifioctl_common <netbsd> [ 50.703622] #13 0xffffffff80fde694 in gif_ioctl <netbsd> [ 50.703622] #14 0xffffffff80fcdb1f in doifioctl <netbsd>
  • 17. FINDING MEMORY BUGS WITH THE ADDRESS SANITIZER BSIDES DELHI ASAN VS OTHER TOOLS ASan Memcheck(Valgrind) Mudflap Technique Compiler Instrumentation Dynamic Binary Instrumentation Compiler Instrumentation Slowdown 2x 20x 2x - 20x Types of Bugs Heap and Stack overflows, UAF, UAR UMR, Heap overflows and UAFs Heap overflows and UAFs UMR - Uninitialised Memory Reads UAF - Use After Free UAR - Use After Return

Editor's Notes

  1. Third year CSE , Amrita , Kollam. CTF player with team bi0s mainly focusing on Reverse engineering and exploitation. I am also a core member of the organising team for InCTF and InCTF junior , CTFs bi0s conducts for college and school students in India. I did my Google Summer of Code this year with the Amazing NetBSD Foundation.
  2. This talk aims at introducing you to the sanitizer family and in specific the Address Sanitizer. We will be looking at how the address Sanitizer works. We will also be looking into the kernel address sanitizer and how its implemented. In the end we will compare the address sanitizer with some similar tools.
  3. The Sanitizer is a programming tool used to detect program errors. The technology behind sanitizers being compiler instrumentation. There are several sanitizers created for various purposes like the Address, Memory , Thread and the Undefined Behavior Sanitizer.
  4. Now let’s look at the Address Sanitizer - It is a open source tool developed by google which is mainly used to detect memory corruption bugs - such as Stack and Heap overflows, Use after Frees etc The sanitizer comes as a compiler option in Clang, Gcc and Xcode. In the user space its linked with the executable as a run time library. The features that the address sanitizer has makes it a pretty cool fuzzing aide that is widely used even in the OSS fuzz tool which google has.
  5. Okay before look deeper into the address Sanitizer there two concepts that are very vital. One of them is the concept of Shadow memory. Shadow memory is a additional memory that is used by the address sanitizer to keep track of what data is legal and what data is illegal. Here we store metadata corresponding to each piece of application data. This metadata is updated with each instruction and is used to determine whether a instruction is legal or not. Compiler instrumentation - This is the underlying technology behind the address sanitizer - The Compiler adds certain instruction during the compilation process which allows the program to update and check the shadow memory(metadata) at every necessary point.
  6. Now let’s look at how the address sanitizer works. During compilation each memory access (that is a memory read or write) is modified to have a check whether the address is poisoned or not. An address is said to be poisoned when the program is not supposed to be access it. If a poisoned memory is accessed then we report an error.
  7. When the Run time library for ASan is linked with the executable it replaces the malloc and free function provided by the libc with its own. This allows it to allocate chunks with the memory around it poisoned like a redzone. This is done to prevent heap overflow bugs since there is now no possibility to overflow and write to another chunk. Also the chunks that are freed are now put into a quarantine list. This means that a chunk that has been recently used would not be used for a long time unless there is a shortage of memory. This is done to make sure that Use after free bugs are seen. If the freed chunk is again malloc’d then we miss it.
  8. That’s the main features that the address sanitizer provides. Now let’s take a better look at the Shadow memory to find out how the mapping works. The address sanitizer maps 1 byte of the application memory to 1 bit of shadow memory. So eight bytes of application memory is taken care by 1 byte of metadata. This is indeed a significant amount of memory - that is we require 1/8th of the total application memory to manage the shadow region. Also during each check/updation the bytes need to converted to the corresponding shadow memory address. This means that the shadow memory conversion process should be pretty simple otherwise we waste a lot of time there.
  9. So the equation to get the shadow address is to take the addr and divide it by 8 (>> 3) and then add it to a predetermined address called the shadow offset. If you look at the diagram you can see the address 0xffff is mapped to - 0x3ffff 0x00 - 0x2000000 This determines the bounds of the shadow memory. The shadow region is marked in light blue. The region marked in red is the shadow address range of the shadow memory region - this region is not supposed to be used. Hence every address in the entire range has been mapped to a shadow region and this has been done in a very simple process.
  10. The kernel address sanitizer is the result of the build in option fsanitize = kernel address. The entire kernel can be built with the address sanitizer using the kernel config file. Sadly, implementing kernel address sanitizer is not very easy - the compiler can only provide a set of API’s which the kernel has to implement. The kernel address Sanitizer has been implemented in the linux kernel, Mac OS X and Now its also there in NetBSD.
  11. There are a lot of things that the OS must implement to get the API working perfectly. Since running the address sanitizer as a shared library isn’t viable in such a situation we need to modify the OS kernel itself. Several things that need to taken care are. Making sure that the Shadow buffer is allocated and populated during the boot process. This is vital since the allocator functions are also used during the boot process. The Memory allocators like slab, slob in linux need to be modified so that they update the shadow memory during allocation and freeing of pages The kernel Virtual Address Space also needs to be managed so that no other allocations happen in the memory region corresponding to the shadow memory. We will take a look at the kernel VA space with resepect to NetBSD in a short while. Also we need to implement the quarantine list for the same reason as earlier. In the case of the kernel the allocator allocates chunks that are the in essence pages. These pages have to undergo the same procedure. We also have to write the bug reporting infrastructure. Since the bugs are supposed to be printed out by the kernel - either in the kernel log or as a kernel crash.
  12. Okay let’s consider a example in the kernel space and see what happens. Assume the kernel code has a pointer in the address 0xdeadbeef. The address is dereferenced at some point of time and we are writing something into it. Since the kernel is compiled with KASAN the compiler now has added certain functions __asan_store(). asan_store checks if the address is poisoned - for this it first passes the address to the kmem to shadow function which converts the address to the shadow address as we saw earlier and then the check_memory_poisoned function checks whether the memory has been poisoned or not. If its poisoned then we call kasan_report error - report the bug and exit else we continue execution. Similar functions exist for kmalloc and other implementations.
  13. This is the virtual address space of the NetBSD kernel for the amd64 architecture. Here you can see that there are several sections in the kernel and there are pointers containing the address of that section. The shadow buffer takes up around 128 Tb of space when it comes to the 64 bit kernel. Hence we placed it in a hole that’s not been used for any other purposes. Also we have to deal with some regions in a certain way and the others slightly different. The kernel memory, the user land, page tables and the Module Map are areas we need to take care of.
  14. This is a example bug report of a bug we found in the NetBSD kernel with Kasan. As you can see the output is slightly different and we are printing it in the kernel log. Kasan_memcpy instead of memcpy.
  15. Compared to Mudflap