r/linuxquestions • u/Rilukian • Oct 31 '21
What does :(){:|:&};: mean?
We know about the troll command 'rm -rf /' where it deletes the whole root partition. But there's this command and I don't understand what it does. Obviously I won't running this in my system because I suspect it does the same kind of trolling as the mentioned rm command does.
84
u/jirbu Oct 31 '21
'rm -rf /' where it deletes the whole root partition
Actually, that's not restricted to a "partition". It will remove all content from all mounted file systems that you have access to, including mounted network drives.
22
u/Caduceus1515 Oct 31 '21
There is an old legendary story where someone did this on the console of an old UNIX minicomputer. The saving grace is that it failed when it tried to remove /bin/rm itself because it was in use - but it deleted the kernel, /dev, much of /bin, etc. before that. They had a tape backup - but couldn't restore because the /dev/tapeX or whatever the device file was was gone, and so was the command to create it, etc.
IIRC they actually were able to write something, using an old editor no one really used any more, that could extract the backups and restore what they needed.
18
u/Spifmeister Oct 31 '21 edited Oct 31 '21
Actually, someone was logged in and using vi (not vim) or ed. vi was in memory, as long as they did not exit the editor, they could rewrite the code necessary to get recover from backup.
They had to recreate a lot of code like ls.
It’s a old Usenet story.
EDIT:
The story might be in Unix Horror Story summery.
5
u/Caduceus1515 Oct 31 '21
Oooh...that's right. They saw someone still logged in with the editor was running. I vaguely recall they needed to write a tape driver...but I haven't been able to find the story. I think it was included in a printed copy of the Jargon file.
3
u/Spifmeister Oct 31 '21
They also had to rush order a technical manual to help with the rewrite. They were lucky to find one available.
2
u/Julian_0x7F Nov 01 '21
thanks for the horror stories...
i am wondering, is there anything that would save the ubuntu linux system i am using right now from erasing everything when i would type "rm -rf /" in the terminal?
2
u/Spifmeister Nov 01 '21
rm normally preserves root and will ask if you are sure .
Do not use -f or —force. -f does not preserve root and does not ask permission to recursively delete directories.
You can use -I to ask once if deleting recursive directories. So instead of running “rm -rf” run “rm -rI” it will prompt you once. “rm -ri” will prompt every directory.
8
1
u/michaelpaoli Nov 01 '21
I remember reading the story, within the last few years or so, linked from somewhere on Reddit. And, at least the version I read, looks like the 3 experts that solved it missed one likely present easy step ... they almost certainly had cpio present, so creating a directory was actually much easier than the particular contortions they went through to create a directory without the missing mkdir command.
23
u/eftepede Oct 31 '21
And nowadays won’t work without
—no-preserve-rootswitch.18
u/jollyGreen_sasquatch Oct 31 '21
This also doesn't protect if you do
rm -rf /*0
Oct 31 '21
[deleted]
5
u/jollyGreen_sasquatch Oct 31 '21
I use set -u at the top of my bash scripts for that, there are a few other options that are good to have as a default: https://gist.github.com/robin-a-meade/58d60124b88b60816e8349d1e3938615
I usually will also use the linked blog post's IFS changes, especially when dealing with looping over directories.
2
u/NL_Gray-Fox Nov 01 '21 edited Nov 01 '21
This one was in a script my colleague made which cleared my homedir...
cd somedir rm -rf *Instead use;
cd somedir || printf "exiting because no such directory\n" && exit 1 rm -rf *Or better yet never
cdin your scripts.0
Nov 01 '21
[deleted]
2
u/NL_Gray-Fox Nov 01 '21
Yep, sadly there are still (badly written) proprietary tools/software that are required to be run from the directory...
0
2
u/brimston3- Nov 01 '21
This bug has happened in some really well known software packages too, eg https://github.com/ValveSoftware/steam-for-linux/issues/3671
1
23
u/imdyingfasterthanyou Oct 31 '21
that's only true on GNU coreutils, busybox rm -rf / will nuke it still for example
1
u/deux3xmachina Oct 31 '21
That ugly hack is only for GNU
rm(1), though it's worth noting thatrm -rf /is never a legal operation anyway.1
u/michaelpaoli Nov 01 '21
rm -rf /
is never a legal operation
Depends how old your *nix or rm is.
1
u/deux3xmachina Nov 01 '21
If
rm -rf .doesn't work, neither shouldrm -rf /since the latter always includes the former.1
u/michaelpaoli Nov 01 '21
If
rm -rf .
doesn't work, neither should
rm -rf /
since the latter always includes the former.
For certain definitions of "always". ;-)
cd into some directory, unlink non-root (/) parent ancestor directory, then . still exists, and may be possible to remove it (especially if the unlinked ancestor wasn't the immediate parent), but even before that, . would no longer be a descendant of the / directory.
But Linux generally won't let one unlink a non-empty directory or do additional hard links on directories ... but more generally UNIX will. Sometimes (highly rarely) there's good reason to do this ... but generally best to avoid such or need for such.
2
1
u/first_byte Nov 01 '21
I saw recently that this is how to “remove French language pack” on Linux. ROFL!
89
u/one_of_them_snowlake Oct 31 '21
: is the name of the function.
a(){} is a function by the name a.
Just a or : executes the function.
| is pipe operator.
& puts things in the background
; terminates one command.
:(){}; defines a function by the name :
This function when invoked by :, the last character, calls itself and then calls itself in the background ad infinitum.
It is a recursive function that never quits and calls itself everytime.
It's also known as a fork bomb. These days, useful shell recognise this pattern and don't execute it.
10
u/thms0 Oct 31 '21
I tried explainshell.com on it with no luck and was too lazy to do this. Thanks!
1
2
0
Oct 31 '21
[deleted]
6
u/marinuso Oct 31 '21
Yes, that's correct.
It spawns two new processes every time through, and fills up the process table, that's how it screws up the whole OS and not just the shell.
4
7
u/firefish5000 Oct 31 '21 edited Oct 31 '21
Others already explained, but a slightly easier to follow breakdown
-----------------function declaration----;--call--
--fn name()--{---function definition----};--call--
--fn name()--{--call---|--call in bg &--};--call--
fork() { fork | fork & }; fork
:() { : | : & }; :Every execution of the forkbomb will execute 2 more, Causing it to use resources exponentially. None of the calls ever end cleanly as they are all waiting on their infinite child process subtree to end. So the resources claimed are never letgo
11
u/AreTheseMyFeet Oct 31 '21
old.reddit friendly version:
-----------------function declaration----;--call-- --fn name()--{---function definition----};--call-- --fn name()--{--call---|--call in bg &--};--call-- fork() { fork | fork & }; fork :() { : | : & }; :4
Oct 31 '21
I like how besides being a good explanation that chart looks like something I could play on my guitar as well.
4
u/rainformpurple Oct 31 '21
"This next piece I've called 'the fork bomb'. It's short and simple."
plays epic guitar solos indefinitely
1
Nov 01 '21
plays epic guitar solos indefinitely
in increasing complexity
1
u/rainformpurple Nov 01 '21
plays epic guitar solos indefinitely in increasing complexity
in increasing intensity
2
u/DrLingy Oct 31 '21
It is a fork bomb, it will fill your RAM with shit process and freeze your system
2
u/smog_alado Oct 31 '21
Follow up question: is there something that can be done after a forkbomb starts executing, other than pressing the power to reset the machine?
6
Oct 31 '21
Depends on you system configuration. E.g, if you configured process boundaries in
/etc/security/limits.confthen all you need to do is wait until you reached that limit.2
u/henry_kr Oct 31 '21
Log in as root, and launch your own version of it but use sleep instead. This will eventually fill the process table with harmless sleeps, so the fork bomb will fail, then you can just kill the sleep processes.
e.g.
while :; do sleep 300 & done1
u/W9CVO Oct 31 '21
Theoretically the pause/break key but I'm not sure how much that's actually implemented in modern machines/CPU's
2
2
Oct 31 '21
I believe that's machine language for "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn." Happy Halloween.
1
2
Oct 31 '21
It crashes your system, except it doesn’t. SystemD prevents you from crashing it with a fork bomb.
7
2
1
u/PCChipsM922U Oct 31 '21
rm -rf / = format C:\
The first one executes completely, the second one doesn't... or it does, but partially...
Hm... now that I come to think of it, the first one SHOULD execute fully, but I'm not quite sure... anyone done it? Just so that I don't have to boot a VM to test it :P :D.
2
u/akaplan Nov 01 '21
rm -rf / actually does more than format C:\ if I am not mistaken. I don't know much about Windows but I assume format C:\ only deletes everything under C but doesn't touch mounted drives. rm -rf / would
1
u/PCChipsM922U Nov 01 '21
Yeah, that's what I meant when I said executes only partially. You can bork your system with format C:\, but it won't erase everything, it'll quit at some point.
Thanks for the info on rm -rf / ;).
1
1
u/Fap_cake_decorator Nov 04 '21 edited Nov 04 '21
It’s a function that loops repeatedly by calling itself. In the example the function name is :, because it makes the code look fun. Here is another example using f as the name:
# Define the function f
f() {
f
}
# Start the loop by calling f
fYour example is the classic fork bomb because each time the function is called, it calls itself once in the foreground and once again in the background, as a new process:
f() {
f &
f
}Each loop starts a new f.
As a one liner, with the call that sets off the loop:
f(){f&f;};fYour example is a bit more pernicious. Because it uses a pipe, each loop starts a new instance of f as well as using file descriptors for the pipe.
f() {
date | f &
f
}Any command will do, on the left of the pipe but for elegance’ sake, why not use f itself?
f() { f | f &; }; f
:() { : | : & }; :Bash lets you use ; or & to separate or terminate statements, so you can see I the second example has one fewer semicolon.
0
Oct 31 '21
[deleted]
3
u/bss03 Oct 31 '21
It's possible to recover if you already have a root shell available. This particular one doesn't actually use that much RAM, it just exhausts the number of PIDs available to the system. Luckily
killis a builtin for most shells, and you can usually sneak in enough PIDs to scan /proc/<pid>/ for the troublemakers (e.g. by uid) and kill them.Last time I did it was on the 2.6 kernel line from bash, but I'm pretty sure the same approach still works.
Even with one that does allocate a lot of RAM, you can use the same technique as long as the kernel oom killer doesn't trash your root shell -- which it probably won't. But, it can take a lot longer, since you'll probably be doing a lot of swapping, reducing performance of everything.
0
Oct 31 '21
[deleted]
2
u/leoechevarria Nov 01 '21
What is the purpose of the piping? What’s the difference with declaring the function as just a&?
And not OP, but thanks for the detailed answer btw.
174
u/[deleted] Oct 31 '21
https://en.m.wikipedia.org/wiki/Fork_bomb
A classic example of a fork bomb is the Unix shell one :(){ :|:& };:, which can be more easily understood as:
fork() { fork | fork & } fork
In it, a function is defined (fork()) as calling itself (fork), then piping (|) its result to a background job of itself (&).