缓冲区溢出校正请 [英] buffer overflow correction please

查看:265
本文介绍了缓冲区溢出校正请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以避免在下面的程序缓冲区溢出漏洞,

  INT主(INT ARGC,CHAR *的argv []){
  诠释有效= FALSE;
  炭STR1; [8]
  CHAR STR2 [8];
  next_tag(STR1);
  得到(STR2);
  如果(STRNCMP(STR1,str2,8)== 0)
    有效= TRUE;
  的printf(缓冲:STR1(%S),STR2(%S),有效(%D)\\ n,STR1,STR2,有效);
}

这是正确的修正它,以修复漏洞?

  INT主(INT ARGC,CHAR *的argv []){
  诠释有效= FALSE;
  炭STR1; [8]
  CHAR STR2 [8];
  next_tag(STR1);
  与fgets(STR2); / *这里是改变! * /
  如果(STRNCMP(STR1,str2,8)== 0)
    有效= TRUE;
  的printf(缓冲:STR1(%S),STR2(%S),有效(%D)\\ n,STR1,STR2,有效);
}


解决方案

我要指出的第一件事就是为什么你第一次实现了缓冲区溢出。

  //分配一个字符数组,可容纳最大8个字符。
CHAR STR2 [8];//要求用户输入之类的东西它变成STR2。如果用户
//为我们提供了超过8个字符,我们最终会覆盖
// STR2超出其分配的缓冲区。
得到(STR2);

因此​​,我们需要的是一种告诉变,要获得不超过8 字符。与fgets帮助我们解决这个问题。花费的参数,要读取的字符的最大数目。看看弗拉基米尔后约与fgets

更多详情

i want to avoid buffer overflow vulnerability in the following program,

int main (int argc, char *argv[ ]) {    
  int valid=FALSE;     
  char str1[8];     
  char str2[8];      
  next_tag(str1);     
  gets(str2);    
  if (strncmp(str1,str2,8)==0)      
    valid=TRUE;     
  printf("buffer: str1(%s),str2(%s),valid(%d) \n", str1, str2, valid); 
} 

is this the right correction to it in order to fix vulnerability?

int main (int argc, char *argv[ ]) {     
  int valid=FALSE;     
  char str1[8];     
  char str2[8];      
  next_tag(str1);     
  fgets(str2);       /* HERE IS THE CHANGE! */
  if (strncmp(str1,str2,8)==0)        
    valid=TRUE;     
  printf("buffer: str1(%s),str2(%s),valid(%d) \n", str1, str2, valid); 
} 

解决方案

The first thing I should point out is why your first implementation has a buffer overflow.

// Allocate a char array that can hold 'max' 8 characters.
char str2[8];

// Ask user for input and stuff it into str2. If the user
// gives us more than 8 characters, we will end up overwriting
// str2 beyond its allocated buffer. 
gets(str2);  

So what we need is a way to tell 'gets', to get no more than 8 characters. fgets helps us solve this. It takes for a parameter, the maximum number of characters to read. Look at Vladimir's post for more details about fgets.

这篇关于缓冲区溢出校正请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆