Bri*_*riz 2 c++ security code-access-security
我的老板告诉我查看下面的代码并告诉他潜在的安全漏洞是什么.我不是很擅长这种事情,因为我不认为试图破解代码.我只看到没有任何东西被宣布为私人,但除此之外,我只是不知道.
#define NAME_SIZE (unsigned char) 255
// user input should contain the user’s name (first name space
// middle initial space last name and a null
// character), and was entered directly by the user.
// Returns the first character in the user input, or -1 if the method failed.
char poor_method(char* user_input, char* first, char *middle, char* last)
{
char*buffer;
char length;
// find first name
buffer = strtok(user_input, " ");
if(buffer==0)
{
return -1;
}
length = strlen(buffer);
if(length <= NAME_SIZE)
{
strcpy(first, buffer);
}
// find middle name
buffer = strtok(NULL, " ");
if(buffer==0)
{
return-1;
}
if(middle)
*middle = buffer[0];
// find last name
buffer = strtok(NULL, "\0");
length = strlen(buffer);
if(length <= NAME_SIZE)
{
strcpy(last, buffer);
}
// Check to make sure that all of the user input was used
buffer = strtok(NULL, "\0");
if(buffer != NULL)
{
return-1;
}
return first[0];
}
Run Code Online (Sandbox Code Playgroud)
有哪些安全漏洞?
我所看到的(绝不是完整的清单):
std::string和"C++方式"(如Cat Plus Plus所说)我假设这是一个错字:
charpoor_method(
你错过了char和之间的空间poor_method(你如果不检查first或last确实有效指针(不幸的是,你能做的最好是检查他们反对NULL).
first或last确实可以保存您正在复制的任何内容.returnfirst[0];Run Code Online (Sandbox Code Playgroud)
缺少return和之间的空间first[0]
学习编写安全代码是非常重要的事情.遵循布莱希特的建议,并擅长它.