Defusing Fork Bomb
Due to their nature, fork bombs can be difficult to stop once started. Stopping a fork bomb from reproducing further requires the termination of all running copies, which can be difficult to achieve. One problem faced is that a separate program to terminate the fork bomb cannot execute if the process table is fully saturated. The second major problem is that in the time taken between finding the processes to terminate and actually terminating them, more may have been created.
Some fork bombs can be stopped relatively easily. Consider the shell fork bomb:
:(){ :|: & };:
bomb() { bomb | bomb & }; bomb
One important "feature" in this computer code means that a fork bomb process which can no longer fork doesn't stick around, but rather exits. In this situation, if we also try to run a new process often enough, eventually one will successfully start. If the new process does nothing, each new do-nothing process we run reduces the number of rampant "fork bomb" processes by one, until eventually all of them can be eradicated. At this point the do-nothing processes can exit. The following short Z Shell code might get rid of the above fork bomb in about a minute:
while (sleep 100 &) do; done
killall -STOP processWithBombName killall -KILL processWithBombName
$ killall -9 processWithBombName bash: fork: Cannot allocate memory
killall -9 is not executed directly from the shell because the command is not atomic and doesn't hold locks on the process list, so by the time it finishes the fork bomb will advance some generations ahead. So one must launch a couple of killall processes, for example:
while :; do killall -9 processWithBombName; done
cd /proc;Part 1: Fork Bomb In-Depth
for p in [0-9]*; do read CMDLINE < $p/cmdline; if [[ $CMDLINE == "processWithBombName" ]];
then kill -s SIGSTOP $p; fi; done
for p in [0-9]*; do read CMDLINE < $p/cmdline; if [[ $CMDLINE == "processWithBombName" ]];
then kill -s SIGKILL $p; fi; done
Part 3: Preventing Fork Bomb
Source:wikipedia
Regards,
Hardeep Singh aka cyb3r.gladiat0r
Comments
Post a Comment