tmpfiles: use safe_glob()

This filters out "." and ".." from glob results. Fixes #5655 and #5644.

Any judgements on whether the path is "safe" are removed. We will not remove
"/" under any name (including "/../" and such), but we will remove stuff that
is specified using paths that include "//", "/./" and "/../". Such paths can be
created when joining strings automatically, or for other reasons, and people
generally know what ".." and "." is.

Tests are added to make sure that the helper functions behave as expected.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2017-04-27 13:20:30 -04:00
parent 48d7c64805
commit 84e72b5ef4
3 changed files with 38 additions and 16 deletions
+1 -8
View File
@@ -182,18 +182,11 @@ int rm_rf(const char *path, RemoveFlags flags) {
/* We refuse to clean the root file system with this
* call. This is extra paranoia to never cause a really
* seriously broken system. */
if (path_equal(path, "/")) {
if (path_equal_or_files_same(path, "/")) {
log_error("Attempted to remove entire root file system, and we can't allow that.");
return -EPERM;
}
/* Another safe-check. Removing "/path/.." could easily remove entire root as well.
* It's especially easy to do using globs in tmpfiles, like "/path/.*", which the glob()
* function expands to both "/path/." and "/path/..".
* Return -EINVAL to be consistent with rmdir("/path/."). */
if (endswith(path, "/..") || endswith(path, "/../"))
return -EINVAL;
if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
/* Try to remove as subvolume first */
r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);