为什么在两个程序中以相同的方式分配变量?

问题描述 投票:1回答:1

我有以下代码来演示基于堆栈的缓冲区溢出。

int check_authentication(char *password) {
  int auth_flag = 0;
  char password_buffer[16];

  strcpy(password_buffer, password);
  if(strcmp(password_buffer, "Admin") == 0)
    auth_flag = 1;
  return auth_flag;
}

这里,当用户输入长度大于16的任何字符串时,将允许访问。为了显示不溢出的其他情况,auth_flag我有以下代码:

int check_authentication(char *password) {
  char password_buffer[16];
  int auth_flag = 0;

  strcpy(password_buffer, password);
  if(strcmp(password_buffer, "Admin") == 0)
    auth_flag = 1;
  return auth_flag;
}

由于堆栈用作LIFO,因此在第二个示例中,auth_flag的地址应比password_buffer的地址低。断点位于strcpy的GDB如下所示:

(gdb) x/16xw password_buffer
0x61fefc:       0x696d6441      0x7659006e      0xc9da078f      0xfffffffe
0x61ff0c:       0x00000001      0x76596cad      0x00401990      0x0061ff38
0x61ff1c:       0x00401497      0x00ae1658      0x00000000      0x0028f000
0x61ff2c:       0x00400080      0x0061ff1c      0x0028f000      0x0061ff94
(gdb) x/x &auth_flag
0x61ff0c:       0x00000001

我希望password_buffer0x61ff10开始,紧接在auth_flag之后。我哪里错了?

我在Windows 10上使用gcc(gcc版本9.2.0(MinGW.org GCC Build-20200227-1)和gdb(GNU gdb(GDB)7.6.1),未对SEHOP或ASLR进行任何修改。

c memory gdb allocation buffer-overflow
1个回答
1
投票

如评论中所述,局部变量不会被压入堆栈或从堆栈中弹出。而是在执行函数调用时,运行时会在堆栈上为局部变量分配一些空间。它称为Function Prologue,并且具有已知序列(在许多情况下,请参见注释)

push ebp
mov ebp, esp
sub esp, N

其中N是为局部变量保留的空间。

[由于某些原因,GCC总是为[rbp-4]局部变量分配内存位置auth_flag,这就是为什么看不到任何差异的原因(请检查thisthis)。可能是编译器的设计方式...

另一方面,至少在为auth_flag局部变量分配堆栈上的位置时,clang会执行您希望编译器执行的操作。没有优化用于编译器

check_authentication:                   # @check_authentication
        push    rbp
        mov     rbp, rsp
        sub     rsp, 48
        lea     rax, [rbp - 32]
        mov     qword ptr [rbp - 8], rdi
        mov     dword ptr [rbp - 12], 0
        mov     rsi, qword ptr [rbp - 8]
        mov     rdi, rax
        mov     qword ptr [rbp - 40], rax # 8-byte Spill
        call    strcpy
        mov     esi, offset .L.str
        mov     rdi, qword ptr [rbp - 40] # 8-byte Reload
        mov     qword ptr [rbp - 48], rax # 8-byte Spill
        call    strcmp
        cmp     eax, 0
        jne     .LBB0_2
        mov     dword ptr [rbp - 12], 1
.LBB0_2:
        mov     eax, dword ptr [rbp - 12]
        add     rsp, 48
        pop     rbp
        ret
.L.str:
        .asciz  "Admin"

将上面的代码与下面的代码进行比较,其中在password_buffer局部变量之前声明了auth_flag

check_authentication:                   # @check_authentication
        push    rbp
        mov     rbp, rsp
        sub     rsp, 64
        lea     rax, [rbp - 32]
        mov     qword ptr [rbp - 8], rdi
        mov     dword ptr [rbp - 36], 0
        mov     rsi, qword ptr [rbp - 8]
        mov     rdi, rax
        mov     qword ptr [rbp - 48], rax # 8-byte Spill
        call    strcpy
        mov     esi, offset .L.str
        mov     rdi, qword ptr [rbp - 48] # 8-byte Reload
        mov     qword ptr [rbp - 56], rax # 8-byte Spill
        call    strcmp
        cmp     eax, 0
        jne     .LBB0_2
        mov     dword ptr [rbp - 36], 1
.LBB0_2:
        mov     eax, dword ptr [rbp - 36]
        add     rsp, 64
        pop     rbp
        ret
.L.str:
        .asciz  "Admin"

上面代码片段中的mov dword ptr [rbp - XXX], 0行是声明和初始化本地auth_flag变量的位置。如您所见,堆栈中局部变量的保留位置根据缓冲区大小而变化。我认为用clang编译代码并用lldb调试代码是值得的。

© www.soinside.com 2019 - 2024. All rights reserved.