Linux Fork Bomb Explained
One famous way to crash your Linux system is to run a “fork bomb” in the terminal. A variant of it looks like this:
WARNING: Malicious code. Don’t run it!
1 | :(){:|:&};: |
This code looks strange, but it’s actually a conventional code in Bash. To make it a little more obvious, let’s replace the : (colon) with the word bomb. So, our code now looks like this:
1 | bomb(){bomb|bomb&}; bomb |
Arranging the code a little more will leave us with this:
1 2 3 4 | bomb() { bomb|bomb & }; bomb |
Namely, the above code defines a bash function called bomb (colon in the original code), which calls itself recursively twice through a pipe, and sends the recursion call to the background. After the function is defined, all you need is to call it.
Now we just have a process which splits itself (a.k.a fork) forever, creating a huge number of processes which will utilize every single cycle of your CPU, causing the system to get stuck/reboot/crash.
Why to send the job to the background?
The answer is simple: If we run the recursive call in the foreground, the calling function will not complete until the call returns.
Since there is no stop condition for this recursion, the calling function would wait forever, which will allow killing it and all its children with [ctrl]+[C].
On the other hand, when running the recursive call in the background, the calling function will complete immediately, and it won’t be that easy to kill its child processes.
(It’s almost impossible to kill them even with the kill or killall commands, because the number of processes will grow exponentially and will utilize the whole CPU in a very short time, leaving you with the only option: To reboot).
Another question: Why calling twice?
In a “C-style” fork, after a process forks, it will continue to be alive, i.e., after a fork, we’ll have two processes: The parent, and the child.
But here in Bash, it’s not the same: Because we did the recursive call in the background, the calling process will “die” as soon as it makes the recursive call, so, if we call the function only once in the recursion, we’ll have always one process which will replace its parent, and this is not what we’re trying to achieve.
A C code with similar functionality to the above one is the following:
1 2 3 4 5 6 7 8 | #include <unistd.h> int main() { while(1) fork(); return 0; } |
One way to protect your system from such fork bombs, is to limit the number of user processes in the system. An explanation of how to do this along with an explanation about the above fork bomb can be found here.
Another explanation about fork bombs, with examples in different languages can be found here.
If you enjoyed this post, make sure you subscribe to my RSS feed!Related posts:
1 Comment to Linux Fork Bomb Explained
Leave a Reply
About Me
Tags
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- iPhone
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Regular Expressions
- Science
- Security
- Shell Scripts
- Short Posts
- Social Networks
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Calendar
Archives
- July 2010 (5)
- June 2010 (1)
- May 2010 (1)
- April 2010 (3)
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)

Does that mean the system will completely crashed ?? in another language does that mean that i will need to reinstall or restore from a previous backup ???
is there any logs produced from this process ???