summaryrefslogtreecommitdiff
path: root/testing/kmod/0008-Use-realpath-to-canonicalize-provided-paths.patch
blob: 652d2e85468375982e7d49c06141e59beca5418c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
From 4148d50c980e20a71b5e284e93863b4f36f4fbe4 Mon Sep 17 00:00:00 2001
From: Dan McGee <dan@archlinux.org>
Date: Fri, 3 Feb 2012 15:01:34 -0600
Subject: [PATCH 8/8] Use realpath() to canonicalize provided paths

The existing function choked in several corner cases:
* '/tmp/../tmp' was seen as absolute, so not cleaned up.
* '/tmp/' and '/tmp' were not equal, causing depmod to act differently
  when called with the -b option for the two paths.

Don't reinvent the wheel; just use the standard library function.

Signed-off-by: Dan McGee <dan@archlinux.org>
---
 libkmod/libkmod-module.c |    6 +++---
 libkmod/libkmod-util.c   |   36 ------------------------------------
 libkmod/libkmod-util.h   |    2 --
 libkmod/libkmod.c        |   13 +++++++++----
 tools/kmod-depmod.c      |    8 ++++++--
 5 files changed, 18 insertions(+), 47 deletions(-)

diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index 48e4aa1..48e7286 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -326,10 +326,10 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
 	if (ctx == NULL || path == NULL || mod == NULL)
 		return -ENOENT;
 
-	abspath = path_make_absolute_cwd(path);
+	abspath = canonicalize_file_name(path);
 	if (abspath == NULL) {
-		DBG(ctx, "no absolute path for %s\n", path);
-		return -ENOMEM;
+		DBG(ctx, "no canonical filename returned for %s\n", path);
+		return -errno;
 	}
 
 	err = stat(abspath, &st);
diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c
index 6a9f697..02b7f63 100644
--- a/libkmod/libkmod-util.c
+++ b/libkmod/libkmod-util.c
@@ -301,42 +301,6 @@ char *strchr_replace(char *s, int c, char r)
 	return s;
 }
 
-bool path_is_absolute(const char *p)
-{
-	assert(p != NULL);
-
-	return p[0] == '/';
-}
-
-char *path_make_absolute_cwd(const char *p)
-{
-	char *cwd, *r;
-	size_t plen;
-	size_t cwdlen;
-
-	if (path_is_absolute(p))
-		return strdup(p);
-
-	cwd = get_current_dir_name();
-	if (cwd == NULL)
-		return NULL;
-
-	plen = strlen(p);
-	cwdlen = strlen(cwd);
-
-	/* cwd + '/' + p + '\0' */
-	r = realloc(cwd, cwdlen + 1 + plen + 1);
-	if (r == NULL) {
-		free(cwd);
-		return NULL;
-	}
-
-	r[cwdlen] = '/';
-	memcpy(&r[cwdlen + 1], p, plen + 1);
-
-	return r;
-}
-
 #define USEC_PER_SEC  1000000ULL
 #define NSEC_PER_USEC 1000ULL
 unsigned long long ts_usec(const struct timespec *ts)
diff --git a/libkmod/libkmod-util.h b/libkmod/libkmod-util.h
index c9a1a21..63c348f 100644
--- a/libkmod/libkmod-util.h
+++ b/libkmod/libkmod-util.h
@@ -19,8 +19,6 @@ ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((no
 int read_str_long(int fd, long *value, int base) __must_check __attribute__((nonnull(2)));
 int read_str_ulong(int fd, unsigned long *value, int base) __must_check __attribute__((nonnull(2)));
 char *strchr_replace(char *s, int c, char r);
-bool path_is_absolute(const char *p) __must_check __attribute__((nonnull(1)));
-char *path_make_absolute_cwd(const char *p) __must_check __attribute__((nonnull(1)));
 int alias_normalize(const char *alias, char buf[PATH_MAX], size_t *len) __must_check __attribute__((nonnull(1,2)));
 char *modname_normalize(const char *modname, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(1, 2)));
 char *path_to_modname(const char *path, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(2)));
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 800a178..c578afb 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -160,13 +160,18 @@ static int log_priority(const char *priority)
 
 static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules";
 
-static char *get_kernel_release(const char *dirname)
+static char *get_kernel_release(struct kmod_ctx *ctx, const char *dirname)
 {
 	struct utsname u;
 	char *p;
 
-	if (dirname != NULL)
-		return path_make_absolute_cwd(dirname);
+	if (dirname != NULL) {
+		p = canonicalize_file_name(dirname);
+		if (p)
+			return p;
+		INFO(ctx, "could not canonicalize directory %s: %m\n", dirname);
+		return strdup(dirname);
+	}
 
 	if (uname(&u) < 0)
 		return NULL;
@@ -215,7 +220,7 @@ KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
 	ctx->log_data = stderr;
 	ctx->log_priority = LOG_ERR;
 
-	ctx->dirname = get_kernel_release(dirname);
+	ctx->dirname = get_kernel_release(ctx, dirname);
 
 	/* environment overwrites config */
 	env = getenv("KMOD_LOG");
diff --git a/tools/kmod-depmod.c b/tools/kmod-depmod.c
index 0cf28f6..72180ef 100644
--- a/tools/kmod-depmod.c
+++ b/tools/kmod-depmod.c
@@ -1160,7 +1160,7 @@ static int depmod_module_add(struct depmod *depmod, struct kmod_module *kmod)
 		}
 	}
 
-	DBG("add %p kmod=%p, path=%s\n", mod, kmod, mod->path);
+	DBG("add %p kmod=%p, path=%s, relpath=%s\n", mod, kmod, mod->path, mod->relpath);
 
 	return 0;
 }
@@ -2547,7 +2547,11 @@ static int do_depmod(int argc, char *argv[])
 			maybe_all = 1;
 			break;
 		case 'b':
-			root = path_make_absolute_cwd(optarg);
+			root = canonicalize_file_name(optarg);
+			if (!root) {
+				CRIT("could not resolve path %s\n", optarg);
+				goto cmdline_failed;
+			}
 			break;
 		case 'C': {
 			size_t bytes = sizeof(char *) * (n_config_paths + 2);
-- 
1.7.9