free() on stack memory

后端 未结 5 1169
余生分开走
余生分开走 2020-12-06 18:07

I\'m supporting some c code on Solaris, and I\'ve seen something weird at least I think it is:

char new_login[64];
...
strcpy(new_login, (char *)login);
...
         


        
5条回答
  •  粉色の甜心
    2020-12-06 18:41

    The free() is definitely a bug.
    However, it's possible there's another bug here:

    
       strcpy(new_login, (char *)login);
    

    If the function isn't pedantically confirming that login is 63 or fewer characters with the appropriate null termination, then this code has a classic buffer overflow bug. If a malicious party can fill login with the right bytes, they can overwrite the return pointer on the stack and execute arbitrary code. One solution is:

    
       new_login[sizeof(new_login)-1]='\0';
       strncpy(new_login, (char *)login, sizeof(new_login)-1 );
    

提交回复
热议问题