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