【发布时间】:2019-09-05 23:17:12
【问题描述】:
我是网络安全新手,我想了解为什么以下代码容易受到堆溢出攻击...
struct data {
char name[128];
};
struct fp {
int (*fp)();
};
void printName() {
printf("Printing function...\n");
}
int main(int argc, char **argv) {
struct data *d;
struct fp *f;
d = malloc(sizeof(struct data));
f = malloc(sizeof(struct fp));
f->fp = printName;
read(stdin,d->name,256);
f->fp();
}
是因为read(stdin, d->name, 256) 读取的缓冲区大小超过了char name 在struct data 中分配的128 的缓冲区大小吗?
任何帮助都会很棒
【问题讨论】:
-
是的,它允许分配更多空间。但我认为这是缓冲区溢出而不是堆溢出
-
有人告诉我这与指针本身有关。但是,伙计,我非常困惑,而且我对 C 和一般计算都是新手 :( :(
-
read(stdin,d->name,256);看起来不对。确定在这里使用stdin和read(int fd, void *buf, size_t count)? (启用所有编译器警告)
标签: c malloc heap-memory buffer-overflow