【发布时间】:2011-12-08 11:40:22
【问题描述】:
我有一个程序,我想了解堆栈在其执行期间的状态。我的示例程序很简单,
#include <stdio.h>
int main(){
setuid(0);
system("/bin/bash");
return 1;
}
现在,当我使用 gdb 调试该程序时,我得到了 setuid() 函数的地址,但是当我查看堆栈时,我无法确定它的地址。
我的堆栈在开始执行main()后的状态,
Ajai@ubuntu:/tmp$ gdb -q mal
Reading symbols from /tmp/mal...done.
(gdb) b 2
Breakpoint 1 at 0x80483fd: file mal.c, line 2.
(gdb) r
Starting program: /tmp/mal
Breakpoint 1, main () at mal.c:4
4 setuid(0);
(gdb) x/32xw $esp
0xbffff3a0: 0x0015ed35 0x0011ea50 0x0804842b 0x0028bff4
0xbffff3b0: 0x08048420 0x00000000 0xbffff438 0x00145e37
0xbffff3c0: 0x00000001 0xbffff464 0xbffff46c 0x0012e414
0xbffff3d0: 0xffffffff 0x0012cff4 0x08048243 0x00000001
0xbffff3e0: 0xbffff420 0x0011da31 0x0012dad0 0xb7fffb48
0xbffff3f0: 0x00000001 0x0028bff4 0x00000000 0x00000000
0xbffff400: 0xbffff438 0xb68cac87 0x61d0d5f8 0x00000000
0xbffff410: 0x00000000 0x00000000 0x00000001 0x08048340
(gdb) p setuid
$1 = {<text variable, no debug info>} 0x1c8ee0 <setuid>
我是不是看错了堆栈?
我也想知道当main()函数开始执行时setuid()函数调用及其参数和system()函数调用及其参数的地址如何入栈。
很抱歉,如果有人问过这类问题,但我找不到。
【问题讨论】:
-
函数的地址可能根本不在栈中。代码中需要它;堆栈中不需要它。在从
main()调用的函数中,main()内部的返回地址将在堆栈上,但这与main()本身的地址不同。