UBC104 Embedded Systems Functions & Pointers.

Slides:



Advertisements
Similar presentations
Recitation 4 Outline Buffer overflow –Practical skills for Lab 3 Code optimization –Strength reduction –Common sub-expression –Loop unrolling Reminders.
Advertisements

Smashing the Stack for Fun and Profit
Recitation 4: 09/30/02 Outline The Stack! Essential skill for Lab 3 –Out-of-bound array access –Put your code on the stack Annie Luo
CS 4284 Systems Capstone Godmar Back Linking and Loading.
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Lecture 10 – Activation Records Eran Yahav 1 Reference: Dragon 7.1,7.2. MCD 6.3,
Programming Project # 1 cs155 Due: Thursday, April 21 st, 11:59pm Shayan Guha Elizabeth Stinson.
Advanced Buffer Overflow Methods
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Windows XP SP2 Stack Protection Jimmy Hermansson Johan Tibell.
Accessing parameters from the stack and calling functions.
UBC104 Embedded Systems Variables, Structures & Pointers.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Foundations of Network and Computer Security J J ohn Black Lecture #19 Nov 3 rd 2005 CSCI 6268/TLEN 5831, Fall 2005.
Attacks Using Stack Buffer Overflow Boxuan Gu
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
CS 3204 Operating Systems Godmar Back Lecture 11.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Instrumentation - initial results Sung Kim, Jeff Perkins MIT.
CrackChat #2 Stack Overflows and Format Strings Part 2: Baking the Egg
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Recitation 4: The Stack & Lab3 Andrew Faulring Section A 30 September 2002.
1 #include void silly(){ char s[30]; gets(s); printf("%s\n",s); } main(){ silly(); return 0; }
Recitation 6 – 2/26/01 Outline Linking Exam Review –Topics Covered –Your Questions Shaheen Gandhi Office Hours: Wednesday.
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.
Smashing the Stack Overview The Stack Region Buffer Overflow
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
Part II Let’s make it real Memory Layout of a Process.
ELF binary # readelf -a foo.out ELF Header:
Stack-based buffer overflows Yves Younan DistriNet, Department of Computer Science Katholieke Universiteit Leuven Belgium
Exploits Buffer Overflows and Format String Attacks David Brumley Carnegie Mellon University.
Bits and Bytes September 1, F’05 class02.ppt “The Class That Gives CMU Its Zip!”
International Summer School on Information and System Security Stack Based Buffer Overflows Alberto Ornaghi Lorenzo Cavallaro.
Buffer Overflow 공격의 이해 송치현 제 11회 해킹캠프.
The Runtime Environment CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1 Linking. 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7.
CS429 Computer Architecture Topics Simple C program Basic structure, functions, separate files Compilation Phases, options Assembler GNU style, byte ordering,
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
Buffer Overflow Attacks 1 Basic Idea Sample Attacks Protection , Computer & Network Security.
OUTLINE 2 Pre-requisite Bomb! Pre-requisite Bomb! 3.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Structured Data –struct s / union s –Arrays of structs.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Buffer Overflow Attacks
Recitation 3: Procedures and the Stack
Computer Architecture and Assembly Language
Homework Reading Machine Projects Labs PAL, pp ,
Exploiting & Defense Day 2 Recap
CS 5204 Operating Systems Linking and Loading Godmar Back.
Computer Architecture and Assembly Language
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
CS 4284 Systems Capstone Linking and Loading Godmar Back.
Assembly Language Programming II: C Compiler Calling Sequences
The Runtime Environment
Understanding Program Address Space
Miscellaneous functions
The Runtime Environment
Machine-Level Programming: Introduction
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
Lecture 2 SCOPE – Local and Global variables
Foundations of Network and Computer Security
X86 Assembly Review.
Instructors: Majd Sakr and Khaled Harras
Functions Reasons Concepts Passing arguments to a function
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2010.
Presentation transcript:

UBC104 Embedded Systems Functions & Pointers

Introduction to C - Arrays, Functions & Pointers <return-type> <function-name> (<param-list>) { <expr> } Example: int fname (int number, void* ptr) { return 0; Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Example Program I 1 void hello(int a, int b) { 2 3 } 4 5 int main(int argc, char** argv) { 6 7 hello(1,2); 8 } (gdb) disass hello Dump of assembler code for function hello: 0x08048354 <hello+0>: push %ebp 0x08048355 <hello+1>: mov %esp,%ebp 0x08048357 <hello+3>: pop %ebp 0x08048358 <hello+4>: ret Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Example Program II (gdb) disass main Dump of assembler code for function main: 0x08048359 <main+0>: push %ebp 0x0804835a <main+1>: mov %esp,%ebp 0x0804835c <main+3>: sub $0x8,%esp 0x0804835f <main+6>: and $0xfffffff0,%esp 0x08048362 <main+9>: mov $0x0,%eax 0x08048367 <main+14>: sub %eax,%esp 0x08048369 <main+16>: movl $0x2,0x4(%esp)‏ 0x08048371 <main+24>: movl $0x1,(%esp)‏ 0x08048378 <main+31>: call 0x8048354 <hello> 0x0804837d <main+36>: leave 0x0804837e <main+37>: ret Put parameter onto stack in reverse order and call address of function Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Example Program III 1 void hello(int *a, int *b) { 2 3 } 4 5 int main(int argc, char** argv) { 6 int i, j; 7 8 i= 1; j= 2; 9 hello(&i, &j); 10 } Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Parameters Pass by value Values of parameters are passed on the stack Pass by reference Addresses of parameter are passed on the stack hello(1, 2); i= 1; j= 2; hello(&i,&j); Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Parameters Pass by value Values of parameters are passed on the stack Pass by reference Addresses of parameter are passed on the stack 0x08048369 <main+16>: movl $0x2,0x4(%esp)‏ 0x08048371 <main+24>: movl $0x1,(%esp)‏ 0x08048378 <main+31>: call 0x8048354 <hello> 0x08048377 <main+30>: lea 0xfffffff8(%ebp),%eax 0x0804837a <main+33>: mov %eax,0x4(%esp)‏ 0x0804837e <main+37>: lea 0xfffffffc(%ebp),%eax 0x08048381 <main+40>: mov %eax,(%esp)‏ 0x08048384 <main+43>: call 0x8048354 <hello> Introduction to C - Arrays, Functions & Pointers

Variable Parameter-List <return-type> <function-name>( <param-list>,…) { <expr> } Example: int foobar(int number, …) { } foobar(3,“Foo”, “Bar”, ”!”); Introduction to C - Arrays, Functions & Pointers

Variable argumentlist - Functions #include <stdarg.h> void va_start(va_list ap, last) :  initializes the argument list ap type va_arg(va_list ap, type) :  returns the next argument in the list void va_end(va_list ap) :  terminates the use of the argument list ap Introduction to C - Arrays, Functions & Pointers

Example: Variable Parameter-List void foobar(int a, ...) { va_list ap; char *s; va_start(ap, a); s = va_arg(ap, char *); printf("Parameter 1: %s\n", s); va_end(ap); } 0x08048369 <main+16>: movl $0x80484a4,0xc(%esp)‏ 0x08048371 <main+24>: movl $0x80484a6,0x8(%esp)‏ 0x08048379 <main+32>: movl $0x80484aa,0x4(%esp)‏ 0x08048381 <main+40>: movl $0x3,(%esp)‏ 0x08048388 <main+47>: call 0x8048354 <foobar> Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Pointers to Functions <return-type> (*<function-name>) (<param-list>)‏ Declaration: int (*foobar_ptr) (int number, void *ptr); typedef int (*mainprt) (int argc, char** arg); Example: foobar_ptr= foobar; foobar_ptr(); Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Pointer-to-Function void hello(int a, int b) { } int main(int argc, char** argv) { void (*func)(int, int); func= hello; func(1,2); Introduction to C - Arrays, Functions & Pointers

Call of Pointer-to-Function (gdb) disass main Dump of assembler code for function main: 0x08048359 <main+0>: push %ebp 0x0804835a <main+1>: mov %esp,%ebp 0x0804835c <main+3>: sub $0x18,%esp 0x0804835f <main+6>: and $0xfffffff0,%esp 0x08048362 <main+9>: mov $0x0,%eax 0x08048367 <main+14>: sub %eax,%esp 0x08048369 <main+16>: movl $0x8048354,0xfffffffc(%ebp)‏ 0x08048370 <main+23>: movl $0x2,0x4(%esp)‏ 0x08048378 <main+31>: movl $0x1,(%esp)‏ 0x0804837f <main+38>: mov 0xfffffffc(%ebp),%eax 0x08048382 <main+41>: call *%eax 0x08048384 <main+43>: leave 0x08048385 <main+44>: ret Introduction to C - Arrays, Functions & Pointers

Example: Pointers-to-Functions struct flist { void (*printstring) (char *s); void (*printnumber) (int n); } typedef struct flist flist; void printer_pstring(char *s) { flist f1, f2; f1.printstring= printer_pstring; f2.printstring= screen_pstring; Introduction to C - Arrays, Functions & Pointers

Summary: Functions & Pointers Pointers contain Addresses! Addresses can reference data OR functions Calls to functions are jumps to addresses It does not matter where this address comes from Introduction to C - Arrays, Functions & Pointers

Introduction to C - Arrays, Functions & Pointers Recommended Reading “A tutorial on pointers and arrays in C” by Ted Jensen http://www.netcom.com/~tjensen/ptr/cpoint.htm Introduction to C - Arrays, Functions & Pointers