cs 251 fall 2019 x86 64 linux memory layout cs 240 spring
play

CS 251 Fall 2019 x86-64 Linux memory layout CS 240 Spring 2020 - PowerPoint PPT Presentation

CS 251 Fall 2019 x86-64 Linux memory layout CS 240 Spring 2020 Principles of Programming Languages Foundations of Computer Systems Ben Wood ... ... Ben Wood 0x00007fffffffffff Stack not drawn to scale Caller Buffer Overflows Frame


  1. λ CS 251 Fall 2019 x86-64 Linux memory layout CS 240 Spring 2020 Principles of Programming Languages Foundations of Computer Systems Ben Wood ... ... Ben Wood 0x00007fffffffffff Stack not drawn to scale Caller Buffer Overflows Frame Extra Arguments Optional Return Addr Frame pointer ) %rbp Old %rbp ( o f n i r d d a t Address space layout e g 6 1 0 2 . Saved b e F the stack discipline Registers + + C's lack of bounds-checking Local HUGE PROBLEM Variables Heap Data Setup Extra Stack pointer Arguments Text %rsp 0x0000000000400000 0x0000000000000000 https://cs.wellesley.edu/~cs240/s20/ Buffer Overflows 1 Buffer Overflows 2 String library code Vulnerable buffer code: C C standard library function gets() /* Echo Line */ void echo() { /* Get string from stdin */ char buf[4]; /* Way too small! */ char* gets(char* dest) { gets(buf); int c = getchar(); puts(buf); char* p = dest; } while (c != EOF && c != '\n') { pointer to start of an array *p++ = c; int main() { c = getchar(); printf("Type a string:"); } echo(); *p = '\0'; $ ./bufdemo return 0; same as: return dest; Type a string: 123 } *p = c; } 123 p = p + 1; $ ./bufdemo What could go wrong in this code? Type a string: 0123456789012345678901234 Segmentation Fault Same problem in many functions: $ ./bufdemo strcpy : Copies string of arbitrary length Type a string: 012345678901234567890123 012345678901234567890123 scanf , fscanf , sscanf , when given %s conversion specification Buffer Overflows 3 Buffer Overflows 4

  2. Buffer overflow example: before input Vulnerable buffer code: disassembled x86 Before call to gets void echo() { echo: char buf[4]; subq $24, %rsp Stack frame for gets(buf); movq %rsp, %rdi 00000000004006cf <echo>: call_echo . . . call gets } . . . 4006cf: 48 83 ec 18 sub $24 ,%rsp echo code 4006d3: 48 89 e7 mov %rsp,%rdi 00 00 00 00 Return 4006d6: e8 a5 ff ff ff callq 400680 <gets> Address call_echo: 00 40 06 f6 4006db: 48 89 e7 mov %rsp,%rdi 4006de: e8 3d fe ff ff callq 400520 <puts@plt> . . . 4006f1: callq 4006cf <echo> 4006e3: 48 83 c4 18 add $24,%rsp 4006f6 : add $0x8,%rsp 4006e7: c3 retq 20 bytes unused . . . [3] [2] [1] [0] buf ⟵ %rsp caller code 4006e8: 48 83 ec 08 sub $0x8,%rsp 4006ec: b8 00 00 00 00 mov $0x0,%eax 4006f1: e8 d9 ff ff ff callq 4006cf <echo> 4006f6 : 48 83 c4 08 add $0x8,%rsp 4006fa: c3 retq Buffer Overflows 5 Buffer Overflows 6 Buffer overflow example: input #1 Buffer overflow example: input #2 After call to gets After call to gets void echo() echo: void echo() echo: { subq $24, %rsp { subq $24, %rsp Stack frame for Stack frame for char buf[4]; movq %rsp, %rdi char buf[4]; movq %rsp, %rdi call_echo call_echo gets(buf); call gets gets(buf); call gets . . . . . . . . . . . . } } 00 00 00 00 00 00 00 00 Return Return Address Address call_echo: call_echo: 00 40 06 f6 00 40 00 34 00 32 31 30 33 32 31 30 . . . . . . 4006f1: callq 4006cf <echo> 4006f1: callq 4006cf <echo> 39 38 37 36 39 38 37 36 4006f6 : add $0x8,%rsp 4006f6 : add $0x8,%rsp 35 34 33 32 35 34 33 32 . . . . . . 31 30 39 38 31 30 39 38 37 36 35 34 37 36 35 34 buf ⟵ %rsp buf ⟵ %rsp 33 32 31 30 33 32 31 30 unix> ./bufdemo $ ./bufdemo Null Terminator Type a string: 01234567890123456789012 34 Type a string: 01234567890123456789012 Segmentation Fault 01234567890123456789012 Overflowed buffer and corrupted return pointer Overflowed buffer, but did not corrupt state Buffer Overflows 7 Buffer Overflows 8

  3. Buffer overflow example: input #3 Buffer overflow example: input #3 After call to gets After call to gets void echo() echo: { subq $24, %rsp Some other place in .text Stack frame for Stack frame for char buf[4]; movq %rsp, %rdi call_echo call_echo gets(buf); call gets . . . . . . . . . 400600 : mov %rsp,%rbp } 400603: mov %rax,%rdx 00 00 00 00 00 00 00 00 Return Return 400606: shr $0x3f,%rdx Address Address call_echo: 00 40 06 00 00 40 06 00 40060a: add %rdx,%rax 33 32 31 30 33 32 31 30 . . . 40060d: sar %rax 4006f1: callq 4006cf <echo> 400610: jne 400614 39 38 37 36 39 38 37 36 4006f6 : add $0x8,%rsp 400612: pop %rbp 35 34 33 32 35 34 33 32 . . . 400613: retq 31 30 39 38 31 30 39 38 37 36 35 34 37 36 35 34 buf ⟵ %rsp buf ⟵ %rsp 33 32 31 30 33 32 31 30 unix> ./bufdemo-nsp “Returns” to unrelated code Type a string: 01234567890123456789012 3 Lots of things happen, without modifying critical state 012345678901234567890123 Eventually executes retq back to main Overflowed buffer, corrupted return pointer, but program seems to work! Buffer Overflows 9 Buffer Overflows 10 Exploiting buffer overflows Exploits in the wild Buffer overflow bugs allow remote attackers to execute Stack after call to gets() arbitrary code on machines running vulnerable software. void foo(){ 1988: Internet worm foo stack frame bar(); ... Early versions of the finger server daemon (fingerd) used return address A } gets() to read the argument sent by the client: B (was A) finger somebody@cs.wellesley.edu int bar() { data written pad char buf[64]; by gets() commandline facebook of the 80s! gets(buf); exploit bar stack frame ... Attack by sending phony argument: code return ...; B } finger “exploit-code padding new-return-address” ... Input string contains byte representation of executable code Still happening ) ( o f n i r "Ghost:" 2015 d d a Overwrite return address A with address of buffer (need to know B) t e g 6 1 0 2 . b When bar() executes ret , will jump to exploit code (instead of A) F e gethostname() Buffer Overflows 11 Buffer Overflows 12

  4. optional Heartbleed (2014) Avoiding overrun vulnerabilities Buffer over-read in OpenSSL 1. Use a memory-safe language (not C)! Widely used encryption library 2. If you have to use C, use library functions that limit string lengths. (https) fgets instead of gets “Heartbeat” packet /* Echo Line */ Specifies length of message void echo() { Server echoes that much back char buf[4]; /* Way too small! */ Library just “trusted” this length fgets (buf, 4 , stdin); puts(buf); Allowed attackers to read } contents of memory anywhere they wanted strncpy instead of strcpy ~17% of Internet affected Don’t use scanf with %s conversion specification “Catastrophic” Use fgets to read the string Or use %ns where n is a suitable integer Github, Yahoo, Stack Overflow, Amazon AWS, ... By FenixFeather - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=32276981 Other ideas? Buffer Overflows 13 Buffer Overflows 15 System-level protections Available in modern OSs/compilers/hardware (We disabled these for buffer assignment.) not drawn to scale Stack 1. Randomize stack base, maybe frame padding 2. Detect stack corruption save and check stack "canary" values 3. Non-executable memory segments stack, heap, data, … everything except text hardware support Helpful, not foolproof! Heap Return-oriented programming, over-reads, etc. Data Text Buffer Overflows 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend