Linux Fork Bomb

Source: Internet
Author: User
Tags posix

As we all know, bash is an extremely powerful shell that provides powerful interaction and programming capabilities. Such a shell naturally does not lack the "function" element to help the program for efficient modular development and management. As a result of its special features, Bash has a fork bomb. Jaromil designed the most streamlined implementation of a fork bomb in 2002.

The so-called fork Bomb is a malicious program, its internal is an endless loop in the fork process, fork bombs do not need to have special permissions to damage the system. The fork bomb is essentially a simple recursive procedure. Because the program is recursive, if there are no restrictions, this can cause the simple program to quickly drain all the resources inside the system. Now look at the simplest fork bombs designed by Jaromil:

:() {:|:&};:

Or

. () { .|. &};.

A single line of 13-character commands that seem to be incomprehensible can take away resources from all systems. In fact, this line of command is not difficult to understand if it is written as a bash script:

:()
{
: |: &
}
;
:

* Line 1th describes the following to define a function, the function name is a decimal point, there are no optional parameters.
* Line 2nd indicates the start of the function body.
* Line 3rd is what the function body really wants to do, first it calls the function recursively, and then invokes a new process with the pipeline (the thing it does is recursively calls the function) and puts it in the background.
* Line 4th Indicates the end of the function body.
* Line 5th does not perform any operations, and is used to separate two commands from the command line. Overall, it shows that the program contains two parts, first defining a function, and then calling the function.
* Line 6th means calling this function.

The colon ":" is actually the function name, the bash script is executing the function continuously, and then constantly fork out the new process.

For the function name, you may be puzzled, the decimal point can also do function name use it? After all, the decimal point is an inline command for the shell to read the specified file in the current shell environment and run the commands in it. Actually, it does, depending on the order in which bash interprets the commands.

By default, bash is in non-POSIX mode, at which point the commands are interpreted in the following order:

* keywords, such as if, for, and so on.
Alias Aliases cannot be the same as keywords, but aliases can be defined for keywords, such as end=fi.
* Special inline commands, such as break, continue, etc. The special inline commands defined by POSIX include:. (decimal point),: (colon), break, continue, eval, exec, exit, export, ReadOnly, return, set, shift, times, trap, and unset. Bash also adds a special inline command to source.
function If in non-POSIX mode, bash prioritizes the function and then matches the inline command.
* Non-specific inline commands, such as CD, test, etc.
* Scripts and executable programs. Searches in the directory specified by the PATH environment variable to return the first occurrence.
Because bash is in non-POSIX mode by default, the decimal point in the fork bomb takes precedence as a function to match. (Note: Using the decimal point instead of the colon can also play the exact same effect.) )

To run a bash script using POSIX mode, you can use the following three methods:

* Start bash with the –posix option.
* After running bash, execute the set-o POSIX command.
* Use/bin/sh.

So, is there a way to curb the situation? The answer is yes, just set the limit number of the process.

[Email protected] ~]# ulimit-u 128
[Email protected] ~]# ulimit-a
Core file size (blocks,-c) 0
Data seg Size (Kbytes,-D) Unlimited
Max Nice (-e) 20
File size (blocks,-f) Unlimited
Pending signals (-i) unlimited
Max locked Memory (Kbytes,-L) Unlimited
Max memory Size (Kbytes,-m) unlimited
Open files (-N) 1024
Pipe Size (bytes,-p) 8
POSIX message queues (bytes,-Q) Unlimited
Max RT Priority (-R) Unlimited
Stack size (Kbytes,-s) 8192
CPU time (seconds,-t) unlimited
MAX User Processes (-u) 128
Virtual Memory (Kbytes,-V) Unlimited
File locks (-X) Unlimited
[Email protected] ~]#. () { .|. &};.
[1] 6152
[Email protected] ~]# Bash:fork:Resource temporarily unavailable
Bash:fork:Resource temporarily unavailable
Bash:fork:Resource temporarily unavailable
...

In the example above, we limit the maximum number of processes that a user can create to 128, the execution of a fork bomb will quickly fork out a lot of processes, and thereafter it will not be able to continue because of insufficient resources. Use the tool Ulimit to set various limits, refer to the tool's man or help for details.

Fork bombs allow us to recognize the power of recursive functions and realize that the damage caused by recursive functions will be huge once used improperly. In fact, the fork bomb is just a very simple recursive function, it does not involve parameter passing, return value and other problems, and these problems in the use of Bash programming with perfect support? You should be aware of the problem when writing recursive functions in bash.

For more information on the Fork bombs, see: Http://en.wikipedia.org/wiki/Fork_bomb

Linux Fork Bomb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.