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