Blob Blame History Raw
From b7f7813318b370d9ecf3833f92c5258d362d9441 Mon Sep 17 00:00:00 2001
From: Michael Marineau <michael.marineau@coreos.com>
Date: Fri, 31 Jan 2014 15:35:04 -0800
Subject: [PATCH] shared: include root when canonicalizing conf paths

The conf_files_list family accepts an alternate root path to prefix all
directories in the list but path_strv_canonicalize_uniq doesn't use it.
This results in the suspicious behavior of resolving directory symlinks
based on the contents of / instead of the alternate root.

This adds a prefix argument to path_strv_canonicalize which will now
prepend the prefix, if given, to every path in the list. To avoid
answering what a relative path means when called with a root prefix
path_strv_canonicalize is now path_strv_canonicalize_absolute and only
considers absolute paths. Fortunately all users of already call
path_strv_canonicalize with a list of absolute paths.

(cherry picked from commit 112cfb181453e38d3ef4a74fba23abbb53392002)

Related: #1111199
---
 src/shared/conf-files.c  | 10 +++-------
 src/shared/path-lookup.c |  6 +++---
 src/shared/path-util.c   | 29 +++++++++++++++++++----------
 src/shared/path-util.h   |  4 ++--
 src/shared/util.c        |  2 +-
 src/udev/udev-rules.c    |  2 +-
 6 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c
index ed4070c..3d5b1df 100644
--- a/src/shared/conf-files.c
+++ b/src/shared/conf-files.c
@@ -37,12 +37,8 @@
 #include "hashmap.h"
 #include "conf-files.h"
 
-static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
+static int files_add(Hashmap *h, const char *dirpath, const char *suffix) {
         _cleanup_closedir_ DIR *dir = NULL;
-        _cleanup_free_ char *dirpath = NULL;
-
-        if (asprintf(&dirpath, "%s%s", root ? root : "", path) < 0)
-                return -ENOMEM;
 
         dir = opendir(dirpath);
         if (!dir) {
@@ -104,7 +100,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
         assert(suffix);
 
         /* This alters the dirs string array */
-        if (!path_strv_canonicalize_uniq(dirs))
+        if (!path_strv_canonicalize_absolute_uniq(dirs, root))
                 return -ENOMEM;
 
         fh = hashmap_new(string_hash_func, string_compare_func);
@@ -112,7 +108,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
                 return -ENOMEM;
 
         STRV_FOREACH(p, dirs) {
-                r = files_add(fh, root, *p, suffix);
+                r = files_add(fh, *p, suffix);
                 if (r == -ENOMEM) {
                         hashmap_free_free(fh);
                         return r;
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
index 1a47ea9..03c1380 100644
--- a/src/shared/path-lookup.c
+++ b/src/shared/path-lookup.c
@@ -316,7 +316,7 @@ int lookup_paths_init(
                 }
         }
 
-        if (!path_strv_canonicalize(p->unit_path))
+        if (!path_strv_canonicalize_absolute(p->unit_path, NULL))
                 return -ENOMEM;
 
         strv_uniq(p->unit_path);
@@ -372,10 +372,10 @@ int lookup_paths_init(
                                 return -ENOMEM;
                 }
 
-                if (!path_strv_canonicalize(p->sysvinit_path))
+                if (!path_strv_canonicalize_absolute(p->sysvinit_path, NULL))
                         return -ENOMEM;
 
-                if (!path_strv_canonicalize(p->sysvrcnd_path))
+                if (!path_strv_canonicalize_absolute(p->sysvrcnd_path, NULL))
                         return -ENOMEM;
 
                 strv_uniq(p->sysvinit_path);
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 45099ee..de291a5 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -165,7 +165,7 @@ char **path_strv_make_absolute_cwd(char **l) {
         return l;
 }
 
-char **path_strv_canonicalize(char **l) {
+char **path_strv_canonicalize_absolute(char **l, const char *prefix) {
         char **s;
         unsigned k = 0;
         bool enomem = false;
@@ -180,13 +180,21 @@ char **path_strv_canonicalize(char **l) {
         STRV_FOREACH(s, l) {
                 char *t, *u;
 
-                t = path_make_absolute_cwd(*s);
-                free(*s);
-                *s = NULL;
-
-                if (!t) {
-                        enomem = true;
+                if (!path_is_absolute(*s))
                         continue;
+
+                if (prefix) {
+                        t = strappend(prefix, *s);
+                        free(*s);
+                        *s = NULL;
+
+                        if (!t) {
+                                enomem = true;
+                                continue;
+                        }
+                } else {
+                        t = *s;
+                        *s = NULL;
                 }
 
                 errno = 0;
@@ -196,7 +204,7 @@ char **path_strv_canonicalize(char **l) {
                                 u = t;
                         else {
                                 free(t);
-                                if (errno == ENOMEM || !errno)
+                                if (errno == ENOMEM || errno == 0)
                                         enomem = true;
 
                                 continue;
@@ -215,11 +223,12 @@ char **path_strv_canonicalize(char **l) {
         return l;
 }
 
-char **path_strv_canonicalize_uniq(char **l) {
+char **path_strv_canonicalize_absolute_uniq(char **l, const char *prefix) {
+
         if (strv_isempty(l))
                 return l;
 
-        if (!path_strv_canonicalize(l))
+        if (!path_strv_canonicalize_absolute(l, prefix))
                 return NULL;
 
         return strv_uniq(l);
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
index 0a42de7..c69cd1f 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -43,8 +43,8 @@ char* path_startswith(const char *path, const char *prefix) _pure_;
 bool path_equal(const char *a, const char *b) _pure_;
 
 char** path_strv_make_absolute_cwd(char **l);
-char** path_strv_canonicalize(char **l);
-char** path_strv_canonicalize_uniq(char **l);
+char** path_strv_canonicalize_absolute(char **l, const char *prefix);
+char** path_strv_canonicalize_absolute_uniq(char **l, const char *prefix);
 
 int path_is_mount_point(const char *path, bool allow_symlink);
 int path_is_read_only_fs(const char *path);
diff --git a/src/shared/util.c b/src/shared/util.c
index fb1e6d1..a5163fb 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -5681,7 +5681,7 @@ static int search_and_fopen_internal(const char *path, const char *mode, char **
         assert(mode);
         assert(_f);
 
-        if (!path_strv_canonicalize_uniq(search))
+        if (!path_strv_canonicalize_absolute_uniq(search, NULL))
                 return -ENOMEM;
 
         STRV_FOREACH(i, search) {
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
index 6f8b127..9092b08 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -1630,7 +1630,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
                 log_error("failed to build config directory array");
                 return udev_rules_unref(rules);
         }
-        if (!path_strv_canonicalize(rules->dirs)) {
+        if (!path_strv_canonicalize_absolute(rules->dirs, NULL)) {
                 log_error("failed to canonicalize config directories\n");
                 return udev_rules_unref(rules);
         }