I have homework in which I am supposed to analyze this C program and answer three questions. This is the program:
int main()
{
// Define buffers to store username and password
char username[16];
char password[16];
// Read username and password from user input
printf("Enter your name: ");
scanf("%s", username);
printf("Enter your password: ");
scanf("%s", password);
printf("[SHOUTING OUT LOUD] Hello, %s!\n", username);
return 0;
}
These are the questions:
- There is a security hole which makes it possible that the password will be printed. How is this possible?
- How can it be achieved that the program crashes?
- Correct the program so that there are no security holes anymore.
I think that the problem lies in the scanf method because the amount of characters which can be scanned is not limited. So I think by inputting a long string it is possible to get a segmentation fault because you want to save the characters in memory which must not be referenced by the user. This would be my answer for 2. The answer for 3 is that the program can be corrected by limiting the number of characters which can be scanned. But I don't seem to find an answer for 1. Could you help me out with this one?