Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Unions and Memory layout.

Slides:



Advertisements
Similar presentations
Smashing the Stack for Fun and Profit
Advertisements

Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
MIPS Assembly Language Programming
Informática II Prof. Dr. Gustavo Patiño MJ
Carnegie Mellon 1 Machine-Level Programming V: Advanced Topics : Introduction to Computer Systems 8 th Lecture, Sep. 16, 2010 Instructors: Randy.
C and Data Structures Baojian Hua
Structured Data II Heterogenous Data Sept. 22, 1998 Topics Structure Allocation Alignment Operating on Byte Strings Unions Byte Ordering Alpha Memory Organization.
Memory Layout C and Data Structures Baojian Hua
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Lecture 6: Buffer Overflow CS 436/636/736 Spring 2014 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
Understand stack Buffer overflow attack and defense Controls against program threats.
University of Washington Today Memory layout Buffer overflow, worms, and viruses 1.
Carnegie Mellon 1 Saint Louis University Virtual Memory CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides adapted from Bryant.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Machine-Level Programming 6 Structured Data Topics Structs Unions.
Introduction: Exploiting Linux. Basic Concepts Vulnerability A flaw in a system that allows an attacker to do something the designer did not intend,
1 Machine-Level Programming V: Advanced Topics Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Ithaca College 1 Machine-Level Programming VIII: Advanced Topics: alignment & Unions Comp 21000: Introduction to Computer Systems & Assembly Lang Systems.
Machine-Level Programming V: Advanced Topics CS304
Machine-Level Programming IV: Structured Data Topics Arrays Structs Unions CS 105 “Tour of the Black Holes of Computing”
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.
Lecture 8: Buffer Overflow CS 436/636/736 Spring 2013 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
Machine-Level Programming V: Miscellaneous Topics
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Buffer overflow Slides.
University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.
Machine-Level Programming V: Advanced Topics
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Topics: Arrays.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data MCS284: Computer.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted.
Lecture 2 Two’s Complement Proofs Topics Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Carnegie Mellon 1 Machine-Level Programming V: Advanced Topics / : Introduction to Computer Systems 9 th Lecture, Sep. 25, 2012 Instructors:
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Course Overview Introduction to Computer Systems 1.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Carnegie Mellon 1 Machine-Level Programming V: Advanced Topics Slides courtesy of: Randy Bryant & Dave O’Hallaron.
Instructors: Randal E. Bryant and David R. O’Hallaron
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
Machine-Level Programming V: Advanced Topics CSCE 312
Data in Memory variables have multiple attributes symbolic name
Credits and Disclaimers
Course Overview CSE 238/2038/2138: Systems Programming
The Hardware/Software Interface CSE351 Winter 2013
Machine-Level Programming V: Miscellaneous Topics
Machine-Level Programming V: Advanced Topics : Introduction to Computer Systems 9th Lecture, Feb. 9, 2016 Instructors: Franz Franchetti & Seth Copen.
Machine-Level Programming V: Unions and Memory layout
Machine-Level Programming V: Miscellaneous Topics
Machine-Level Programming IV: Structured Data
Machine-Level Programming V: Miscellaneous Topics
Instructors: Franz Franchetti, Seth Copen Goldstein, and Brian Railing
Machine-Level Programming V: Advanced Topics /18-213/14-513/15-513: Introduction to Computer Systems 9th Lecture, September 25, 2018.
Machine-Level Programming 6 Structured Data
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Structured Data II Heterogenous Data Feb. 15, 2000
Memory Allocation CS 217.
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming V: Advanced Topics
Lecture 9: Buffer Overflow*
Computer Architecture and System Programming Laboratory
Instructors: Majd Sakr and Khaled Harras
Instructor: Brian Railing
Presentation transcript:

Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Unions and Memory layout Slides adapted from Bryant and O’Hallaron

Carnegie Mellon 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today Unions Memory layout

Carnegie Mellon 3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition C unions All members in a union start at the same location  i.e. only one member can contain a value at a given time union { unsigned char bytes[4]; int x; } U; union { unsigned char bytes[4]; int x; } U; U.x = 0x ; U.bytes[3] = ?? U.x = 0x ; U.bytes[3] = ??

Carnegie Mellon 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Byte Ordering Example union { unsigned char c[8]; unsigned short s[4]; unsigned int i[2]; unsigned long l[1]; } dw; 64-bit

Carnegie Mellon 5 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Byte Ordering on x86-64 Little Endian U.l[0] = 0xf7f6f5f4f3f2f1f0 U.i[0] = ?? U.s[0] = ?? U.c[0]?? Output on x86-64: LSB MSB

Carnegie Mellon 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition typedef union { float f; unsigned u; } bit_float_t; typedef union { float f; unsigned u; } bit_float_t; float bit2float(unsigned u) { bit_float_t arg; arg.u = u; return arg.f; } float bit2float(unsigned u) { bit_float_t arg; arg.u = u; return arg.f; } unsigned float2bit(float f) { bit_float_t arg; arg.f = f; return arg.u; } unsigned float2bit(float f) { bit_float_t arg; arg.f = f; return arg.u; } Using Union to Access Bit Patterns Same as (float) u ?Same as (unsigned) f ?

Carnegie Mellon 7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Union Allocation Allocate according to largest element union U1 { char c; int i[2]; double v; } u; union U1 { char c; int i[2]; double v; } u; struct S1 { char c; int i[2]; double v; } s; struct S1 { char c; int i[2]; double v; } s; c 3 bytes i[0]i[1] 4 bytes v x+0x+4x+8x+16x+24 c i[0]i[1] v x+0x+4x+8

Carnegie Mellon 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Unions in assembly typedef union { char c; int i[2]; double v; } u_t; int get_member(u_t *u, int first) { if (first) { return U->c; }else{ return U->i[1]; } typedef union { char c; int i[2]; double v; } u_t; int get_member(u_t *u, int first) { if (first) { return U->c; }else{ return U->i[1]; } testl %esi, %esi je.L2 movsbl (%rdi), %eax ret.L2: movl 4(%rdi), %eax ret testl %esi, %esi je.L2 movsbl (%rdi), %eax ret.L2: movl 4(%rdi), %eax ret

Carnegie Mellon 9 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today Unions Memory layout

Carnegie Mellon 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition x86-64 Linux Memory Layout Stack  Runtime stack (8MB limit)  E. g., local variables Heap  Dynamically allocated as needed  When call malloc(), calloc(), new() Data  Statically allocated data  E.g., global vars, static vars, string constants Text / Shared Libraries  Executable machine instructions  Read-only Hex Address 00007FFFFFFFFFFF Stack Text Data Heap MB not drawn to scale Shared Libraries

Carnegie Mellon 11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Memory Allocation Example char big_array[1L<<24]; /* 16 MB */ char huge_array[1L<<31]; /* 2 GB */ int global = 0; int useless() { return 0; } int main () { void *p1, *p2, *p3, *p4; int local = 0; p1 = malloc(1L << 28); /* 256 MB */ p2 = malloc(1L << 8); /* 256 B */ p3 = malloc(1L << 32); /* 4 GB */ p4 = malloc(1L << 8); /* 256 B */ … } not drawn to scale Where does everything go? Stack Text Data Heap Shared Libraries

Carnegie Mellon 12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition x86-64 Example Addresses local0x00007ffe4d3be87c p1 0x00007f7262a1e010 p3 0x00007f7162a1d010 p40x d120 p20x d010 big_array 0x huge_array 0x main()0x c useless() 0x address range ~ F Text Data Heap not drawn to scale Heap Stack

Carnegie Mellon 13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Segmentation Fault Each memory segment can be readable, executable, writable (or none at all) Segmentation fault occurs when program tries to access illegal memory  Read from segment with no permission  Write to read-only segments Text Data Heap Stack Read-only data r/x r/w -

Carnegie Mellon 14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Segmentation fault example char str1[100] = ”hello world1”; int main () { char *str2 = ”hello world2”; printf(”str1 %p str2 %p\n”); str1[0] = ’H’; str2[0] = ’H’ … }

Carnegie Mellon 15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Not all Bad Memory Access lead to immediate segmentation  Result is system specific fun(0) ➙ 3.14 fun(1) ➙ 3.14 fun(2) ➙ fun(3) ➙ fun(4) ➙ 3.14 fun(6) ➙ Segmentation fault typedef struct { int a[2]; double d; } struct_t; double fun(int i) { volatile struct_t s; s.d = 3.14; s.a[i] = ; /* Possibly out of bounds */ return s.d; }

Carnegie Mellon 16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Memory Referencing Bug Example typedef struct { int a[2]; double d; } struct_t; fun(0) ➙ 3.14 fun(1) ➙ 3.14 fun(2) ➙ fun(3) ➙ fun(4) ➙ 3.14 fun(6) ➙ Segmentation fault Location accessed by fun(i) Critical State6 ? 5 ? 4 d7... d4 3 d3... d0 2 a[1] 1 a[0] 0 struct_t

Carnegie Mellon 17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Such problems are a BIG deal Generally called a “buffer overflow”  when exceeding the memory size allocated for an array Why a big deal?  It’s the #1 technical cause of security vulnerabilities  #1 overall cause is social engineering / user ignorance Most common form  Unchecked lengths on string inputs  Particularly for bounded character arrays on the stack  sometimes referred to as stack smashing