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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Check that the current test executable only links known exported libraries
// dynamically. Intended to be statically linked into standalone tests.
#include <dlfcn.h>
#include <fcntl.h>
#include <gelf.h>
#include <libelf.h>
#include <algorithm>
#include <filesystem>
#include <string>
#include <vector>
#include "android-base/result-gmock.h"
#include "android-base/result.h"
#include "android-base/scopeguard.h"
#include "android-base/strings.h"
#include "android-base/unique_fd.h"
#include "base/stl_util.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace {
using ::android::base::ErrnoError;
using ::android::base::Error;
using ::android::base::Result;
// The allow-listed libraries. Standalone tests can assume that the ART module
// is from the same build as the test(*), but not the platform nor any other
// module. Hence all dynamic libraries listed here must satisfy at least one of
// these conditions:
//
// - Have a stable ABI and be available since the APEX min_sdk_version (31).
// This includes NDK and system APIs.
// - Be loaded from the ART APEX itself(*). Note that linker namespaces aren't
// set up to allow this for libraries that aren't exported, so in practice it
// is restricted to them.
// - Always be pushed to device together with the test.
// - Be a runtime instrumentation library or similar, e.g. for sanitizer test
// builds, where everything is always built from source - platform, module,
// and tests.
//
// *) (Non-MCTS) CTS tests is an exception - they must work with any future
// version of the module and hence restrict themselves to the exported module
// APIs.
constexpr const char* kAllowedDynamicLibDeps[] = {
// LLVM
"libclang_rt.hwasan-aarch64-android.so",
// Bionic
"libc.so",
"libdl.so",
"libdl_android.so",
"libm.so",
// Platform
"heapprofd_client_api.so",
"libbinder_ndk.so",
"liblog.so",
"libselinux.so",
"libz.so",
// Other modules
"libstatspull.so",
"libstatssocket.so",
// ART exported
"libdexfile.so",
"libnativebridge.so",
"libnativehelper.so",
"libnativeloader.so",
};
Result<std::string> GetCurrentElfObjectPath() {
Dl_info info;
if (dladdr(reinterpret_cast<void*>(GetCurrentElfObjectPath), &info) == 0) {
return Error() << "dladdr failed to map own address to a shared object.";
}
return info.dli_fname;
}
Result<std::vector<std::string>> GetDynamicLibDeps(const std::string& filename) {
if (elf_version(EV_CURRENT) == EV_NONE) {
return Errorf("libelf initialization failed: {}", elf_errmsg(-1));
}
android::base::unique_fd fd(open(filename.c_str(), O_RDONLY));
if (fd.get() == -1) {
return ErrnoErrorf("Error opening {}", filename);
}
Elf* elf = elf_begin(fd.get(), ELF_C_READ, /*ref=*/nullptr);
if (elf == nullptr) {
return Errorf("Error creating ELF object for {}: {}", filename, elf_errmsg(-1));
}
auto elf_cleanup = android::base::make_scope_guard([&]() { elf_end(elf); });
std::vector<std::string> libs;
// Find the dynamic section.
for (Elf_Scn* dyn_scn = nullptr; (dyn_scn = elf_nextscn(elf, dyn_scn)) != nullptr;) {
GElf_Shdr scn_hdr;
if (gelf_getshdr(dyn_scn, &scn_hdr) != &scn_hdr) {
return Errorf("Failed to retrieve ELF section header in {}: {}", filename, elf_errmsg(-1));
}
if (scn_hdr.sh_type == SHT_DYNAMIC) {
Elf_Data* data = elf_getdata(dyn_scn, /*data=*/nullptr);
// Iterate through dynamic section entries.
for (int i = 0; i < scn_hdr.sh_size / scn_hdr.sh_entsize; i++) {
GElf_Dyn dyn_entry;
if (gelf_getdyn(data, i, &dyn_entry) != &dyn_entry) {
return Errorf("Failed to get entry {} in ELF dynamic section of {}: {}",
i,
filename,
elf_errmsg(-1));
}
if (dyn_entry.d_tag == DT_NEEDED) {
const char* lib_name = elf_strptr(elf, scn_hdr.sh_link, dyn_entry.d_un.d_val);
if (lib_name == nullptr) {
return Errorf("Failed to get string from entry {} in ELF dynamic section of {}: {}",
i,
filename,
elf_errmsg(-1));
}
libs.push_back(lib_name);
}
}
break; // Found the dynamic section, no need to continue.
}
}
return libs;
}
} // namespace
TEST(StandaloneTestAllowedLibDeps, test) {
Result<std::string> path_to_self = GetCurrentElfObjectPath();
ASSERT_RESULT_OK(path_to_self);
Result<std::vector<std::string>> dyn_lib_deps = GetDynamicLibDeps(path_to_self.value());
ASSERT_RESULT_OK(dyn_lib_deps);
// Allow .so files in the same directory as the test binary, for shared libs
// pushed with the test using `data_libs`.
std::filesystem::path self_dir = std::filesystem::path(path_to_self.value()).parent_path();
std::vector<std::string> test_libs;
for (const std::filesystem::directory_entry& entry :
std::filesystem::directory_iterator(self_dir)) {
if (entry.is_regular_file() && entry.path().extension() == ".so") {
test_libs.push_back(entry.path().filename());
}
}
std::vector<std::string> disallowed_libs;
for (const std::string& dyn_lib_dep : dyn_lib_deps.value()) {
if (std::find(std::begin(kAllowedDynamicLibDeps),
std::end(kAllowedDynamicLibDeps),
dyn_lib_dep) != std::end(kAllowedDynamicLibDeps)) {
continue;
}
if (art::ContainsElement(test_libs, dyn_lib_dep)) {
continue;
}
disallowed_libs.push_back(dyn_lib_dep);
}
EXPECT_THAT(disallowed_libs, ::testing::IsEmpty())
<< path_to_self.value() << " has disallowed shared library dependencies.";
}
|