解释这个运行函数但没有明确调用它的代码?

时间:2012-11-13 13:24:21

标签: c buffer-overflow

以下代码的输出是"溢出",但我没有明确调用func函数。它是如何工作的?

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

int copy(char *input)
{
    char var[20];
    strcpy(var, input);
    return 0;
}

int func(void)
{
    printf("Overflow\n");
    return 0;
}

int main(int argc, char *argv[])
{
    char str[] = "AAAABBBBCCCCDDDDEEEEFFFFGGGG";
    int *p = (int *)&str[24];
    *p = (int)func;

    copy(str);
    return 0;
}

1 个答案:

答案 0 :(得分:11)

copy函数溢出var函数中的copy缓冲区,并使用main函数的地址覆盖func返回地址。

copy函数返回时,它不会在main函数调用后返回copy,而是返回func函数。