Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash the program with cmd line args

Tags:

c++

c

Lets us consider the following program :

#include <stdlib.h>

int main(int argc, char **argv){
   int a,b;

   if (argc != 3)
       return -1;

   a = atoi(argv[1]);
   b = atoi(argv[2]);

   a = b ? a/b : 0;

   return a;
}

The task is to crash the program by providing arguments in command-line.

like image 270
Quixotic Avatar asked Apr 23 '10 19:04

Quixotic


People also ask

How do I pass a command line argument to a program?

Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.

What is args in terminal?

Go by Example: Command-Line Arguments os. Args provides access to raw command-line arguments. Note that the first value in this slice is the path to the program, and os. Args[1:] holds the arguments to the program.


2 Answers

Pass a as the platform's INT_MIN and b as -1. Then you get an overflow error on any two's complement machine, although that's not necessarily a crash.

like image 164
Potatoswatter Avatar answered Oct 13 '22 07:10

Potatoswatter


The answer to this question is: It depends.

One of the critical pieces of information you need to know is how atoi is implemented and if it's standards compliant. The standard says very little about implementation details and is more specific on the input output behavior. Lets assume for a minute it's indeed standards compliant and focus on the implementation.

There are several methods which can be validly used and one of them is recursively. Lets assume for a second that it's implemented as a head recursive algorithm which forces a build up of stack. I could then cause this program to crash by providing a long enough argument that it forced atoi to recurse deep enough to stack overflow and hence crash the application.

like image 34
JaredPar Avatar answered Oct 13 '22 07:10

JaredPar