summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-04-25 23:50:35 -0400
committerSven Eden <yamakuzure@gmx.net>2017-07-25 09:46:52 +0200
commit9045f431a2a7d05a3a897829eb2c2f0b77edd2be (patch)
tree27ad548a1354f633eebbf1ad27284ed08092f26d /src
parent42031136036b3da794a2575d91de8378931141ed (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/basic/rm-rf.c9
-rw-r--r--src/test/test-path-util.c33
2 files changed, 34 insertions, 8 deletions
diff --git a/src/basic/rm-rf.c b/src/basic/rm-rf.c
index 565f240e1..87a7bd845 100644
--- a/src/basic/rm-rf.c
+++ b/src/basic/rm-rf.c
@@ -184,19 +184,12 @@ 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;
}
#if 0 /// elogind does not support BTRFS this directly
- /* 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);
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index 4d957cd3f..72b931568 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -104,6 +104,38 @@ static void test_path(void) {
assert_se(!path_equal_ptr(NULL, "/a"));
}
+static void test_path_equal_root(void) {
+ /* Nail down the details of how path_equal("/", ...) works. */
+
+ assert_se(path_equal("/", "/"));
+ assert_se(path_equal("/", "//"));
+
+ assert_se(!path_equal("/", "/./"));
+ assert_se(!path_equal("/", "/../"));
+
+ assert_se(!path_equal("/", "/.../"));
+
+ /* Make sure that files_same works as expected. */
+
+ assert_se(files_same("/", "/") > 0);
+ assert_se(files_same("/", "//") > 0);
+
+ assert_se(files_same("/", "/./") > 0);
+ assert_se(files_same("/", "/../") > 0);
+
+ assert_se(files_same("/", "/.../") == -ENOENT);
+
+ /* The same for path_equal_or_files_same. */
+
+ assert_se(path_equal_or_files_same("/", "/"));
+ assert_se(path_equal_or_files_same("/", "//"));
+
+ assert_se(path_equal_or_files_same("/", "/./"));
+ assert_se(path_equal_or_files_same("/", "/../"));
+
+ assert_se(!path_equal_or_files_same("/", "/.../"));
+}
+
static void test_find_binary(const char *self) {
char *p;
@@ -555,6 +587,7 @@ int main(int argc, char **argv) {
log_open();
test_path();
+ test_path_equal_root();
test_find_binary(argv[0]);
test_prefixes();
test_path_join();