carnegie mellon 1 today arrays one-dimensional multi-dimensional (nested) structures buffer...
TRANSCRIPT
![Page 1: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/1.jpg)
Carnegie Mellon
1
Today Arrays
One-dimensional Multi-dimensional (nested)
Structures Buffer Overflow
![Page 2: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/2.jpg)
Carnegie Mellon
2
Basic Data Types Integral
Stored & operated on in general (integer) registers Signed vs. unsigned depends on instructions used
Intel ASM Bytes Cbyte b 1 [unsigned] charword w 2 [unsigned] shortdouble word l 4 [unsigned] intquad word q 8 [unsigned] long int (x86-64)
Floating Point Stored & operated on in floating point registers
Intel ASM Bytes CSingle s 4 floatDouble l 8 doubleExtended t 10/12/16 long double
![Page 3: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/3.jpg)
Carnegie Mellon
3
Array Allocation Basic Principle
T A[L]; Array of data type T and length L Contiguously allocated region of L * sizeof(T) bytes
char string[12];
x x + 12
int val[5];
x x + 4 x + 8 x + 12 x + 16 x + 20
double a[3];
x + 24x x + 8 x + 16
char *p[3];
x x + 8 x + 16 x + 24
x x + 4 x + 8 x + 12
IA32
x86-64
![Page 4: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/4.jpg)
Carnegie Mellon
4
Array Access Basic Principle
T A[L]; Array of data type T and length L Identifier A can be used as a pointer to array element 0: Type T*
Reference Type Valueval[4] int 3val int * xval+1 int * x + 4&val[2] int * x + 8val[5] int ??*(val+1)int 5val + i int * x + 4 i
int val[5]; 1 5 2 1 3
x x + 4 x + 8 x + 12 x + 16 x + 20
![Page 5: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/5.jpg)
Carnegie Mellon
5
Array Example
Declaration “zip_dig cmu” equivalent to “int cmu[5]” Example arrays were allocated in successive 20 byte blocks
Not guaranteed to happen in general
#define ZLEN 5typedef int zip_dig[ZLEN];
zip_dig cmu = { 1, 5, 2, 1, 3 };zip_dig mit = { 0, 2, 1, 3, 9 };zip_dig ucb = { 9, 4, 7, 2, 0 };
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
zip_dig mit; 0 2 1 3 9
36 40 44 48 52 56
zip_dig ucb; 9 4 7 2 0
56 60 64 68 72 76
![Page 6: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/6.jpg)
Carnegie Mellon
6
Array Accessing Example
Register %edx contains starting address of array
Register %eax contains array index
Desired digit at 4*%eax + %edx
Use memory reference (%edx,%eax,4)
int get_digit (zip_dig z, int dig){ return z[dig];}
# %edx = z # %eax = dig
movl (%edx,%eax,4),%eax # z[dig]
IA32
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
![Page 7: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/7.jpg)
Carnegie Mellon
7
# edx = zmovl $0, %eax # %eax = i
.L4: # loop:addl $1, (%edx,%eax,4) # z[i]++addl $1, %eax # i++cmpl $5, %eax # i:5jne .L4 # if !=, goto loop
Array Loop Example (IA32)
void zincr(zip_dig z) { int i; for (i = 0; i < ZLEN; i++) z[i]++;}
![Page 8: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/8.jpg)
Carnegie Mellon
8
Pointer Loop Example (IA32)void zincr_p(zip_dig z) { int *zend = z+ZLEN; do { (*z)++; z++; } while (z != zend); }
void zincr_v(zip_dig z) { void *vz = z; int i = 0; do { (*((int *) (vz+i)))++; i += ISIZE; } while (i != ISIZE*ZLEN);}
# edx = z = vzmovl $0, %eax # i = 0
.L8: # loop:addl $1, (%edx,%eax) # Increment vz+iaddl $4, %eax # i += 4cmpl $20, %eax # Compare i:20jne .L8 # if !=, goto loop
![Page 9: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/9.jpg)
Carnegie Mellon
9
Nested Array Example
“zip_dig pgh[4]” equivalent to “int pgh[4][5]” Variable pgh: array of 4 elements, allocated contiguously Each element is an array of 5 int’s, allocated contiguously
“Row-Major” ordering of all elements guaranteed
#define PCOUNT 4zip_dig pgh[PCOUNT] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }};
zip_digpgh[4];
76 96 116 136 156
1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1
![Page 10: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/10.jpg)
Carnegie Mellon
10
Multidimensional (Nested) Arrays Declaration
T A[R][C]; 2D array of data type T R rows, C columns Type T element requires K bytes
Array Size R * C * K bytes
Arrangement Row-Major Ordering
A[0][0] A[0][C-1]
A[R-1][0]
• • •
• • • A[R-1][C-1]
•••
•••
int A[R][C];
• • •A[0][0]
A[0]
[C-1]• • •
A[1][0]
A[1]
[C-1]• • •
A[R-1][0]
A[R-1][C-1]
• • •
4*R*C Bytes
![Page 11: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/11.jpg)
Carnegie Mellon
11
• • •
Nested Array Row Access Row Vectors
A[i] is array of C elements Each element of type T requires K bytes Starting address A + i * (C * K)
• • •A[i][0]
A[i]
[C-1]
A[i]
• • •A
[R-1][0]
A[R-1][C-1]
A[R-1]
• • •
A
• • •A[0][0]
A[0]
[C-1]
A[0]
A+i*C*4 A+(R-1)*C*4
int A[R][C];
![Page 12: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/12.jpg)
Carnegie Mellon
12
Nested Array Row Access Code
Row Vector pgh[index] is array of 5 int’s Starting address pgh+20*index
IA32 Code Computes and returns address Compute as pgh + 4*(index+4*index)
int *get_pgh_zip(int index){ return pgh[index];}
# %eax = indexleal (%eax,%eax,4),%eax # 5 * indexleal pgh(,%eax,4),%eax # pgh + (20 * index)
#define PCOUNT 4zip_dig pgh[PCOUNT] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }};
![Page 13: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/13.jpg)
Carnegie Mellon
13
• • •
Nested Array Row Access Array Elements
A[i][j] is element of type T, which requires K bytes Address A + i * (C * K) + j * K = A + (i * C + j)* K
• • • • • •A[i][j]
A[i]
• • •A
[R-1][0]
A[R-1][C-1]
A[R-1]
• • •
A
• • •A[0][0]
A[0]
[C-1]
A[0]
A+i*C*4 A+(R-1)*C*4
int A[R][C];
A+i*C*4+j*4
![Page 14: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/14.jpg)
Carnegie Mellon
14
Nested Array Element Access Code
Array Elements pgh[index][dig] is int Address: pgh + 20*index + 4*dig
= pgh + 4*(5*index + dig) IA32 Code
Computes address pgh + 4*((index+4*index)+dig)
int get_pgh_digit (int index, int dig){ return pgh[index][dig];}
movl 8(%ebp), %eax # indexleal (%eax,%eax,4), %eax # 5*indexaddl 12(%ebp), %eax # 5*index+digmovl pgh(,%eax,4), %eax # offset 4*(5*index+dig)
![Page 15: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/15.jpg)
Carnegie Mellon
15
Today Arrays Structures
Allocation Access Alignment
Buffer Overflow
![Page 16: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/16.jpg)
Carnegie Mellon
16
struct rec { int a[3]; int i; struct rec *n;};
Structure Allocation
Concept Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types
Memory Layoutia n
0 12 16 20
![Page 17: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/17.jpg)
Carnegie Mellon
17
struct rec { int a[3]; int i; struct rec *n;};
IA32 Assembly# %edx = val# %eax = rmovl %edx, 12(%eax) # Mem[r+12] = val
void set_i(struct rec *r, int val){ r->i = val;}
Structure Access
Accessing Structure Member Pointer indicates first byte of structure Access elements with offsets
ia n
0 12 16 20
r+12r
![Page 18: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/18.jpg)
Carnegie Mellon
18
movl 12(%ebp), %eax # Get idxsall $2, %eax # idx*4addl 8(%ebp), %eax # r+idx*4
int *get_ap (struct rec *r, int idx){ return &r->a[idx];}
Generating Pointer to Structure Member
Generating Pointer to Array Element Offset of each structure
member determined at compile time
Arguments Mem[%ebp+8]: r Mem[%ebp+12]: idx
r+idx*4r
ia n
0 12 16 20
struct rec { int a[3]; int i; struct rec *n;};
![Page 19: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/19.jpg)
Carnegie Mellon
19
.L17: # loop: movl 12(%edx), %eax # r->i movl %ecx, (%edx,%eax,4) # r->a[i] = val movl 16(%edx), %edx # r = r->n testl %edx, %edx # Test r jne .L17 # If != 0 goto loop
void set_val (struct rec *r, int val){ while (r) { int i = r->i; r->a[i] = val; r = r->n; }}
Following Linked List C Code
struct rec { int a[3]; int i; struct rec *n;};
ia n
0 12 16 20
Element i
Register Value
%edx r
%ecx val
![Page 20: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/20.jpg)
Carnegie Mellon
20
Structures & Alignment Unaligned Data
Aligned Data Primitive data type requires K bytes Address must be multiple of K
c i[0] i[1] v3 bytes 4 bytes
p+0 p+4 p+8 p+16 p+24
Multiple of 4 Multiple of 8
Multiple of 8
Multiple of 8
c i[0] i[1] v
p p+1 p+5 p+9 p+17
struct S1 { char c; int i[2]; double v;} *p;
struct S1 { char c; int i[2]; double v;} *p;
![Page 21: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/21.jpg)
Carnegie Mellon
21
Alignment Principles Aligned Data
Primitive data type requires K bytes Address must be multiple of K Required on some machines; advised on IA32
treated differently by IA32 Linux, x86-64 Linux, and Windows! Motivation for Aligning Data
Memory accessed by (aligned) chunks of 4 or 8 bytes (system dependent) Inefficient to load or store datum that spans quad word
boundaries Virtual memory very tricky when datum spans 2 pages
Compiler Inserts gaps in structure to ensure correct alignment of fields
![Page 22: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/22.jpg)
Carnegie Mellon
22
Specific Cases of Alignment (IA32) 1 byte: char, …
no restrictions on address 2 bytes: short, …
lowest 1 bit of address must be 02
4 bytes: int, float, char *, … lowest 2 bits of address must be 002
8 bytes: double, … Windows (and most other OS’s & instruction sets):
lowest 3 bits of address must be 0002
Linux: lowest 2 bits of address must be 002
i.e., treated the same as a 4-byte primitive data type 12 bytes: long double
Windows, Linux: lowest 2 bits of address must be 002
i.e., treated the same as a 4-byte primitive data type
![Page 23: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/23.jpg)
Carnegie Mellon
23
struct S1 { char c; int i[2]; double v;} *p;
struct S1 { char c; int i[2]; double v;} *p;
Satisfying Alignment with Structures Within structure:
Must satisfy each element’s alignment requirement Overall structure placement
Each structure has alignment requirement K K = Largest alignment of any element
Initial address & structure length must be multiples of K Example (under Windows or x86-64):
K = 8, due to double element
c i[0] i[1] v3 bytes 4 bytes
p+0 p+4 p+8 p+16 p+24
Multiple of 4 Multiple of 8
Multiple of 8
Multiple of 8
![Page 24: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/24.jpg)
Carnegie Mellon
24
Different Alignment Conventions x86-64 or IA32 Windows:
K = 8, due to double element
IA32 Linux K = 4; double treated like a 4-byte data type
struct S1 { char c; int i[2]; double v;} *p;
struct S1 { char c; int i[2]; double v;} *p;
c 3 bytes i[0] i[1] 4 bytes v
p+0 p+4 p+8 p+16 p+24
c 3 bytes i[0] i[1] v
p+0 p+4 p+8 p+12 p+20
![Page 25: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/25.jpg)
Carnegie Mellon
25
Meeting Overall Alignment Requirement
For largest alignment requirement K Overall structure must be multiple of K
struct S2 { double v; int i[2]; char c;} *p;
struct S2 { double v; int i[2]; char c;} *p;
v i[0] i[1] c 7 bytes
p+0 p+8 p+16 p+24
![Page 26: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/26.jpg)
Carnegie Mellon
26
Arrays of Structures Overall structure length
multiple of K Satisfy alignment requirement
for every element
struct S2 { double v; int i[2]; char c;} a[10];
struct S2 { double v; int i[2]; char c;} a[10];
v i[0] i[1] c 7 bytes
a+24 a+32 a+40 a+48
a[0] a[1] a[2] • • •
a+0 a+24 a+48 a+72
![Page 27: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/27.jpg)
Carnegie Mellon
27
Accessing Array Elements Compute array offset 12i
sizeof(S3), including alignment spacers Element j is at offset 8 within structure Assembler gives offset a+8
Resolved during linking
struct S3 { short i; float v; short j;} a[10];
struct S3 { short i; float v; short j;} a[10];
short get_j(int idx){ return a[idx].j;}
short get_j(int idx){ return a[idx].j;}
# %eax = idxleal (%eax,%eax,2),%eax # 3*idxmovswl a+8(,%eax,4),%eax
# %eax = idxleal (%eax,%eax,2),%eax # 3*idxmovswl a+8(,%eax,4),%eax
a[0] • • • a[i] • • •a+0 a+12 a+12i
i 2 bytes
v j 2 bytes
a+12i a+12i+8
![Page 28: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/28.jpg)
Carnegie Mellon
28
Saving Space Put large data types first
Effect (K=4)
struct S4 { char c; int i; char d;} *p;
struct S4 { char c; int i; char d;} *p;
struct S5 { int i; char c; char d;} *p;
struct S5 { int i; char c; char d;} *p;
c i3 bytes d 3 bytes
ci d 2 bytes
![Page 29: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/29.jpg)
Carnegie Mellon
29
Today Arrays Structures Buffer Overflow
Vulnerability Protection
![Page 30: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/30.jpg)
Carnegie Mellon
30
Internet Worm and IM War November, 1988
Internet Worm attacks thousands of Internet hosts. How did it happen?
![Page 31: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/31.jpg)
Carnegie Mellon
31
Internet Worm and IM War November, 1988
Internet Worm attacks thousands of Internet hosts. How did it happen?
July, 1999 Microsoft launches MSN Messenger (instant messaging system). Messenger clients can access popular AOL Instant Messaging Service
(AIM) servers
AIMserver
AIMclient
AIMclient
MSNclient
MSNserver
![Page 32: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/32.jpg)
Carnegie Mellon
32
Internet Worm and IM War (cont.) August 1999
Mysteriously, Messenger clients can no longer access AIM servers. Microsoft and AOL begin the IM war:
AOL changes server to disallow Messenger clients Microsoft makes changes to clients to defeat AOL changes. At least 13 such skirmishes.
How did it happen?
The Internet Worm and AOL/Microsoft War were both based on stack buffer overflow exploits!
many library functions do not check argument sizes. allows target buffers to overflow.
![Page 33: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/33.jpg)
Carnegie Mellon
33
String Library Code Implementation of Unix function gets()
No way to specify limit on number of characters to read Similar problems with other library functions
strcpy, strcat: Copy strings of arbitrary length scanf, fscanf, sscanf, when given %s conversion specification
/* Get string from stdin */char *gets(char *dest){ int c = getchar(); char *p = dest; while (c != EOF && c != '\n') { *p++ = c; c = getchar(); } *p = '\0'; return dest;}
![Page 34: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/34.jpg)
Carnegie Mellon
34
Vulnerable Buffer Code
void call_echo() { echo();}
/* Echo Line */void echo(){ char buf[4]; /* Way too small! */ gets(buf); puts(buf);}
unix>./bufdemoType a string:123123
unix>./bufdemoType a string:123456789ABC123456789ABCSegmentation Fault
unix>./bufdemoType a string:12345671234567
![Page 35: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/35.jpg)
Carnegie Mellon
35
Buffer Overflow Disassembly 80485c5: 55 push %ebp 80485c6: 89 e5 mov %esp,%ebp 80485c8: 53 push %ebx 80485c9: 83 ec 14 sub $0x14,%esp 80485cc: 8d 5d f8 lea 0xfffffff8(%ebp),%ebx 80485cf: 89 1c 24 mov %ebx,(%esp) 80485d2: e8 9e ff ff ff call 8048575 <gets> 80485d7: 89 1c 24 mov %ebx,(%esp) 80485da: e8 05 fe ff ff call 80483e4 <puts@plt> 80485df: 83 c4 14 add $0x14,%esp 80485e2: 5b pop %ebx 80485e3: 5d pop %ebp 80485e4: c3 ret
80485eb: e8 d5 ff ff ff call 80485c5 <echo> 80485f0: c9 leave 80485f1: c3 ret
call_echo:
echo:
![Page 36: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/36.jpg)
Carnegie Mellon
36
Buffer Overflow Stack
echo:pushl %ebp # Save %ebp on stackmovl %esp, %ebppushl %ebx # Save %ebxsubl $20, %esp # Allocate stack spaceleal -8(%ebp),%ebx # Compute buf as %ebp-8movl %ebx, (%esp) # Push buf on stackcall gets # Call gets. . .
/* Echo Line */void echo(){ char buf[4]; /* Way too small! */ gets(buf); puts(buf);}
Return AddressSaved %ebp %ebp
Stack Framefor main
Stack Framefor echo
[3][2][1][0] buf
Before call to gets
Saved %ebx
![Page 37: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/37.jpg)
Carnegie Mellon
37
Buffer Overflow Stack Example
unix> gdb bufdemo(gdb) b echoBreakpoint 1, 0x0804841a in echo ()(gdb) rBreakpoint 1, 0x0804841a in echo ()(gdb) p/x $ebp$1 = 0xbffff778(gdb) x/x ($ebp)0xbffff778: 0xbffff788(gdb) x/x ($ebp+4)0xbffff77c: 0x0804843d
8048438: e8 d7 ff ff ff call 8048414 <echo> 804843d: c9 leave
0xbffff778
buf
0xbffff788
Return AddressSaved %ebp
Stack Framefor main
Stack Framefor echo
[3][2][1][0]
Stack Framefor main
Stack Framefor echo
xx xx xx xx buf
bf ff f7 8808 04 84 3d
Before call to gets Before call to gets
Saved %ebx Saved %ebx
![Page 38: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/38.jpg)
Carnegie Mellon
38
Buffer Overflow Example #1
Overflow buf, and corrupt %ebx,but no problem
Stack Framefor echo
xx xx xx xx buf
Stack Framefor echo
34 33 32 31 buf00 37 36 35
Before call to gets Input 1234567
0xbffff778
0xbfff788Stack Framefor main
bf ff f7 8808 04 84 3d
0xbffff778
0xbffff788Stack Framefor main
bf ff f7 8808 04 84 3d
Saved %ebx
![Page 39: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/39.jpg)
Carnegie Mellon
39
Buffer Overflow Example #2
Base pointer corrupted
Stack Framefor echo
34 33 32 31 buf
0038 37 36 35
Input 12345678
. . . 8048438: e8 d7 ff ff ff call 8048414 <echo> 804843d: c9 leave # Set %ebp to corrupted value 804843e: c3 ret
0xbffff778
0xbffff788Stack Framefor main
bf ff f708 04 84 3d
Stack Framefor echo
xx xx xx xx buf
Before call to gets
0xbffff778
0xbffff788Stack Framefor main
bf ff f7 8808 04 84 3d
Saved %ebx
![Page 40: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/40.jpg)
Carnegie Mellon
40
Buffer Overflow Example #3
Return address corrupted
Stack Framefor echo
34 33 32 31 buf
43 42 41 3900
38 37 36 35
Input 123456789ABC
8048438: e8 d7 ff ff ff call 8048414 <echo> 804843d: c9 leave # Desired return point
0xbffff778
0xbffff788Stack Framefor main
08 04 84
Stack Framefor echo
xx xx xx xx buf
Before call to gets
0xbffff778
0xbffff788Stack Framefor main
bf ff f7 8808 04 84 3d
Saved %ebx
![Page 41: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/41.jpg)
Carnegie Mellon
41
Malicious Use of Buffer Overflow
Input string contains byte representation of executable codeOverwrite return address A with address of buffer BWhen bar() executes ret, will jump to exploit code
int bar() { char buf[64]; gets(buf); ... return ...; }
void foo(){ bar(); ...}
Stack after call to gets()
B
returnaddressA
foo stack frame
bar stack frame
B
exploitcode
paddata writtenby gets()
![Page 42: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/42.jpg)
Carnegie Mellon
42
Exploits Based on Buffer Overflows Buffer overflow bugs allow remote machines to execute
arbitrary code on victim machines Internet worm
Early versions of the finger server (fingerd) used gets() to read the argument sent by the client:
finger [email protected] Worm attacked fingerd server by sending phony argument:
finger “exploit-code padding new-return-address”
exploit code: executed a root shell on the victim machine with a direct TCP connection to the attacker.
![Page 43: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/43.jpg)
Carnegie Mellon
43
Exploits Based on Buffer Overflows Buffer overflow bugs allow remote machines to execute
arbitrary code on victim machines IM War
AOL exploited existing buffer overflow bug in AIM clients exploit code: returned 4-byte signature (the bytes at some location in
the AIM client) to server. When Microsoft changed code to match signature, AOL changed
signature location.
![Page 44: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/44.jpg)
Carnegie Mellon
44
Date: Wed, 11 Aug 1999 11:30:57 -0700 (PDT) From: Phil Bucking <[email protected]> Subject: AOL exploiting buffer overrun bug in their own software! To: [email protected]
Mr. Smith,
I am writing you because I have discovered something that I think you might find interesting because you are an Internet security expert with experience in this area. I have also tried to contact AOL but received no response.
I am a developer who has been working on a revolutionary new instant messaging client that should be released later this year....It appears that the AIM client has a buffer overrun bug. By itself this might not be the end of the world, as MS surely has had its share. But AOL is now *exploiting their own buffer overrun bug* to help in its efforts to block MS Instant Messenger.....Since you have significant credibility with the press I hope that youcan use this information to help inform people that behind AOL'sfriendly exterior they are nefariously compromising peoples' security.
Sincerely,Phil Bucking Founder, Bucking Consulting [email protected]
It was later determined that this email originated from within Microsoft!
![Page 45: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/45.jpg)
Carnegie Mellon
45
Code Red Exploit Code Starts 100 threads running Spread self
Generate random IP addresses & send attack string Between 1st & 19th of month
Attack www.whitehouse.gov Send 98,304 packets; sleep for 4-1/2 hours; repeat
Denial of service attack Between 21st & 27th of month
Deface server’s home page After waiting 2 hours
![Page 46: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/46.jpg)
Carnegie Mellon
46
Avoiding Overflow Vulnerability
Use library routines that limit string lengths fgets instead of gets strncpy instead of strcpy Don’t use scanf with %s conversion specification
Use fgets to read the string Or use %ns where n is a suitable integer
/* Echo Line */void echo(){ char buf[4]; /* Way too small! */ fgets(buf, 4, stdin); puts(buf);}
![Page 47: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/47.jpg)
Carnegie Mellon
47
System-Level Protections
unix> gdb bufdemo(gdb) break echo
(gdb) run(gdb) print /x $ebp$1 = 0xffffc638
(gdb) run(gdb) print /x $ebp$2 = 0xffffbb08
(gdb) run(gdb) print /x $ebp$3 = 0xffffc6a8
Randomized stack offsets At start of program, allocate random amount
of space on stack Makes it difficult for hacker to predict
beginning of inserted code
Nonexecutable code segments In traditional x86, can mark region of memory
as either “read-only” or “writeable” Can execute anything readable
X86-64 added explicit “execute” permission
![Page 48: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/48.jpg)
Carnegie Mellon
48
Stack Canaries Idea
Place special value (“canary”) on stack just beyond buffer Check for corruption before exiting function
GCC Implementation -fstack-protector -fstack-protector-all
unix>./bufdemo-protectedType a string:12341234
unix>./bufdemo-protectedType a string:12345*** stack smashing detected ***
![Page 49: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/49.jpg)
Carnegie Mellon
49
Protected Buffer Disassembly804864d: 55 push %ebp 804864e: 89 e5 mov %esp,%ebp 8048650: 53 push %ebx 8048651: 83 ec 14 sub $0x14,%esp 8048654: 65 a1 14 00 00 00 mov %gs:0x14,%eax 804865a: 89 45 f8 mov %eax,0xfffffff8(%ebp) 804865d: 31 c0 xor %eax,%eax 804865f: 8d 5d f4 lea 0xfffffff4(%ebp),%ebx 8048662: 89 1c 24 mov %ebx,(%esp) 8048665: e8 77 ff ff ff call 80485e1 <gets> 804866a: 89 1c 24 mov %ebx,(%esp) 804866d: e8 ca fd ff ff call 804843c <puts@plt> 8048672: 8b 45 f8 mov 0xfffffff8(%ebp),%eax 8048675: 65 33 05 14 00 00 00 xor %gs:0x14,%eax 804867c: 74 05 je 8048683 <echo+0x36> 804867e: e8 a9 fd ff ff call 804842c <FAIL> 8048683: 83 c4 14 add $0x14,%esp 8048686: 5b pop %ebx 8048687: 5d pop %ebp 8048688: c3 ret
echo:
![Page 50: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/50.jpg)
Carnegie Mellon
50
Setting Up Canary
echo:. . .movl %gs:20, %eax # Get canarymovl %eax, -8(%ebp) # Put on stackxorl %eax, %eax # Erase canary. . .
/* Echo Line */void echo(){ char buf[4]; /* Way too small! */ gets(buf); puts(buf);}Return Address
Saved %ebp %ebp
Stack Framefor main
Stack Framefor echo
[3][2][1][0] buf
Before call to gets
Saved %ebxCanary
![Page 51: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/51.jpg)
Carnegie Mellon
51
Checking Canary
echo:. . .movl -8(%ebp), %eax # Retrieve from stackxorl %gs:20, %eax # Compare with Canaryje .L24 # Same: skip aheadcall __stack_chk_fail # ERROR
.L24:. . .
/* Echo Line */void echo(){ char buf[4]; /* Way too small! */ gets(buf); puts(buf);}Return Address
Saved %ebp %ebp
Stack Framefor main
Stack Framefor echo
[3][2][1][0] buf
Before call to gets
Saved %ebxCanary
![Page 52: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/52.jpg)
Carnegie Mellon
52
Canary Example
(gdb) break echo(gdb) run(gdb) stepi 3(gdb) print /x *((unsigned *) $ebp - 2)$1 = 0x3e37d00
Return AddressSaved %ebp %ebp
Stack Framefor main
Stack Framefor echo
[3][2][1][0] buf
Before call to gets
Saved %ebx03 e3 7d 00
Return AddressSaved %ebp %ebp
Stack Framefor main
Stack Framefor echo
buf
Input 1234
Saved %ebx03 e3 7d 0034 33 32 31
Benign corruption!(allows programmers to make silent off-by-one errors)
![Page 53: Carnegie Mellon 1 Today Arrays One-dimensional Multi-dimensional (nested) Structures Buffer Overflow](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649f055503460f94c1ae5a/html5/thumbnails/53.jpg)
Carnegie Mellon
53
Worms and Viruses Worm: A program that
Can run by itself Can propagate a fully working version of itself to other computers
Virus: Code that Add itself to other programs Cannot run independently
Both are (usually) designed to spread among computers and to wreak havoc