阅读如下代码,其中main函数参数可以被攻击者控制,那么main函数中存在哪些漏洞,指出漏洞具体位置及问题,并说明这些代码中漏洞的修复方法
//读取指定的文件中的内容并返回该文件base64编码后的内容,函数失败则返回NULL,在非NULL情况下需要调用该函数后手动释放内存,该函数中不存在安全性问题
char *readBase64File(char *fileName);
int main(int argc, char* argv[])
{
char filePath[260];
char fileCmd[512];
char *fileName;
int dirIdx;
char *fileContent;
char *dirPaths[4];
dirPaths[0] = ”/home/data1/”;
dirPaths[1] = ”/home/data2/”;
dirPaths[2] = ”/home/data3/”;
dirPaths[3] = ”/home/data4/”;
fileName = argv[1];
dirIdx = atoi(argv[2]);
if (dirIdx > 3)
{
printf(“file index error!\n”);
return -1;
}
strcpy(filePath, dirPaths[dirIdx]);
if (strlen(filePath) + strlen(fileName) < 260)
strcat(filePath, fileName);
else
return -1;
fileContent = readBase64File(filePath);
if (fileContent == NULL)
{
printf(“read filecontent error!\n”);
}
else
{
printf(“file content is: %s\n”, fileContent);
free(fileContents);
}
strcpy(fileCmd, “rm ”);
strcat(fileCmd, fileName);
printf(fileCmd);
system(fileCmd);
return 0;
}