blob: b2035696a1ec036b5ce027e51a125f35501abcf8 [file] [log] [blame]
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08001/*
2 * Copyright (C) 2017 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_config.h"
30
31#include "linker_globals.h"
32#include "linker_debug.h"
33#include "linker_utils.h"
34
35#include <android-base/file.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070036#include <android-base/scopeguard.h>
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080037#include <android-base/strings.h>
38
Christopher Ferris7a3681e2017-04-24 17:48:32 -070039#include <async_safe/log.h>
40
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080041#include <stdlib.h>
42
43#include <string>
44#include <unordered_map>
45
Sundong Ahn8fc50322017-10-10 14:12:40 +090046#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
47#include <sys/_system_properties.h>
48
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080049class ConfigParser {
50 public:
51 enum {
Jiyong Park8b029512017-11-29 18:30:53 +090052 kPropertyAssign,
53 kPropertyAppend,
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080054 kSection,
55 kEndOfFile,
56 kError,
57 };
58
59 explicit ConfigParser(std::string&& content)
60 : content_(content), p_(0), lineno_(0), was_end_of_file_(false) {}
61
62 /*
63 * Possible return values
Jiyong Park8b029512017-11-29 18:30:53 +090064 * kPropertyAssign: name is set to property name and value is set to property value
65 * kPropertyAppend: same as kPropertyAssign, but the value should be appended
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080066 * kSection: name is set to section name.
67 * kEndOfFile: reached end of file.
68 * kError: error_msg is set.
69 */
70 int next_token(std::string* name, std::string* value, std::string* error_msg) {
71 std::string line;
72 while(NextLine(&line)) {
73 size_t found = line.find('#');
74 line = android::base::Trim(line.substr(0, found));
75
76 if (line.empty()) {
77 continue;
78 }
79
80 if (line[0] == '[' && line[line.size() - 1] == ']') {
81 *name = line.substr(1, line.size() - 2);
82 return kSection;
83 }
84
Jiyong Park8b029512017-11-29 18:30:53 +090085 size_t found_assign = line.find('=');
86 size_t found_append = line.find("+=");
87 if (found_assign != std::string::npos && found_append == std::string::npos) {
88 *name = android::base::Trim(line.substr(0, found_assign));
89 *value = android::base::Trim(line.substr(found_assign + 1));
90 return kPropertyAssign;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080091 }
92
Jiyong Park8b029512017-11-29 18:30:53 +090093 if (found_append != std::string::npos) {
94 *name = android::base::Trim(line.substr(0, found_append));
95 *value = android::base::Trim(line.substr(found_append + 2));
96 return kPropertyAppend;
97 }
98
99 *error_msg = std::string("invalid format: ") +
100 line +
101 ", expected \"name = property\", \"name += property\", or \"[section]\"";
102 return kError;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800103 }
104
105 // to avoid infinite cycles when programmer makes a mistake
106 CHECK(!was_end_of_file_);
107 was_end_of_file_ = true;
108 return kEndOfFile;
109 }
110
111 size_t lineno() const {
112 return lineno_;
113 }
114
115 private:
116 bool NextLine(std::string* line) {
117 if (p_ == std::string::npos) {
118 return false;
119 }
120
121 size_t found = content_.find('\n', p_);
122 if (found != std::string::npos) {
123 *line = content_.substr(p_, found - p_);
124 p_ = found + 1;
125 } else {
126 *line = content_.substr(p_);
127 p_ = std::string::npos;
128 }
129
130 lineno_++;
131 return true;
132 }
133
134 std::string content_;
135 size_t p_;
136 size_t lineno_;
137 bool was_end_of_file_;
138
139 DISALLOW_IMPLICIT_CONSTRUCTORS(ConfigParser);
140};
141
142class PropertyValue {
143 public:
144 PropertyValue() = default;
145
146 PropertyValue(std::string&& value, size_t lineno)
147 : value_(value), lineno_(lineno) {}
148
149 const std::string& value() const {
150 return value_;
151 }
152
Jiyong Park8b029512017-11-29 18:30:53 +0900153 void append_value(std::string&& value) {
154 value_ = value_ + value;
155 // lineno isn't updated as we might have cases like this:
156 // property.x = blah
157 // property.y = blah
158 // property.x += blah
159 }
160
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800161 size_t lineno() const {
162 return lineno_;
163 }
164
165 private:
166 std::string value_;
167 size_t lineno_;
168};
169
170static std::string create_error_msg(const char* file,
171 size_t lineno,
172 const std::string& msg) {
173 char buf[1024];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700174 async_safe_format_buffer(buf, sizeof(buf), "%s:%zu: error: %s", file, lineno, msg.c_str());
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800175
176 return std::string(buf);
177}
178
179static bool parse_config_file(const char* ld_config_file_path,
180 const char* binary_realpath,
181 std::unordered_map<std::string, PropertyValue>* properties,
182 std::string* error_msg) {
183 std::string content;
184 if (!android::base::ReadFileToString(ld_config_file_path, &content)) {
185 if (errno != ENOENT) {
186 *error_msg = std::string("error reading file \"") +
187 ld_config_file_path + "\": " + strerror(errno);
188 }
189 return false;
190 }
191
192 ConfigParser cp(std::move(content));
193
194 std::string section_name;
195
196 while(true) {
197 std::string name;
198 std::string value;
199 std::string error;
200
201 int result = cp.next_token(&name, &value, &error);
202 if (result == ConfigParser::kError) {
203 DL_WARN("error parsing %s:%zd: %s (ignoring this line)",
204 ld_config_file_path,
205 cp.lineno(),
206 error.c_str());
207 continue;
208 }
209
210 if (result == ConfigParser::kSection || result == ConfigParser::kEndOfFile) {
211 return false;
212 }
213
Jiyong Park8b029512017-11-29 18:30:53 +0900214 if (result == ConfigParser::kPropertyAssign) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800215 if (!android::base::StartsWith(name, "dir.")) {
216 DL_WARN("error parsing %s:%zd: unexpected property name \"%s\", "
217 "expected format dir.<section_name> (ignoring this line)",
218 ld_config_file_path,
219 cp.lineno(),
220 name.c_str());
221 continue;
222 }
223
224 // remove trailing '/'
225 while (value[value.size() - 1] == '/') {
226 value = value.substr(0, value.size() - 1);
227 }
228
229 if (value.empty()) {
230 DL_WARN("error parsing %s:%zd: property value is empty (ignoring this line)",
231 ld_config_file_path,
232 cp.lineno());
233 continue;
234 }
235
236 if (file_is_under_dir(binary_realpath, value)) {
237 section_name = name.substr(4);
238 break;
239 }
240 }
241 }
242
243 // skip everything until we meet a correct section
244 while (true) {
245 std::string name;
246 std::string value;
247 std::string error;
248
249 int result = cp.next_token(&name, &value, &error);
250
251 if (result == ConfigParser::kSection && name == section_name) {
252 break;
253 }
254
255 if (result == ConfigParser::kEndOfFile) {
256 *error_msg = create_error_msg(ld_config_file_path,
257 cp.lineno(),
258 std::string("section \"") + section_name + "\" not found");
259 return false;
260 }
261 }
262
263 // found the section - parse it
264 while (true) {
265 std::string name;
266 std::string value;
267 std::string error;
268
269 int result = cp.next_token(&name, &value, &error);
270
271 if (result == ConfigParser::kEndOfFile || result == ConfigParser::kSection) {
272 break;
273 }
274
Jiyong Park8b029512017-11-29 18:30:53 +0900275 if (result == ConfigParser::kPropertyAssign) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800276 if (properties->find(name) != properties->end()) {
277 DL_WARN("%s:%zd: warning: property \"%s\" redefinition",
278 ld_config_file_path,
279 cp.lineno(),
280 name.c_str());
281 }
282
283 (*properties)[name] = PropertyValue(std::move(value), cp.lineno());
Jiyong Park8b029512017-11-29 18:30:53 +0900284 } else if (result == ConfigParser::kPropertyAppend) {
285 if (properties->find(name) == properties->end()) {
286 DL_WARN("%s:%zd: warning: appending to property \"%s\" which isn't defined",
287 ld_config_file_path,
288 cp.lineno(),
289 name.c_str());
290 (*properties)[name] = PropertyValue(std::move(value), cp.lineno());
291 } else {
292 if (android::base::EndsWith(name, ".links") ||
293 android::base::EndsWith(name, ".namespaces")) {
294 value = "," + value;
295 (*properties)[name].append_value(std::move(value));
296 } else if (android::base::EndsWith(name, ".paths") ||
297 android::base::EndsWith(name, ".shared_libs")) {
298 value = ":" + value;
299 (*properties)[name].append_value(std::move(value));
300 } else {
301 DL_WARN("%s:%zd: warning: += isn't allowed to property \"%s\". Ignoring.",
302 ld_config_file_path,
303 cp.lineno(),
304 name.c_str());
305 }
306 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800307 }
308
309 if (result == ConfigParser::kError) {
310 DL_WARN("error parsing %s:%zd: %s (ignoring this line)",
311 ld_config_file_path,
312 cp.lineno(),
313 error.c_str());
314 continue;
315 }
316 }
317
318 return true;
319}
320
Sundong Ahn8fc50322017-10-10 14:12:40 +0900321static std::string getVndkVersionString() {
322 char vndk_version_str[1 + PROP_VALUE_MAX] = {};
323 __system_property_get("ro.vndk.version", vndk_version_str + 1);
324 if (strlen(vndk_version_str + 1) != 0 && strcmp(vndk_version_str + 1, "current") != 0) {
325 vndk_version_str[0] = '-';
326 }
327 return vndk_version_str;
328}
329
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800330static Config g_config;
331
332static constexpr const char* kDefaultConfigName = "default";
333static constexpr const char* kPropertyAdditionalNamespaces = "additional.namespaces";
334#if defined(__LP64__)
335static constexpr const char* kLibParamValue = "lib64";
336#else
337static constexpr const char* kLibParamValue = "lib";
338#endif
339
340class Properties {
341 public:
342 explicit Properties(std::unordered_map<std::string, PropertyValue>&& properties)
343 : properties_(properties), target_sdk_version_(__ANDROID_API__) {}
344
345 std::vector<std::string> get_strings(const std::string& name, size_t* lineno = nullptr) const {
346 auto it = find_property(name, lineno);
347 if (it == properties_.end()) {
348 // return empty vector
349 return std::vector<std::string>();
350 }
351
352 std::vector<std::string> strings = android::base::Split(it->second.value(), ",");
353
354 for (size_t i = 0; i < strings.size(); ++i) {
355 strings[i] = android::base::Trim(strings[i]);
356 }
357
358 return strings;
359 }
360
361 bool get_bool(const std::string& name, size_t* lineno = nullptr) const {
362 auto it = find_property(name, lineno);
363 if (it == properties_.end()) {
364 return false;
365 }
366
367 return it->second.value() == "true";
368 }
369
370 std::string get_string(const std::string& name, size_t* lineno = nullptr) const {
371 auto it = find_property(name, lineno);
372 return (it == properties_.end()) ? "" : it->second.value();
373 }
374
Jiyong Park0f33f232017-09-21 10:27:36 +0900375 std::vector<std::string> get_paths(const std::string& name, bool resolve, size_t* lineno = nullptr) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800376 std::string paths_str = get_string(name, lineno);
377
378 std::vector<std::string> paths;
379 split_path(paths_str.c_str(), ":", &paths);
380
381 std::vector<std::pair<std::string, std::string>> params;
382 params.push_back({ "LIB", kLibParamValue });
383 if (target_sdk_version_ != 0) {
384 char buf[16];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700385 async_safe_format_buffer(buf, sizeof(buf), "%d", target_sdk_version_);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800386 params.push_back({ "SDK_VER", buf });
387 }
388
Sundong Ahn8fc50322017-10-10 14:12:40 +0900389 static std::string vndk = getVndkVersionString();
390 params.push_back({ "VNDK_VER", vndk });
391
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800392 for (auto&& path : paths) {
393 format_string(&path, params);
394 }
395
Jiyong Park0f33f232017-09-21 10:27:36 +0900396 if (resolve) {
397 std::vector<std::string> resolved_paths;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800398
Jiyong Park0f33f232017-09-21 10:27:36 +0900399 // do not remove paths that do not exist
400 resolve_paths(paths, &resolved_paths);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800401
Jiyong Park0f33f232017-09-21 10:27:36 +0900402 return resolved_paths;
403 } else {
404 return paths;
405 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800406 }
407
408 void set_target_sdk_version(int target_sdk_version) {
409 target_sdk_version_ = target_sdk_version;
410 }
411
412 private:
413 std::unordered_map<std::string, PropertyValue>::const_iterator
414 find_property(const std::string& name, size_t* lineno) const {
415 auto it = properties_.find(name);
416 if (it != properties_.end() && lineno != nullptr) {
417 *lineno = it->second.lineno();
418 }
419
420 return it;
421 }
422 std::unordered_map<std::string, PropertyValue> properties_;
423 int target_sdk_version_;
424
425 DISALLOW_IMPLICIT_CONSTRUCTORS(Properties);
426};
427
428bool Config::read_binary_config(const char* ld_config_file_path,
429 const char* binary_realpath,
430 bool is_asan,
431 const Config** config,
432 std::string* error_msg) {
433 g_config.clear();
434
435 std::unordered_map<std::string, PropertyValue> property_map;
436 if (!parse_config_file(ld_config_file_path, binary_realpath, &property_map, error_msg)) {
437 return false;
438 }
439
440 Properties properties(std::move(property_map));
441
Tom Cherryb8ab6182017-04-05 16:20:29 -0700442 auto failure_guard = android::base::make_scope_guard([] { g_config.clear(); });
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800443
444 std::unordered_map<std::string, NamespaceConfig*> namespace_configs;
445
446 namespace_configs[kDefaultConfigName] = g_config.create_namespace_config(kDefaultConfigName);
447
448 std::vector<std::string> additional_namespaces = properties.get_strings(kPropertyAdditionalNamespaces);
449 for (const auto& name : additional_namespaces) {
450 namespace_configs[name] = g_config.create_namespace_config(name);
451 }
452
453 bool versioning_enabled = properties.get_bool("enable.target.sdk.version");
454 int target_sdk_version = __ANDROID_API__;
455 if (versioning_enabled) {
456 std::string version_file = dirname(binary_realpath) + "/.version";
457 std::string content;
458 if (!android::base::ReadFileToString(version_file, &content)) {
459 if (errno != ENOENT) {
460 *error_msg = std::string("error reading version file \"") +
461 version_file + "\": " + strerror(errno);
462 return false;
463 }
464 } else {
465 content = android::base::Trim(content);
466 errno = 0;
467 char* end = nullptr;
468 const char* content_str = content.c_str();
469 int result = strtol(content_str, &end, 10);
470 if (errno == 0 && *end == '\0' && result > 0) {
471 target_sdk_version = result;
472 properties.set_target_sdk_version(target_sdk_version);
473 } else {
474 *error_msg = std::string("invalid version \"") + version_file + "\": \"" + content +"\"";
475 return false;
476 }
477 }
478 }
479
480 g_config.set_target_sdk_version(target_sdk_version);
481
482 for (auto ns_config_it : namespace_configs) {
483 auto& name = ns_config_it.first;
484 NamespaceConfig* ns_config = ns_config_it.second;
485
486 std::string property_name_prefix = std::string("namespace.") + name;
487
488 size_t lineno = 0;
489 std::vector<std::string> linked_namespaces =
490 properties.get_strings(property_name_prefix + ".links", &lineno);
491
492 for (const auto& linked_ns_name : linked_namespaces) {
493 if (namespace_configs.find(linked_ns_name) == namespace_configs.end()) {
494 *error_msg = create_error_msg(ld_config_file_path,
495 lineno,
496 std::string("undefined namespace: ") + linked_ns_name);
497 return false;
498 }
499
500 std::string shared_libs = properties.get_string(property_name_prefix +
501 ".link." +
502 linked_ns_name +
503 ".shared_libs", &lineno);
504
505 if (shared_libs.empty()) {
506 *error_msg = create_error_msg(ld_config_file_path,
507 lineno,
508 std::string("list of shared_libs for ") +
509 name +
510 "->" +
511 linked_ns_name +
512 " link is not specified or is empty.");
513 return false;
514 }
515
516 ns_config->add_namespace_link(linked_ns_name, shared_libs);
517 }
518
519 ns_config->set_isolated(properties.get_bool(property_name_prefix + ".isolated"));
Jiyong Park01de74e2017-04-03 23:10:37 +0900520 ns_config->set_visible(properties.get_bool(property_name_prefix + ".visible"));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800521
522 // these are affected by is_asan flag
523 if (is_asan) {
524 property_name_prefix += ".asan";
525 }
526
Jiyong Park0f33f232017-09-21 10:27:36 +0900527 // search paths are resolved (canonicalized). This is required mainly for
528 // the case when /vendor is a symlink to /system/vendor, which is true for
529 // non Treble-ized legacy devices.
530 ns_config->set_search_paths(properties.get_paths(property_name_prefix + ".search.paths", true));
531
532 // However, for permitted paths, we are not required to resolve the paths
533 // since they are only set for isolated namespaces, which implies the device
534 // is Treble-ized (= /vendor is not a symlink to /system/vendor).
535 // In fact, the resolving is causing an unexpected side effect of selinux
536 // denials on some executables which are not allowed to access some of the
537 // permitted paths.
538 ns_config->set_permitted_paths(properties.get_paths(property_name_prefix + ".permitted.paths", false));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800539 }
540
Tom Cherryb8ab6182017-04-05 16:20:29 -0700541 failure_guard.Disable();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800542 *config = &g_config;
543 return true;
544}
545
546NamespaceConfig* Config::create_namespace_config(const std::string& name) {
547 namespace_configs_.push_back(std::unique_ptr<NamespaceConfig>(new NamespaceConfig(name)));
548 NamespaceConfig* ns_config_ptr = namespace_configs_.back().get();
549 namespace_configs_map_[name] = ns_config_ptr;
550 return ns_config_ptr;
551}
552
553void Config::clear() {
554 namespace_configs_.clear();
555 namespace_configs_map_.clear();
556}