tags:

views:

2643

answers:

4

According to Wikipedia, the following is a very elegant bash fork bomb:

:(){ :|:& };:

How does it work?

+3  A: 

There's a good explanation here of how the fork bomb works:

http://www.cyberciti.biz/faq/understanding-bash-fork-bomb/

Unfortunately, it is littered with smilies, I've uploaded it here in plain text also:

http://pbin.oogly.co.uk/listings/viewlistingdetail/7e9399079ac13111492326d01ed16d

Enjoy, it's a good read.

Jon
+44  A: 

Breaking it down, there are three big pieces:

:()      # Defines a function, ":". It takes no arguments.
{ ... }; # The body of the function.
:        # Invoke the function ":" that was just defined.

Inside the body, the function is invoked twice and the pipeline is backgrounded; each successive invocation on the processes spawns even more calls to ":". This leads rapidly to an explosive consumption in system resources, grinding things to a halt.

Note that invoking it once, infinitely recursing, wouldn't be good enough, since that would just lead to a stack overflow on the original process, which is messy but can be dealt with.

A more human-friendly version looks like this:

kablammo() {             # Declaration
  kablammo | kablammo&   # The problematic body.
}; kablammo              # End function definition; invoke function.

Edit: William's comment below was a better wording of what I said above, so I've edited to incorporate that suggestion.

John Feminella
...and, kablammo!
Suvesh Pratapa
Just make sure you stand clear of the blast when your system goes up in smoke!
John Feminella
Minor nit: "the function is invoked twice and an instance is backgrounded" is not quite right. The function is invoked twice in a pipeline and the pipe is backgrounded, so both instances are being run in the background. Although that's a semantic detail as in reality it is likely that only one is ever attempted, and its recursion halts the system before the other ever starts.
William Pursell
@William: Thanks! I've modified my answer to reflect your correction.
John Feminella
@william: "its recursion halts the system before the other ever starts", so does that mean that :(){ :: would work as a bomb too?
Dan
+3  A: 

Short answer:

The colon (":") becomes a function, so you are running the function piped to the function and putting it in the backgroun which means for every invocation of the function 2 copies of the function are invoked. Recursion takes hold.

Talljoe
A: 

Another version is available here: http://planet.admon.org/howto/bash-shell-fork-bomb/

dotos