LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2006, 04:47 PM   #1
yapp
Member
 
Registered: Apr 2003
Location: Netherlands
Distribution: SuSE (before: Gentoo, Slackware)
Posts: 613

Rep: Reputation: 30
[bash] How does this work: :(){ :|:&};:


In a slashdot signature I found the bash command:
Code:
:(){ :|:&};:
I'm sure it's a fork-bomb, but is someone able to explain how it works?
 
Old 12-12-2006, 04:50 PM   #2
yapp
Member
 
Registered: Apr 2003
Location: Netherlands
Distribution: SuSE (before: Gentoo, Slackware)
Posts: 613

Original Poster
Rep: Reputation: 30
Sorry, a Google search did help me already:
http://www.euglug.org/pipermail/eugl...st/004338.html

 
Old 12-12-2006, 05:22 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Certainly looks something like that. It is more intelligible once you recognize the function definition pattern:
Code:
function_name () { commands ... }
The function name in question is ":". Inside the function it calls itself twice, connecting the stdout of the first instance to the stdin of the second instance. This keeps both running indefinitely. This pipeline is executed in the background, so the function can return. The ;: at the end terminates the function declaration and executes it.

So, because it's calling itself and returning without waiting for the child invocations to finish, you quickly end up with exponential growth of numbers of running instances of this function.

Exactly what happens when you get a lot of instances of this function isn't completely clear to me. As far as I understand it, they will all execute in the same shell process. So I would imagine you'd pin your CPU and either exhaust memory or fill up that shell's allocation of file handles. If the ulimits are set badly on your system (too many Linux distros don't set paranoid ulimits!), you might fill up the whole file tables in the kernel.

Perhaps someone would care to execute this on a test machine and report what it does (whether it creates lots of processes, or just one which swamps memory / pegs the CPU?).
 
Old 12-13-2006, 06:20 PM   #4
Tralce
Member
 
Registered: Nov 2006
Location: Maine
Distribution: Debian, Ubuntu, Gentoo
Posts: 109

Rep: Reputation: 15
Thanks. I actually understand this thing now.
 
Old 12-14-2006, 03:34 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Well since I have a pretty robust system which doesn't blink twice if you hit the powerswitch I tried it. After about 4 seconds I got
bash: fork: process temporarily unavailable
along with whirring of disks. Then more repititions of the message occasionally and continual whirring -probaly swap drive. But you'll never get any window to refresh or display after running this.
Perhaps doing a 'swapoff -a' first and then running this would serve as quick way to test OOM finctionality.
Don't try this unless you are using reiserfs or you might be very sorry...
 
Old 12-14-2006, 03:59 AM   #6
cyber-worx
Member
 
Registered: May 2004
Location: England / Switzerland
Distribution: Gentoo, Ubuntu, Debian, Slackware
Posts: 138

Rep: Reputation: 15
Simple infinite recursion
 
Old 12-14-2006, 07:08 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I once tested a meaty sunos machine with a fork bomb. It was quite robust. Responsiveness dropped terribly, but I still had a shell which worked (very slowly). Most of the time I for "resource unavailable" messages but every so often I could run something. I managed to write a little loop which ran ps, iterated over the output and extracted the PIDs of the forking nasty and put them all to sleep using "kill -SIGSTOP". Then I killed them.

I believe that test included a bomb with a pipe in like this one. I suspect this makes the bomb less effective. If the user's process limit it hit with one process left before the limit, it can't "fill it up" totally because it needs two spare processes to start the pipeline.

What I don't understand is that if I write a shell function which prints the PID, it doesn't create a new process - there is no fork to speak of. Not with bash anyhow. Anyone know how hat happens - does it exhaust memory?

Lastly. How does one prevent a nasty little package like this killing your Linux box? How do you configure the limits? I know that in a shell you can use the ulimit command, but what about system-wide limits?
 
Old 12-14-2006, 03:23 PM   #8
yapp
Member
 
Registered: Apr 2003
Location: Netherlands
Distribution: SuSE (before: Gentoo, Slackware)
Posts: 613

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42
What I don't understand is that if I write a shell function which prints the PID, it doesn't create a new process - there is no fork to speak of. Not with bash anyhow. Anyone know how hat happens - does it exhaust memory?
There might be some bash builtins. See 'help' for a nice list, or "man bash" or "man bashbuiltins".


Quote:
Originally Posted by matthewg42
Lastly. How does one prevent a nasty little package like this killing your Linux box? How do you configure the limits? I know that in a shell you can use the ulimit command, but what about system-wide limits?
I believe you can set this from 'pam', or /etc/profile so every user gets this. Still makes me wonder about non-login shells though.

Last edited by yapp; 12-14-2006 at 03:25 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM
'emacs&' doesn't work verbose Debian 27 06-01-2005 07:20 AM
IBM laptop & touchpad & TrackPoint - touchpad doesn't work! espinosa_cz Linux - Laptop and Netbook 1 08-20-2004 12:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration