SlideShare a Scribd company logo
@pati_gallardo
Secure Programming
Practices in C++
@pati_gallardo Patricia Aas
NDC { Oslo } 2018
Patricia Aas - Vivaldi Browser
Programmer - mainly in C++
Currently : Vivaldi Technologies
Previously : Cisco Systems, Knowit, Opera Software
Master in Computer Science
Twitter : @pati_gallardo
Photos: pixabay.com - CC0
Undefined Behavior
Compiler Optimizations
@pati_gallardo
@pati_gallardo
Undefined Behaviour
undefined behavior
“Examples of undefined behavior are memory accesses outside of array bounds, signed
integer overflow, null pointer dereference, modification of the same scalar more than
once in an expression without sequence points, access to an object through a pointer of a
different type, etc. Compilers are not required to diagnose undefined behavior (although
many simple situations are diagnosed), and the compiled program is not required to do
anything meaningful.”
http://en.cppreference.com/w/cpp/language/ub
@pati_gallardo
- Don’t reason about undefined
behaviour
- Assume that it crashes or is
never executed
- Changing compiler, compiler
version or optimization level
can break your application
Undefined Behaviour
Infinite Loop (Undefined Behavior)
#include <iostream>
#include <complex>
using namespace std;
int main(void) {
complex<int> delta;
complex<int> mc[4] = {0};
for(int di = 0; di < 4; di++, delta = mc[di]) {
cout << di << endl;
}
}
@pati_gallardo
(Thanks to @shafikyaghmour) https://stackoverflow.com/questions/32506643/c-compilation-bug
Undefined
Behavior!
Infinite Loop (Undefined Behavior)
Can we run it?
https://godbolt.org/g/TDjM8h
@pati_gallardo
(Thanks to @shafikyaghmour) https://stackoverflow.com/questions/32506643/c-compilation-bug
@pati_gallardo Compiler Optimization
@pati_gallardo
The Case Of The Disappearing Memset
0) CWE-14: Compiler Removal of Code to Clear Buffers
void GetData(char *MFAddr) {
char pwd[64];
if (GetPasswordFromUser(pwd, sizeof(pwd))) {
if (ConnectToMainframe(MFAddr, pwd)) {
// Interaction with mainframe
}
}
memset(pwd, 0, sizeof(pwd)); // <- Removed by the optimizer
}
@pati_gallardo
SEI: MSC06-C. Beware of compiler optimizations
SEI: MEM03-C. Clear sensitive information stored in reusable resources
0) CWE-14: Compiler Removal of Code to Clear Buffers
Should we Godbolt this?
https://godbolt.org/g/FpEsht
@pati_gallardo
SEI: MSC06-C. Beware of compiler optimizations
SEI: MEM03-C. Clear sensitive information stored in reusable resources
Memset_s : Zeroing Memory
// Compliant Solution (C11)
memset_s(pwd, 0, sizeof(pwd));
// Windows Solution
SecureZeroMemory(pwd, sizeof(pwd));
@pati_gallardo
SEI: MSC06-C. Beware of compiler optimizations
SEI: MEM03-C. Clear sensitive information stored in reusable resources
Code is on GitHub:
https://github.com/patricia-gallardo/insecure-coding-examples
@pati_gallardo
@pati_gallardo

More Related Content

PDF
I love Automation
PDF
How to send gzipped requests with boto3
PDF
clara-rules
PDF
V8 javascript engine for フロントエンドデベロッパー
PDF
QCon São Paulo 2018
PDF
Locarise,reagent and JavaScript Libraries
PDF
Android 触って AWS 触って TV のお知らせ
PDF
Traefik on google kubernetes engine
I love Automation
How to send gzipped requests with boto3
clara-rules
V8 javascript engine for フロントエンドデベロッパー
QCon São Paulo 2018
Locarise,reagent and JavaScript Libraries
Android 触って AWS 触って TV のお知らせ
Traefik on google kubernetes engine

Similar to Undefined Behavior and Compiler Optimizations (NDC Oslo 2018) (20)

PDF
C++ The Principles of Most Surprise
PDF
Secure Programming Practices in C++ (NDC Oslo 2018)
PDF
Finding target for hacking on internet is now easier
PPTX
Developing with the Go client for Apache Kafka
PDF
Middy.js - A powerful Node.js middleware framework for your lambdas​
PDF
Docker and Django Meet For A Tango - London Meetup
PDF
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
PDF
Microservices Application Tracing Standards and Simulators - Adrians at OSCON
PDF
Querying 1.8 billion reddit comments with python
PPTX
drupal ci cd concept cornel univercity.pptx
ZIP
Google Developer Fest 2010
PDF
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
PPTX
OWASP Poland Day 2018 - Pedro Fortuna - Are your Java Script based protection...
PPTX
Azure from scratch part 4
PDF
Cleaning your architecture with android architecture components
ZIP
Building Web Apps Sanely - EclipseCon 2010
PDF
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
PPTX
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
PPTX
Expanding APIs beyond the Web
PDF
Security Tips to run Docker in Production
C++ The Principles of Most Surprise
Secure Programming Practices in C++ (NDC Oslo 2018)
Finding target for hacking on internet is now easier
Developing with the Go client for Apache Kafka
Middy.js - A powerful Node.js middleware framework for your lambdas​
Docker and Django Meet For A Tango - London Meetup
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Microservices Application Tracing Standards and Simulators - Adrians at OSCON
Querying 1.8 billion reddit comments with python
drupal ci cd concept cornel univercity.pptx
Google Developer Fest 2010
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
OWASP Poland Day 2018 - Pedro Fortuna - Are your Java Script based protection...
Azure from scratch part 4
Cleaning your architecture with android architecture components
Building Web Apps Sanely - EclipseCon 2010
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Expanding APIs beyond the Web
Security Tips to run Docker in Production
Ad

More from Patricia Aas (20)

PDF
The fundamental misunderstanding in Team Topologies
PDF
NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
PDF
Telling a story
PDF
Return Oriented Programming, an introduction
PDF
I can't work like this (KDE Academy Keynote 2021)
PDF
Dependency Management in C++ (NDC TechTown 2021)
PDF
Introduction to Memory Exploitation (Meeting C++ 2021)
PDF
Classic Vulnerabilities (MUCplusplus2022).pdf
PDF
Classic Vulnerabilities (ACCU Keynote 2022)
PDF
Introduction to Memory Exploitation (CppEurope 2021)
PDF
Thoughts On Learning A New Programming Language
PDF
Trying to build an Open Source browser in 2020
PDF
Trying to build an Open Source browser in 2020
PDF
DevSecOps for Developers, How To Start (ETC 2020)
PDF
The Anatomy of an Exploit (NDC TechTown 2019)
PDF
Elections: Trust and Critical Infrastructure (NDC TechTown 2019)
PDF
The Anatomy of an Exploit (NDC TechTown 2019))
PDF
Elections, Trust and Critical Infrastructure (NDC TechTown)
PDF
Survival Tips for Women in Tech (JavaZone 2019)
PDF
Embedded Ethics (EuroBSDcon 2019)
The fundamental misunderstanding in Team Topologies
NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
Telling a story
Return Oriented Programming, an introduction
I can't work like this (KDE Academy Keynote 2021)
Dependency Management in C++ (NDC TechTown 2021)
Introduction to Memory Exploitation (Meeting C++ 2021)
Classic Vulnerabilities (MUCplusplus2022).pdf
Classic Vulnerabilities (ACCU Keynote 2022)
Introduction to Memory Exploitation (CppEurope 2021)
Thoughts On Learning A New Programming Language
Trying to build an Open Source browser in 2020
Trying to build an Open Source browser in 2020
DevSecOps for Developers, How To Start (ETC 2020)
The Anatomy of an Exploit (NDC TechTown 2019)
Elections: Trust and Critical Infrastructure (NDC TechTown 2019)
The Anatomy of an Exploit (NDC TechTown 2019))
Elections, Trust and Critical Infrastructure (NDC TechTown)
Survival Tips for Women in Tech (JavaZone 2019)
Embedded Ethics (EuroBSDcon 2019)
Ad

Recently uploaded (20)

PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
AI in Product Development-omnex systems
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Transform Your Business with a Software ERP System
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Introduction to Artificial Intelligence
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
How Creative Agencies Leverage Project Management Software.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
AI in Product Development-omnex systems
Odoo Companies in India – Driving Business Transformation.pdf
ai tools demonstartion for schools and inter college
Wondershare Filmora 15 Crack With Activation Key [2025
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PTS Company Brochure 2025 (1).pdf.......
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Transform Your Business with a Software ERP System
Navsoft: AI-Powered Business Solutions & Custom Software Development
wealthsignaloriginal-com-DS-text-... (1).pdf
Introduction to Artificial Intelligence
Which alternative to Crystal Reports is best for small or large businesses.pdf
Odoo POS Development Services by CandidRoot Solutions
Understanding Forklifts - TECH EHS Solution
Adobe Illustrator 28.6 Crack My Vision of Vector Design
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf

Undefined Behavior and Compiler Optimizations (NDC Oslo 2018)

  • 2. Secure Programming Practices in C++ @pati_gallardo Patricia Aas NDC { Oslo } 2018
  • 3. Patricia Aas - Vivaldi Browser Programmer - mainly in C++ Currently : Vivaldi Technologies Previously : Cisco Systems, Knowit, Opera Software Master in Computer Science Twitter : @pati_gallardo Photos: pixabay.com - CC0
  • 6. undefined behavior “Examples of undefined behavior are memory accesses outside of array bounds, signed integer overflow, null pointer dereference, modification of the same scalar more than once in an expression without sequence points, access to an object through a pointer of a different type, etc. Compilers are not required to diagnose undefined behavior (although many simple situations are diagnosed), and the compiled program is not required to do anything meaningful.” http://en.cppreference.com/w/cpp/language/ub @pati_gallardo
  • 7. - Don’t reason about undefined behaviour - Assume that it crashes or is never executed - Changing compiler, compiler version or optimization level can break your application Undefined Behaviour
  • 8. Infinite Loop (Undefined Behavior) #include <iostream> #include <complex> using namespace std; int main(void) { complex<int> delta; complex<int> mc[4] = {0}; for(int di = 0; di < 4; di++, delta = mc[di]) { cout << di << endl; } } @pati_gallardo (Thanks to @shafikyaghmour) https://stackoverflow.com/questions/32506643/c-compilation-bug Undefined Behavior!
  • 9. Infinite Loop (Undefined Behavior) Can we run it? https://godbolt.org/g/TDjM8h @pati_gallardo (Thanks to @shafikyaghmour) https://stackoverflow.com/questions/32506643/c-compilation-bug
  • 11. @pati_gallardo The Case Of The Disappearing Memset
  • 12. 0) CWE-14: Compiler Removal of Code to Clear Buffers void GetData(char *MFAddr) { char pwd[64]; if (GetPasswordFromUser(pwd, sizeof(pwd))) { if (ConnectToMainframe(MFAddr, pwd)) { // Interaction with mainframe } } memset(pwd, 0, sizeof(pwd)); // <- Removed by the optimizer } @pati_gallardo SEI: MSC06-C. Beware of compiler optimizations SEI: MEM03-C. Clear sensitive information stored in reusable resources
  • 13. 0) CWE-14: Compiler Removal of Code to Clear Buffers Should we Godbolt this? https://godbolt.org/g/FpEsht @pati_gallardo SEI: MSC06-C. Beware of compiler optimizations SEI: MEM03-C. Clear sensitive information stored in reusable resources
  • 14. Memset_s : Zeroing Memory // Compliant Solution (C11) memset_s(pwd, 0, sizeof(pwd)); // Windows Solution SecureZeroMemory(pwd, sizeof(pwd)); @pati_gallardo SEI: MSC06-C. Beware of compiler optimizations SEI: MEM03-C. Clear sensitive information stored in reusable resources
  • 15. Code is on GitHub: https://github.com/patricia-gallardo/insecure-coding-examples @pati_gallardo