summaryrefslogtreecommitdiff
path: root/3/10_control_and_data/48_stack_protection.c
blob: 08cb4e8a1009d60f742242a2b8bd3a4929be4a5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int len(char *s) {
    return strlen(s);
}

void iptoa(char *s, long *p) {
    long val = *p;
    sprintf(s, "%ld", val);
}

// int inlen(long x)
// x in %rdi
// without canary
//
// intlen:
//      subq        $40, %rsp           allocate 5*8 bytes
//      movq        %rdi, 24(%rsp)      put x on the stack
//      leaq        24(%rsp), %rsi      set &v = &x to the second argument
//      movq        %rsp, %rdi          set buf[12] to %rsp
//      call        iptoa
//
//  0<rsp+32    <-?
//  0<rsp+24    <-v
//  0<rsp+16    <-?
//  0<rsp+8     <-buf[12]
//  0<rsp       <-buf[12]
//
// with canary
//
// intlen:
//      subq        $56, %rsp
//      movq        %fs:40, %rax
//      movq        %rax, 40(%rsp)
//      xorl        %eax, %eax
//      movq        %rdi, 8(%rsp)
//      leaq        8(%rsp), %rsi
//      leaq        16(%rsp), %rdi
//      call        iptoa
//
//  0<rsp+48    <-
//  0<rsp+40    <-canary
//  0<rsp+32    <-
//  0<rsp+24    <-
//  0<rsp+16    <-buf[12]
//  0<rsp+8     <-v
//  0<rsp       <-
int intlen(long x) {
    long v;
    char buf[12];
    v = x;
    iptoa(buf, &v);
    return len(buf);
}