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
This commit is contained in:
Kevin Lo 2012-06-20 02:21:53 +00:00
parent 83b322edc6
commit 4ec1405b39
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=237284

View File

@ -408,7 +408,7 @@ rm_file(char **argv)
int int
rm_overwrite(char *file, struct stat *sbp) rm_overwrite(char *file, struct stat *sbp)
{ {
struct stat sb; struct stat sb, sb2;
struct statfs fsb; struct statfs fsb;
off_t len; off_t len;
int bsize, fd, wlen; int bsize, fd, wlen;
@ -427,8 +427,15 @@ rm_overwrite(char *file, struct stat *sbp)
file, sbp->st_ino); file, sbp->st_ino);
return (0); return (0);
} }
if ((fd = open(file, O_WRONLY, 0)) == -1) if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1)
goto err; 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) if (fstatfs(fd, &fsb) == -1)
goto err; goto err;
bsize = MAX(fsb.f_iosize, 1024); bsize = MAX(fsb.f_iosize, 1024);