Making C Less Dangerous

Similar documents
Program Security and Vulnerabilities Class 2

Outline. Classic races: files in /tmp. Race conditions. TOCTTOU example. TOCTTOU gaps. Vulnerabilities in OS interaction

Buffer overflow background

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Software security, secure programming

Runtime Defenses against Memory Corruption

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I

Important From Last Time

Software Security II: Memory Errors - Attacks & Defenses

Software Security: Buffer Overflow Defenses

Kernel Self Protection

Important From Last Time

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Software Security: Buffer Overflow Attacks

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

My malloc: mylloc and mhysa. Johan Montelius HT2016

Fundamentals of Computer Security

Lecture 05 Integer overflow. Stephen Checkoway University of Illinois at Chicago

A program execution is memory safe so long as memory access errors never occur:

CSE 565 Computer Security Fall 2018

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

CSE 509: Computer Security

Lecture 2: C Programm

Thwarting unknown bugs: hardening features in the mainline Linux kernel

A brief introduction to C programming for Java programmers

Reflections on using C(++) Root Cause Analysis

Secure Software Development: Theory and Practice

Buffer overflow prevention, and other attacks

CSE 374 Programming Concepts & Tools

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

ECS 153 Discussion Section. April 6, 2015

MIPS Functions and Instruction Formats

Android Kernel Security

This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Buffer Overflows Defending against arbitrary code insertion and execution

CMPSC 497 Buffer Overflow Vulnerabilities

CSCE 548 Building Secure Software Buffer Overflow. Professor Lisa Luo Spring 2018

Distributed Systems. How do regular procedure calls work in programming languages? Problems with sockets RPC. Regular procedure calls

0x1A Great Papers in Computer Security

Heap Arrays. Steven R. Bagley

Stanford University Computer Science Department CS 295 midterm. May 14, (45 points) (30 points) total

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Programmer s Points to Remember:

Distributed Systems 8. Remote Procedure Calls

Identifying Memory Corruption Bugs with Compiler Instrumentations. 이병영 ( 조지아공과대학교

CNIT 127: Exploit Development. Ch 18: Source Code Auditing. Updated

Operating Systems. 18. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Spring /20/ Paul Krzyzanowski

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Lecture 9 Assertions and Error Handling CS240

Control-flow Enforcement Technology H.J. Lu. Intel November, 2018

EL2310 Scientific Programming

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

Buffer Overflow Attacks

Topics in Software Security Vulnerability

Programming refresher and intro to C programming

Undefined Behaviour in C

Software Security: Buffer Overflow Attacks (continued)

Secure Programming I. Steven M. Bellovin September 28,

Foundations of Network and Computer Security

CSE 227 Computer Security Spring 2010 S f o t ftware D f e enses I Ste St f e an f Sa v Sa a v g a e g

Language Security. Lecture 40

Pointers and References

UniSan: Proactive Kernel Memory Initialization to Eliminate Data Leakages

Software Security (cont.): Defenses, Adv. Attacks, & More

Defeat Exploit Mitigation Heap Attacks. compass-security.com 1

Computer Systems A Programmer s Perspective 1 (Beta Draft)

CSc 466/566. Computer Security. 20 : Operating Systems Application Security

Lecture 4 September Required reading materials for this class

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

CSCE 548 Building Secure Software Integers & Integer-related Attacks & Format String Attacks. Professor Lisa Luo Spring 2018

Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Review

Final CSE 131B Spring 2004

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Sandboxing Untrusted Code: Software-Based Fault Isolation (SFI)

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Security and Exploit Mitigation. CMSC Spring 2016 Lawrence Sebald

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Other array problems. Integer overflow. Outline. Integer overflow example. Signed and unsigned

CS-527 Software Security

New features in AddressSanitizer. LLVM developer meeting Nov 7, 2013 Alexey Samsonov, Kostya Serebryany

When an instruction is initially read from memory it goes to the Instruction register.

Lecture 08 Control-flow Hijacking Defenses

CSE 565 Computer Security Fall 2018

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Limitations of the stack

CS558 Programming Languages

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Baggy bounds with LLVM

Black Hat Webcast Series. C/C++ AppSec in 2014

Software Security: Buffer Overflow Defenses and Miscellaneous

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

DAY 3. CS3600, Northeastern University. Alan Mislove

C++ Undefined Behavior

Machine-Level Programming V: Unions and Memory layout

Secure C Coding...yeah right. Andrew Zonenberg Alex Radocea

Practical Malware Analysis

Machine-Level Programming V: Advanced Topics

Transcription:

Making C Less Dangerous Linux Security Summit August 27, 2018 Vancouver, Canada Kees ( Case ) Cook keescook@chromium.org @kees_cook https://outflux.net/slides/2018/lss/danger.pdf

Agenda Background Kernel Self Protection Project C as a fancy assembler Towards less dangerous C Variable Length Arrays are bad and slow Explicit switch case fall-through Always-initialized automatic variables Arithmetic overflow detection Hope for bounds checking Control Flow Integrity: forward edges Control Flow Integrity: backward edges Where are we now? How you can help @Rob_Russell

Kernel Self Protection Project https://kernsec.org/wiki/index.php/kernel_self_protection_project KSPP focuses on the kernel protecting the kernel from attack (e.g. refcount overflow) rather than the kernel protecting userspace from attack (e.g. execve brute force detection) but any area of related development is welcome Currently ~12 organizations and ~10 individuals working on about ~20 technologies Slow and steady

C as a fancy assembler: almost machine code The kernel wants to be as fast and small as possible At the core, kernel wants to do very architecture-specific things for memory management, interrupt handling, scheduling, No C API for setting up page tables, switching to 64-bit mode

C as a fancy assembler: undefined behavior The C langauge comes with some operational baggage, and weak standard libraries What are the contents of uninitialized variables? void pointers have no type yet we can call typed functions through them? assembly doesn t care: everything can be an address to call! Why does memcpy() have no max destination length argument? whatever was in memory from before now! just do what I say; memory areas are all the same! With undefined behavior, anything is possible! https://raphlinus.github.io/programming/rust/2018/08/17/undefined-behavior.html

Variable Length Arrays are bad Exhaust stack, linear overflow: write to things following it Jump over guard pages and write to things following it But easy to find with compiler flag: -Wvla size = 8192; char buf[size]; strcpy(buf, src, size); stack 1 stack 2 size = 8192; u8 array[size]; array[big] = foo; stack 1 guard page stack 2

Variable Length Arrays are slow While this seems conceptually sound: more instructions to change stack size, it seems like it would be hard to notice. But 13% speed up measured during lib/bch.c VLA removal: https://git.kernel.org/linus/02361bc77888 (Ivan Djelic)

Variable Length Arrays: stop it fixed-size array variable length array

Switch case fall-through: did I mean it? CWE-484 Omitted Break Statement in Switch Semantic weakness in C ( switch is just assembly test/jump) Commit logs with missing break statement : 67 Did they mean to leave out break;??

Switch case fall-through: new statement Use -Wimplicit-fallthrough to add a new switch statement Actually a comment, but is parsed by compilers now, following the lead of static checkers Mark all non-breaks with a fall through comment, for example https://git.kernel.org/linus/4597b62f7a60 (Gustavo A. R. Silva)

Always-initialized local variables: just do it CWE-200 Information Exposure, CWE-457 Use of Uninitialized Variable gcc -finit-local-vars not upstream Clang -fsanitize=init-local not upstream CONFIG_GCC_PLUGIN_ STRUCTLEAK (for structs with user pointers) STRUCTLEAK_BYREF (when passed into funcs) Soon, plugin to mimic -finit-local-vars too

Always-initialized local variables: switch gotcha warning: statement will never be executed [-Wswitch-unreachable]

Arithmetic overflow detection: gcc? gcc s -fsanitize=signed-integer-overflow (CONFIG_UBSAN) Only signed. Fast: in the noise. Big: warnings grow kernel image by 6% (aborts grow it by 0.1%) But we can use explicit single-operation helpers. To quote Rasmus Villemoes:

Arithmetic overflow detection: Clang :) Clang can do signed and unsigned instrumentation: -fsanitize=signed-integer-overflow -fsanitize=unsigned-integer-overflow

Bounds checking: explicit checking is slow :( Explicit checks for linear overflows of SLAB objects, stack, etc copy_{to,from}_user() checking: <~1% performance hit strcpy()-family checking: ~2% performance hit memcpy()-family checking: ~1% performance hit Can we get better APIs? strncpy() doesn t always NULL terminate, NULL pads entire destination strlcpy() reads source beyond max destination size strscpy() okay, I guess? How about memcpy() that takes (and updates?) destination remaining size?

Bounds checking: memory tagging :) Hardware memory tagging/coloring is so much faster! SPARC Application Data Integrity (ADI) ARM? Intel? char *buf; hes c t a buf = kmalloc(128, ); tag m r e t n oi /* 0x5beef0000 */ ok: p match s i m g a buf[40] = 0xaa; inter t o p : L I FA buf[130] = 0xbb; 128 byte alloc (tag 5): data 128 byte alloc (tag 3): data

Control Flow Integrity: indirect calls With memory W^X, gaining execution control needs to change function pointers saved in heap or stack, where all type information was lost! heap: int action_launch(int idx) { } int (*action)(struct thing *); int rc; action = saved_actions[idx]; rc = action(info); saved_actions[] my_action ed d r a forw ge backward edge stack: return address int my_action(struct thing *info) { stuff; and; things; return 0; }

CFI, forward edges: just call pointers :( Ignore function prototype Normally just a call to a memory address:

CFI, forward edges: enforce prototype :) Ignore function prototype Clang -fsanitize=cfi will check at runtime:

CFI, backward edges: two stacks Clang s Safe Stack Clang: -fsanitize=safe-stack regular stack: all local variables register spills return address local variables return address unsafe stack: buffers by-referenced vars etc safe stack: safe variables register spills return address

CFI, backward edges: shadow call stack Clang s Shadow Call Stack Clang: -fsanitize=shadow-call-stack Results in two stack registers: sp and unspilled x18 regular stack: all local variables register spills return address local variables return address sp stack: all local variables register spills etc x18 stack: return address return address

CFI, backward edges: hardware support Intel CET: hardware-based read-only shadow call stack Implicit use of otherwise read-only shadow stack during call and ret instructions ARM v8.3a Pointer Authentication ( signed return address ) New instructions: paciasp and autiasp Clang and gcc: -msign-return-address

Where is the Linux kernel now? Variable Length Arrays Explicit switch case fall-through Crying about performance hits Waiting (im)patiently for hardware support Control Flow Integrity: forward edges Memory allocations now doing explicit overflow detection Needs better kernel support for Clang and gcc Bounds checking Various partial options, needs more compiler work Arithmetic overflow detection Steady progress on full markings (745 of 2311 remain) Always-initialized automatic variables Nearly eradicated from kernel (only handful in crypto remain) Need Clang LTO support in kernel, but works on Android Control Flow Integrity: backward edges Shadow Call Stack works on Android Waiting (im)patiently for hardware support https://www.flickr.com/photos/wonderlane/5270711224

Challenges in Kernel Security Development Cultural: Conservatism, Responsibility, Sacrifice, Patience Technical: Complexity, Innovation, Collaboration Resource: Dedicated Developers, Reviewers, Testers, Backporters

Thoughts? Kees ( Case ) Cook keescook@chromium.org keescook@google.com kees@outflux.net https://outflux.net/slides/2018/lss/danger.pdf http://www.openwall.com/lists/kernel-hardening/ http://kernsec.org/wiki/index.php/kernel_self_protection_project