blob: 362b093fabe7ec47fd6a58d4ff1c4ea5e8cff6cc [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 <sys/mman.h>
18
19#include "private/CFIShadow.h"
20
21__attribute__((__weak__, visibility("default"))) extern "C" void __loader_cfi_fail(
22 uint64_t CallSiteTypeId, void* Ptr, void* DiagData, void* CallerPc);
23
24// Base address of the CFI shadow. Passed down from the linker in __cfi_init()
25// and does not change after that. The contents of the shadow change in
26// dlopen/dlclose.
27static struct {
28 uintptr_t v;
29 char padding[PAGE_SIZE - sizeof(v)];
30} shadow_base_storage alignas(PAGE_SIZE);
31
32extern "C" uintptr_t* __cfi_init(uintptr_t shadow_base) {
33 shadow_base_storage.v = shadow_base;
34 static_assert(sizeof(shadow_base_storage) == PAGE_SIZE, "");
Evgenii Stepanovbeb3eb12017-01-31 17:10:03 -080035 mprotect(&shadow_base_storage, PAGE_SIZE, PROT_READ);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070036 return &shadow_base_storage.v;
37}
38
39static uint16_t shadow_load(void* p) {
40 uintptr_t addr = reinterpret_cast<uintptr_t>(p);
41 uintptr_t ofs = CFIShadow::MemToShadowOffset(addr);
42 if (ofs > CFIShadow::kShadowSize) return CFIShadow::kInvalidShadow;
43 return *reinterpret_cast<uint16_t*>(shadow_base_storage.v + ofs);
44}
45
46static uintptr_t cfi_check_addr(uint16_t v, void* Ptr) {
47 uintptr_t addr = reinterpret_cast<uintptr_t>(Ptr);
48 uintptr_t aligned_addr = align_up(addr, CFIShadow::kShadowAlign);
49 uintptr_t p = aligned_addr - (static_cast<uintptr_t>(v - CFIShadow::kRegularShadowMin)
50 << CFIShadow::kCfiCheckGranularity);
51#ifdef __arm__
52 // Assume Thumb encoding. FIXME: force thumb at compile time?
53 p++;
54#endif
55 return p;
56}
57
58static inline void cfi_slowpath_common(uint64_t CallSiteTypeId, void* Ptr, void* DiagData) {
59 uint16_t v = shadow_load(Ptr);
60 switch (v) {
61 case CFIShadow::kInvalidShadow:
62 __loader_cfi_fail(CallSiteTypeId, Ptr, DiagData, __builtin_return_address(0));
63 break;
64 case CFIShadow::kUncheckedShadow:
65 break;
66 default:
67 reinterpret_cast<CFIShadow::CFICheckFn>(cfi_check_addr(v, Ptr))(CallSiteTypeId, Ptr, DiagData);
68 }
69}
70
71extern "C" void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr) {
72 cfi_slowpath_common(CallSiteTypeId, Ptr, nullptr);
73}
74
75extern "C" void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData) {
76 cfi_slowpath_common(CallSiteTypeId, Ptr, DiagData);
77}