This is based upon this hoax question here. The problem described is having a bash script which contains something to the effect of:
rm -rf {pattern1}/{pattern2}
...which if both patterns include one or more empty elements will expand to at least one instance of rm -rf /
, assuming that the original command was transcribed correctly and the OP was doing brace expansion rather than parameter expansion.
In the OP's explanation of the hoax, he states:
The command [...] is harmless but it seems that almost no one has noticed.
The Ansible tool prevents these errors, [...] but [...] no one seemed to know that, otherwise they would know that what I have described could not happen.
So assuming you have a shell script that emits an rm -rf /
command through either brace expansion or parameter expansion, is it true that using Ansible will prevent that command from being executed, and if so, how does it do this?
Is executing rm -rf /
with root privileges really "harmless" so long as you're using Ansible to do it?
I have virtual machines, let's blow a bunch of them up! For science.
First attempt:
OK, so
command
just passes the literals along, and nothing happens.How about our favorite safety bypass,
raw
?No go again! How hard can it possibly be to delete all your files?
Oh, but what if they were undefined variables or something?
Well, that didn't work.
But what if the variables are defined, but empty?
Finally, some progress! But it still complains that I didn't use
--no-preserve-root
.Of course, it also warns me that I should try using the
file
module andstate=absent
. Let's see if that works.Good news, everyone! It started trying to delete all my files! But unfortunately it ran into an error. I'll leave fixing that and getting the playbook to destroy everything using the
file
module as an exercise to the reader.DO NOT run any playbooks you see beyond this point! You'll see why in a moment.
Finally, for the coup de grâce...
This VM is an ex-parrot!
Interestingly, the above failed to do anything with
command
instead ofraw
. It just printed the same warning about usingfile
withstate=absent
.I'm going to say that it appears that if you aren't using
raw
that there is some protection fromrm
gone amok. You should not rely on this, though. I took a quick look through Ansible's code, and while I found the warning, I did not find anything that would actually suppress running therm
command.Will Ansible prevent the execution of
rm -rf /
in a shell script?I did inspect the coreutils rm source, which has the following:
The only way to wipe from the root is to get past this code block. From this source:
I interpret this to mean that the the function
get_root_dev_ino
returns null on/
, and thus rm fails.The only way to bypass the first code block (with recursion) is to have
--no-preserve-root
and it does no use an environment variable to override, so it would have to be passed explicitly to rm.I believe this proves that unless Ansible explicitly passes
--no-preserve-root
torm
, it will not do this.Conclusion
I do not believe that Ansible explicitly prevents
rm -rf /
becauserm
itself prevents it.