From e4a7a596e18ff6d8f69e2b751187fac375d95fd9 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Fri, 15 Apr 2016 23:09:58 +0000 Subject: don't allow removal of /. more robust approach involving stat this time. posix uses the language "resolves to the root directory" in this case. ok millert --- bin/rm/rm.1 | 6 +++--- bin/rm/rm.c | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'bin/rm') diff --git a/bin/rm/rm.1 b/bin/rm/rm.1 index d04e4873272..5c8aefaab7d 100644 --- a/bin/rm/rm.1 +++ b/bin/rm/rm.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: rm.1,v 1.39 2015/10/13 04:30:53 daniel Exp $ +.\" $OpenBSD: rm.1,v 1.40 2016/04/15 23:09:57 tedu Exp $ .\" $NetBSD: rm.1,v 1.8 1995/07/25 19:37:30 jtc Exp $ .\" .\" Copyright (c) 1990, 1993, 1994 @@ -33,7 +33,7 @@ .\" .\" @(#)rm.1 8.5 (Berkeley) 12/5/94 .\" -.Dd $Mdocdate: October 13 2015 $ +.Dd $Mdocdate: April 15 2016 $ .Dt RM 1 .Os .Sh NAME @@ -101,7 +101,7 @@ The .Nm utility removes symbolic links, not the files referenced by the links. .Pp -It is an error to attempt to remove the files +It is an error to attempt to remove the root directory or the files .Dq \&. or .Dq .. . diff --git a/bin/rm/rm.c b/bin/rm/rm.c index f9c19549d5f..2f919ffad9c 100644 --- a/bin/rm/rm.c +++ b/bin/rm/rm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rm.c,v 1.36 2016/02/01 22:34:19 gsoares Exp $ */ +/* $OpenBSD: rm.c,v 1.37 2016/04/15 23:09:57 tedu Exp $ */ /* $NetBSD: rm.c,v 1.19 1995/09/07 06:48:50 jtc Exp $ */ /*- @@ -395,9 +395,17 @@ checkdot(char **argv) { char *p, **save, **t; int complained; + struct stat sb, root; + stat("/", &root); complained = 0; for (t = argv; *t;) { + if (lstat(*t, &sb) == 0 && + root.st_ino == sb.st_ino && root.st_dev == sb.st_dev) { + if (!complained++) + warnx("\"/\" may not be removed"); + goto skip; + } /* strip trailing slashes */ p = strrchr(*t, '\0'); while (--p > *t && *p == '/') @@ -412,6 +420,7 @@ checkdot(char **argv) if (ISDOT(p)) { if (!complained++) warnx("\".\" and \"..\" may not be removed"); +skip: eval = 1; for (save = t; (t[0] = t[1]) != NULL; ++t) continue; -- cgit v1.2.3