我正在查看这个类的代码示例,我是缓冲区溢出的新手。如何修改此示例以避免缓冲区溢出攻击?另外,如果有人知道关于缓冲区溢出的好文章,请张贴。谢谢!
void GetProfileFor( const char *name,
char *profile,
int profileLen );
int main() {
char *profile = malloc( 1024 );
char name[128];
printf( “Enter your name: ” );
gets( name );
GetProfileFor( name, profile, 1024 );
printf( “\nYour profile: %s\n”, profile );
return 0; }
最佳答案
要确定哪里会发生缓冲区溢出,您必须确定所有输入路径及其填满的缓冲区 - 内部缓冲区是否足以满足所有可能的输入?还是对允许的输入量有任何限制?
在您的情况下,gets(name) 在内部缓冲区中有限制,但 gets() 本身在它可以接受的输入方面没有限制:
http://www.tutorialspoint.com/c_standard_library/c_function_gets.htm
因此缓冲区溢出是可能的。
防止这种攻击的具体方案是使用fgets():
http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
确实对允许的外部输入有限制。
关于c - 缓冲区溢出;避免溢出攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038684/