1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "linker_utils.h"
30
31 #include "linker_debug.h"
32 #include "linker_globals.h"
33
34 #include "android-base/strings.h"
35
36 #include <sys/stat.h>
37 #include <unistd.h>
38
format_string(std::string * str,const std::vector<std::pair<std::string,std::string>> & params)39 void format_string(std::string* str, const std::vector<std::pair<std::string, std::string>>& params) {
40 size_t pos = 0;
41 while (pos < str->size()) {
42 pos = str->find("$", pos);
43 if (pos == std::string::npos) break;
44 for (const auto& param : params) {
45 const std::string& token = param.first;
46 const std::string& replacement = param.second;
47 if (str->substr(pos + 1, token.size()) == token) {
48 str->replace(pos, token.size() + 1, replacement);
49 // -1 to compensate for the ++pos below.
50 pos += replacement.size() - 1;
51 break;
52 } else if (str->substr(pos + 1, token.size() + 2) == "{" + token + "}") {
53 str->replace(pos, token.size() + 3, replacement);
54 pos += replacement.size() - 1;
55 break;
56 }
57 }
58 // Skip $ in case it did not match any of the known substitutions.
59 ++pos;
60 }
61 }
62
dirname(const char * path)63 std::string dirname(const char* path) {
64 const char* last_slash = strrchr(path, '/');
65
66 if (last_slash == path) {
67 return "/";
68 } else if (last_slash == nullptr) {
69 return ".";
70 } else {
71 return std::string(path, last_slash - path);
72 }
73 }
74
normalize_path(const char * path,std::string * normalized_path)75 bool normalize_path(const char* path, std::string* normalized_path) {
76 // Input should be an absolute path
77 if (path[0] != '/') {
78 PRINT("normalize_path - invalid input: \"%s\", the input path should be absolute", path);
79 return false;
80 }
81
82 const size_t len = strlen(path) + 1;
83 char buf[len];
84
85 const char* in_ptr = path;
86 char* out_ptr = buf;
87
88 while (*in_ptr != 0) {
89 if (*in_ptr == '/') {
90 char c1 = in_ptr[1];
91 if (c1 == '.') {
92 char c2 = in_ptr[2];
93 if (c2 == '/') {
94 in_ptr += 2;
95 continue;
96 } else if (c2 == '.' && (in_ptr[3] == '/' || in_ptr[3] == 0)) {
97 in_ptr += 3;
98 while (out_ptr > buf && *--out_ptr != '/') {
99 }
100 if (in_ptr[0] == 0) {
101 // retain '/' (or write the initial '/' for "/..")
102 *out_ptr++ = '/';
103 }
104 continue;
105 }
106 } else if (c1 == '/') {
107 ++in_ptr;
108 continue;
109 }
110 }
111 *out_ptr++ = *in_ptr++;
112 }
113
114 *out_ptr = 0;
115 *normalized_path = buf;
116 return true;
117 }
118
file_is_in_dir(const std::string & file,const std::string & dir)119 bool file_is_in_dir(const std::string& file, const std::string& dir) {
120 const char* needle = dir.c_str();
121 const char* haystack = file.c_str();
122 size_t needle_len = strlen(needle);
123
124 return strncmp(haystack, needle, needle_len) == 0 &&
125 haystack[needle_len] == '/' &&
126 strchr(haystack + needle_len + 1, '/') == nullptr;
127 }
128
file_is_under_dir(const std::string & file,const std::string & dir)129 bool file_is_under_dir(const std::string& file, const std::string& dir) {
130 const char* needle = dir.c_str();
131 const char* haystack = file.c_str();
132 size_t needle_len = strlen(needle);
133
134 return strncmp(haystack, needle, needle_len) == 0 &&
135 haystack[needle_len] == '/';
136 }
137
138 const char* const kZipFileSeparator = "!/";
139
parse_zip_path(const char * input_path,std::string * zip_path,std::string * entry_path)140 bool parse_zip_path(const char* input_path, std::string* zip_path, std::string* entry_path) {
141 std::string normalized_path;
142 if (!normalize_path(input_path, &normalized_path)) {
143 return false;
144 }
145
146 const char* const path = normalized_path.c_str();
147 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
148
149 // Treat an '!/' separator inside a path as the separator between the name
150 // of the zip file on disk and the subdirectory to search within it.
151 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
152 // "bar/bas/x.so" within "foo.zip".
153 const char* const separator = strstr(path, kZipFileSeparator);
154 if (separator == nullptr) {
155 return false;
156 }
157
158 char buf[512];
159 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
160 PRINT("Warning: ignoring very long library path: %s", path);
161 return false;
162 }
163
164 buf[separator - path] = '\0';
165
166 *zip_path = buf;
167 *entry_path = &buf[separator - path + 2];
168
169 return true;
170 }
171
safe_add(off64_t * out,off64_t a,size_t b)172 bool safe_add(off64_t* out, off64_t a, size_t b) {
173 CHECK(a >= 0);
174 if (static_cast<uint64_t>(INT64_MAX - a) < b) {
175 return false;
176 }
177
178 *out = a + b;
179 return true;
180 }
181
split_path(const char * path,const char * delimiters,std::vector<std::string> * paths)182 void split_path(const char* path, const char* delimiters,
183 std::vector<std::string>* paths) {
184 if (path != nullptr && path[0] != 0) {
185 *paths = android::base::Split(path, delimiters);
186 }
187 }
188
resolve_paths(std::vector<std::string> & paths,std::vector<std::string> * resolved_paths)189 void resolve_paths(std::vector<std::string>& paths,
190 std::vector<std::string>* resolved_paths) {
191 resolved_paths->clear();
192 for (const auto& path : paths) {
193 // skip empty paths
194 if (path.empty()) {
195 continue;
196 }
197 std::string resolved = resolve_path(path);
198 if (!resolved.empty()) {
199 resolved_paths->push_back(std::move(resolved));
200 }
201 }
202 }
203
resolve_path(const std::string & path)204 std::string resolve_path(const std::string& path) {
205 char resolved_path[PATH_MAX];
206 const char* original_path = path.c_str();
207 if (realpath(original_path, resolved_path) != nullptr) {
208 struct stat s;
209 if (stat(resolved_path, &s) == -1) {
210 DL_WARN("Warning: cannot stat file \"%s\": %s (ignoring)", resolved_path, strerror(errno));
211 return "";
212 }
213 if (!S_ISDIR(s.st_mode)) {
214 DL_WARN("Warning: \"%s\" is not a directory (ignoring)", resolved_path);
215 return "";
216 }
217 return resolved_path;
218 } else {
219 std::string normalized_path;
220 if (!normalize_path(original_path, &normalized_path)) {
221 DL_WARN("Warning: unable to normalize \"%s\" (ignoring)", original_path);
222 return "";
223 }
224
225 std::string zip_path;
226 std::string entry_path;
227 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
228 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
229 DL_WARN("Warning: unable to resolve \"%s\": %s (ignoring)",
230 zip_path.c_str(), strerror(errno));
231 return "";
232 }
233
234 return std::string(resolved_path) + kZipFileSeparator + entry_path;
235 } else {
236 struct stat s;
237 if (stat(normalized_path.c_str(), &s) == 0 && S_ISDIR(s.st_mode)) {
238 // Path is not a zip path, but an existing directory. Then add it
239 // although we failed to resolve it. b/119656753
240 return normalized_path;
241 }
242 }
243 }
244 return "";
245 }
246
is_first_stage_init()247 bool is_first_stage_init() {
248 static bool ret = (getpid() == 1 && access("/proc/self/exe", F_OK) == -1);
249 return ret;
250 }
251