Why is this code vulnerable to buffer overflow attacks?

前端 未结 5 1102
鱼传尺愫
鱼传尺愫 2021-01-29 18:23
int func(char* str)
{
   char buffer[100];
   unsigned short len = strlen(str);

   if(len >= 100)
   {
        return (-1);
   }

   strncpy(buffer,str,strlen(str));         


        
5条回答
  •  梦如初夏
    2021-01-29 18:34

    The answer with the wrapping is right. But there is a problem I think was not mentioned if(len >= 100)

    Well if Len would be 100 we'd copy 100 elements an we'd not have trailing \0. That clearly would mean any other function depending on proper ended string would walk way beyond the original array.

    The string problematic from C is IMHO unsolvable. You'd alway better have some limits before the call, but even that won't help. There is no bounds checking and so buffer overflows always can and unfortunately will happen....

提交回复
热议问题