anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0390-tmpfiles-fix-crash-with-NULL-in-arg_root-and-other-f.patch

52b84b
From 3569b29eb8b082229dd97b8aae60bbe4d2f96ef5 Mon Sep 17 00:00:00 2001
52b84b
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
52b84b
Date: Wed, 19 Dec 2018 23:05:48 +0100
52b84b
Subject: [PATCH] tmpfiles: fix crash with NULL in arg_root and other fixes and
52b84b
 tests
52b84b
52b84b
The function to replacement paths into the configuration file list was borked.
52b84b
Apart from the crash with empty root prefix, it would incorrectly handle the
52b84b
case where root *was* set, and the replacement file was supposed to override
52b84b
an existing file.
52b84b
52b84b
prefix_root is used instead of path_join because prefix_root removes duplicate
52b84b
slashes (when --root=dir/ is used).
52b84b
52b84b
A test is added.
52b84b
52b84b
Fixes #11124.
52b84b
52b84b
(cherry picked from commit 082bb1c59bd4300bcdc08488c94109680cfadf57)
52b84b
52b84b
Resolves: #1836024
52b84b
---
52b84b
 src/basic/conf-files.c     | 21 ++++++++-----
52b84b
 src/test/test-conf-files.c | 61 +++++++++++++++++++++++++++++++++++++-
52b84b
 2 files changed, 73 insertions(+), 9 deletions(-)
52b84b
52b84b
diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c
52b84b
index d6ef0e941e..5ca83091c9 100644
52b84b
--- a/src/basic/conf-files.c
52b84b
+++ b/src/basic/conf-files.c
52b84b
@@ -204,14 +204,17 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
52b84b
                 if (c == 0) {
52b84b
                         char **dir;
52b84b
 
52b84b
-                        /* Oh, we found our spot and it already contains something. */
52b84b
+                        /* Oh, there already is an entry with a matching name (the last component). */
52b84b
+
52b84b
                         STRV_FOREACH(dir, dirs) {
52b84b
+                                _cleanup_free_ char *rdir = NULL;
52b84b
                                 char *p1, *p2;
52b84b
 
52b84b
-                                p1 = path_startswith((*strv)[i], root);
52b84b
-                                if (p1)
52b84b
-                                        /* Skip "/" in *dir, because p1 is without "/" too */
52b84b
-                                        p1 = path_startswith(p1, *dir + 1);
52b84b
+                                rdir = prefix_root(root, *dir);
52b84b
+                                if (!rdir)
52b84b
+                                        return -ENOMEM;
52b84b
+
52b84b
+                                p1 = path_startswith((*strv)[i], rdir);
52b84b
                                 if (p1)
52b84b
                                         /* Existing entry with higher priority
52b84b
                                          * or same priority, no need to do anything. */
52b84b
@@ -220,7 +223,8 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
52b84b
                                 p2 = path_startswith(path, *dir);
52b84b
                                 if (p2) {
52b84b
                                         /* Our new entry has higher priority */
52b84b
-                                        t = path_join(root, path, NULL);
52b84b
+
52b84b
+                                        t = prefix_root(root, path);
52b84b
                                         if (!t)
52b84b
                                                 return log_oom();
52b84b
 
52b84b
@@ -236,7 +240,8 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
52b84b
                 /* … we are not there yet, let's continue */
52b84b
         }
52b84b
 
52b84b
-        t = path_join(root, path, NULL);
52b84b
+        /* The new file has lower priority than all the existing entries */
52b84b
+        t = prefix_root(root, path);
52b84b
         if (!t)
52b84b
                 return log_oom();
52b84b
 
52b84b
@@ -322,7 +327,7 @@ int conf_files_list_with_replacement(
52b84b
                 if (r < 0)
52b84b
                         return log_error_errno(r, "Failed to extend config file list: %m");
52b84b
 
52b84b
-                p = path_join(root, replacement, NULL);
52b84b
+                p = prefix_root(root, replacement);
52b84b
                 if (!p)
52b84b
                         return log_oom();
52b84b
         }
52b84b
diff --git a/src/test/test-conf-files.c b/src/test/test-conf-files.c
52b84b
index 2ec2dfc261..5789767161 100644
52b84b
--- a/src/test/test-conf-files.c
52b84b
+++ b/src/test/test-conf-files.c
52b84b
@@ -13,6 +13,7 @@
52b84b
 #include "macro.h"
52b84b
 #include "mkdir.h"
52b84b
 #include "parse-util.h"
52b84b
+#include "path-util.h"
52b84b
 #include "rm-rf.h"
52b84b
 #include "string-util.h"
52b84b
 #include "strv.h"
52b84b
@@ -42,7 +43,7 @@ static void test_conf_files_list(bool use_root) {
52b84b
         _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
52b84b
         const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c, *mask;
52b84b
 
52b84b
-        log_debug("/* %s */", __func__);
52b84b
+        log_debug("/* %s(%s) */", __func__, yes_no(use_root));
52b84b
 
52b84b
         setup_test_dir(tmp_dir,
52b84b
                        "/dir1/a.conf",
52b84b
@@ -92,6 +93,60 @@ static void test_conf_files_list(bool use_root) {
52b84b
         assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
52b84b
 }
52b84b
 
52b84b
+static void test_conf_files_insert(const char *root) {
52b84b
+        _cleanup_strv_free_ char **s = NULL;
52b84b
+
52b84b
+        log_info("/* %s root=%s */", __func__, strempty(root));
52b84b
+
52b84b
+        char **dirs = STRV_MAKE("/dir1", "/dir2", "/dir3");
52b84b
+
52b84b
+        _cleanup_free_ const char
52b84b
+                *foo1 = prefix_root(root, "/dir1/foo.conf"),
52b84b
+                *foo2 = prefix_root(root, "/dir2/foo.conf"),
52b84b
+                *bar2 = prefix_root(root, "/dir2/bar.conf"),
52b84b
+                *zzz3 = prefix_root(root, "/dir3/zzz.conf"),
52b84b
+                *whatever = prefix_root(root, "/whatever.conf");
52b84b
+
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(foo2)));
52b84b
+
52b84b
+        /* The same file again, https://github.com/systemd/systemd/issues/11124 */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(foo2)));
52b84b
+
52b84b
+        /* Lower priority → new entry is ignored */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir3/foo.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(foo2)));
52b84b
+
52b84b
+        /* Higher priority → new entry replaces */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir1/foo.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(foo1)));
52b84b
+
52b84b
+        /* Earlier basename */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(bar2, foo1)));
52b84b
+
52b84b
+        /* Later basename */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
52b84b
+
52b84b
+        /* All lower priority → all ignored */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir3/bar.conf") == 0);
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
52b84b
+
52b84b
+        /* Two entries that don't match any of the directories, but match basename */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/dir4/zzz.conf") == 0);
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/zzz.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
52b84b
+
52b84b
+        /* An entry that doesn't match any of the directories, no match at all */
52b84b
+        assert_se(conf_files_insert(&s, root, dirs, "/whatever.conf") == 0);
52b84b
+        assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, whatever, zzz3)));
52b84b
+}
52b84b
+
52b84b
 int main(int argc, char **argv) {
52b84b
         log_set_max_level(LOG_DEBUG);
52b84b
         log_parse_environment();
52b84b
@@ -99,5 +154,9 @@ int main(int argc, char **argv) {
52b84b
 
52b84b
         test_conf_files_list(false);
52b84b
         test_conf_files_list(true);
52b84b
+        test_conf_files_insert(NULL);
52b84b
+        test_conf_files_insert("/root");
52b84b
+        test_conf_files_insert("/root/");
52b84b
+
52b84b
         return 0;
52b84b
 }