The first Secure Programming Laboratory will be today! 3pm-6pm in Forrest Hill labs 1.B31, 1.B32.

Similar documents
Secure Programming Lecture 6: Memory Corruption IV (Countermeasures)

Memory corruption countermeasures

Buffer overflow risks have been known for over 30 years. Is it still a problem? Try searching at to see.

Secure Programming Lecture 5: Memory Corruption III (Countermeasures)

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

Lecture 08 Control-flow Hijacking Defenses

Beyond Stack Smashing: Recent Advances in Exploiting. Jonathan Pincus(MSR) and Brandon Baker (MS)

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

CSE 565 Computer Security Fall 2018

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Return-orientated Programming

Software Security II: Memory Errors - Attacks & Defenses

Is stack overflow still a problem?

CMPSC 497 Buffer Overflow Vulnerabilities

Advanced Buffer Overflow

Computer Systems CEN591(502) Fall 2011

Secure Systems Engineering

CMSC 414 Computer and Network Security

Buffer Overflow Vulnerability

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls

Advanced Buffer Overflow

Basic Buffer Overflows

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Buffer overflow prevention, and other attacks

Architecture-level Security Vulnerabilities

Buffer Overflow Vulnerability Lab Due: September 06, 2018, Thursday (Noon) Submit your lab report through to

Software Vulnerabilities August 31, 2011 / CS261 Computer Security

Security Workshop HTS. LSE Team. February 3rd, 2016 EPITA / 40

buffer overflow exploitation

Software Security: Buffer Overflow Attacks

CS 161 Computer Security

Software Security: Buffer Overflow Defenses

Software Security: Buffer Overflow Attacks (continued)

Foundations of Network and Computer Security

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Writing Exploits. Nethemba s.r.o.

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

Università Ca Foscari Venezia

CSC 591 Systems Attacks and Defenses Return-into-libc & ROP

Lecture 9: Buffer Overflow* CS 392/6813: Computer Security Fall Nitesh Saxena

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Buffer overflow is still one of the most common vulnerabilities being discovered and exploited in commodity software.

Architecture-level Security Vulnerabilities. Julian Stecklina

Buffer Overflow Attack

Exploits and gdb. Tutorial 5

Runtime Defenses against Memory Corruption

CNIT 127: Exploit Development. Ch 14: Protection Mechanisms. Updated

20: Exploits and Containment

Sungkyunkwan University

Buffer overflow background

Inject malicious code Call any library functions Modify the original code

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

CSE 127 Computer Security

CSE 127: Computer Security. Memory Integrity. Kirill Levchenko

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

Reserve Engineering & Buffer Overflow Attacks. Tom Chothia Computer Security, Lecture 17

Software Vulnerabilities. Jeff Foster University of Maryland, College Park

putting m bytes into a buffer of size n, for m>n corrupts the surrounding memory check size of data before/when writing

Buffer Overflow Attacks

Lab 10: Introduction to x86 Assembly

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CSE 127 Computer Security

Memory Safety (cont d) Software Security

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Secure Programming I. Steven M. Bellovin September 28,

Lab 2: Buffer Overflows

putting m bytes into a buffer of size n, for m>n corrupts the surrounding memory check size of data before/when writing

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Offensive Security My First Buffer Overflow: Tutorial

CS 161 Computer Security

Buffer Overflows Defending against arbitrary code insertion and execution

Secure Programming Lecture 4: Memory Corruption II (Stack & Heap Overflows)

Exercise 6: Buffer Overflow and return-into-libc Attacks

Lecture 6: Buffer Overflow. CS 436/636/736 Spring Nitesh Saxena

Secure Programming Lecture 4: Memory Corruption II (Stack & Heap Overflows)

Security and Privacy in Computer Systems. Lecture 5: Application Program Security

Topics. What is a Buffer Overflow? Buffer Overflows

CSE 127 Computer Security

Midterm 1 Review. Memory Safety & Web Attacks

String Oriented Programming Exploring Format String Attacks. Mathias Payer

Control Flow Hijacking Attacks. Prof. Dr. Michael Backes

Smashing the Buffer. Miroslav Štampar

Lecture 04 Control Flow II. Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Based on Michael Bailey s ECE 422

INFLUENTIAL OPERATING SYSTEM RESEARCH: SECURITY MECHANISMS AND HOW TO USE THEM CARSTEN WEINHOLD

CS 392/681 Lab 6 Experiencing Buffer Overflows and Format String Vulnerabilities

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

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

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

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

BUFFER OVERFLOW. Jo, Heeseung

Module: Return-oriented Programming. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Buffer Overflow. Jo, Heeseung

CSC 405 Computer Security Stack Canaries & ASLR

18-600: Recitation #4 Exploits

Lecture 1: Buffer Overflows

Transcription:

Lab session this afternoon Memory corruption attacks Secure Programming Lecture 6: Memory Corruption IV (Countermeasures) David Aspinall, Informatics @ Edinburgh 2nd February 2016 The first Secure Programming Laboratory will be today! 3pm-6pm in Forrest Hill labs 1.B31, 1.B32. Please arrive on time, there will be a short introduction in the first half hour of the lab (delivered in groups). Recommended: find someone to work with (pairs rather than larger groups), for all the labs. preparation: study the lectures on overflows carefully, try out some examples. We ve seen memory corruption attacks on the heap, on the stack and mentioned them elsewhere. Overflow vulnerabilities in code are caused by, for example: unchecked buffer boundaries out-by-one errors integer overflow type confusion errors Memory corruption countermeasures Generic defences Defences against overflows Two basic programming-related countermeasures: 1. Treat the symptoms: special technologies in execution or compilation limit the damage that can be done by attacks containment and curtailment 2. Treat the cause ensure that code does not contain vulnerabilities secure programming through code review, analysis tools Question. Why might choice 2 be impossible? Defensive technologies are not a real substitute for proper fixes, but: give defence in depth that can protect in case of new attacks, malware, regressions to vulnerable code sometimes code replacement is simply prohibitively expensive or impossible (e.g., non-upgradeable firmware) Question. Can you give/find some examples of the latter? Several generic protection mechanisms have been invented to prevent overflow attacks and new ones are evolving. These reduce the attackers chance of reliably exploiting a bug on the host system. We will look at: Tamper detection in software Memory protection in OS and hardware Diversification methods

Canaries on the stack Stack without canaries Stack with canary Each stack frame includes vulnerable location pointers which may be corrupted in a stack overflow attack. Idea: wrap frame with protective layer, a canary canary sits below return address attacker overflows stack buffer to hit return address necessarily overwrites canary generated code adds and checks canaries saved EIP (return addr) saved EBP (frame ptr) Local variables saved EIP (return addr) saved EBP (frame ptr) canary word(s) Local variables The "canary" is special data written into the stack to detect unexpected modifications. If a stack overflow or other corruption occurs, the canary may be altered. The compiler adds extra instructions to insert canaries and check their integrity. Early proposal: StackGuard compiler. Question. How might this mechanism be defeated? GCC s Stack Smashing Protector Consider this C program: #include <stdio.h> #include <string.h> int fun1(char *arg) { char buffer[1024]; strcpy(buffer,arg); void main(int argc, char *argv[]) { fun1(argv[1]); Let s compare the assembler compiled with gcc -S -m32 and gcc -S -m32 -fno-stack-protector. main: ; code without SSP: gcc -S -m32 -fno-stack-protector pushl %ebp movl %esp, %ebp andl $-16, %esp ; align stack to 16-byte subl $16, %esp ; movl 12(%ebp), %eax ; eax = addr of argv addl $4, %eax ; eax = addr of argv[1] movl (%eax), %eax ; eax = contents of argv[1] movl %eax, (%esp) ; push it call fun1 ; leave ret fun1: pushl %ebp ; save old frame ptr movl %esp, %ebp ; set new frame ptr subl $1048, %esp ; allocate space movl 8(%ebp), %eax ; movl %eax, 4(%esp) ; push arg (strcpy src) leal -1032(%ebp), %eax movl %eax, (%esp) ; push buffer (strcpy dest) call strcpy leave ret fun1: ; code with SSP (main function stays the same) ; NB: GS register points to per-cpu thread storage pushl %ebp movl %esp, %ebp subl $1064, %esp ; use 16 bytes more movl 8(%ebp), %eax ; fetch arg movl %eax, -1052(%ebp) ; >> keep a copy in our frame movl %gs:20, %eax ; >> set EAX=canary value movl %eax, -12(%ebp) ; >> store near return address xorl %eax, %eax movl -1052(%ebp), %eax ; fetch local copy of arg movl %eax, 4(%esp) ; push it leal -1036(%ebp), %eax ; movl %eax, (%esp) ; push buffer call strcpy movl -12(%ebp), %edx ; >> EDX=canary from stack xorl %gs:20, %edx ; >> has it changed? je.l3 ; call stack_chk_fail ; if it has, we'll abort.l3: leave ret

the stack protection spots an overflow with 1026 characters: $ gcc -m32 overflow.c -o overflow.out $./overflow.out xxxx $./overflow.out `perl -e 'print "x"x1025'` *** stack smashing detected ***:./overflow.out terminated Aborted (core dumped) Exercise. Try this example for yourself, compiling with/without protection, and stepping through it using gdb. Draw the stack layout in each case. Make up some more complex examples and try them out. Canary arms race The arms race in general: attackers respond to new protection mechanisms by looking for vulnerabilities in those mechanisms... For example: Attack code/probing discovers a constant canary e.g., canary is 0x0af237ab6, so write that near return address Canary defence uses pseudorandom sequence attacker learns sequence or discovers seed Canary defences uses cryptographic PRNG attacker finds where value is stored finds another exploit to copy it Stack canary effectiveness Doesn t protect against local variable overwriting related protection mechanisms reorder local variables Other attacks work by overwriting parameters aim to change where subsequent writes occur overwrite return address, but don t return Hardened heap implementations have also been developed glibc and Windows since XP SP2 have heap canaries Self-managed memory mechanisms not addressed e.g., HLL runtimes, application specific heaps Better attacks, better detection Operating system separation (review) Hardware memory protection mechanisms Return-to-libc, and return-oriented programming (ROP) state-of-the-art: use existing executable code evades canaries, also defeats NX (see later) A more powerful and defence mechanism is Control-Flow Integrity, which ensures that code execution follows a pre-determined call graph. This can defend against ROP and similar attacks, depending on the accuracy and granularity of the enforcement. Isolation different processes have different resources (address spaces, file systems,... ) Sharing resources are shared between processes, partial isolation. Sharing may be: all or nothing mediated with access controls mediated with usage controls (capabilities) Concern: granularity of protection. OSes have provided separation mechanisms since the early days of multi-user systems. For memory, direct support was added to the CPU and memory system hardware. Original mechanisms introduced to provide separation (mainly for safety) between different programs on multi-user systems: Fences: separate memory accesses between OS and user code (one boundary, one way protection). Base and bounds registers: enforce separation between several programs allowing access control on memory ranges. Tagged architecture: more fine-grained, tags on each memory location set access rights to stored word (R, RW, X). Supervisor mode instructions required to set tag. Not currently supported in modern architectures.

Memory separation: segmentation & paging Segmentation splits a program into named variable-sized logical pieces, (main,data,module,... ). Programs use names and offsets; segment registers and an OS segment table for indexing. Paging splits a program into fixed-sized pieces. These get mapped onto memory which is split into equal sized page frames. Some OSes use a flat memory model without hardware-supported segmentation. The x86_64 architecture and Linux work over a flat model. Exercise. Investigate the pros and cons of each mechanism, particularly for security (consider possible attacks). Non-executable memory pages CPUs have often included R, RW, X protection for memory pages. x86 series CPUs added page-level XD/NX in 2001-4 By enforcing non-executable regions, if the program keeps code and data separate, shellcode can be prevented from running when it s injected into data regions on the heap or stack. Compared with C, more tricky for certain languages/compilers/interpreters, which may manipulate executable code during runtime. Address Space Layout Randomization (ASLR) Concept: use diversification to make many versions of same program; thwarts general attacks that make assumptions about fixed structure. ASLR: make it harder to find data or code locations, by randomising layout during load time. Breaks hard-coded static locations. Implemented in Linux by the PaX Team. Effectiveness: good, but doesn t remove main vulnerability and vulnerabilities in ASLR implementation become target of attack. Early implementations randomised by small amounts (e.g. 256 addresses), so attacker could use brute force to find the vulnerable locations. Such attacks may attract attention (since failures cause crashes). Defensive programming: bounds checking Responsibility for bounds checking Bounds checks by programmer Defensive programming to avoid overflow requires bounds checking. Check data lengths before writing Check array subscripts are within limits Check boundary conditions to avoid OBO Constrain size of inputs Beware of dangerous API calls to risky code Like many security checks, this is a shared responsibility. It requires checking at each point, by the: programmer programming language, compiler OS hardware Exercise. For each role, give an example of what could be done to check bounds. int a[20], i; for (i=0, i<20; i++) { a[i] = 0; Question. How can this go wrong?

Bounds checks by programmer Bounds checks by programmer Safety from programming languages int a[20], i; for (i=0, i<20; i++) { if (i<0) signal error; if (i >= 20) signal error; a[i] = 0; Checking every time seems inefficient Are both checks required? Tempting to skip... int a[20], i, max; for (i=0, i<max; i++) { if (i<0) signal error; if (i >= 20) signal error; a[i] = 0; If bound is computed, both checks essential Code reviews, programmer reasoning are brittle Programming languages may provide memory safety and type safety, automatically for all programs: Memory safety disallow reading/writing with arbitrary memory addressing. Prevents overflow attacks from corrupting memory. Type safety prevent storing arbitrary data into data values. Makes it harder for attacker to inject data that will be executed as binary code. Safety from compilers and tools To try to ensure safety with an unsafe language, we may use: a safe compiler to automatically generate checking code that checks bounds or types dynamically, during execution. a verifying compiler that checks statically at compile time that the code it produces is safe. security testing tools that generate inputs to programs to try to find security bugs. program analysis tools that ensure that input source code is free from certain vulnerabilities. We ll look at these technologies in more detail later in the course. Safety from OS, libraries See lists of unsafe functions and explanations: Microsoft s recommendations CERT Secure C Coding Use a code security scanning tool Have lists of dangerous API calls built in Simple to find these in code, need deeper analysis to identify certain vulnerabilities Examples: RATS, cppcheck, SPLint, Clang. Switch to using safe(r) library functions The Safe C Library introduced in VS 2005. Bounds checking functions part of the latest C11 standard (appendix K, also ISO/IEC TR 24731-1) But has been contentious, not clear how widely/quickly will be adopted. Review questions Overflow protection mechanisms Explain how StackGuard canaries prevent overflows. What attacks are they not effective against? Describe how hardware-assisted memory protection can prevent the worst kinds of overflow attacks. In what cases may it be difficult to use? Explain the strategy of program diversification and how it is achieved in ASLR. Avoiding overflow vulnerabilities Explain where bounds checking should be performed, especially to ensure defence-in-depth. List some checks which a programmer or static analysis tool should do to prevent overflow vulnerabilities in released code.

Coming next Lab session this afternoon! Next lecture: looking at injections.