ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
9fc0f6
From b7f7813318b370d9ecf3833f92c5258d362d9441 Mon Sep 17 00:00:00 2001
9fc0f6
From: Michael Marineau <michael.marineau@coreos.com>
9fc0f6
Date: Fri, 31 Jan 2014 15:35:04 -0800
9fc0f6
Subject: [PATCH] shared: include root when canonicalizing conf paths
9fc0f6
9fc0f6
The conf_files_list family accepts an alternate root path to prefix all
9fc0f6
directories in the list but path_strv_canonicalize_uniq doesn't use it.
9fc0f6
This results in the suspicious behavior of resolving directory symlinks
9fc0f6
based on the contents of / instead of the alternate root.
9fc0f6
9fc0f6
This adds a prefix argument to path_strv_canonicalize which will now
9fc0f6
prepend the prefix, if given, to every path in the list. To avoid
9fc0f6
answering what a relative path means when called with a root prefix
9fc0f6
path_strv_canonicalize is now path_strv_canonicalize_absolute and only
9fc0f6
considers absolute paths. Fortunately all users of already call
9fc0f6
path_strv_canonicalize with a list of absolute paths.
9fc0f6
9fc0f6
(cherry picked from commit 112cfb181453e38d3ef4a74fba23abbb53392002)
9fc0f6
9fc0f6
Related: #1111199
9fc0f6
---
9fc0f6
 src/shared/conf-files.c  | 10 +++-------
9fc0f6
 src/shared/path-lookup.c |  6 +++---
9fc0f6
 src/shared/path-util.c   | 29 +++++++++++++++++++----------
9fc0f6
 src/shared/path-util.h   |  4 ++--
9fc0f6
 src/shared/util.c        |  2 +-
9fc0f6
 src/udev/udev-rules.c    |  2 +-
9fc0f6
 6 files changed, 29 insertions(+), 24 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c
9fc0f6
index ed4070c..3d5b1df 100644
9fc0f6
--- a/src/shared/conf-files.c
9fc0f6
+++ b/src/shared/conf-files.c
9fc0f6
@@ -37,12 +37,8 @@
9fc0f6
 #include "hashmap.h"
9fc0f6
 #include "conf-files.h"
9fc0f6
 
9fc0f6
-static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
9fc0f6
+static int files_add(Hashmap *h, const char *dirpath, const char *suffix) {
9fc0f6
         _cleanup_closedir_ DIR *dir = NULL;
9fc0f6
-        _cleanup_free_ char *dirpath = NULL;
9fc0f6
-
9fc0f6
-        if (asprintf(&dirpath, "%s%s", root ? root : "", path) < 0)
9fc0f6
-                return -ENOMEM;
9fc0f6
 
9fc0f6
         dir = opendir(dirpath);
9fc0f6
         if (!dir) {
9fc0f6
@@ -104,7 +100,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
9fc0f6
         assert(suffix);
9fc0f6
 
9fc0f6
         /* This alters the dirs string array */
9fc0f6
-        if (!path_strv_canonicalize_uniq(dirs))
9fc0f6
+        if (!path_strv_canonicalize_absolute_uniq(dirs, root))
9fc0f6
                 return -ENOMEM;
9fc0f6
 
9fc0f6
         fh = hashmap_new(string_hash_func, string_compare_func);
9fc0f6
@@ -112,7 +108,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
9fc0f6
                 return -ENOMEM;
9fc0f6
 
9fc0f6
         STRV_FOREACH(p, dirs) {
9fc0f6
-                r = files_add(fh, root, *p, suffix);
9fc0f6
+                r = files_add(fh, *p, suffix);
9fc0f6
                 if (r == -ENOMEM) {
9fc0f6
                         hashmap_free_free(fh);
9fc0f6
                         return r;
9fc0f6
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
9fc0f6
index 1a47ea9..03c1380 100644
9fc0f6
--- a/src/shared/path-lookup.c
9fc0f6
+++ b/src/shared/path-lookup.c
9fc0f6
@@ -316,7 +316,7 @@ int lookup_paths_init(
9fc0f6
                 }
9fc0f6
         }
9fc0f6
 
9fc0f6
-        if (!path_strv_canonicalize(p->unit_path))
9fc0f6
+        if (!path_strv_canonicalize_absolute(p->unit_path, NULL))
9fc0f6
                 return -ENOMEM;
9fc0f6
 
9fc0f6
         strv_uniq(p->unit_path);
9fc0f6
@@ -372,10 +372,10 @@ int lookup_paths_init(
9fc0f6
                                 return -ENOMEM;
9fc0f6
                 }
9fc0f6
 
9fc0f6
-                if (!path_strv_canonicalize(p->sysvinit_path))
9fc0f6
+                if (!path_strv_canonicalize_absolute(p->sysvinit_path, NULL))
9fc0f6
                         return -ENOMEM;
9fc0f6
 
9fc0f6
-                if (!path_strv_canonicalize(p->sysvrcnd_path))
9fc0f6
+                if (!path_strv_canonicalize_absolute(p->sysvrcnd_path, NULL))
9fc0f6
                         return -ENOMEM;
9fc0f6
 
9fc0f6
                 strv_uniq(p->sysvinit_path);
9fc0f6
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
9fc0f6
index 45099ee..de291a5 100644
9fc0f6
--- a/src/shared/path-util.c
9fc0f6
+++ b/src/shared/path-util.c
9fc0f6
@@ -165,7 +165,7 @@ char **path_strv_make_absolute_cwd(char **l) {
9fc0f6
         return l;
9fc0f6
 }
9fc0f6
 
9fc0f6
-char **path_strv_canonicalize(char **l) {
9fc0f6
+char **path_strv_canonicalize_absolute(char **l, const char *prefix) {
9fc0f6
         char **s;
9fc0f6
         unsigned k = 0;
9fc0f6
         bool enomem = false;
9fc0f6
@@ -180,13 +180,21 @@ char **path_strv_canonicalize(char **l) {
9fc0f6
         STRV_FOREACH(s, l) {
9fc0f6
                 char *t, *u;
9fc0f6
 
9fc0f6
-                t = path_make_absolute_cwd(*s);
9fc0f6
-                free(*s);
9fc0f6
-                *s = NULL;
9fc0f6
-
9fc0f6
-                if (!t) {
9fc0f6
-                        enomem = true;
9fc0f6
+                if (!path_is_absolute(*s))
9fc0f6
                         continue;
9fc0f6
+
9fc0f6
+                if (prefix) {
9fc0f6
+                        t = strappend(prefix, *s);
9fc0f6
+                        free(*s);
9fc0f6
+                        *s = NULL;
9fc0f6
+
9fc0f6
+                        if (!t) {
9fc0f6
+                                enomem = true;
9fc0f6
+                                continue;
9fc0f6
+                        }
9fc0f6
+                } else {
9fc0f6
+                        t = *s;
9fc0f6
+                        *s = NULL;
9fc0f6
                 }
9fc0f6
 
9fc0f6
                 errno = 0;
9fc0f6
@@ -196,7 +204,7 @@ char **path_strv_canonicalize(char **l) {
9fc0f6
                                 u = t;
9fc0f6
                         else {
9fc0f6
                                 free(t);
9fc0f6
-                                if (errno == ENOMEM || !errno)
9fc0f6
+                                if (errno == ENOMEM || errno == 0)
9fc0f6
                                         enomem = true;
9fc0f6
 
9fc0f6
                                 continue;
9fc0f6
@@ -215,11 +223,12 @@ char **path_strv_canonicalize(char **l) {
9fc0f6
         return l;
9fc0f6
 }
9fc0f6
 
9fc0f6
-char **path_strv_canonicalize_uniq(char **l) {
9fc0f6
+char **path_strv_canonicalize_absolute_uniq(char **l, const char *prefix) {
9fc0f6
+
9fc0f6
         if (strv_isempty(l))
9fc0f6
                 return l;
9fc0f6
 
9fc0f6
-        if (!path_strv_canonicalize(l))
9fc0f6
+        if (!path_strv_canonicalize_absolute(l, prefix))
9fc0f6
                 return NULL;
9fc0f6
 
9fc0f6
         return strv_uniq(l);
9fc0f6
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
9fc0f6
index 0a42de7..c69cd1f 100644
9fc0f6
--- a/src/shared/path-util.h
9fc0f6
+++ b/src/shared/path-util.h
9fc0f6
@@ -43,8 +43,8 @@ char* path_startswith(const char *path, const char *prefix) _pure_;
9fc0f6
 bool path_equal(const char *a, const char *b) _pure_;
9fc0f6
 
9fc0f6
 char** path_strv_make_absolute_cwd(char **l);
9fc0f6
-char** path_strv_canonicalize(char **l);
9fc0f6
-char** path_strv_canonicalize_uniq(char **l);
9fc0f6
+char** path_strv_canonicalize_absolute(char **l, const char *prefix);
9fc0f6
+char** path_strv_canonicalize_absolute_uniq(char **l, const char *prefix);
9fc0f6
 
9fc0f6
 int path_is_mount_point(const char *path, bool allow_symlink);
9fc0f6
 int path_is_read_only_fs(const char *path);
9fc0f6
diff --git a/src/shared/util.c b/src/shared/util.c
9fc0f6
index fb1e6d1..a5163fb 100644
9fc0f6
--- a/src/shared/util.c
9fc0f6
+++ b/src/shared/util.c
9fc0f6
@@ -5681,7 +5681,7 @@ static int search_and_fopen_internal(const char *path, const char *mode, char **
9fc0f6
         assert(mode);
9fc0f6
         assert(_f);
9fc0f6
 
9fc0f6
-        if (!path_strv_canonicalize_uniq(search))
9fc0f6
+        if (!path_strv_canonicalize_absolute_uniq(search, NULL))
9fc0f6
                 return -ENOMEM;
9fc0f6
 
9fc0f6
         STRV_FOREACH(i, search) {
9fc0f6
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
9fc0f6
index 6f8b127..9092b08 100644
9fc0f6
--- a/src/udev/udev-rules.c
9fc0f6
+++ b/src/udev/udev-rules.c
9fc0f6
@@ -1630,7 +1630,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
9fc0f6
                 log_error("failed to build config directory array");
9fc0f6
                 return udev_rules_unref(rules);
9fc0f6
         }
9fc0f6
-        if (!path_strv_canonicalize(rules->dirs)) {
9fc0f6
+        if (!path_strv_canonicalize_absolute(rules->dirs, NULL)) {
9fc0f6
                 log_error("failed to canonicalize config directories\n");
9fc0f6
                 return udev_rules_unref(rules);
9fc0f6
         }