C/C++ Security Vulnerabilities

Quiz on common security vulnerabilities in C and C++ including buffer overflows, integer overflows, input validation, and memory management issues

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which Compilation switch will you use to check Buffer Overflows?

  1. /GS on Visual C++ and -fmudflap -fmudflapth -fmudflapir on GCC
  2. /O in Vc++ and -O2 in GCC
  3. /S in Vc++ and -fcrossjumping in GCC
  4. /S in VC++ and -fno-function-cse in GCC
Question 2 Multiple Choice (Single Answer)

What can go wrong in following code? #include <stdio.h> int main(int argc, char *argv[]) { if(argc != 3) { printf("usage: %s [source] [dest]\n", argv[0]); exit(1); } char x; FILE *file[2]; file[0] = fopen(argv[1],"r+"); file[1] = fopen(argv[2],"w+"); for(x = 0; x < 2; x++) { if(file[x] == NULL) { printf("error opening file.\n"); exit(1); } } do { x = fgetc(file[0]); fputc(x,file[1]); } while(x != EOF); for(x = 0; x < 2; x++) fclose(file[x]); return 0; }

  1. SQL Injection
  2. Arc Injection
  3. Buffer Overflow
  4. both 2 and 3
Question 3 Multiple Choice (Single Answer)

Which compilation switch should be enabled for stack protection? Choose the best and most secure option.

  1. fstack-protector
  2. fstack-protector-all
  3. fdelete-null-pointer-checks
  4. Both a and b
Question 4 Multiple Choice (Single Answer)

What vulnerability is present in following code: unsigned char j,k; j=getchar(); k=getchar(); unsigned char result = j + k;

  1. Heap Overflow
  2. Integer overflow
  3. Buffer overflow
  4. No Vulnerability
Question 5 Multiple Choice (Single Answer)

Which statement creates a buffer over flow? #include <iostream.h> #include <stdio.h> #include <string.h> int main (int argc, char *argv[]) { int i=0,j=1; char ipstring[80]; for (;i<=3;i++){ cout<<"\n entering a new character\n"; j=getchar(); /1/ cout<<”enter a string”; gets(ipstring); /2/ cout<<j<<"\n"; } return 0; }

  1. j=getchar();
  2. gets(ipstring);
  3. Both
  4. None
Question 6 Multiple Choice (Single Answer)

In this code, x is freed twice. What is the risk of this code? x = malloc(200); /* do something with x / free(x); / do something else */ free(x);

  1. This is a double free vulnerability and must be fixed
  2. The second call to free() will return an error
  3. There might be compiler warnings, but the program will run fine
  4. This is not a security issue
Question 7 Multiple Choice (Single Answer)

Is there a vulnerability in this code? If yes, which line(s) int main (int argc, char argv[]){ char chararray[3]; int intarray[3]; int i; strncpy(chararray, argv[1], sizeof(chararray) - 1); for (i=0;i<=3;i++){ /1/ chararray[i]= getchar(); intarray[i]=ii; /2/ } }

  1. for (i=0;i<=3;i++)
  2. intarray[i]=i*i;
  3. Both
  4. None
Question 8 Multiple Choice (Single Answer)

What is the vulnerability ? int main (int argc, char *argv[]){ char k[3]; int i=0,j=1; char buffer[50]; strncpy(buffer, argv[1], sizeof(buffer) - 1); buffer[49]='/0'; unsigned char ch='a'; k[0]=1; do{ i++; k[i]=ch+i; } while(i<3); return 0; }

  1. Heap overflow
  2. Integer overflow
  3. Off by one error
  4. None
Question 9 Multiple Choice (Single Answer)

The options show various uses of strncpy. Choose which use of strncpy is most secure while not wasting storage space at dst? Src is an untrusted input obtained from an external source.

  1. strncpy(dst,src,len(dst))
  2. strncpy(dst,src,len(src)+1)
  3. strncpy(dst,src,len(dst)+1)
  4. strncpy(dst,src,len(dst)-1)
Question 10 Multiple Choice (Single Answer)

While trying to print an eight character long name, which of the following will introduce a security vulnerability ? printf ("%.8s",name); /* 1 / printf (name); / 2 / printf ("%s",name); / 3 / printf ("%8c", name); / 4 */

  1. printf ("%.8s",name); AND printf (name);
  2. printf (name); AND printf ("%s",name);
  3. printf ("%s",name); AND printf ("%8c", name);
  4. printf (name);
Question 11 Multiple Choice (Single Answer)

Which of the following is a secure way to use scanf?

  1. scanf("%.8s", name);
  2. scanf("%8s", name);
  3. scanf("%8c", name);
  4. scanf("%s", name);
Question 12 Multiple Choice (Single Answer)

Which line of the code should be deleted to remove vulnerability? int main(int argc,char* argv[]) { int *ptr1=new int; if(ptr1==NULL) exit(1); int ptr2=new int; if(ptr2==NULL) exit(1); char j; j=argv[1]; int k=atoi(j); if (j==0){ Ptr1=&k; delete ptr2; /1/ } else { Ptr2=&k; } delete ptr1; /2/ delete ptr2; /3/ return 0; }

  1. delete ptr2; (within if loop) AND delete ptr1;
  2. delete ptr1; AND delete ptr2; (outside if loop )
  3. delete ptr2; (within if loop)
  4. delete ptr2; (outside if loop )
Question 13 Multiple Choice (Single Answer)

What will be sizeof(name) return? char *name="32000";

  1. 4 - it is the size of the pointer
  2. 5 - it is the number of characters in the string that the pointer points to
  3. 4 - it is the size when 32000 is stored as integer
  4. 1 - it is the size of a character variable
Question 14 Multiple Choice (Single Answer)

Will following program execute successfully ? int main(int argc,char* argv[]){ int *ptr=new int; if(ptr==NULL) exit(1); char *j; for(int i=1;i<=4;i++) { j=argv[i]; int k=atoi(j); if (k!=0){ *ptr=k; delete ptr; } } }

  1. Program works when there is only 1 argument with program
  2. Program works when there are 3 arguments with program
  3. Program works when there are 4 arguments with program
  4. Program never executes successfully
Question 15 Multiple Choice (Single Answer)

With the size of unsigned integers being 4 bytes, What happens when a negative number is entered? unsigned int i; scanf("%u",&i);

  1. A run-time error is encountered and the program aborts
  2. unsigned int variables cannot store the sign (+ or -) of the number. The sign is discarded and only the number is stored in i
  3. A large positive number will be stored in i
  4. Unsigned int variables cannot store signed numbers. Hence in this program i will contain garbage values.
Question 16 Multiple Choice (Single Answer)

What is the value of j (size of integer is 4 bytes)? int i=987987987; int j= i*10;

  1. 1289945278
  2. garbage. Integer j cannot hold such large values
  3. 9879879870
  4. Program is aborted
Question 17 Multiple Choice (Single Answer)

The application is receiving input from an external source. Which of the following external sources can be considered safe?

  1. Shell environment variables
  2. Data received via encrypted network channels
  3. argv[0] can only have either null or program name
  4. no external input must be trusted
Question 18 Multiple Choice (Single Answer)

In the following code snippet, how to handle pointer? int main (int argc, char argv[]) { char j; j=argv[1]; int k=atoi(j); /delete here/ return 0; }

  1. delete j;
  2. realloc j;
  3. free j;
  4. It need not be deleted
Question 19 Multiple Choice (Single Answer)

Code is vulnerable to buffer overflow attack and

  1. DNS Spoofing
  2. Command Injection
  3. Path Traversal
  4. Command Injection AND Path Traversal
Question 20 Multiple Choice (Single Answer)

What can go wrong in following code? #include <stdio.h> int main(int argc, char *argv[]){ if(argc != 3){ printf("usage: %s [source] [dest]\n", argv[0]); exit(1); } char buffer1[5]; strcpy(buffer1, argv[1]); char buffer2[5]; strcpy(buffer1, argv[1]); char x; FILE *file[2]; file[0] = fopen(buffer1,"r+"); file[1] = fopen(buffer2,"w+"); for(x = 0; x < 2; x++){ if(file[x] == NULL){ printf("error opening file.\n"); exit(1); } } do { x = fgetc(file[0]); fputc(x,file[1]); } while(x != EOF); for(x = 0; x < 2; x++) fclose(file[x]); return 0; }

  1. XSS
  2. Arc Injection
  3. Buffer Overflow
  4. Arc Injection AND Buffer Overflow