Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.

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.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Procedure Calls Prof. Sirer CS 316 Cornell University.
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.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Run time vs. Compile time
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
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
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.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
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.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
1 Assembly Language: Function Calls Jennifer Rexford.
Section 5: Procedures & Stacks
CS 177 Computer Security Lecture 9
Reading Condition Codes (Cont.)
Computer Science 210 Computer Organization
C function call conventions and the stack
CSCE 212 Computer Architecture
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
The Stack & Procedures CSE 351 Spring 2017
Machine-Level Programming III: Procedures
Functions and Procedures
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming 4 Procedures
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Condition Codes Single Bit Registers
Assembly Language Programming II: C Compiler Calling Sequences
MIPS Instructions.
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
Multi-modules programming
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
Presentation transcript:

Assembly תרגול 8 פונקציות והתקפת buffer.

Procedures (Functions) A procedure call involves passing both data and control from one part of the code to another.  Data = procedure parameters and return values. When passing control to another procedure, the procedure must allocate space for the local variables of the procedure on entry and deallocate them on exit. IA32 provides only simple instructions for transferring control to and from procedures. The passing of data and the allocation and deallocation of local variables is handled by manipulating the program stack.

Using the Stack IA32 programs make use of the program stack to support procedure calls. The stack is used to:  pass procedure arguments.  store return information.  save registers for later restoration  local storage. The portion of the stack allocated for a single procedure call is called a stack frame.

Transferring Control Instructors

call Instructor Similar to the Jump ( jmp ) instructor.  Has a target indicating the address of the instruction where the called procedure starts.  Can either be direct (with label) or indirect (*Operand). The effect of a call instruction is to:  Push a return address on the stack.  Jump to the start of the called procedure. The return address is the address of the instruction immediately following the call in the program, so that execution will resume at this location when the called procedure returns.

ret Instructor It pops an address off the stack and jumps to this location. The proper use of this instruction is to have prepared the stack so that the stack pointer points to the place where the preceding call instruction stored its return address.

leave Instructor Is used to prepare the stack for returning. Equivalent to the following code sequence: movl %ebp, %esp popl %ebp Alternatively, this preparation can be performed by an explicit sequence of move and pop operations.

Registers The set of program registers acts as a single resource shared by all of the procedures. We must make sure that when one procedure (the caller) calls another (the callee), the callee does not overwrite some register value that the caller planned to use later. For this reason, IA32 adopts a uniform set of conventions for register usage that must be respected by all procedures, including those in program libraries.

Registers conventions Registers %eax, %edx, and %ecx are classified as caller save registers.  When procedure Q is called by P, it can overwrite these registers without destroying any data required by P. Registers %ebx, %esi, and %edi are classified as callee save registers.  This means that Q must save the values of any of these registers on the stack before overwriting them, and restore them before returning, because P (or some higher level procedure) may need these values for its future computations.

Registers conventions (2) Registers %ebp and %esp must be maintained according to the conventions learnt.  They will be used only in order to maintain the stack and frame. Register %eax is used for returning the value of any function that returns an integer or pointer.

Caller vs. Callee  Store the value of y in its own stack frame before calling Q. When Q returns, it can then retrieve the value of y from the stack.  Store the value of y in a callee save register. If Q, or any procedure called by Q, wants to use this register, it must save the register value in its stack frame and restore the value before it returns. Thus, when Q returns to P, the value of y will be in the callee save register, either because the register was never altered or because it was saved and restored. Procedure P computes y before calling Q, but it must also ensure that the value of y is available after Q returns. It can do this by one of two means:

An example: swap_add The C code:

An example: swap_add (2) Calling swap_add from caller :

An example: swap_add (2) Calling swap_add from caller :  This code computes the addresses of local variables arg2 and arg1 and pushes them on the stack. It then calls swap_add.

An example: swap_add (3) Swap_add has three parts:  The “setup,” where the stack frame is initialized.  The “body,” where the actual computation of the procedure is performed.  The “finish,” where the stack state is restored and the procedure returns.

swap_add : Setup Setup code: pushl %ebp#save old FP movl %esp, %ebp #%ebp = FP pushl %ebx#save %ebx Procedure swap_add requires register %ebx for temporary storage. Since this is a callee save register, it pushes the old value on the stack as part of the stack frame setup.

swap_add : Setup (2)

swap_add : Body Body code:

swap_add : Finish Finish code:

swap_add : Finish (2) This code:  Restores the values of the three registers %ebx, %esp, and %ebp.  Then it executes the ret instruction Note that instructions F2 and F3 could be replaced by a single leave instruction. The following code in caller comes immediately after the instruction calling swap_add: movl %eax, %edx

Another example func.s

Recursive Procedures The stack and linkage conventions described in the previous section allow procedures to call themselves recursively. Since each call has its own private space on the stack, the local variables of the multiple outstanding calls do not interfere with one another. Furthermore, the stack discipline naturally provides the proper policy for allocating local storage when the procedure is called and deallocating it when it returns.

Fibonacci function:

Stack frame for Recursive Fibonacci:

Out-of-Bounds Memory References and Buffer Overflow

Buffer Overflow C does not perform any bounds checking for array references. Local variables are stored on the stack along with state information such as register values and return pointers. This combination can lead to serious program errors, where the state stored on the stack gets corrupted by a write to an out-of-bounds array element. When the program then tries to reload the register or execute a ret instruction with this corrupted state, things can go seriously wrong.

Buffer overflow example The function get, assumes that dest is big enough!

Buffer overflow example (2) The problem with gets is that it has no way to determine whether sufficient space has been allocated to hold the entire string. In the echo example, we have purposely made the buffer very small - just four characters long. Any string longer than three characters will cause an out-of-bounds write.

Buffer overflow example (3) Writing to buf[4] through buf[7] - the saved value of %ebp will be corrupted. Write to buf[8] through buf[11] - the return address will be corrupted. As this example illustrates, buffer overflow can cause a program to seriously misbehave.

Buffer attack The get function was sloppy and cause a buffer overflow by mistake. A more pernicious use of buffer overflow is to get a program to perform a function that it would otherwise be unwilling to do. This is one of the most common methods to attack the security of a system over a computer network. The program is fed with a string that contains the byte encoding of some executable code, called the exploit code, plus some extra bytes that overwrite the return pointer with a pointer to the code in the buffer. The effect of executing the ret instruction is then to jump to the exploit code. Illegal!

Forms of attacks The exploit code uses a system call to start up a shell program, providing the attacker with a range of operating system functions. The exploit code performs some otherwise unauthorized task, repairs the damage to the stack, and then executes ret a second time, causing an (apparently) normal return to the caller (one of the tools of the “Great Worm” of November, 1988). etc. “buff_att.s” - an example how to “damage” the return address and then fixing it.