2d8d482
From 677a50e8e931741a174f45d9cae981253dfae3ff Mon Sep 17 00:00:00 2001
2d8d482
From: Jan Synacek <jan.synacek@gmail.com>
2d8d482
Date: Wed, 29 Mar 2017 08:25:52 +0200
2d8d482
Subject: [PATCH] basic: forbid rm_rf() to remove paths ending with ".."
2d8d482
 (#5653)
2d8d482
2d8d482
Fixes: #5644(cherry picked from commit ab883125704b9310dcdfcf7451a27e85609da76c)
2d8d482
---
2d8d482
 src/basic/rm-rf.c | 7 +++++++
2d8d482
 1 file changed, 7 insertions(+)
2d8d482
2d8d482
diff --git a/src/basic/rm-rf.c b/src/basic/rm-rf.c
2d8d482
index 08497af729..bdaca264ff 100644
2d8d482
--- a/src/basic/rm-rf.c
2d8d482
+++ b/src/basic/rm-rf.c
2d8d482
@@ -187,6 +187,13 @@ int rm_rf(const char *path, RemoveFlags flags) {
2d8d482
                 return -EPERM;
2d8d482
         }
2d8d482
 
2d8d482
+        /* Another safe-check. Removing "/path/.." could easily remove entire root as well.
2d8d482
+         * It's especially easy to do using globs in tmpfiles, like "/path/.*", which the glob()
2d8d482
+         * function expands to both "/path/." and "/path/..".
2d8d482
+         * Return -EINVAL to be consistent with rmdir("/path/."). */
2d8d482
+        if (endswith(path, "/..") || endswith(path, "/../"))
2d8d482
+                return -EINVAL;
2d8d482
+
2d8d482
         if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
2d8d482
                 /* Try to remove as subvolume first */
2d8d482
                 r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);