Question

In: Computer Science

How would you correct this function in C to prevent buffer overflow using the fgets() function...

How would you correct this function in C to prevent buffer overflow using the fgets() function and strncat() function

void nameBuilder()
{
   char fname[10];
   char lname[10];
   char fullname[20];

   printf("Enter your first name: ");
   scanf("%s", fname);

   printf("Enter your last name: ");
   scanf("%s", lname);

   strcat(fullname, fname);
   strcat(fullname, " ");
   strcat(fullname, lname);

   printf("Welcome. %s\n", fullname);
   return;
}

Solutions

Expert Solution

As seen from the program, the array size of fname and lname is 10 which means that it can contain maximum of 10 characters including the null character.

But, scanf function does not put any restriction on the input size. Let’s say if someone gives an fname or lname of length >=9, it will result in a buffer overflow.

Using fgets, the maximum size of input can be specified which prevents the buffer overflow.

strcat produces a string which is a null-terminated string whereas strncat produces a string which is either terminated at the length specified or by a null termination whichever is shorter in length.

Code:

void nameBuilder()

{

   char fname[10];

   char lname[10];

   char fullname[20];

   printf("Enter your first name: ");

// specify 10 as size because max size of fname can be 10

   fgets(fname, 10, stdin);

   printf("Enter your last name: ");

   // specify 10 as size because max size of lname can be 10

   fgets(lname, 10, stdin);

   // specify 10 as size because max size of fname can be 10 which will be appended to fullname

   strncat(fullname, fname, 10);

   // specify 1 as size because size of “ “ can be 1 which will be appended to fullname

   strncat(fullname, " ", 1);

   // specify 10 as size because max size of lname can be 10 which will be appended to fullname

   strncat(fullname, lname, 10);

   printf("Welcome. %s\n", fullname);

   return;

}

Output:

Enter your first name: Nelson

Enter your last name: Mandela

Welcome. Nelson Mandela


Related Solutions

How integer overflow can be exploited for buffer overflow attacks?
How integer overflow can be exploited for buffer overflow attacks?
Discuss how a stack buffer overflow attack is implemented.
Discuss how a stack buffer overflow attack is implemented.
How can buffer overflows be avoided and what are the steps involved in a buffer overflow...
How can buffer overflows be avoided and what are the steps involved in a buffer overflow exploit? What are some of the C functions susceptible to buffer overflow?
Research on buffer overflow attacks. How do the various types of overflow attacks differ? When did...
Research on buffer overflow attacks. How do the various types of overflow attacks differ? When did they first start to occur? What can they do and not do? What must a programmer do to prevent a buffer overflow? Answer briefly in your own words.
What properties would make a buffer overflow condition in a program exploitable or useful to an...
What properties would make a buffer overflow condition in a program exploitable or useful to an attacker?
How format string vulnerabilities can be exploited for buffer overflow attacks?
How format string vulnerabilities can be exploited for buffer overflow attacks?
What is an NX (no-execute) bit, and how can it be used to counter buffer overflow...
What is an NX (no-execute) bit, and how can it be used to counter buffer overflow attacks?
Select the item below that best describes a buffer overflow and how is it used against...
Select the item below that best describes a buffer overflow and how is it used against a web server? a. A buffer overflow occurs when more data is sent to an input buffer field than its size. This can be used to overwrite the stack with malicious code and a return pointer to the malicious code. b. A buffer overflow is caused over a network when there is a mismatch in the processing rates between the two entities involved in...
Using linux; how would you construct a void function using strfttime() to display time. Also a...
Using linux; how would you construct a void function using strfttime() to display time. Also a function using user_from_uid and group_from_gid to show the users name or group name. Trying to recreate the ls -l command. Thank you in advance!
How would you call a username in a different function when coding in C? I have...
How would you call a username in a different function when coding in C? I have a function that logs a user in, and I would like to say if (username == 'abc'){do function} but everytime I use the 'username' variable in a different function, it says it is an undeclared identifier.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT