1 /*
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 <stdlib.h>
30 #include <string.h>
31 #include <sys/mman.h>
32
33 #include <gtest/gtest.h>
34
35 #include "linker_config.h"
36 #include "linker_utils.h"
37
38 #include <unistd.h>
39
40 #include <android-base/file.h>
41 #include <android-base/scopeguard.h>
42 #include <android-base/stringprintf.h>
43
44 #if defined(__LP64__)
45 #define ARCH_SUFFIX "64"
46 #else
47 #define ARCH_SUFFIX ""
48 #endif
49
50 // clang-format off
51 static const char* config_str =
52 "# comment \n"
53 "dir.test = /data/local/tmp\n"
54 "\n"
55 "[test]\n"
56 "\n"
57 "enable.target.sdk.version = true\n"
58 "additional.namespaces=system\n"
59 "additional.namespaces+=vndk\n"
60 "additional.namespaces+=vndk_in_system\n"
61 "namespace.default.isolated = true\n"
62 "namespace.default.search.paths = /vendor/${LIB}\n"
63 "namespace.default.permitted.paths = /vendor/${LIB}\n"
64 "namespace.default.asan.search.paths = /data\n"
65 "namespace.default.asan.search.paths += /vendor/${LIB}\n"
66 "namespace.default.asan.permitted.paths = /data:/vendor\n"
67 "namespace.default.links = system\n"
68 "namespace.default.links += vndk\n"
69 // irregular whitespaces are added intentionally for testing purpose
70 "namespace.default.link.system.shared_libs= libc.so\n"
71 "namespace.default.link.system.shared_libs += libm.so:libdl.so\n"
72 "namespace.default.link.system.shared_libs +=libstdc++.so\n"
73 "namespace.default.link.vndk.shared_libs = libcutils.so:libbase.so\n"
74 "namespace.system.isolated = true\n"
75 "namespace.system.visible = true\n"
76 "namespace.system.search.paths = /system/${LIB}\n"
77 "namespace.system.permitted.paths = /system/${LIB}\n"
78 "namespace.system.asan.search.paths = /data:/system/${LIB}\n"
79 "namespace.system.asan.permitted.paths = /data:/system\n"
80 "namespace.vndk.isolated = tr\n"
81 "namespace.vndk.isolated += ue\n" // should be ignored and return as 'false'.
82 "namespace.vndk.search.paths = /system/${LIB}/vndk\n"
83 "namespace.vndk.asan.search.paths = /data\n"
84 "namespace.vndk.asan.search.paths += /system/${LIB}/vndk\n"
85 "namespace.vndk.links = default\n"
86 "namespace.vndk.link.default.allow_all_shared_libs = true\n"
87 "namespace.vndk.link.vndk_in_system.allow_all_shared_libs = true\n"
88 "namespace.vndk_in_system.isolated = true\n"
89 "namespace.vndk_in_system.visible = true\n"
90 "namespace.vndk_in_system.search.paths = /system/${LIB}\n"
91 "namespace.vndk_in_system.permitted.paths = /system/${LIB}\n"
92 "namespace.vndk_in_system.whitelisted = libz.so:libyuv.so\n"
93 "namespace.vndk_in_system.whitelisted += libtinyxml2.so\n"
94 "namespace.vndk_in_system.allowed_libs = libfoo.so:libbar.so\n"
95 "namespace.vndk_in_system.allowed_libs += libtinyxml3.so\n"
96 "\n";
97 // clang-format on
98
write_version(const std::string & path,uint32_t version)99 static bool write_version(const std::string& path, uint32_t version) {
100 std::string content = android::base::StringPrintf("%d", version);
101 return android::base::WriteStringToFile(content, path);
102 }
103
resolve_paths(std::vector<std::string> paths)104 static std::vector<std::string> resolve_paths(std::vector<std::string> paths) {
105 std::vector<std::string> resolved_paths;
106 resolve_paths(paths, &resolved_paths);
107 return resolved_paths;
108 }
109
run_linker_config_smoke_test(bool is_asan)110 static void run_linker_config_smoke_test(bool is_asan) {
111 const std::vector<std::string> kExpectedDefaultSearchPath =
112 resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/vendor/lib" ARCH_SUFFIX }) :
113 std::vector<std::string>({ "/vendor/lib" ARCH_SUFFIX }));
114
115 const std::vector<std::string> kExpectedDefaultPermittedPath =
116 resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/vendor" }) :
117 std::vector<std::string>({ "/vendor/lib" ARCH_SUFFIX }));
118
119 const std::vector<std::string> kExpectedSystemSearchPath =
120 resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system/lib" ARCH_SUFFIX }) :
121 std::vector<std::string>({ "/system/lib" ARCH_SUFFIX }));
122
123 const std::vector<std::string> kExpectedSystemPermittedPath =
124 resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system" }) :
125 std::vector<std::string>({ "/system/lib" ARCH_SUFFIX }));
126
127 const std::vector<std::string> kExpectedVndkSearchPath =
128 resolve_paths(is_asan ? std::vector<std::string>({ "/data", "/system/lib" ARCH_SUFFIX "/vndk"}) :
129 std::vector<std::string>({ "/system/lib" ARCH_SUFFIX "/vndk"}));
130
131 TemporaryFile tmp_file;
132 close(tmp_file.fd);
133 tmp_file.fd = -1;
134
135 android::base::WriteStringToFile(config_str, tmp_file.path);
136
137 TemporaryDir tmp_dir;
138
139 std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
140 std::string version_file = std::string(tmp_dir.path) + "/.version";
141
142 auto file_guard =
143 android::base::make_scope_guard([&version_file] { unlink(version_file.c_str()); });
144
145 ASSERT_TRUE(write_version(version_file, 113U)) << strerror(errno);
146
147 // read config
148 const Config* config = nullptr;
149 std::string error_msg;
150 ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
151 executable_path.c_str(),
152 is_asan,
153 &config,
154 &error_msg)) << error_msg;
155 ASSERT_TRUE(config != nullptr);
156 ASSERT_TRUE(error_msg.empty());
157
158 ASSERT_EQ(113, config->target_sdk_version());
159
160 const NamespaceConfig* default_ns_config = config->default_namespace_config();
161 ASSERT_TRUE(default_ns_config != nullptr);
162
163 ASSERT_TRUE(default_ns_config->isolated());
164 ASSERT_FALSE(default_ns_config->visible());
165 ASSERT_EQ(kExpectedDefaultSearchPath, default_ns_config->search_paths());
166 ASSERT_EQ(kExpectedDefaultPermittedPath, default_ns_config->permitted_paths());
167
168 const auto& default_ns_links = default_ns_config->links();
169 ASSERT_EQ(2U, default_ns_links.size());
170
171 ASSERT_EQ("system", default_ns_links[0].ns_name());
172 ASSERT_EQ("libc.so:libm.so:libdl.so:libstdc++.so", default_ns_links[0].shared_libs());
173 ASSERT_FALSE(default_ns_links[0].allow_all_shared_libs());
174
175 ASSERT_EQ("vndk", default_ns_links[1].ns_name());
176 ASSERT_EQ("libcutils.so:libbase.so", default_ns_links[1].shared_libs());
177 ASSERT_FALSE(default_ns_links[1].allow_all_shared_libs());
178
179 auto& ns_configs = config->namespace_configs();
180 ASSERT_EQ(4U, ns_configs.size());
181
182 // find second namespace
183 const NamespaceConfig* ns_system = nullptr;
184 const NamespaceConfig* ns_vndk = nullptr;
185 const NamespaceConfig* ns_vndk_in_system = nullptr;
186 for (auto& ns : ns_configs) {
187 std::string ns_name = ns->name();
188 ASSERT_TRUE(ns_name == "system" || ns_name == "default" ||
189 ns_name == "vndk" || ns_name == "vndk_in_system")
190 << "unexpected ns name: " << ns->name();
191
192 if (ns_name == "system") {
193 ns_system = ns.get();
194 } else if (ns_name == "vndk") {
195 ns_vndk = ns.get();
196 } else if (ns_name == "vndk_in_system") {
197 ns_vndk_in_system = ns.get();
198 }
199 }
200
201 ASSERT_TRUE(ns_system != nullptr) << "system namespace was not found";
202
203 ASSERT_TRUE(ns_system->isolated());
204 ASSERT_TRUE(ns_system->visible());
205 ASSERT_EQ(kExpectedSystemSearchPath, ns_system->search_paths());
206 ASSERT_EQ(kExpectedSystemPermittedPath, ns_system->permitted_paths());
207
208 ASSERT_TRUE(ns_vndk != nullptr) << "vndk namespace was not found";
209
210 ASSERT_FALSE(ns_vndk->isolated()); // malformed bool property
211 ASSERT_FALSE(ns_vndk->visible()); // undefined bool property
212 ASSERT_EQ(kExpectedVndkSearchPath, ns_vndk->search_paths());
213
214 const auto& ns_vndk_links = ns_vndk->links();
215 ASSERT_EQ(1U, ns_vndk_links.size());
216 ASSERT_EQ("default", ns_vndk_links[0].ns_name());
217 ASSERT_TRUE(ns_vndk_links[0].allow_all_shared_libs());
218
219 ASSERT_TRUE(ns_vndk_in_system != nullptr) << "vndk_in_system namespace was not found";
220 ASSERT_EQ(std::vector<std::string>({"libz.so", "libyuv.so", "libtinyxml2.so", "libfoo.so",
221 "libbar.so", "libtinyxml3.so"}),
222 ns_vndk_in_system->allowed_libs());
223 }
224
TEST(linker_config,smoke)225 TEST(linker_config, smoke) {
226 run_linker_config_smoke_test(false);
227 }
228
TEST(linker_config,asan_smoke)229 TEST(linker_config, asan_smoke) {
230 run_linker_config_smoke_test(true);
231 }
232
TEST(linker_config,ns_link_shared_libs_invalid_settings)233 TEST(linker_config, ns_link_shared_libs_invalid_settings) {
234 // This unit test ensures an error is emitted when a namespace link in ld.config.txt specifies
235 // both shared_libs and allow_all_shared_libs.
236
237 static const char config_str[] =
238 "dir.test = /data/local/tmp\n"
239 "\n"
240 "[test]\n"
241 "additional.namespaces = system\n"
242 "namespace.default.links = system\n"
243 "namespace.default.link.system.shared_libs = libc.so:libm.so\n"
244 "namespace.default.link.system.allow_all_shared_libs = true\n"
245 "\n";
246
247 TemporaryFile tmp_file;
248 close(tmp_file.fd);
249 tmp_file.fd = -1;
250
251 android::base::WriteStringToFile(config_str, tmp_file.path);
252
253 TemporaryDir tmp_dir;
254
255 std::string executable_path = std::string(tmp_dir.path) + "/some-binary";
256
257 const Config* config = nullptr;
258 std::string error_msg;
259 ASSERT_FALSE(Config::read_binary_config(tmp_file.path,
260 executable_path.c_str(),
261 false,
262 &config,
263 &error_msg));
264 ASSERT_TRUE(config == nullptr);
265 ASSERT_EQ(std::string(tmp_file.path) + ":6: "
266 "error: both shared_libs and allow_all_shared_libs are set for default->system link.",
267 error_msg);
268 }
269
TEST(linker_config,dir_path_resolve)270 TEST(linker_config, dir_path_resolve) {
271 // This unit test ensures the linker resolves paths of dir.${section}
272 // properties to real path.
273
274 TemporaryDir tmp_dir;
275
276 std::string sub_dir = std::string(tmp_dir.path) + "/subdir";
277 mkdir(sub_dir.c_str(), 0755);
278
279 auto subdir_guard =
280 android::base::make_scope_guard([&sub_dir] { rmdir(sub_dir.c_str()); });
281
282 std::string symlink_path = std::string(tmp_dir.path) + "/symlink";
283 symlink(sub_dir.c_str(), symlink_path.c_str());
284
285 auto symlink_guard =
286 android::base::make_scope_guard([&symlink_path] { unlink(symlink_path.c_str()); });
287
288 std::string config_str =
289 "dir.test = " + symlink_path + "\n"
290 "\n"
291 "[test]\n";
292
293 TemporaryFile tmp_file;
294 close(tmp_file.fd);
295 tmp_file.fd = -1;
296
297 android::base::WriteStringToFile(config_str, tmp_file.path);
298
299 std::string executable_path = sub_dir + "/some-binary";
300
301 const Config* config = nullptr;
302 std::string error_msg;
303
304 ASSERT_TRUE(Config::read_binary_config(tmp_file.path,
305 executable_path.c_str(),
306 false,
307 &config,
308 &error_msg)) << error_msg;
309
310 ASSERT_TRUE(config != nullptr) << error_msg;
311 ASSERT_TRUE(error_msg.empty()) << error_msg;
312 }
313