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
  • 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; }...

  • 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...

  • 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...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

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
ADVERTISEMENT