From owner-freebsd-hackers@FreeBSD.ORG Sat Oct 2 08:40:02 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CD15F16A4D0 for ; Sat, 2 Oct 2004 08:40:02 +0000 (GMT) Received: from rosebud.otenet.gr (rosebud.otenet.gr [195.170.0.26]) by mx1.FreeBSD.org (Postfix) with ESMTP id 05CAF43D58 for ; Sat, 2 Oct 2004 08:40:02 +0000 (GMT) (envelope-from keramida@freebsd.org) Received: from gothmog.gr (patr530-a232.otenet.gr [212.205.215.232]) i928dv4A024997 for ; Sat, 2 Oct 2004 11:39:57 +0300 Received: from gothmog.gr (gothmog [127.0.0.1]) by gothmog.gr (8.13.1/8.13.1) with ESMTP id i928Xfts044355 for ; Sat, 2 Oct 2004 11:33:41 +0300 (EEST) (envelope-from keramida@freebsd.org) Received: (from giorgos@localhost) by gothmog.gr (8.13.1/8.13.1/Submit) id i928XfDh044354 for freebsd-hackers@freebsd.org; Sat, 2 Oct 2004 11:33:41 +0300 (EEST) (envelope-from keramida@freebsd.org) Date: Sat, 2 Oct 2004 11:33:41 +0300 From: Giorgos Keramidas To: freebsd-hackers@freebsd.org Message-ID: <20041002083341.GB21439@gothmog.gr> References: <20041002081928.GA21439@gothmog.gr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20041002081928.GA21439@gothmog.gr> Subject: Re: Protection from the dreaded "rm -fr /" X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Oct 2004 08:40:02 -0000 On 2004-10-02 11:19, Giorgos Keramidas wrote: > John Beck, who works for Sun, has posted an entry in his blog yesterday > about "rm -fr /" protection, which I liked a lot: > http://blogs.sun.com/roller/page/jbeck/20041001#rm_rf_protection Here's a simpler diff, which I wrote after I looked a bit in the while-loop I had and realized it was really an obfuscated for-loop: %%% Index: rm.c =================================================================== RCS file: /home/ncvs/src/bin/rm/rm.c,v retrieving revision 1.47 diff -u -r1.47 rm.c --- rm.c 6 Apr 2004 20:06:50 -0000 1.47 +++ rm.c 2 Oct 2004 08:28:36 -0000 @@ -157,6 +157,7 @@ void rm_tree(char **argv) { + char **argv_tmp; FTS *fts; FTSENT *p; int needstat; @@ -164,6 +165,14 @@ int rval; /* + * If one of the members of argv[] is the root directory abort the + * entire operation. + */ + for (argv_tmp = argv; *argv_tmp != NULL; argv_tmp++) + if (strcmp(*argv_tmp, "/") == 0) + errx(1, "recursive rm of / is not allowed"); + + /* * Remove a file hierarchy. If forcing removal (-f), or interactive * (-i) or can't ask anyway (stdin_ok), don't stat the file. */ %%%