September 22, 2014 Pengju (Jimmy) Jin Section E

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

Calling sequence ESP.
15/18-213: Introduction to Computer Systems
Recitation 4 Outline Buffer overflow –Practical skills for Lab 3 Code optimization –Strength reduction –Common sub-expression –Loop unrolling Reminders.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
RECITATION - 09/20/2010 BY SSESHADR Buflab. Agenda Reminders  Bomblab should be finished up  Exam 1 is on Tuesday 09/28/2010 Stack Discipline Buflab.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Machine-Level Programming III: Procedures Jan 30, 2003
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly, Stacks, and Registers Kevin C. Su 9/26/2011.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Goals: To gain an understanding of assembly To get your hands dirty in GDB.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
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.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
Compiler Construction Code Generation Activation Records
Reminder Bomb lab is due tomorrow! Attack lab is released tomorrow!!
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
CS642: Computer Security X86 Review Process Layout, ISA, etc. Drew Davidson
1 Assembly Language: Function Calls Jennifer Rexford.
Carnegie Mellon 1 Stacks and Buflab : Introduction to Computer Systems Recitation 4: Monday, Sept. 23, 2013 Marjorie Carlson Section A.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Section 5: Procedures & Stacks
Recitation 3: Procedures and the Stack
CS 177 Computer Security Lecture 9
Assembly function call convention
Reading Condition Codes (Cont.)
Recitation 5: Attack Lab
C function call conventions and the stack
Recitation: Attack Lab
Homework Reading Machine Projects Labs PAL, pp ,
Aaron Miller David Cohen Spring 2011
Homework In-line Assembly Code Machine Language
Introduction to Compilers Tim Teitelbaum
Assembly Language Programming V: In-line Assembly Code
Recitation: Attack Lab
Chapter 3 Machine-Level Representation of Programs
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Recitation: Attack Lab
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Machine Level Representation of Programs (IV)
Machine-Level Programming: Introduction
Chapter 3 Machine-Level Representation of Programs
“Way easier than when we were students”
Presentation transcript:

September 22, 2014 Pengju (Jimmy) Jin Section E 15/18-213 Recitation 5 September 22, 2014 Pengju (Jimmy) Jin Section E

Agenda Bomb lab reminder Machine Programming Procedures Demo Stack Frames Function calls in x86(IA32) and x86-64 (briefly) Demo Buffer lab Preview

Reminder In case you didn’t know, bomb lab is due tomorrow. If you haven’t started it yet, good luck. Buflab comes out tomorrow night.

Register Recap Caller saved: %eax, %ecx, %edx Must be saved before a function call (by the caller) if needed. Callee saved: %ebx, %edi, %esi Must save these (by the callee) before any work if needed in the child function. Base pointer: %ebp (IA32) Stack pointer: %esp Instruction pointer: %eip

IA32 Stack “Upside down” stack Grows downward => new things are pushed to a lower memory address %esp holds the ADDRESS of top of the stack %esp has the lowest address Stack “bottom” 0xffffffff %esp Stack growth Stack “top”

Stack Operation - Push pushl src  subl $4, %esp movl src, (%esp) Frist move the stack pointer to a lower (empty) address Then move the value into the empty address. ... 0x110 0x10c 0x108 0x123 0x104 0x213 ... 0x110 0x10c 0x108 0x123 %esp %esp

Stack Operation - pop popl dest  movl (%esp), dest addl $4, %esp Move the value stored at the top of the stack to dest. The address now becomes empty, move the stack pointer up. ... 0x110 0x10c 0x108 0x123 0x104 0x213 ... 0x110 0x10c 0x108 0x123 0x104 0x213 %esp %esp

Stack frames Every function call is given a new stack frame. Stack frames are in the stack memory region growing down. Things included in the frame: Local variables (scalars, arrays, structs) Scalars: if the compiler couldn’t allocate enough registers Space to save callee saved registers Space to put computations A way to give arguments and call other functions A way to grab arguments %ebp Old %ebp Saved registers + local variables Argument build for function call %esp

Function Calls - setup Shifting the stack frame when a function is called. Creating a new stack frame: Parent function: call label (ex: call <add>) Push the return address (the address of next instruction after the call) Child function: push %ebp, move $esp to %ebp, decrease $esp Save the old $ebp to the stack Move the $esp to $ebp, $ebp now points at the $ebp of the parent function. Decrease $esp, creating space for new function.

Visualization 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call <main> ... 0x110 0x10c 0x108 0x123 ... 0x110 0x10c 0x108 0x123 0x104 0x8048553 %esp %eip 0x804854e %esp 0x108 %eip 0x8048b90 %esp 0x104

Function Calls - Return Child function calls leave then ret leave – two machine operation combined into one move the $ebp to $esp (esp now points at the base of the stack) pops the stack into $ebp (ebp is restored back to the ebp of parent function) ret – pop return address into $eip, the function call is over.

Returning 8048591: c3 ret ret ... 0x110 0x10c 0x108 0x123 0x104 %esp %eip 0x8048591 %esp 0x104 %eip 0x8048553 %esp 0x108

Function calls and stack frames Arguments Return addr Suppose you have sum grabs arguments by reaching up the caller’s stack frame! If we scale up this example, we see that arguments should be pushed in reverse order main int main(void) { int x = 3; return sum(x, 0); } %ebp Old %ebp Saved registers + local variables Argument build %esp sum

Demo

X86-64 If you understand 32bit machines, 64 bit is easy. No more frame pointers (%ebp is now a free register) Many arguments are passed in registers Less stack manipulation, more use of registers Overall a lot less stack usage Good for performance You are expected to know how the stack works for 64 bits

Buffer lab Overview Hacking the IA32 function call procedure. Overflows the stack frame memory space and over-writes some important information (return address). A thorough understanding of procedure call is needed.

Details on Buffer Lab Disassembling your code using objdump USE GDB Find out how long to make your inputs. Write exploits to divert program execution

Buffer Lab Tricks Canaries NOP sleds Detect overrun buffers Sit at the end of the buffer If the array overflows, hopefully we can detect this with a change in the canary value NOP sleds The nop instruction means “no operation” Used to “pad” instructions (or exploits)

Buffer Lab Tools ./makecookie andrewID ./hex2raw ./bufbomb –t andrewID Makes a unique “cookie” based on your AndrewID ./hex2raw Use the hex generated from assembly to pass raw strings into bufbomb Use with –n in the last stage ./bufbomb –t andrewID The actual program to attack Always pass in with your AndrewID so you will be graded on autolab

How to Input Answer Put your byte code exploit into a text file Then feed it through hex2raw Later stages: write(corruption) assembly Compiling Gcc –m32 –c example.s Get the byte codes Objdump –d example.o > outfile Feed it thrught hex2raw

Read every line of the handout. Good luck have fun Buffer Lab Hint Read every line of the handout. Good luck have fun