The heap sucks. The Stack is Back Jon Oberheide. Slide #2



Similar documents
Bypassing Browser Memory Protections in Windows Vista

Bypassing Memory Protections: The Future of Exploitation

There s a kernel security researcher named Dan Rosenberg whose done a lot of linux kernel vulnerability research

Review and Exploit Neglected Attack Surface in ios 8. Tielei Wang, Hao Xu, Xiaobo Chen of TEAM PANGU

Exploiting nginx chunked overflow bug, the undisclosed attack vector

Dynamic VM Monitoring using Hypervisor Probes

I Control Your Code Attack Vectors Through the Eyes of Software-based Fault Isolation. Mathias Payer, ETH Zurich

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Kernel Intrusion Detection System

ERNW Newsletter 51 / September 2015

Exploiting Trustzone on Android

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering

Buffer Overflows. Code Security: Buffer Overflows. Buffer Overflows are everywhere. 13 Buffer Overflow 12 Nov 2015

Applying Clang Static Analyzer to Linux Kernel

Introduction to Information Security

Eugene Tsyrklevich. Ozone HIPS: Unbreakable Windows

Defense in Depth: Protecting Against Zero-Day Attacks

Unix Security Technologies. Pete Markowsky <peterm[at] ccs.neu.edu>

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

telnetd exploit FreeBSD Telnetd Remote Exploit Für Compass Security AG Öffentliche Version 1.0 Januar 2012

Software Vulnerabilities

Reverse Engineering and Computer Security

Hotpatching and the Rise of Third-Party Patches

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

A Dozen Years of Shellphish From DEFCON to the Cyber Grand Challenge

CS Computer Security Thirteenth topic: System attacks. defenses

esrever gnireenigne tfosorcim seiranib

Chapter 15 Operating System Security

Advanced IBM AIX Heap Exploitation. Tim Shelton V.P. Research & Development HAWK Network Defense, Inc. tshelton@hawkdefense.com

Attacking Host Intrusion Prevention Systems. Eugene Tsyrklevich

Modern Binary Exploitation Course Syllabus

Lecture 22: C Programming 4 Embedded Systems

Custom Penetration Testing

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Linux Driver Devices. Why, When, Which, How?

The Linux Kernel: Process Management. CS591 (Spring 2001)

Windows XP SP3 Registry Handling Buffer Overflow

Linux Kernel Networking. Raoul Rivas

0days: How hacking really works. V 1.0 Jan 29, 2005 Dave Aitel dave@immunitysec.com

Securing your Virtual Datacenter. Part 1: Preventing, Mitigating Privilege Escalation

Turn the Page: Why now is the time to migrate off Windows Server 2003

Fine-Grained User-Space Security Through Virtualization. Mathias Payer and Thomas R. Gross ETH Zurich

MSc Computer Science Dissertation

Stack Overflows. Mitchell Adair

CORE SECURITY. Exploiting Adobe Flash Player in the era of Control Flow Guard. Francisco Falcon Black Hat Europe 2015 November 12-13, 2015

A Test Suite for Basic CWE Effectiveness. Paul E. Black.

Safety measures in Linux

Vulnerability Disclosure Guideline for Software Developers

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

COS 318: Operating Systems

SECURITY B-SIDES: ATLANTA STRATEGIC PENETRATION TESTING. Presented by: Dave Kennedy Eric Smith

1 Abstract Data Types Information Hiding

Operating Systems Design 16. Networking: Sockets

Database's Security Paradise. Joxean Koret

Sistemi Operativi. Lezione 25: JOS processes (ENVS) Corso: Sistemi Operativi Danilo Bruschi A.A. 2015/2016

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

sys socketcall: Network systems calls on Linux

Carnegie Mellon Virtual Memory of a Linux Process Process-specific data structs (ptables, Different for

Data Structure Reverse Engineering

Betriebssysteme KU Security

Configuring CoreNet Platform Cache (CPC) as SRAM For Use by Linux Applications

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Virtual Memory. How is it possible for each process to have contiguous addresses and so many of them? A System Using Virtual Addressing

Leak Check Version 2.1 for Linux TM

Practical taint analysis for protecting buggy binaries

static void insecure (localhost *unix)

Manual vs. Automated Vulnerability Assessment: A Case Study

Linux Kernel. Security Report

Web Application Security Payloads. Andrés Riancho Director of Web Security OWASP AppSec USA Minneapolis

A Survey of Parallel Processing in Linux

Why Do Software Assurance Tools Have Problems Finding Bugs Like Heartbleed?

Segmentation and Fragmentation

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

Source Code Security Analysis Tool Functional Specification Version 1.0

Intel TSX (Transactional Synchronization Extensions) Mike Dai Wang and Mihai Burcea

Toasterkit - A NetBSD Rootkit. Anthony Martinez Thomas Bowen

CSC 2405: Computer Systems II

COS 318: Operating Systems

How To Write Portable Programs In C

Embedded Systems. 6. Real-Time Operating Systems

CVE Adobe Flash Player Integer Overflow Vulnerability Analysis

Application-Level Debugging and Profiling: Gaps in the Tool Ecosystem. Dr Rosemary Francis, Ellexus

QUIRE: : Lightweight Provenance for Smart Phone Operating Systems

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

LLVMLinux: Embracing the Dragon

CSCE 465 Computer & Network Security

Coverity White Paper. Reduce Your Costs: Eliminate Critical Security Vulnerabilities with Development Testing

Mining for Bugs with Graph Database Queries

Get the Better of Memory Leaks with Valgrind Whitepaper

Transcription:

b

The heap sucks Slide #2

Heap vs. stack Excerpt from Objective quantitative scientific comparison of the heap and stack by Dr. Jono, PhD from the journal of Useless Computer Science: Heap: Stack: Complicated Easy Requires skillz Doesn't Bad connotation: heap of trash The 1%, elitist, pro-life, racist Good connotation: stack of bills Saves kittens from burning buildings Slide #3

Bringing the stack back What's left to exploit with the stack? Smashing? ROP'ing? Jacking? Let's exploit stack overflows! Slide #4

The stack is back A brief history of stack overflows Stack overflows in the Linux kernel Exploiting exotic stack overflows Discovering and mitigating stack overflows Slide #5

Fake stack overflows high address start of stack grows down NO! return addr some bullshit memcpy THIS IS A stack stack pointer STACK-BASED BUFFER OVERFLOW unused char buf[16]; end of stack low address Slide #6

Real stack overflows high address start of stack grows down stack stack pointer end of stack low address stack pointer Slide #7

Stack overflows Stack overflows Misuse of terminology Jono's definition: Stack pointer decremented beyond the intended bounds of the stack's allocated VMA. Types of overflows Incremental overflows Allocation overflows Slide #8

Incremental overflows start of stack Incremental overflows Deep call chains Recursion Frame 1 void func() { func(); } Frame 2 Frame 3 Frame 4 end of stack Frame 5 Frame 6 Slide #9

Allocation overflows start of stack Allocation overflows Frame 1 Frame 2 alloca(crap_load); Large local stack vars Dynamic allocations: VLAs, alloca(3) alloca stack space end of stack Slide #10

Exploiting stack overflows Stack overflows in userspace Not uncommon Lots of controllable (and uncontrollable) recursion Some use of C99 VLAs and alloca(3) Exploitable stack overflows Exploitable = more than DoS Quite rare! Slide #11

Trivia #1 What is one scenario where a userspace stack overflow might be exploitable? 1 855 FOR 0DAY Android Phone T-Shirt Phone Number Slide #12

Large MM vulns Slide #13

Stack overlap Slide #14

Real-world stack overflows Not a lot of real-world examples...maybe one? Xorg large MM vuln by Rafal @ ITL No guard page at end of stack on <= Linux 2.6.36 Allocate large pixmaps to exhaust address space Force a shm allocation adjacent to the stack Call recursive function to cause stack/shm overlap Write to the shm and therefore the Xorg stack Slide #15

Embedded platforms Limited memory limited stack stack overflows Slide #16

Remote kernel overflows? BSD IPComp kernel stack overflow Travis Normandy Recursive decompression in IP stack Exploitable? Ehhhh... Slide #17

The stack is back A brief history of stack overflows Stack overflows in the Linux kernel Exploiting exotic stack overflows Discovering and mitigating stack overflows Slide #18

Linux kernel stacks Each userspace thread is allocated a kernel stack Stores stack frames for kernel syscalls and other metadata Most commonly 8k, some distros use 4k THREAD_SIZE = 2*PAGE_SIZE = 2*4086 = 8192 high address grows down 4k/8k stack unused low address Slide #19

Metadata on kernel stack start of stack struct thread_info { struct task_struct *task; struct exec_domain *exec_domain; u32 flags; u32 status; u32 cpu; int preempt_count; mm_segment_t addr_limit; struct restart_block restart_block; void user *sysenter_return; #ifdef CONFIG_X86_32 unsigned long previous_esp; u8 supervisor_stack; #endif int uaccess_err; current_thread_info }; grows down unused thread_info thread_info struct is at the base of kstack! Slide #20

Exploiting stack overflows start of stack If we control an incremental or allocation stack overflow in the Linux kernel, we can cause our thread's kernel stack to collide with the thread_info structure. current_thread_info thread_info Slide #21

Targeting thread_info What would the overflow collide with? uaccess_err restart_block A function pointer, BINGO! addr_limit No security impact, but safe to clobber struct restart_block { long (*fn)(struct restart_block *); union {} /* safe to clobber */ }; access_ok()/ range_not_ok(): Test whether a block of memory is a valid user space address. Define u/k boundary, BONGO! addr + size > addr_limit.seg preempt_count.. task_struct Pretty sensitive members, avoid clobbering Slide #22

Controlling the clobber Can we control the clobbering value? Incremental overflow: tip of the stack, unlikely Allocation overflow: VLA values, maybe Good news, don't need much control! Two categories: Value represents a kernel space address Value > TASK_SIZE Value represents a user space address Value < TASK_SIZE Slide #23

Clobber Code Exec If value < TASK_SIZE Clobber restart_block fptr with userspace value mmap privesc payload at that address in userspace Trigger fptr via syscall(sys_restart_syscall); If value > TASK_SIZE Clobber addr_limit with a high kernel space value You can now pass copy_from_user()/access_ok() checks up to that kernel address So we can read(2) from a fd and write into kmem Slide #24

Vanilla exploitation We consider these vanilla stack overflows. thread_info clobbering technique Will work in the common case for Linux kernel stack overflows Example vuln @ CSAW CTF Controlled recursion with userspace value at tip of the stack http://jon.oberheide.org/blog/2011/11/27/csaw-ctf-2011-kernel-exploitation-challenge/ Slide #25

Architecture specifics x86_64 x86_32 Pretty clean, dedicated interrupt stacks Interrupt stack shared with process stack Less predictability, but more opportunity to trigger a stack overflow ARM, alpha, others restart_block is on end of thread_info :-) Slide #26

The stack is back A brief history of stack overflows Stack overflows in the Linux kernel Exploiting exotic stack overflows Discovering and mitigating stack overflows Slide #27

Real world vulnerability Let's look at a real-world Linux kernel stack overflow vulnerability. Two great bugs from Nelson CVE-2010-3848 CVE-2010-3850 And a bonus bug that will come into play later Econet packet family Stack overflow in econet_sendmsg() Slide #28

Vulnerable code int econet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) {... struct iovec iov[msg->msg_iovlen+1]; Oh snap! A VLA on the stack with an attacker controlled length! for (i = 0; i < msg->msg_iovlen; i++) {... iov[i+1].iov_base = base; iov[i+1].iov_len = iov_len;... } Hey, we (mostly) control the contents too! Game over, eh? Slide #29

Attempt #1 start of stack udp_sendmsg Attempt #1 inet_sendmsg sock_sendmsg Expand VLA to hit thread_info directly Overwrite restart_block/addr_limit with attacker controlled values Thwarted! econet_sendmsg VLA Subsequent function calls in sendmsg will clobber sensitive restart_block thread_info members addr_limit end of stack restart_block addr_limit sock_sendmsg thread_info Slide #30

Attempt #2 start of stack udp_sendmsg Attempt #2 inet_sendmsg sock_sendmsg Expand VLA to just above thread_info Overwrite using the stack frames of subsequent calls (sock_sendmsg) Semi-thwarted! Overwrite value is uncontrolled and a kernel space value so restart_block restart_block is no good addr_limit What about addr_limit? end of stack econet_sendmsg VLA sock_sendmsg call frames restart_block addr_limit thread_info Slide #31

Attempt #2 continued We can hit addr_limit with a value that represents a high kernel space value Overwrite of addr_limit occurs in sock_sendmsg call oldfs = get_fs(); set_fs(kernel_ds); err = sock_sendmsg(udpsock, &udpmsg, size); set_fs(oldfs); You can't be serious... addr_limit is being saved/restored before/after the sock_sendmsg call, nullifying our overwrite Slide #32

Attempt #2 continued We could try other subsequent function calls besides sock_sendmsg Cause error condition, return from econet_sendmsg early with a terminating mutex_unlock call. Eg: if (len + 15 > dev->mtu) { mutex_unlock(&econet_mutex); return -EMSGSIZE; } Write offsets of the stack frame don't align Pattern: chunks of two 8-byte writes w/kernel value Hit restart_block with kernel value (useless) or hit both addr_limit (good) and preempt_count (crash) Slide #33

Attempt #3 start of stack udp_sendmsg Attempt #3 inet_sendmsg sock_sendmsg Blow past thread_info and with VLA econet_sendmsg and write-back towards the end of the kernel stack if (!access_ok(verify_read, base, iov_len)) { mutex_unlock(&econet_mutex); return -EFAULT; Overwrite task_struct with an attacker controlled address } Ok, this is just insane... Yes, you can make a fake task_struct in userspace, but not in this century abort writes end of stack VLA task_struct write write write Slide #34

Need a different approach It's clear the thread_info technique is not going to work here due to extenuating circumstances If thread_info is out, what can we do? Nothing useful on the stack, but... Need some audience help here... Slide #35

Trivia #2 Any ideas of what to do if the thread_info technique isn't going to work? 1 855 FOR 0DAY Android Phone T-Shirt Phone Number Slide #36

Beyond our stack A thread's kstack doesn't exist in a vacuum Each kstack allocated from the buddy allocator esp kstack 1 esp esp kstack 2 kstack 3 Screw intra-stack exploitation, let's talk interstack exploitation Slide #37

Attacking adjacent kstacks In an allocation-based overflow, we can blow past the end of our stack and into an adjacent stack! Two big questions: We sort of did this with stackjacking self-discovery! How do we get two thread kernel stacks allocated adjacently? How do we sanely modify another thread's stack to gain code exec? We sort of did this with stackjacking Obergrope! Slide #38

Kernel stack disclosures kstack frame kstack frame foo.bar sensitive data... 1) process makes syscall and leaves sensitive data on kstack sensitive foo.leak data foo.baz 2) kstack is reused on subsequent syscall and struct overlaps with sensitive data struct foo { uint32_t bar; uint32_t leak; uint32_t baz; }; syscall() { struct foo; foo.bar = 1; foo.baz = 2; copy_to_user(foo); } 3) foo struct is copied to userspace, leaking 4 bytes of kstack through uninitialized foo.leak member Slide #39

Kernel stack self-discovery If we can leak an pointer to the kstack off the kstack, we can calculate the base address of the kstack 0xcdef2000 kstack frame 0xcdef1234 kstack_base = addr & ~(THREAD_SIZE 1); 0xdeadbeef 0xcdef1234 kstack_base = 0xcdef1234 & ~(8192 1) kstack_base = 0xcdef0000 0xcdef0000... We call this kstack self-discovery Slide #40

Writing the adjacent kstack start of stack 1 Getting adjacent kstacks Spawn children, have them self-discover their kstack address, spin until we get two adjacent allocations start of stack 2 Writing the adjacent stack Process #2 kstack needs to be in a stable predictable state Process #1 needs a sufficient landing zone to absorb mutex_unlock stack frame econet_sendmsg VLA thread_info write write write mutex_unlock write thread_info Slide #41

Sleepy syscalls are back Process #2 will enter a sleepy syscall Arbitrary sleeping to avoid dangerous race conditions with the overflow write While asleep, process #1 will overwrite a return address on process #2's kstack compat_sys_wait4 looks good Hey, same function we used for stackjacking! Large unused local stack vars to absorb the mutex_unlock stack frame Slide #42

Final exploit flow Achieve adjacent kstacks Process #2 goes to sleep Stack overflow in process #1 Overwrite return address on process #2 kernel stack start of stack 1 econet_sendmsg VLA start of stack 2 thread_info retaddr write write Process #2 wakes up mutex_unlock Process #2 returns to attacker control address Privilege escalation payload executed! compat_sys_wait4...sleep... write thread_info Slide #43

Demo DEMO TIME? http://jon.oberheide.org/files/half-nelson.c Slide #44

The stack is back A brief history of stack overflows Stack overflows in the Linux kernel Exploiting exotic stack overflows Discovering and mitigating stack overflows Slide #45

Trivia #3 What is one way to discover potential stack overflow vulnerabilities? 1 855 FOR 0DAY Android Phone T-Shirt Phone Number Slide #46

jono discovery method Ghetto kstack overflow discovery mechanism: Advanced l33t static analysis: egrep -R "^[[:space:]]*(struct char (u)?int(8_t 16_t 32_t 64_t)? void ) [^=]+\[[a-z]+.*[\+\*]?.*\];" * grep -v sizeof Projected to win grugq's #grep2pwn 2012. Slide #47

pipacs discovery method The proper way to do it: gcc plugin 13:27 < pipacs> jono btw, i'm sorry to burst your infiltrate bubble but the next stackleak plugin will fix the alloca problems... 13:28 < pipacs> (and if you want to find all those bugs, the same plugin can tell you exactly where they occur ;) pax_check_alloca verifies kstack sanity after alloca calls. Inserted at compile time by stackleak_check_alloca into any functions that use builtin_alloca. Artist's depiction of theowl See tools/gcc/stackleak_plugin.c in latest PaX patch Slide #48

Exploiting hardened kernels On grsec/pax kernels, thread_info is no longer stored at the base of the kernel stack Mitigated the Rosengrope stackjacking method So, the standard thread_info overwrite is ineffective Can we use the adjacent process exploitation technique against hardened kernels? Yes... But RANDKSTACK makes it hard and new STACKLEAK plugin makes it near infeasible Slide #49

Mitigating exploitation Move thread_info off the stack! Thwarts vanilla thread_info exploitation technique Patches years ago to LKML, rejected by mainline Thwarting the adjacent process technique is a bit harder Something like PaX's RANDKSTACK would make things harder Slide #50

Wrap-up GIVE UP HEAPSTERS! Win8 fixed everything, the heap is over Stack overflows are exploitable At least in the Linux kernel How about your favorite OS? Windows/BSD/etc? Don't shun unexploitable vuln classes Other situations? Userspace via browser/js? Slide #51

Greetz #busticati $1$kk1q85Xp$Id.gAcJOg7uelf36VQwJQ/ ;PpPppPpPpPPPpP Slide #52

Q&A QUESTIONS? Jon Oberheide jon@oberheide.org Duo Security Slide #53