Memory Management III: Perils and pitfalls Oct 14, 1999

Slides:



Advertisements
Similar presentations
Buffer Overflows Nick Feamster CS 6262 Spring 2009 (credit to Vitaly S. from UT for slides)
Advertisements

Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Instructors: Seth Copen Goldstein, Franz Franchetti, Greg Kesden
Some topics in Memory Management
1 Dynamic Memory Allocation: Advanced Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron Joke of the day: Why did the.
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
Carnegie Mellon 1 Dynamic Memory Allocation: Advanced Concepts : Introduction to Computer Systems 18 th Lecture, Oct. 26, 2010 Instructors: Randy.
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
Dynamic Memory Allocation II Nov 7, 2002 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and pitfalls.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
– 1 – Dynamic Memory Allocation – beyond the stack and globals Stack Easy to allocate (decrement esp) Easy to deallocate (increment esp) Automatic allocation.
Dynamic Memory Allocation I May 21, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies.
Pointers and Memory Allocation – part 2 -L. Grewe.
Dynamic Memory Allocation II November 7, 2007 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
Dynamic Memory Allocation II March 27, 2008 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
Pointers Applications
University of Washington Memory Allocation III The Hardware/Software Interface CSE351 Winter 2013.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
University of Washington Today Happy Monday! HW2 due, how is Lab 3 going? Today we’ll go over:  Address space layout  Input buffers on the stack  Overflowing.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Dynamic Memory Allocation II
A Tool for Pro-active Defense Against the Buffer Overrun Attack D. Bruschi, E. Rosti, R. Banfi Presented By: Warshavsky Alex.
Dynamic Memory Allocation II Topics Explicit doubly-linked free lists Segregated free lists Garbage collection.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Dynamic Memory Allocation II April 1, 2004 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and.
Dynamic Memory Allocation II November 4, 2004 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and.
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
1 Debugging (Part 2). “Programming in the Large” Steps Design & Implement Program & programming style (done) Common data structures and algorithms Modularity.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
University of Washington Today More memory allocation!
Dynamic Memory Management Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
1 Advanced Topics. 2 Outline Garbage collection Memory-related perils and pitfalls Suggested reading: 9.10, 9.11.
Dynamic Memory Allocation II March 27, 2008 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Advanced Concepts :
Programming in C Advanced Pointers.
Memory-Related Perils and Pitfalls in C
Dynamic Allocation in C
Content Coverity Static Analysis Use cases of Coverity Examples
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
Instructor: Fatma CORUT ERGİN
Dynamic Memory Allocation II
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
The Hardware/Software Interface CSE351 Winter 2013
Debugging Memory Issues
CSE 451 C refresher.
Instructors: Roger Dannenberg and Greg Ganger
Memory Allocation III CSE 351 Spring 2017
Dynamic Memory Allocation II Nov 7, 2000
Memory Management: Dynamic Allocation
Checking Memory Management
The Hardware/Software Interface CSE351 Winter 2013
Dynamic Memory Allocation – beyond the stack and globals
Dynamic Memory Allocation: Advanced Concepts /18-213/14-513/15-513: Introduction to Computer Systems 20th Lecture, November 2, 2017.
Memory Allocation III CSE 351 Autumn 2016
Memory Management III: Perils and pitfalls Mar 13, 2001
Memory Allocation CS 217.
Memory Management III: Perils and pitfalls Mar 9, 2000
Memory Allocation III CSE 351 Winter 2018
Introduction to Static Analyzer
Memory Allocation III CSE 351 Summer 2018
Pointer & Memory Allocation Review
Memory Allocation III CSE 351 Spring 2017
Instructors: Majd Sakr and Khaled Harras
Memory Allocation III CSE 351 Spring 2017
Understanding and Preventing Buffer Overflow Attacks in Unix
Presentation transcript:

Memory Management III: Perils and pitfalls Oct 14, 1999 15-213 “The course that gives CMU its Zip!” Memory Management III: Perils and pitfalls Oct 14, 1999 Topics Memory-related bugs Debugging versions of malloc Binary translation class16.ppt

C operators Operators Associativity () [] -> . left to right ! ~ ++ -- + - * & (type) sizeof right to left * / % left to right + - left to right << >> left to right < <= > >= left to right == != left to right & left to right ^ left to right | left to right && left to right || left to right ?: right to left = += -= *= /= %= &= ^= != <<= >>= right to left , left to right Note: Unary +, -, and * have higher precedence than binary forms

C pointer declarations int *p p is a pointer to int int *p[13] p is an array[13] of pointer to int int *(p[13]) p is an array[13] of pointer to int int **p p is a pointer to a pointer to an int int (*p)[13] p is a pointer to an array[13] of int int *f() f is a function returning a pointer to int int (*f)() f is a pointer to a function returning int int (*(*f())[13])() f is a function returning ptr to an array[13] of pointers to functions returning int int (*(*x[3])())[5] x is an array[3] of pointers to functions returning pointers to array[5] of ints

Memory-related bugs Dereferencing bad pointers Reading uninitialized memory Overwriting memory Referencing nonexistent variables Freeing blocks multiple times Referencing freed blocks Failing to free blocks

Dereferencing bad pointers The classic scanf bug scanf(“%d”, val);

Reading uninitialized memory Assuming that heap data is initialized to zero /* return y = Ax */ int *matvec(int **A, int *x) { int *y = malloc(N*sizeof(int)); int i, j; for (i=0; i<N; i++) for (j=0; j<N; j++) y[i] += A[i][j]*x[j]; return y; }

Allocating the wrong sized object Overwriting memory Allocating the wrong sized object int **p; p = malloc(N*sizeof(int)); for (i=0; i<N; i++) { p[i] = malloc(M*sizeof(int)); }

Overwriting memory Off-by-one int **p; p = malloc(N*sizeof(int *)); for (i=0; i<=N; i++) { p[i] = malloc(M*sizeof(int)); }

Overwriting memory Off-by-one redux int i=0, done=0; int s[4]; while (!done) { if (i > 3) done = 1; else s[++i] = 10; }

Forgetting that strings end with ‘/0’ Overwriting memory Forgetting that strings end with ‘/0’ char t[7]; char s[8] = “1234567”; strcpy(t, s);

Not checking the max string size Overwriting memory Not checking the max string size char s[8]; int i; gets(s); /* reads “123456789” from stdin */ Basis for classic buffer overflow attacks 1988 Internet worm modern attacks on Web servers

Buffer overflow attacks Description of hole: Servers that use C library routines such as gets() that don’t check input sizes when they write into buffers on the stack. local variables Stack frame for proc a proc a() { b(); # call procedure b } return addr 64 bytes for buffer proc b() { char buffer[64]; # alloc 64 bytes on stack gets(buffer); # read STDIN line into stack buf } Stack frame for proc b return addr

Buffer overflow attacks Vulnerability stems from possibility of the gets() routine overwriting the return address for b. overwrite stack frame with machine code instruction that execs a shell a bogus return address to that instruction local variables proc a() { b(); # call procedure b } # b should return here, instead it # returns to an address inside of buffer return addr exec(“/bin/sh”) proc b() { char buffer[64]; # alloc 64 bytes on stack gets(buffer); # read STDIN line to stack buffer } stack region overwritten by call to gets() in b attacker’s return addr

Buffer overflow attacks on servers Example attack: classic buffer overflow attack Early versions of the finger server (fingerd) used gets() to read the argument sent by the client: finger droh@cs.cmu.edu To attack fingerd, send a binary string that puts a program to execute a shell on the stack followed by a new return address to that stack location, padded with enough bytes so that it overwrites the real return address. finger “binary program padding new return address” After the finger server reads the argument from the client, the client has a direct TCP connection to a root shell running on the server! STDIN and STDOUT on the server are bound to an open TCP socket Bottom line: client can now execute any command on the server.

Famous buffer overflow attack: The 1988 Internet Worm Worm: an independent program that replicates itself across the host machines on a network. November 1988: Thousands of Sun and DEC machines on the Internet are attacked by a “worm” written by Cornell grad student Robert Morris. Because of a bug in the worm, it replicated itself multiple times on many of the Internet hosts, causing them to crash. had the effect of a denial of service attack Resulted (after a similar attack weeks later) in the formation of CERT (Computer Emergency Response Team) and increased awareness of security.

Referencing a pointer instead of the object it points to Overwriting memory Referencing a pointer instead of the object it points to int *BinheapDelete(int **binheap, int *size) { int *packet; packet = binheap[0]; binheap[0] = binheap[*size - 1]; *size--; Heapify(binheap, *size, 0); return(packet); }

Misunderstanding pointer arithmetic Overwriting memory Misunderstanding pointer arithmetic int *search(int *p, int val) { while (*p && *p != val) p += sizeof(int); return p; }

Referencing nonexistent variables Forgetting that local variables disappear when a function returns int *foo () { int val; return &val; }

Freeing blocks multiple times Nasty! x = malloc(N*sizeof(int)); <manipulate x> free(x); y = malloc(M*sizeof(int)); <manipulate y>

Referencing freed blocks Evil! x = malloc(N*sizeof(int)); <manipulate x> free(x); ... y = malloc(M*sizeof(int)); for (i=0; i<M; i++) y[i] = x[i]++;

Failing to free blocks (memory leaks) slow, long-term killer! foo() { int *x = malloc(N*sizeof(int)); ... return; }

Failing to free blocks (memory leaks) Freeing only part of a data structure struct list { int val; struct list *next; }; foo() { struct list *head = malloc(sizeof(struct list)); head->val = 0; head->next = NULL; <create and manipulate the rest of the list> ... free(head); return; }

Dealing with memory bugs Conventional debugger (gdb) good for finding bad pointer dereferences hard to detect the other memory bugs Debugging malloc (CSRI UToronto malloc) wrapper around conventional malloc detects memory bugs at malloc and free boundaries memory overwrites that corrupt heap structures some instances of freeing blocks multiple times memory leaks Cannot detect all memory bugs overwrites into the middle of allocated blocks freeing block twice that has been reallocated in the interim referencing freed blocks

Dealing with memory bugs (cont.) Binary translator (Atom, Purify) powerful debugging and analysis technique rewrites text section of executable object file can detect all errors as debugging malloc can also check each individual reference at runtime bad pointers overwriting referencing outside of allocated block Garbage collection (Boehm-Weiser Conservative GC) let the system free blocks instead of the programmer does not guarantee that all garbage is collected -- an integer might look like a pointer to the collector, and the memory it points to will not be freed.

Debugging malloc mymalloc.h: #define malloc(size) mymalloc(size, __FILE__, __LINE__) #define free(p) myfree(p, __FILE__, __LINE__) Application program: ifdef DEBUG #include <mymalloc.h> #endif main() { ... p = malloc(128); free(p); q = malloc(32); }

Debugging malloc (cont.) Debugging malloc library: void *mymalloc(int size, char *file, int line) { <prologue code> p = malloc(...); <epilogue code> return q; } void myfree(void *p, char *file, int line) { free(p);

Debugging malloc (cont.) block size block ID file name (of allocation) line number (of allocation) header checksum (of previous fields) ptr to next allocated block ptr to prev allocated block guard bytes Block requested by application application block guard bytes footer

Debugging malloc (cont.) mymalloc(size): p = malloc(size + sizeof(header) + sizeof(footer)); add p to list of allocated blocks initialize application block to 0xdeadbeef return pointer to application block myfree(p): already free (line # = 0xfefefefefefefefe)? checksum OK? guard bytes OK? free(p - sizeof(hdr)); line # = 0xfefefefefefefefe;

Binary translator Converts an executable object file to an instrumented executable object file. hello.c main() { printf(“hello, world\n”); } analysis file (ptrace.anal.c) original executable ELF object file (hello) instrumented executable ELF object file (hello.ptrace) binary translator (e.g. Atom) gcc instrumentation file (ptrace.inst.c)

Atom example (procedure call tracing) Instrumentation file (ptrace.inst.c): #include <stdio.h> #include <cmplrs/atom.inst.h> Instrument() { Proc *proc; AddCallProto(“ProcTrace(char*)”); for (proc = GetFirstProc(); proc != NULL; proc = GetNextProc(proc)) AddCallProc(proc, ProcBefore, ”ProcTrace”, ProcName(proc)); } Analysis file (ptrace.anal.c): #include <stdio.h> void ProcTrace(char *name) { printf(“%s\n”, name); }

Instrumenting “hello,world” % cc -non_shared hello.c -o hello.rr % atom hello.rr ptrace.inst.c ptrace.anal.c -o hello.ptrace % hello.ptrace __start __init_libc __tis_init __libc_locks_init calloc malloc __sbrk _errno __getpagesize __tis_mutex_lock _unlocked_sbrk __tis_mutex_unblock memset main printf _doprnt __getmbcurmax memcpy __fwrite_unlocked _wrtchk _findbuf __geterrno __isatty __ioctl _cerror __seterrno memcpy __tis_mutex_unblock exit __ldr_atexit __fini_libc _cleanup __tis_mutex_trylock __fclose_unlocked __fflush_unlocked __close_nc __close __fflush_unlocked _xflsbuf __write_nc __write hello, world __close_nc __close __tis_mutex_trylock __fclose_unlocked

Tools built with Atom iprof - instruction profiling liprof - instruction profiling at basic block level syscall - syscall trace and performance analyzer memsys - memory system simulator io - io performance summary gprof - call graph execution time profile 3rd - memory checker and leak finder (like Purify) pixie - basic block profiling

Atom on Linux Other tools like Atom for IA32 machines Etch: Univ. of Wash (commercial product for NT) DynInst: Maryland (limited run-time instrumentation) Eel: Wisconsin (licensed code base) Would you like to develop a tool like Atom for the Linux community? potentially high impact and high visibility Issues: parsing IA32 machine code difficult, but examples exist (I.e, Eel) In the r/o .text section, what’s code and what’s data? research problem Incremental relocation tricky, but straightforward conceptually