Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "public_libraries.h" |
| 18 | #define LOG_TAG "nativeloader" |
| 19 | |
| 20 | #include <dirent.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <memory> |
| 24 | |
| 25 | #include "android-base/file.h" |
| 26 | #include "android-base/logging.h" |
| 27 | #include "android-base/properties.h" |
| 28 | #include "android-base/strings.h" |
| 29 | #include "log/log.h" |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 30 | #include "utils.h" |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 31 | |
| 32 | namespace android::nativeloader { |
| 33 | |
| 34 | using namespace std::string_literals; |
| 35 | |
| 36 | namespace { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 37 | |
| 38 | constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt"; |
| 39 | constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-"; |
| 40 | constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt"; |
| 41 | constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt"; |
| 42 | constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt"; |
| 43 | constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt"; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 44 | |
| 45 | const std::vector<const std::string> kRuntimePublicLibraries = { |
| 46 | "libicuuc.so", |
| 47 | "libicui18n.so", |
| 48 | }; |
| 49 | |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 50 | constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 51 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 52 | // TODO(b/130388701): do we need this? |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 53 | std::string root_dir() { |
| 54 | static const char* android_root_env = getenv("ANDROID_ROOT"); |
| 55 | return android_root_env != nullptr ? android_root_env : "/system"; |
| 56 | } |
| 57 | |
| 58 | bool debuggable() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 59 | static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 60 | return debuggable; |
| 61 | } |
| 62 | |
| 63 | std::string vndk_version_str() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 64 | static std::string version = android::base::GetProperty("ro.vndk.version", ""); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 65 | if (version != "" && version != "current") { |
| 66 | return "." + version; |
| 67 | } |
| 68 | return ""; |
| 69 | } |
| 70 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 71 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 72 | // variable to add libraries to the list. This is intended for platform tests only. |
| 73 | std::string additional_public_libraries() { |
| 74 | if (debuggable()) { |
| 75 | const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 76 | return val ? val : ""; |
| 77 | } |
| 78 | return ""; |
| 79 | } |
| 80 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 81 | void InsertVndkVersionStr(std::string* file_name) { |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 82 | CHECK(file_name != nullptr); |
| 83 | size_t insert_pos = file_name->find_last_of("."); |
| 84 | if (insert_pos == std::string::npos) { |
| 85 | insert_pos = file_name->length(); |
| 86 | } |
| 87 | file_name->insert(insert_pos, vndk_version_str()); |
| 88 | } |
| 89 | |
| 90 | const std::function<bool(const std::string&, std::string*)> always_true = |
| 91 | [](const std::string&, std::string*) { return true; }; |
| 92 | |
| 93 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, |
| 94 | const std::function<bool(const std::string& /* soname */, |
| 95 | std::string* /* error_msg */)>& check_soname, |
| 96 | std::string* error_msg = nullptr) { |
| 97 | // Read list of public native libraries from the config file. |
| 98 | std::string file_content; |
| 99 | if (!base::ReadFileToString(configFile, &file_content)) { |
| 100 | if (error_msg) *error_msg = strerror(errno); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 105 | |
| 106 | for (auto& line : lines) { |
| 107 | auto trimmed_line = base::Trim(line); |
| 108 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 109 | continue; |
| 110 | } |
| 111 | size_t space_pos = trimmed_line.rfind(' '); |
| 112 | if (space_pos != std::string::npos) { |
| 113 | std::string type = trimmed_line.substr(space_pos + 1); |
| 114 | if (type != "32" && type != "64") { |
| 115 | if (error_msg) *error_msg = "Malformed line: " + line; |
| 116 | return false; |
| 117 | } |
| 118 | #if defined(__LP64__) |
| 119 | // Skip 32 bit public library. |
| 120 | if (type == "32") { |
| 121 | continue; |
| 122 | } |
| 123 | #else |
| 124 | // Skip 64 bit public library. |
| 125 | if (type == "64") { |
| 126 | continue; |
| 127 | } |
| 128 | #endif |
| 129 | trimmed_line.resize(space_pos); |
| 130 | } |
| 131 | |
| 132 | if (check_soname(trimmed_line, error_msg)) { |
| 133 | sonames->push_back(trimmed_line); |
| 134 | } else { |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 142 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 143 | if (dir != nullptr) { |
| 144 | // Failing to opening the dir is not an error, which can happen in |
| 145 | // webview_zygote. |
| 146 | while (struct dirent* ent = readdir(dir.get())) { |
| 147 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 148 | continue; |
| 149 | } |
| 150 | const std::string filename(ent->d_name); |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 151 | std::string_view fn = filename; |
| 152 | if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) && |
| 153 | android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) { |
| 154 | const std::string company_name(fn); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 155 | const std::string config_file_path = dirname + "/"s + filename; |
| 156 | LOG_ALWAYS_FATAL_IF( |
| 157 | company_name.empty(), |
| 158 | "Error extracting company name from public native library list file path \"%s\"", |
| 159 | config_file_path.c_str()); |
| 160 | |
| 161 | std::string error_msg; |
| 162 | |
| 163 | LOG_ALWAYS_FATAL_IF( |
| 164 | !ReadConfig(config_file_path, sonames, |
| 165 | [&company_name](const std::string& soname, std::string* error_msg) { |
| 166 | if (android::base::StartsWith(soname, "lib") && |
| 167 | android::base::EndsWith(soname, "." + company_name + ".so")) { |
| 168 | return true; |
| 169 | } else { |
| 170 | *error_msg = "Library name \"" + soname + |
| 171 | "\" does not end with the company name: " + company_name + |
| 172 | "."; |
| 173 | return false; |
| 174 | } |
| 175 | }, |
| 176 | &error_msg), |
| 177 | "Error reading public native library list from \"%s\": %s", config_file_path.c_str(), |
| 178 | error_msg.c_str()); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 184 | static std::string InitDefaultPublicLibraries() { |
| 185 | std::string config_file = root_dir() + kDefaultPublicLibrariesFile; |
| 186 | std::vector<std::string> sonames; |
| 187 | std::string error_msg; |
| 188 | LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg), |
| 189 | "Error reading public native library list from \"%s\": %s", |
| 190 | config_file.c_str(), error_msg.c_str()); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 191 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 192 | std::string additional_libs = additional_public_libraries(); |
| 193 | if (!additional_libs.empty()) { |
| 194 | auto vec = base::Split(additional_libs, ":"); |
| 195 | std::copy(vec.begin(), vec.end(), std::back_inserter(sonames)); |
| 196 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 197 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 198 | // Remove the public libs in the runtime namespace. |
| 199 | // These libs are listed in public.android.txt, but we don't want the rest of android |
| 200 | // in default namespace to dlopen the libs. |
| 201 | // For example, libicuuc.so is exposed to classloader namespace from runtime namespace. |
| 202 | // Unfortunately, it does not have stable C symbols, and default namespace should only use |
| 203 | // stable symbols in libandroidicu.so. http://b/120786417 |
| 204 | for (const std::string& lib_name : kRuntimePublicLibraries) { |
| 205 | std::string path(kRuntimeApexLibPath); |
| 206 | path.append("/").append(lib_name); |
| 207 | |
| 208 | struct stat s; |
| 209 | // Do nothing if the path in /apex does not exist. |
| 210 | // Runtime APEX must be mounted since libnativeloader is in the same APEX |
| 211 | if (stat(path.c_str(), &s) != 0) { |
| 212 | continue; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 213 | } |
| 214 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 215 | auto it = std::find(sonames.begin(), sonames.end(), lib_name); |
| 216 | if (it != sonames.end()) { |
| 217 | sonames.erase(it); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 218 | } |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 219 | } |
| 220 | return android::base::Join(sonames, ':'); |
| 221 | } |
| 222 | |
| 223 | static std::string InitRuntimePublicLibraries() { |
| 224 | CHECK(sizeof(kRuntimePublicLibraries) > 0); |
| 225 | std::string list = android::base::Join(kRuntimePublicLibraries, ":"); |
| 226 | |
| 227 | std::string additional_libs = additional_public_libraries(); |
| 228 | if (!additional_libs.empty()) { |
| 229 | list = list + ':' + additional_libs; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 230 | } |
| 231 | return list; |
| 232 | } |
| 233 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 234 | static std::string InitVendorPublicLibraries() { |
| 235 | // This file is optional, quietly ignore if the file does not exist. |
| 236 | std::vector<std::string> sonames; |
| 237 | ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr); |
| 238 | return android::base::Join(sonames, ':'); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 241 | // read /system/etc/public.libraries-<companyname>.txt and |
| 242 | // /product/etc/public.libraries-<companyname>.txt which contain partner defined |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 243 | // system libs that are exposed to apps. The libs in the txt files must be |
| 244 | // named as lib<name>.<companyname>.so. |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 245 | static std::string InitExtendedPublicLibraries() { |
| 246 | std::vector<std::string> sonames; |
| 247 | ReadExtensionLibraries("/system/etc", &sonames); |
| 248 | ReadExtensionLibraries("/product/etc", &sonames); |
| 249 | return android::base::Join(sonames, ':'); |
| 250 | } |
| 251 | |
| 252 | static std::string InitLlndkLibraries() { |
| 253 | std::string config_file = kLlndkLibrariesFile; |
| 254 | InsertVndkVersionStr(&config_file); |
| 255 | std::vector<std::string> sonames; |
| 256 | ReadConfig(config_file, &sonames, always_true, nullptr); |
| 257 | return android::base::Join(sonames, ':'); |
| 258 | } |
| 259 | |
| 260 | static std::string InitVndkspLibraries() { |
| 261 | std::string config_file = kVndkLibrariesFile; |
| 262 | InsertVndkVersionStr(&config_file); |
| 263 | std::vector<std::string> sonames; |
| 264 | ReadConfig(config_file, &sonames, always_true, nullptr); |
| 265 | return android::base::Join(sonames, ':'); |
| 266 | } |
| 267 | |
| 268 | } // namespace |
| 269 | |
| 270 | const std::string& default_public_libraries() { |
| 271 | static std::string list = InitDefaultPublicLibraries(); |
| 272 | return list; |
| 273 | } |
| 274 | |
| 275 | const std::string& runtime_public_libraries() { |
| 276 | static std::string list = InitRuntimePublicLibraries(); |
| 277 | return list; |
| 278 | } |
| 279 | |
| 280 | const std::string& vendor_public_libraries() { |
| 281 | static std::string list = InitVendorPublicLibraries(); |
| 282 | return list; |
| 283 | } |
| 284 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 285 | const std::string& extended_public_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 286 | static std::string list = InitExtendedPublicLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 287 | return list; |
| 288 | } |
| 289 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 290 | const std::string& llndk_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 291 | static std::string list = InitLlndkLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 292 | return list; |
| 293 | } |
| 294 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 295 | const std::string& vndksp_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame^] | 296 | static std::string list = InitVndkspLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 297 | return list; |
| 298 | } |
| 299 | |
| 300 | } // namespace android::nativeloader |