• Aucun résultat trouvé

Sécurité des logiciels

N/A
N/A
Protected

Academic year: 2022

Partager "Sécurité des logiciels"

Copied!
51
0
0

Texte intégral

(1)

1

Sécurité des logiciels

Assembly language, part 4/4

Samuel Thibault <samuel.thibault@u-bordeaux.fr>

Pieces from Emmanuel Fleury <emmanuel.fleury@u-bordeaux.fr>

CC-BY-NC-SA

(2)

2

Calling a function

(3)

3

Calling a function

Call instruction call f

esp

0 0 0 0 0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(4)

4

Calling a function

Call instruction

0x0810: call f

<=>

0x0810: pushl %eip+5 jmp f

esp

0 0 0 0 0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(5)

5

Calling a function

Call instruction

0x0810: call f

<=>

0x0810: pushl %eip+5 jmp f

esp

0 0 0 0 0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(6)

6

Calling a function

Call instruction

0x0810: call f

<=>

0x0810: pushl %eip+5

jmp f esp

0 0 0 0x0815

0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

return address

(7)

7

Calling a function

Call instruction

0x0810: call f

<=>

0x0810: pushl %eip+5

jmp f esp

0 0 0 0x0815

0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

return address

(8)

8

Inside a function

0x07a0: f: movl $0, %eax 0x07a5: ret

esp

0 0 0 0x0815

0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(9)

9

Returning from a function

Ret instruction 0x07a5: ret

<=>

0x07a5: popl %eip

<=>

0x07a5: jmpl (%esp) addl $4,%esp

esp

0 0 0 0x0815

0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(10)

10

Returning from a function

Ret instruction 0x07a5: ret

<=>

0x07a5: popl %eip

<=>

0x07a5: jmpl (%esp) addl $4,%esp

esp

0 0 0 0x0815

0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(11)

11

Passing parameters

(32bit version)

(12)

12

Calling a function

Call instruction

x = f(32,52) pushl $52 pushl $32 call f

esp

0 0 0 0 0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(13)

13

Calling a function

Call instruction

x = f(32,52) pushl $52 pushl $32 call f

esp

0 0 0 0 0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(14)

14

Calling a function

Call instruction

x = f(32,52) pushl $52 pushl $32

call f esp

0 0 0 52

0 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(15)

15

Calling a function

Call instruction

x = f(32,52) pushl $52 pushl $32 call f

esp

0 0 0 52 32 0 0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

parameters

(16)

16

Calling a function

Call instruction

x = f(32,52) pushl $52 pushl $32 call f

I.e. push in reverse order esp

0 0 0 52 32 0x0815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

parameters return address

(17)

17

Inside the function

f: movl 4(%esp),%eax addl 8(%esp),%eax ret

esp

0 0 0 52 32 0x0815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(18)

18

Back to the caller

Call instruction

x = f(32,52) pushl $52 pushl $32 call f

; result in %eax esp

0 0 0 52 32 0x815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(19)

19

Back to the caller

Call instruction

x = f(32,52) pushl $52 pushl $32 call f

addl $8, %esp

; result in %eax esp

0 0 0 52 32 0x815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(20)

20

Back to the caller

Call instruction

x = f(32,52) pushl $52 pushl $32 call f

addl $8, %esp

; result in %eax

(On Windows this is to be done by f by using ret $8)

esp

0 0 0 52 32 0x815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(21)

21

Local variables

(22)

22

Inside the function

f: subl $4,%esp

movl 8(%esp),%eax addl 12(%esp),%eax movl %eax,(%esp) ...

ret esp

0 0 0 52 32 0x0815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

(23)

23

Inside the function

f: subl $4,%esp

movl 8(%esp),%eax addl 12(%esp),%eax movl %eax,(%esp) ...

ret

0 0 0 52 32 0x0815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

local variableesp

(24)

24

Inside the function

f: subl $4,%esp

movl 8(%esp),%eax addl 12(%esp),%eax movl %eax,(%esp) ...

ret

esp

0 0 0 52 32 0x0815

84 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

local variable

(25)

25

Inside the function

f: subl $4,%esp

movl 8(%esp),%eax addl 12(%esp),%eax movl %eax,(%esp) ...

addl $4,%esp

ret esp

0 0 0 52 32 0x0815

84 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

local variable

(26)

26

Base Pointer (bp)

(27)

27

Inside the function

f: pushl %ebp

movl %esp,%ebp ...

esp

0 0 0 52 32 0x0815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

(28)

28

Inside the function

f: pushl %ebp

movl %esp,%ebp ...

esp

0 0 0 52 32 0x0815

0 ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

(29)

29

Inside the function

f: pushl %ebp

movl %esp,%ebp ...

esp

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

(30)

30

Inside the function

f: pushl %ebp

movl %esp,%ebp ...

Also available as one-instruction enter

esp

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

(31)

31

Inside the function

f: pushl %ebp

movl %esp,%ebp subl $4, %esp

movl 8(%ebp),%eax addl 12(%ebp),%eax movl %eax,-4(%ebp) ...

movl -4(%ebp), %eax ret

esp

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec

0x0fe4 ...

ebp

0x0fe8

(32)

32

Inside the function

f: pushl %ebp

movl %esp,%ebp subl $4, %esp

movl 8(%ebp),%eax addl 12(%ebp),%eax movl %eax,-4(%ebp) ...

movl -4(%ebp), %eax ret

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

esp 0x0fe0 0

(33)

33

Inside the function

f: pushl %ebp

movl %esp,%ebp subl $4, %esp

movl 8(%ebp),%eax addl 12(%ebp),%eax movl %eax,-4(%ebp) ...

movl -4(%ebp), %eax ret

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

esp 0x0fe0 84

(34)

34

Inside the function

f: pushl %ebp

movl %esp,%ebp subl $4, %esp

movl 8(%ebp),%eax addl 12(%ebp),%eax movl %eax,-4(%ebp) ...

movl -4(%ebp), %eax ret

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

esp 0x0fe0 84

(35)

35

Inside the function

f: pushl %ebp

movl %esp,%ebp subl $4, %esp

movl 8(%ebp),%eax addl 12(%ebp),%eax movl %eax,-4(%ebp) ...

movl -4(%ebp), %eax movl %ebp, %esp

popl %ebp ret

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

ebp

esp 0x0fe0 84

(36)

36

Inside the function

f: pushl %ebp

movl %esp,%ebp subl $4, %esp

movl 8(%ebp),%eax addl 12(%ebp),%eax movl %eax,-4(%ebp) ...

movl -4(%ebp), %eax movl %ebp, %esp

popl %ebp ret

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fe0 84 esp ebp

(37)

37

Inside the function

f: pushl %ebp

movl %esp,%ebp subl $4, %esp

movl 8(%ebp),%eax addl 12(%ebp),%eax movl %eax,-4(%ebp) ...

movl -4(%ebp), %eax movl %ebp, %esp

popl %ebp ret

Also known as one-instruction leave

0 0 0 52 32 0x0815

0x0ffc ...

1 2 42 0x1000

0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fe0 84 esp

ebp

(38)

38

Summing it up

32bit Conventions

(39)

39

Calling a function

f(32, 52)

On function call, on the stack:

parameters call f

Which means pushing them in reverse order...

esp

52 32

...

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

...

parameters

(40)

40

Function entry

f(32, 52)

On function entry, on the stack:

parameters

return address

f: ...

ret

esp

52 32

...

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

...

parameters

0x0815 return address

(41)

41

Ebp setup

f(32, 52)

After ebp setup (optional), on the stack:

parameters (8(%ebp), 12(%ebp), ...)

return address

ebp backup

f: pushl %ebp

movl %esp, %ebp ...

ret

ebp

52 32

...

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

...

parameters

0x0815 return address

espebp backup 0x0ffc

(42)

42

Now the real meat!

f(32, 52)

After local variables allocation, on the stack:

parameters (8(%ebp), 12(%ebp, ...)

return address

ebp backup

local variables (-4(%ebp), ...) f: pushl %ebp

movl %esp, %ebp subl $42, %esp ...

ret

52 32

...

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

parameters

0x0815 return address

ebpebp backup 0x0ffc

esp

local variables ...

Learn this slide by heart

...

(43)

43

The ABI

(44)

44

What is an ABI?

Function call interoperability between application and libraries

→ Need to agree on

Location of return address

Content of the stack (parameters, return address)

Location of returned value

Volatile registers (see next slides)

SystemV ABIs (Linux, BSD, MacOS)

i386, some variants: cdecl, stdcall, fastcall

amd64 (~= i386 fastcall)

Microsoft x32/x64 ABIs

(45)

45

ABI vs API?

In C,

.h headers provide the API

C types

Function/variables names

I.e. what the C caller should respect

Interpretation of .h for a given platform provides the ABI

Type sizes

Calling convention

Symbol names

I.e. what the assembly language caller should respect

(46)

46

Vocabulary

Function call: when foo() calls bar()

foo() is the caller function (appelant)

bar() is the callee function (appelé)

Local variable (aka automatic variable): a variable whose scope is not getting outside of the function

Parameters (aka arguments): Data set by the caller for the callee

Return value: Data set by the callee for the caller

Call stack (aka backtrace): The chain of functions that have been currently called

e.g. main() → foo() → bar()

(47)

47

SystemV ABI

32bit

Return address in (%esp)

Parameters in 4(%esp), 8(%esp), ...

Return value in

%eax for ≤ 32bit integers

st(0) for float

64bit

Return address in (%rsp)

≤ 64bit integer parameters in %rdi, %rsi, %rdx, %rcx, %r8, %r9

Float/Double parameters in %xmm0, %xmm1, ...

struct members split in registers

Otherwise, on the stack

Return value in

%rax for ≤ 64bit integers

%xmm0 for float/double

(48)

48

A horrible 64bit example

(49)

49

Case of returning a structure

struct mys foo(int a, float b)

The caller is in charge of providing the memory space of the structure

Pointer passed as additional argument before a

Callee eventually sets %eax to that pointer

(32bit) Callee drops the additional argument with ret $4 So, actually becomes

struct mys *foo(struct mys *ret, int a, float b)

(50)

50

Volatile?

Caller may want to keep values in registers across function call

Avoid having to store in a local variable

But callee could want to use registers too!

→ Meet halfway: volatile / non-volatile

Volatile registers: callee can use it at will

eax, ecx, edx

aka caller-saved

Non-volatile registers: callee has to save/restore it

ebx, edi, esi, ebp, r12-r15

aka callee-saved

(51)

51

References

Références

Documents relatifs

In both situations, the requirements induced at the first echelon are assigned to the second echelon center u with the lowest unit production and transportation cost ( f puw + c jpu

Une variable est associé à symbole (un nom qui sert d'identificateur) qui renvoie à une position de la mémoire (une adresse) dont le contenu peut prendre successivement

The purpose of this study has been to estimate a variable cost function using panel data for a sample of 32 Italian water companies, in order to analyze the economies of scale

We present a series of theoretical arguments supporting the claim that a large class of modern learning algorithms that rely solely on the smooth- ness prior – with similarity

A number of semi- supervised learning algorithms are based on the idea of propagating labels on the nodes of a neighbor- hood graph, starting from known labels, until some conver-

— We give a relation between the sign of the mean of an integer-valued, left bounded, random variable X and the number of zeros of 1 − Φ(z) inside the unit disk, where Φ is

Toute utili- sation commerciale ou impression systématique est constitutive d’une infraction pénale.. Toute copie ou impression de ce fichier doit conte- nir la présente mention

First Normal Form = all attributes are atomic Second Normal Form (2NF) = old and obsolete. Boyce Codd Normal Form (BCNF) Third Normal