rm -rf / isn’t funny

Perlmonks discusses File::Remove, which until recently, seems to have a test that would effectively test rm -rf /:

my %links = (
  l_ex   => curdir(),
  l_ex_a => rootdir(),
  l_nex  => 'does_not_exist'
);
my $errs = 0;
foreach my $link (keys %links) {
  my $path = catdir( $testdir, $link );
  unless( symlink($links{$link}, $path )) {
    diag("Cannot create symlink $link -> $links{$link}: $!");
    $errs++;
  }
}
if ( $errs ) {
  die("Could not create test links");
}

ok( File::Remove::remove(\1, map { catdir($testdir, $_) } keys %links),
                                            "remove \\1: all links" );

If everything goes fine, nothing bad happens & the test passes.

Apparently in some cases when the test fails, the remove call — invoked recursively — transverses through l_ex_a — which is assigned rootdir() — and tries to delete everything.

Add to the fact most people install Perl modules as root and … oops. No more anything.

It’s fixed now & Bad Idea aside, there was some interesting discussion on installing Perl modules.

Old skool users will remember the days of perl Makefile.PL; make; make test; make install.

A longtime ago it became much simpler: cpan install Date::Calc. A lot of systems are set to install in /usr/lib/perl5, meaning sudo is needed — thus sudo cpan install Date::Calc. Hence the possibility of blowing away /.

schwern had what I thought was a really good suggestion: Setup CPAN.pm to do all its work as a normal user & only invoke sudo during the install phase. Just launch cpan and tweak your config:

  • o conf make_install_make_command ’sudo make’
  • o conf mbuild_install_build_command ’sudo ./Build’

4 Responses to “rm -rf / isn’t funny”

  1. Christopher Smith Says:

    When you think about it, it’s kind of criminal that this isn’t how it works from the start.

  2. rick Says:

    Do you think I would like ‘cpan install’?

  3. wdr1 Says:

    Rick, that you took the time to harass me from a trip in *Hawaii* — frankly, I’m flattered.

  4. rick Says:

    I think you would like Hawaii.

Leave a Reply