News for hackers. Stuff that matters.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by jedisct1 on Tuesday March 24, 2015 @02:55AM (#9255200)
My good old trick to mitigate that is:

    touch /-@
I also always do it in my home directory:

    touch ~/-@
That's the first thing I do on a new host.

When accidentally running rm -f *, the command expands to -@ first, which is not a valid option and makes the command fail before doing any harm

    rm: illegal option -- @
    usage: rm [-f | -i] [-dPRrvW] file ...
Re:Bug 1202858 – Restarting squid results in deleting all fi... by ulrikrasmussen on Tuesday March 24, 2015 @03:16AM (#9255248)
While this is a really nice hack, stuff like this is also the reason I feel really uneasy when writing shell scripts. What works now may suddenly break in the future due to inadequate escaping of filenames.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by JoshTriplett on Tuesday March 24, 2015 @03:07AM (#9255229)
That won't actually help with rm -rf /* , only with rm -rf * in / or $HOME.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by hk__2 on Tuesday March 24, 2015 @04:20AM (#9255394)
Would it work with `touch "/ -@"` (not the space)?
Re:Bug 1202858 – Restarting squid results in deleting all fi... by a3_nm on Tuesday March 24, 2015 @05:51AM (#9255629)
No, it doesn't work. "./*" expands to "./ -@" as a single field, which rm has no problems with. (Note that, however, this is still the globbing of the shell, as far as I understand.)
Re:Bug 1202858 – Restarting squid results in deleting all fi... by iso8859-1 on Tuesday March 24, 2015 @05:49AM (#9255621)
how would changing the filename fix it? it's a hack relying on the globbing of the shell. if you're not using the globbing, the hack can never help you.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by Zenst on Tuesday March 24, 2015 @12:01PM (#9257439)
Very handy trick. More so if no root access, myself I preer to rename rm and drop a shell wrapper in place and can be simply a case of changing all passed "" into "Sorry_Dave_I_can_not_allow_you_to_do_that" (or words to that effect) and all "-AsteRISKdeleteALL" into "-" and then that modified input is passed onto rm. But can adjust how and add rules to taste.

That way the pain of having to type AsteRISKdeleteALL instead of * for rm events offsets any anxiety by far.

You can also catch the rm and mv to a difectory with quota's you can call a recycle bin, some low end attached storage can be fine as well as not many situations when your wildcard deleting with a time factor. Can accommodate this in your own skulker to clean up in a more organised way overall in a timely manner. and and scripts you can path to the real rm command if needs be, last time I called it P45Generator, but not the finest for readability in any such scripts.

Re:Bug 1202858 – Restarting squid results in deleting all fi... by lambeosaurus on Tuesday March 24, 2015 @03:00AM (#9255217)
As someone who uses the commandline a lot but isn't exactly a wizard, why does this work?
Re:Bug 1202858 – Restarting squid results in deleting all fi... by lsb on Tuesday March 24, 2015 @03:05AM (#9255225)
globbing expression expand to all the files that match.

if a directory has 1.txt, 2.txt, and 3.txt then << rm * >> expands to "rm 1.txt 2.txt 3.txt" and is then executed.

if you have -@, 1.txt, 2.txt, and 3.txt, that expands to << rm -@ 1.txt 2.txt 3.txt >>, and that can't execute.

(if you really wanted to remove your -@ you'd do << rm -- * >> because a double-dash signals the end of command-line options.)

Re:Bug 1202858 – Restarting squid results in deleting all fi... by ulrikrasmussen on Tuesday March 24, 2015 @03:07AM (#9255228)
The * is expanded by the shell to a space-delimited list of filenames, but the shell does not adequately escape filenames that can be misinterpreted as arguments to 'rm'.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by gmac on Tuesday March 24, 2015 @03:24AM (#9255272)
That's kind of scary — in that case I guess I should avoid creating a file named -rf.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by coldpie on Tuesday March 24, 2015 @09:14AM (#9256316)
Yes. There was an article linked from HN ages ago (at least a year) that went into mitigation techniques for these issues. As you expect, it basically became fractal, and even then still had bugs. I wish I still had the URL.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by un1xl0ser on Tuesday March 24, 2015 @02:31PM (#9258466)
I think it may be more scary for code that allows arbitrary execution using command-line arguments. Commands like find or xargs using without defense against this would be a problem. For example, site that does something precious with your uploaded pet pictures.

Defending against this being the use of -- to signal an end of command line arguments.

Re:Bug 1202858 – Restarting squid results in deleting all fi... by wodenokoto on Tuesday March 24, 2015 @04:13AM (#9255372)
That is really interesting.

Can someone knowledgeable about the shell expand on this? I don't dare test it on my machine.

Re:Bug 1202858 – Restarting squid results in deleting all fi... by vbezhenar on Tuesday March 24, 2015 @04:37AM (#9255439)
Shell session to demonstrate (DO NOT DO IT IN THE DIRECTORY WITH IMPORTANT FILES):

  $ touch important
  $ chmod 400 important
  $ rm *
  override r--------  vbezhenar/staff for important? n
  $ touch -- -rf
  $ ls -l
  total 0
  -rw-r--r--  1 vbezhenar  staff  0 Mar 24 14:35 -rf
  -r--------  1 vbezhenar  staff  0 Mar 24 14:35 important
  $ rm *
  $ ls -l
  total 0
  -rw-r--r--  1 vbezhenar  staff  0 Mar 24 14:35 -rf
  $ rm -- -rf
Re:Bug 1202858 – Restarting squid results in deleting all fi... by theothertom on Tuesday March 24, 2015 @04:41AM (#9255450)
I've not tested it, but it should expand just like anything else. The effect would broadly be that running "rm *" in the directory would recurse into subfolders without warning.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by pbhjpbhj on Tuesday March 24, 2015 @11:10AM (#9257051)
Re testing. If you follow this link - https://www.digitalocean.com/?refcode=3fc9a5a35c52 - you get $10 free credit (affiliate link I get $25 if you spend that much in future) on DigitalOcean.

You can spin up a droplet and use the online shell tool or ssh in (very easy when you've set up a cert as the droplet can have the cert setup automatically).

Then you can mess about with a droplet as much as you like, virtually speaking. Once you're done then use the control panel to destroy the droplet - it costs a few ¢ a day and if you don't have a droplet in use (which means active or paused; preserving images is cheaper but non-zero) then you don't pay anything.

Basically sign up and have a year of uptime to mess with a full install of various OS with no charge.

Make sure you don't write "rm -rf /*" in the wrong terminal!

Re:Bug 1202858 – Restarting squid results in deleting all fi... by bentcorner on Tuesday March 24, 2015 @12:47PM (#9257737)
> shell does not adequately escape filenames that can be misinterpreted as arguments to 'rm'

That sounds like a bug to me, or at least depending on suboptimal behavior.

Re:Bug 1202858 – Restarting squid results in deleting all fi... by raverbashing on Tuesday March 24, 2015 @04:24AM (#9255406)
More specifically, it is expanded on the shell

Example:

$ echo /*

/bin /boot /dev /etc /home (...)

Re:Bug 1202858 – Restarting squid results in deleting all fi... by atmosx on Tuesday March 24, 2015 @06:26AM (#9255723)
Hm that's a cool trick. IIRC some distributions (Suse?) had a 'bash' clause where you couldn't do "rm -rf /" without 'y'.

My 'zsh' has this one too, when I 'rm -rf /some/dir/' always asks if I'm "sure". Truth to be told, I'm not even expecting the text in "stdout" anymore, my finger goes to the 'y' automatically, which means that if I make something stupid it won't be able to protect me :-P

The last couple of years I stopped doing 'stupid things' by stop working on the shell when I'm very tired. That was the cause of my rm-related-incidents in the past :-)

Re:Bug 1202858 – Restarting squid results in deleting all fi... by iancarroll on Tuesday March 24, 2015 @08:25AM (#9256120)
Some versions of RM (Ubuntu/Debian if I remember correctly) require --no-preserve-root.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by amadvance on Tuesday March 24, 2015 @03:21AM (#9255268)
Likely you mean: touch ~/-@
Re:Bug 1202858 – Restarting squid results in deleting all fi... by miles on Tuesday March 24, 2015 @03:18AM (#9255259)
Neat idea! Just tested that in an OS X 10.8 virtual machine; while it works nicely against "rm -rf *", sadly it does not help stop an accidental sudo rm -rf / or ~/. Also, "touch ~-@" created a file in the home directory called "~-@"; in order to set the correct filename, I cd'd into ~ and then ran "touch ./-@".
Re:Bug 1202858 – Restarting squid results in deleting all fi... by liviu- on Tuesday March 24, 2015 @08:16AM (#9256084)

  touch /--
  rm -rf *
(admittedly, this would be a malicious attempt rather than a careless bug)
Re:Bug 1202858 – Restarting squid results in deleting all fi... by quoiquoi on Friday March 27, 2015 @08:30PM (#9279933)
alias rm='trash-put'
Re:Bug 1202858 – Restarting squid results in deleting all fi... by ioquatix on Tuesday March 24, 2015 @03:07AM (#9255232)
Or just use zsh.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by michaelx386 on Tuesday March 24, 2015 @04:56AM (#9255484)
Would zsh still protect me if the script explicitly uses Bash (i.e. #!/bin/bash)? Sorry if this is a dumb question, I'm unsure how shells work when calling other kinds of shell scripts.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by phireal on Tuesday March 24, 2015 @05:43AM (#9255600)
No. If the script has the #! set as /bin/bash, then it will run as bash.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by anh79 on Tuesday March 24, 2015 @04:56AM (#9255486)
zsh is an interactive shell. It is not to replace #bash as system shell as I know.
Re:Bug 1202858 – Restarting squid results in deleting all fi... by jedisct1 on Tuesday March 24, 2015 @08:58AM (#9256262)
Zsh doesn't have to be used interactively.

It's also excellent for scripting, and has far more features than bash.

Re:Bug 1202858 – Restarting squid results in deleting all fi... by Aissen on Tuesday March 24, 2015 @06:29AM (#9255731)
rm -rf -- $EMPTY_VAR/*

Ooops.