/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
/*
There's a bit tricky race condition with recursive deletion.
Suppose this happens:
lstat(dir, ..) -> OK, it's a directory
// attacker deletes dir, replaces it with symlink to /
opendir(dir) -> it actually opens /
Most portable solution is to lstat() the dir, chdir() there, then check
assumes that the device has usable inodes, most should except for some NFS
implementations.
Filesystems may also reassign a deleted inode to another file
immediately after it's deleted. That in theory makes it possible to exploit
this race to delete the new directory. However, the new inode is quite
unlikely to be any important directory, and attacker is quite unlikely to
find out which directory even got the inode. Maybe with some setuid program
or daemon interaction something could come out of it though.
Another less portable solution is to fchdir(open(dir, O_NOFOLLOW)).
This should be completely safe.
The actual file deletion also has to be done relative to current
directory, to make sure that the whole directory structure isn't replaced
with another one while we're deleting it. Going back to parent directory
isn't too easy either - safest (and easiest) way again is to open() the
directory and fchdir() back there.
*/
#include "lib.h"
#include "path-util.h"
#include "unlink-directory.h"
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
int *first_errno,
const char *fmt, ...)
{
if (first_errno != NULL)
*first_errno = errno;
} else
}
static int
const char **error)
{
struct dirent *d;
#ifdef O_NOFOLLOW
if (dir_fd == -1) {
"open(%s, O_RDONLY | O_NOFOLLOW) failed: %m",
dir);
return -1;
}
#else
return -1;
}
} else {
/* be compatible with O_NOFOLLOW */
}
return -1;
}
if (dir_fd == -1) {
return -1;
}
i_close_fd(&dir_fd);
return -1;
}
/* directory was just replaced with something else. */
i_close_fd(&dir_fd);
return -1;
}
#endif
i_close_fd(&dir_fd);
return -1;
}
i_close_fd(&dir_fd);
return -1;
}
int first_errno = 0;
for (;;) {
errno = 0;
if (d == NULL) {
if (errno != 0) {
"readdir",
dir);
}
break;
}
if (d->d_name[0] == '.') {
/* skip . and .. */
continue;
}
if ((flags & UNLINK_DIRECTORY_FLAG_SKIP_DOTFILES) != 0)
continue;
}
"lstat",
dir);
break;
}
(flags & UNLINK_DIRECTORY_FLAG_FILES_ONLY) == 0) {
if (first_errno == 0)
first_errno = errno;
break;
}
"fchdir",
dir);
break;
}
/* standardize errno */
"rmdir",
dir);
break;
}
(flags & UNLINK_DIRECTORY_FLAG_FILES_ONLY) != 0) {
/* skip directory */
/* can't delete NFS files that are still
in use. let the caller decide if this error
is worth logging about */
break;
} else
/* so it wasn't a directory */
"unlink",
dir,
d->d_name);
}
}
i_close_fd(&dir_fd);
"closedir",
dir);
errno = first_errno;
return -1;
}
return 0;
}
const char **error_r)
{
i_warning("Could not get working directory in unlink_directory(): %s",
error);
orig_dir = ".";
}
if (fd == -1) {
"Can't preserve current directory %s: "
"open(.) failed: %m", orig_dir);
return -1;
}
/* Cannot set error_r to NULL inside of unlink_directory_r()
because of recursion */
i_fatal("unlink_directory(%s): "
}
i_close_fd(&fd);
if (ret < 0) {
}
if ((flags & UNLINK_DIRECTORY_FLAG_RMDIR) != 0) {
/* standardize errno */
}
}
}
return 1;
}