C/C++ Security Vulnerabilities
Quiz on common security vulnerabilities in C and C++ including buffer overflows, integer overflows, input validation, and memory management issues
Questions
Which Compilation switch will you use to check Buffer Overflows?
- /GS on Visual C++ and -fmudflap -fmudflapth -fmudflapir on GCC
- /O in Vc++ and -O2 in GCC
- /S in Vc++ and -fcrossjumping in GCC
- /S in VC++ and -fno-function-cse in GCC
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; }
- SQL Injection
- Arc Injection
- Buffer Overflow
- both 2 and 3
Which compilation switch should be enabled for stack protection? Choose the best and most secure option.
- fstack-protector
- fstack-protector-all
- fdelete-null-pointer-checks
- Both a and b
What vulnerability is present in following code: unsigned char j,k; j=getchar(); k=getchar(); unsigned char result = j + k;
- Heap Overflow
- Integer overflow
- Buffer overflow
- No Vulnerability
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; }
- j=getchar();
- gets(ipstring);
- Both
- None
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);
- This is a double free vulnerability and must be fixed
- The second call to free() will return an error
- There might be compiler warnings, but the program will run fine
- This is not a security issue
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/ } }
- for (i=0;i<=3;i++)
- intarray[i]=i*i;
- Both
- None
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; }
- Heap overflow
- Integer overflow
- Off by one error
- None
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.
- strncpy(dst,src,len(dst))
- strncpy(dst,src,len(src)+1)
- strncpy(dst,src,len(dst)+1)
- strncpy(dst,src,len(dst)-1)
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 */
- printf ("%.8s",name); AND printf (name);
- printf (name); AND printf ("%s",name);
- printf ("%s",name); AND printf ("%8c", name);
- printf (name);
Which of the following is a secure way to use scanf?
- scanf("%.8s", name);
- scanf("%8s", name);
- scanf("%8c", name);
- scanf("%s", name);
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; }
- delete ptr2; (within if loop) AND delete ptr1;
- delete ptr1; AND delete ptr2; (outside if loop )
- delete ptr2; (within if loop)
- delete ptr2; (outside if loop )
What will be sizeof(name) return? char *name="32000";
- 4 - it is the size of the pointer
- 5 - it is the number of characters in the string that the pointer points to
- 4 - it is the size when 32000 is stored as integer
- 1 - it is the size of a character variable
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; } } }
- Program works when there is only 1 argument with program
- Program works when there are 3 arguments with program
- Program works when there are 4 arguments with program
- Program never executes successfully
With the size of unsigned integers being 4 bytes, What happens when a negative number is entered? unsigned int i; scanf("%u",&i);
- A run-time error is encountered and the program aborts
- unsigned int variables cannot store the sign (+ or -) of the number. The sign is discarded and only the number is stored in i
- A large positive number will be stored in i
- Unsigned int variables cannot store signed numbers. Hence in this program i will contain garbage values.
What is the value of j (size of integer is 4 bytes)? int i=987987987; int j= i*10;
- 1289945278
- garbage. Integer j cannot hold such large values
- 9879879870
- Program is aborted
The application is receiving input from an external source. Which of the following external sources can be considered safe?
- Shell environment variables
- Data received via encrypted network channels
- argv[0] can only have either null or program name
- no external input must be trusted
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; }
- delete j;
- realloc j;
- free j;
- It need not be deleted
Code is vulnerable to buffer overflow attack and
- DNS Spoofing
- Command Injection
- Path Traversal
- Command Injection AND Path Traversal
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; }
- XSS
- Arc Injection
- Buffer Overflow
- Arc Injection AND Buffer Overflow