#include<stdio.h>
#include<stdlib.h>

void bug()
{
        system("reboot\n");
        exit(0);
}

int stack_test(int a,int b)
{
        printf("before write:0x%x\n",b);
        int *p=&a;
        p++;
        *p=0xdddd;
        printf("after write:0x%x\n",b);
        int c=0xcccc;
        return c;
}

int main()
{
        int a=0xaaaa;
        int b=0xbbbb;
        int ret=stack_test(a,b);
        printf("you should run here\n");
        return 0;
}

运行结果:

before write:0xbbbb

after write:0xdddd

you should run here

分析:函数参数压栈从右至左进行,先输出b的地址,后p指向a的地址,p++后再指向b所在的那块空间,在进行解引用修改其内容,即修改了b的地址。

#include<stdio.h>
#include<stdlib.h>
void bug()
{
        system("reboot");
        exit(0);
}
int stack_test(int a,int b)
{
        int *p=&a;
        p--;
        *p=bug;
//      printf("before write:0x%x\n",b);
//      int *p=&a;
//      p++;
//      *p=0xdddd;
//      printf("after write:0x%x\n",b);
        int c=0xcccc;
        return c;
}
int main()
{
        int a=0xaaaa;
        int b=0xbbbb;
        int ret=stack_test(a,b);
        printf("you should run here\n");
        return 0;
}

运行结果:系统重启

分析:p指向a所在的内存空间,再让p--,解引用改变其内容。