blob: f788c168cb1d4a11ac8741cb44155c6063617e99 [file] [log] [blame]
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001/*
2 * Copyright (C) 2016 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 "linker_cfi.h"
18
19#include "linker_debug.h"
20#include "linker_globals.h"
21#include "private/bionic_page.h"
22#include "private/bionic_prctl.h"
23
24#include <sys/mman.h>
25#include <sys/types.h>
26#include <cstdint>
27
28// Update shadow without making it writable by preparing the data on the side and mremap-ing it in
29// place.
30class ShadowWrite {
31 char* shadow_start;
32 char* shadow_end;
33 char* aligned_start;
34 char* aligned_end;
35 char* tmp_start;
36
37 public:
38 ShadowWrite(uint16_t* s, uint16_t* e) {
39 shadow_start = reinterpret_cast<char*>(s);
40 shadow_end = reinterpret_cast<char*>(e);
41 aligned_start = reinterpret_cast<char*>(PAGE_START(reinterpret_cast<uintptr_t>(shadow_start)));
42 aligned_end = reinterpret_cast<char*>(PAGE_END(reinterpret_cast<uintptr_t>(shadow_end)));
43 tmp_start =
44 reinterpret_cast<char*>(mmap(nullptr, aligned_end - aligned_start, PROT_READ | PROT_WRITE,
45 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
46 CHECK(tmp_start != MAP_FAILED);
47 memcpy(tmp_start, aligned_start, shadow_start - aligned_start);
48 memcpy(tmp_start + (shadow_end - aligned_start), shadow_end, aligned_end - shadow_end);
49 }
50
51 uint16_t* begin() {
52 return reinterpret_cast<uint16_t*>(tmp_start + (shadow_start - aligned_start));
53 }
54
55 uint16_t* end() {
56 return reinterpret_cast<uint16_t*>(tmp_start + (shadow_end - aligned_start));
57 }
58
59 ~ShadowWrite() {
60 size_t size = aligned_end - aligned_start;
61 mprotect(tmp_start, size, PROT_READ);
62 void* res = mremap(tmp_start, size, size, MREMAP_MAYMOVE | MREMAP_FIXED,
63 reinterpret_cast<void*>(aligned_start));
64 CHECK(res != MAP_FAILED);
65 }
66};
67
68void CFIShadowWriter::FixupVmaName() {
69 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, *shadow_start, kShadowSize, "cfi shadow");
70}
71
72void CFIShadowWriter::AddConstant(uintptr_t begin, uintptr_t end, uint16_t v) {
73 uint16_t* shadow_begin = MemToShadow(begin);
74 uint16_t* shadow_end = MemToShadow(end - 1) + 1;
75
76 ShadowWrite sw(shadow_begin, shadow_end);
77 std::fill(sw.begin(), sw.end(), v);
78}
79
80void CFIShadowWriter::AddUnchecked(uintptr_t begin, uintptr_t end) {
81 AddConstant(begin, end, kUncheckedShadow);
82}
83
84void CFIShadowWriter::AddInvalid(uintptr_t begin, uintptr_t end) {
85 AddConstant(begin, end, kInvalidShadow);
86}
87
88void CFIShadowWriter::Add(uintptr_t begin, uintptr_t end, uintptr_t cfi_check) {
89 CHECK((cfi_check & (kCfiCheckAlign - 1)) == 0);
90
91 // Don't fill anything below cfi_check. We can not represent those addresses
92 // in the shadow, and must make sure at codegen to place all valid call
93 // targets above cfi_check.
94 begin = std::max(begin, cfi_check) & ~(kShadowAlign - 1);
95 uint16_t* shadow_begin = MemToShadow(begin);
96 uint16_t* shadow_end = MemToShadow(end - 1) + 1;
97
98 ShadowWrite sw(shadow_begin, shadow_end);
99 uint16_t sv = ((begin + kShadowAlign - cfi_check) >> kCfiCheckGranularity) + kRegularShadowMin;
100
101 // With each step of the loop below, __cfi_check address computation base is increased by
102 // 2**ShadowGranularity.
103 // To compensate for that, each next shadow value must be increased by 2**ShadowGranularity /
104 // 2**CfiCheckGranularity.
105 uint16_t sv_step = 1 << (kShadowGranularity - kCfiCheckGranularity);
106 for (uint16_t& s : sw) {
107 // If there is something there already, fall back to unchecked. This may happen in rare cases
108 // with MAP_FIXED libraries. FIXME: consider using a (slow) resolution function instead.
109 s = (s == kInvalidShadow) ? sv : kUncheckedShadow;
110 sv += sv_step;
111 }
112}
113
114static soinfo* find_libdl(soinfo* solist) {
115 for (soinfo* si = solist; si != nullptr; si = si->next) {
116 const char* soname = si->get_soname();
117 if (soname && strcmp(soname, "libdl.so") == 0) {
118 return si;
119 }
120 }
121 return nullptr;
122}
123
124static uintptr_t soinfo_find_symbol(soinfo* si, const char* s) {
125 SymbolName name(s);
126 const ElfW(Sym) * sym;
127 if (si->find_symbol_by_name(name, nullptr, &sym) && sym) {
128 return si->resolve_symbol_address(sym);
129 }
130 return 0;
131}
132
133uintptr_t soinfo_find_cfi_check(soinfo* si) {
134 return soinfo_find_symbol(si, "__cfi_check");
135}
136
137uintptr_t CFIShadowWriter::MapShadow() {
138 void* p =
139 mmap(nullptr, kShadowSize, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
140 CHECK(p != MAP_FAILED);
141 return reinterpret_cast<uintptr_t>(p);
142}
143
144bool CFIShadowWriter::AddLibrary(soinfo* si) {
145 CHECK(shadow_start != nullptr);
146 if (si->base == 0 || si->size == 0) {
147 return true;
148 }
149 uintptr_t cfi_check = soinfo_find_cfi_check(si);
150 if (cfi_check == 0) {
151 INFO("[ CFI add 0x%zx + 0x%zx %s ]", static_cast<uintptr_t>(si->base),
152 static_cast<uintptr_t>(si->size), si->get_soname());
153 AddUnchecked(si->base, si->base + si->size);
154 return true;
155 }
156
157 INFO("[ CFI add 0x%zx + 0x%zx %s: 0x%zx ]", static_cast<uintptr_t>(si->base),
158 static_cast<uintptr_t>(si->size), si->get_soname(), cfi_check);
159#ifdef __arm__
160 // Require Thumb encoding.
161 if ((cfi_check & 1UL) != 1UL) {
162 DL_ERR("__cfi_check in not a Thumb function in the library \"%s\"", si->get_soname());
163 return false;
164 }
165 cfi_check &= ~1UL;
166#endif
167 if ((cfi_check & (kCfiCheckAlign - 1)) != 0) {
168 DL_ERR("unaligned __cfi_check in the library \"%s\"", si->get_soname());
169 return false;
170 }
171 Add(si->base, si->base + si->size, cfi_check);
172 return true;
173}
174
175// Pass the shadow mapping address to libdl.so. In return, we get an pointer to the location
176// libdl.so uses to store the address.
177bool CFIShadowWriter::NotifyLibDl(soinfo* solist, uintptr_t p) {
178 soinfo* libdl = find_libdl(solist);
179 if (libdl == nullptr) {
180 DL_ERR("CFI could not find libdl");
181 return false;
182 }
183
184 uintptr_t cfi_init = soinfo_find_symbol(libdl, "__cfi_init");
185 CHECK(cfi_init != 0);
186 shadow_start = reinterpret_cast<uintptr_t* (*)(uintptr_t)>(cfi_init)(p);
187 CHECK(shadow_start != nullptr);
188 CHECK(*shadow_start == p);
189 return true;
190}
191
192bool CFIShadowWriter::MaybeInit(soinfo* new_si, soinfo* solist) {
193 CHECK(initial_link_done);
194 // Check if CFI shadow must be initialized at this time.
195 bool found = false;
196 if (new_si == nullptr) {
197 // This is the case when we've just completed the initial link. There may have been earlier
198 // calls to MaybeInit that were skipped. Look though the entire solist.
199 for (soinfo* si = solist; si != nullptr; si = si->next) {
200 if (soinfo_find_cfi_check(si)) {
201 found = true;
202 break;
203 }
204 }
205 } else {
206 // See if the new library uses CFI.
207 found = soinfo_find_cfi_check(new_si);
208 }
209
210 // Nothing found.
211 if (!found) {
212 return true;
213 }
214
215 // Init shadow and add all currently loaded libraries (not just the new ones).
216 if (!NotifyLibDl(solist, MapShadow()))
217 return false;
218 for (soinfo* si = solist; si != nullptr; si = si->next) {
219 if (!AddLibrary(si))
220 return false;
221 }
222 FixupVmaName();
223 return true;
224}
225
226bool CFIShadowWriter::AfterLoad(soinfo* si, soinfo* solist) {
227 if (!initial_link_done) {
228 // Too early.
229 return true;
230 }
231
232 if (shadow_start == nullptr) {
233 return MaybeInit(si, solist);
234 }
235
236 // Add the new library to the CFI shadow.
237 if (!AddLibrary(si))
238 return false;
239 FixupVmaName();
240 return true;
241}
242
243void CFIShadowWriter::BeforeUnload(soinfo* si) {
244 if (shadow_start == nullptr) return;
245 if (si->base == 0 || si->size == 0) return;
246 INFO("[ CFI remove 0x%zx + 0x%zx: %s ]", static_cast<uintptr_t>(si->base),
247 static_cast<uintptr_t>(si->size), si->get_soname());
248 AddInvalid(si->base, si->base + si->size);
249 FixupVmaName();
250}
251
252bool CFIShadowWriter::InitialLinkDone(soinfo* solist) {
253 CHECK(!initial_link_done)
254 initial_link_done = true;
255 return MaybeInit(nullptr, solist);
256}
257
258// Find __cfi_check in the caller and let it handle the problem. Since caller_pc is likely not a
259// valid CFI target, we can not use CFI shadow for lookup. This does not need to be fast, do the
260// regular symbol lookup.
261void CFIShadowWriter::CfiFail(uint64_t CallSiteTypeId, void* Ptr, void* DiagData, void* CallerPc) {
262 soinfo* si = find_containing_library(CallerPc);
263 if (!si) {
264 __builtin_trap();
265 }
266
267 uintptr_t cfi_check = soinfo_find_cfi_check(si);
268 if (!cfi_check) {
269 __builtin_trap();
270 }
271
272 reinterpret_cast<CFICheckFn>(cfi_check)(CallSiteTypeId, Ptr, DiagData);
273}