Secure Coding and Vulnerabilities Quiz

Test your knowledge of secure programming practices, common vulnerabilities like SQL injection, buffer overflow, and command injection, and memory management security in Java and C/C++.

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)
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;
}

which line of the code should be deleted to remove vulnerability

  1. line 1 and line 3
  2. line 2 and line 3
  3. line 1
  4. line 3
Question 2 Multiple Choice (Single Answer)

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

  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 3 Multiple Choice (Single Answer)
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;
        }
    }
}

Will this program execute successfully ?

  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 4 Multiple Choice (Single Answer)
unsigned int i; 
scanf("%u",&i);

With the size of unsigned integers being 4 bytes, What happens when a negative number is entered?

  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 5 Multiple Choice (Single Answer)
int i=987987987;
int j= i*10;  

what is the value of j (size of integer is 4 bytes)?

  1. 1289945278
  2. garbage. Integer j cannot hold such large values
  3. 9879879870
  4. Program is aborted.
Question 6 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 7 Multiple Choice (Single Answer)

In the following code snippet, should the pointer be deleted?

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 8 Multiple Choice (Single Answer)

How many of following are security code review tools

  • OWASP WebScarab
  • Fortify
  • WebInspect
  • AppScan
  • Nikto
  • FindBugs
  1. 1
  2. 2
  3. 4
  4. 6
Question 9 Multiple Choice (Single Answer)
int main(char* argc, char** argv) { 
	char cmd[CMD_MAX] = "/usr/bin/cat "; 
	strcat(cmd, argv[1]); 
	system(cmd); 
}

Code is vulnerable to buffer overflow attack and

  1. DNS Spoofing
  2. Command Injection
  3. Path Traversal
  4. Both 2 and 3
Question 10 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. both 2 and 3
Question 11 Multiple Choice (Single Answer)

In the following code snippet, how should a pointer be deleted

int main (int argc, char *argv[]) { 
	char* j=new char[100];  
	j=argv[1];  
	int k=atoi(j);  
	/*delete here*/  
	return 0; 
}
  1. delete j
  2. free j
  3. it is not supposed to be deleted
  4. delete [] j
Question 12 Multiple Choice (Single Answer)

A program wants to do some activities with root privileges when it starts up and shuts down. Other activities it does can be done as a lesser privileged user. Which of the options given below is most secure?

  1. The program should be started with root privileges. Then it should use setuid(UID) to change privileges between root and another account.
  2. The program should be started with root privileges. Then it should use seteuid(UID) to change privileges between root and another account.
  3. Starting the program as root is a security risk. The program should run with least privileges and obtain root using seteuid(UID) whenever necessary.
  4. The program has to run with root privileges entirely. Once root privileges are dropped they cannot be regained.
Question 13 Multiple Choice (Single Answer)

What value is stored in *leaf in the program given below?

int *myfunc(int tree) {   
	int *node;   
	int i=rand();   
	if(0==tree/pow(2,i))       
		node=&tree;   
	else       
		node=&i;   
	return node; 
}  

int main(int argc, char * argv[]) {   
	int *leaf;   
	leaf=myfunc(7); 
}
  1. value of tree
  2. value of node
  3. value of i
  4. garbage-- its a dangling pointer
Question 14 Multiple Choice (Single Answer)

List the correct entries in the web.xml deployment descriptor for Exception & Error Handling

  1. <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/error.jsp</location> </error-page>
  2. <error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page>
  3. <error-page> <location>/error.jsp</location> </error-page>
  4. a & b
Question 15 Multiple Choice (Single Answer)

Choose the correct answer:

  1. HTTP PUT & DELETE method can be disabled in web.xml from the below code: <Web-resource-collection> <web-resource-name>Disallowed Location</web-resource-name> <url-pattern>/*</url-pattern> <http-method>PUT</http-m
  2. HTTP PUT & DELETE methods are disabled by default
  3. HTTP PUT & DELETE methods should not be disabled
  4. HTTP PUT & DELETE methods cannot be disabled
Question 16 Multiple Choice (Single Answer)
try { 
	//code to do  IO operations 
	return var1; 
} Catch(Exception e) { 
	return var2; 
} finally{ 
	return var3; 
} 

From security view point problem with above code is

  1. It is returning a value in finally block
  2. It is catching Exception
  3. Both a & b
  4. Nothing is wrong
Question 17 Multiple Choice (Single Answer)
public void dummyFunction(String var1,String var2){ 
	try{ 
		Connection con=getConnection(); 
		String query=”select * from table1 where col1=”+var1 +”and col2=”+var2; 
		Statement st=conn.createStatement(); 
		ResultSet rs=st.executeQuery(query); 
		…… ….. 
	} catch(Exception e) { } 
	
} 

var1 and var2 are inputs from user directly passed to this functions. This code is

  1. Vulnerable to SQL Injection
  2. Vulnerable to DoS
  3. Both a & b
  4. None of the above
Question 18 Multiple Choice (Single Answer)

Below function is used to read file from a directory on the filesystem. This code runs with read only OS level privilege on this directory. fileName is parameter from user directly passed to this function

public void dummyFunction(String fileName){             
	FileInputStream fis = new FileInputStream(fileName); // code to read file content only, no write modify or delete 
}
  1. Security is handled at OS level by giving only read level privilege so no need to put an extra check here.
  2. Only problem here is that fileName may not be syntactically incorrect so it should be validated before using it in the function.
  3. This code can lead to information disclosure attack
  4. Java provides enough security by default for IO operations so this code is not vulnerable.
Question 19 Multiple Choice (Single Answer)

Please select which of the following statements are NOT true regarding the AccessController class?

  1. Can be used to mark code as being "privileged", thus affecting subsequent access determinations
  2. Can be to decide whether an access to a critical system resource is to be allowed or denied, based on the security policy currently in effect
  3. Can be used to obtain a "snapshot" of the current calling context
  4. Can be used to compute a cryptographically secure hash
Question 20 Multiple Choice (Single Answer)

Which of the following best describes how to sign a document using a digital signature?

  1. Create a hash of the document and encrypt the resulting hash using the signer's private key
  2. Encrypt the document using the signer's private key
  3. Encrypt the document using the signer's private key and create a hash of the encrypted document
  4. Encrypt the document using the signer's public key