Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 Jun 2012 02:21:53 +0000 (UTC)
From:      Kevin Lo <kevlo@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r237284 - head/bin/rm
Message-ID:  <201206200221.q5K2LrXM012793@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevlo
Date: Wed Jun 20 02:21:53 2012
New Revision: 237284
URL: http://svn.freebsd.org/changeset/base/237284

Log:
  Fix potential symlink race condition in "rm -P" by adding a check
  that the file we have opened is the one we expected.  Also open in
  non-blocking mode to avoid a potential hang with FIFOs.
  
  Obtained from:	NetBSD via OpenBSD

Modified:
  head/bin/rm/rm.c

Modified: head/bin/rm/rm.c
==============================================================================
--- head/bin/rm/rm.c	Wed Jun 20 00:41:31 2012	(r237283)
+++ head/bin/rm/rm.c	Wed Jun 20 02:21:53 2012	(r237284)
@@ -408,7 +408,7 @@ rm_file(char **argv)
 int
 rm_overwrite(char *file, struct stat *sbp)
 {
-	struct stat sb;
+	struct stat sb, sb2;
 	struct statfs fsb;
 	off_t len;
 	int bsize, fd, wlen;
@@ -427,8 +427,15 @@ rm_overwrite(char *file, struct stat *sb
 		    file, sbp->st_ino);
 		return (0);
 	}
-	if ((fd = open(file, O_WRONLY, 0)) == -1)
+	if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1)
 		goto err;
+	if (fstat(fd, &sb2))
+		goto err;
+	if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino ||
+	    !S_ISREG(sb2.st_mode)) {
+		errno = EPERM;
+		goto err;
+	}
 	if (fstatfs(fd, &fsb) == -1)
 		goto err;
 	bsize = MAX(fsb.f_iosize, 1024);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201206200221.q5K2LrXM012793>