teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame 0017-basic-forbid-rm_rf-to-remove-paths-ending-with-.-565.patch

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