Question

1) In C++, come up with a scenario where you should use a catch(...). What should...

1) In C++, come up with a scenario where you should use a catch(...). What should you do in that catch block?

2) Describe in detail any problems you see in the following code.

void funcOne() {

     Dog *ptr = new Dog;

     cout << “Presenting the result of a division: “ << funcDivide();

     delete ptr;

}

int funcDivide() {

    int a, b;

    cin >> a >> b;

    if (b == 0)

      throw “Divide by Zero”;

    return a / b;

}

3) Define command injection. What is of primary importance in helping to stop it?

4a) Write a class in C++ and a code fragment that uses that class in a function call. I want your code to have a double free condition simply based on the copy constructor.

   b) Give a fix to the above code to stop the double free condition.

5) First define what TOCTOU means and then demonstrate an example of this in a small code fragment of your own devising in C++. Please put enough comments in your code to demonstrate an understanding of what your code is doing.

6) Why is an allow-list usually better than a deny-list when doing input validation? Come up with an example where a deny-list might serve a particular environment better than an allow-list and be specific if you can.

7) Suppose you were asked to test some code for possible issues handling exceptions. Describe briefly how you would go about testing to see if your code was exception-safe.

8) You are tasked with taking raw input data from a user, creating a string with it and then doing a system() call with that string in C. Describe how you might go about ensuring that this operation is safe for your system.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer1 :

Catch(...)

This is a special catch block called ‘catch all’ .

1. It is used to catch all types of exceptions. for eg .

in the following program, an int is thrown as an exception, but there is no catch block for int, so catch(…) block will be executed.

#include <iostream>
using namespace std;

int main()
{
   try {
throw 100; // throw exception
   }
   catch (char *excption) {   
       cout << "Catch and out " << excption;
   }
   catch (...) { // catch all block
       cout << " Exception\n";  
   }
   return 0;
}

2. catch all should be used whenever we are throwing primitive type because Implicit type conversion doesn’t happen for primitive types. For example, in the following program ‘a’ is not implicitly converted to int

#include <iostream>
using namespace std;

int main()
{
   try {
   throw 'a';
   }
   catch (int x) {
       cout << "Caught " << x;
   }
   catch (...) {
       cout << "Default Exception\n";
   }
   return 0;
}

Syntax of try - catch block:

try
{
     //statements that may cause an exception
}
catch (exception(type) e(object))‏
{
     //error handling code
}

in the catch block exception need to be handled.

either exception can be rectified in the catch block or it can be removed in this catch block.

Answer3:

Command Injection

Originally known as shell command injection.

The most common form of command injection is known as SQL command injection or simply SQL injection, a security exploit in which a cracker adds SQL (Structured Query Language) code to a Web form input box to gain access to resources or make changes to data.

Command injection is an attack method in which a hacker changes dynamically generated content on a Web page by entering HTML code into an input mechanism. Cracker can exploit that vulnerability to gain unauthorized access to data or network resources. When users visit an affected Web page, their browsers interpret the code, which may cause malicious commands to execute in the users' computers and across their networks. The main goal of the cracker is the execution of arbitrary commands on the host operating system via a vulnerable application. Command injection attacks are possible when an application passes unsafe user supplied data (forms, cookies, HTTP headers etc.) to a system shell. In this attack, the attacker-supplied operating system commands are usually executed with the privileges of the vulnerable application. Command injection attacks are possible largely due to insufficient input validation.

This attack differs from Code Injection, in that code injection allows the attacker to add his own code that is then executed by the application. In Command Injection, the attacker extends the default functionality of the application, which execute system commands, without the necessity of injecting code.

Prevention:

Command Injection is one of the most serious security vulnerabilities that can appear within an application and extreme care must be taken when using the OS to execute commands.

Prevention in order of importance are:

Validate untrusted inputs.

All input to the application that has not been previously validated must be examined to ensure it meets the expectations of the application. Use “whitelist validation”, which means that the application verifies that the input conforms to what it accepts and rejects everything else. Input Validation can include validation of the input’s:

  • Character set
  • Minimum and maximum length
  • Numeric bounds
  • Date bounds
  • Match to a Regular Expression Pattern
  • Membership in a discrete set (e.g. US States, list of colors, salutations, etc.

Try to Avoid Command Line Calls Altogether

Modern programming languages have interfaces that permit you to read files, send emails, and perform other operation system functions. Use APIs wherever possible – only use shell commands where absolutely necessary. This will reduce the number of attack vectors in your application, and will also simplify your codebase.

Run with Restricted Permissions

It is a good practice to run your server processes with only the permissions that they require to function – the principle of least privilege. This can help limit the impact of command injection vulnerabilities as a second line of defense.

Do not “exec” out to the Operating System if it can be avoided. This is the best solution if it can be adopted because it eliminates the risk. Make every effort to do the application’s work within the application.

Add a comment
Know the answer?
Add Answer to:
1) In C++, come up with a scenario where you should use a catch(...). What should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with this one using a try-catch. Thanks! import java.util.LinkedList; /** This question uses the...

    Please help with this one using a try-catch. Thanks! import java.util.LinkedList; /** This question uses the same code as Question 5 in Question_5_Add_Exception_Handling.java Instead of waiting for an exception and catching it, it is usually better to try and prevent problems. Can you think of a better way to stop this code crashing? Modify the printLanguageList and wordCount methods program so that both methods work correctly without using a try-catch statements. */ public class Question_6_Fix_Code_No_Exception_Handling { public static void main(String[]...

  • 1. What is the output? System.out.print(3 + 3 * 3); a. 18 b. 12 c. 9 d. 0 e. 10             2.   What is output by the code below? System.out.print("\\dog\\cat&#...

    1. What is the output? System.out.print(3 + 3 * 3); a. 18 b. 12 c. 9 d. 0 e. 10             2.   What is output by the code below? System.out.print("\\dog\\cat"); a. dog b. dogcat c. \\dog\\cat d. \dog\cat e. catdog\\\\             3.   What is returned by the call     getIt(9) ? public static int getIt(int num){ int ans = 0; if( num >=2 ) {      if( num >= 7)         ans += 2;      else         ans += 3; } ans += 4; return ans; }...

  • 1.In the first question, you will read some code and write a concise, high-level, English description...

    1.In the first question, you will read some code and write a concise, high-level, English description of the code’s action. Here is an example: (1) double mystery(int[] a) { if (a.length == 0) { return 0.0; } double x = 0.0; for (int i: a) { x += i; } return x / a.length; } (2) int mystery(char c, String s) { int x = 0; for (char k : s.toCharArray()) { if (c != k) { x++; } }...

  • In this module you learned about Object-Oriented programming in C++ and how to combine this approach...

    In this module you learned about Object-Oriented programming in C++ and how to combine this approach with the concepts covered in previous modules For this assignment you will write a class called Dog that has the following member variables: birthyear. An int that holds the dog’s birth year. breed. A string that holds the breed of dog. vaccines. A Boolean holding a yes/no value indicating whether the dog is currently on vaccinations. In addition, the class should have the following...

  • C++, use the skeleton code to make a program of the following

    c++, use the skeleton code to make a program of the following include <iostream> tinclude <string> using namespace std; class car public: //define your functions here, at least 5 private: string name; int mpg; double price int horsepower; // feel free to add more atributes int main() // create you objects, call your functions // define member functions here For this lab, write a program that does the following: Creates a class based on the car skeleton code (you may...

  • A contact list is a place where you can store a specific contact with other associated...

    A contact list is a place where you can store a specific contact with other associated information such as a phone number, email address, birthday, etc. Write a program that first takes as input an integer N that represents the number of word pairs in the list to follow. Word pairs consist of a name and a phone number (both strings). That list is followed by a name, and your program should output the phone number associated with that name....

  • Could someone help me out. I am not sure what I should be doing. Seeing it...

    Could someone help me out. I am not sure what I should be doing. Seeing it worked out will allow me to understand what I should be doing and then I can complete it on my own. Usando 2. Complete the Dog Class: a. Using the UML Class diagram to the right declare the instance variables. A text version is available: UML Class Diagram Text Version b. Create a constructor that incorporates the type, breed, and name variables (do not...

  • CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow...

    CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow a user to buy, sell and view stocks in a stock portfolio. This document will describe the minimum expected functions for a grade of 90. Your mission is to “go and do better.” You’ll find a list of enhancement options at the end of this document. Objectives By the end of this project, the student will be able to • write a GUI program...

  • Can someone please help me with this code? I'm writing in C++. Thank you in advance....

    Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers). In order to complete this, you will need a couple of new functions. First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline: string question; cout <<...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT