bug#79218: BUG: sudo rm -rf /* removes without --no-preserve-root

Collin Funk <[email protected]>
Newsgroups gmane.comp.gnu.core-utils.bugs
Message-ID <[email protected]>
Hi,

Doctorixx <[email protected]> writes:

> Hello coreutils maintainers,
>
> I noticed a potentially dangerous difference in how rm handles the / and /*
> patterns.
>
> Currently:
>
> $ sudo rm -rf /
> rm: it is dangerous to operate recursively on '/'
> rm: use --no-preserve-root to override this failsafe
>
> This prevents accidental deletion of the root directory.
>
> However:
>
> $ sudo rm -rf /*
>
> This command will proceed to remove the contents of /, effectively
> destroying the system, without any warning.
>
> While this is technically correct according to shell expansion rules, it
> may be surprising for some users. People might assume /* is just as
> protected as /, but the safeguard doesn’t apply.

The 'rm' command does not handle /* since globbing is handled by the
shell. So 'rm' has no way of knowing it is passed /*, it just sees file
names passed to the command-line. Here is an example:

    $ cat main.c 
    #include <stdio.h>
    #include <stdlib.h>
    int
    main (int argc, char **argv)
    {
      for (int i = 1; i < argc; ++i)
        printf ("%s\n", argv[i]);
      return EXIT_SUCCESS;
    }
    $ gcc main.c
    $ ./a.out /*
    /afs
    /bin
    /boot
    /dev
    /etc
    /home
    [...]

Collin
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.