Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 27 | static struct { |
| 28 | uintptr_t v; |
Steven Moreland | 8401230 | 2024-04-27 01:17:21 +0000 | [diff] [blame] | 29 | char padding[max_android_page_size() - sizeof(v)]; |
| 30 | } shadow_base_storage alignas(max_android_page_size()); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 31 | |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 32 | // __cfi_init is called by the loader as soon as the shadow is mapped. This may happen very early |
| 33 | // during startup, before libdl.so global constructors, and, on i386, even before __libc_sysinfo is |
| 34 | // initialized. This function should not do any system calls. |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 35 | extern "C" uintptr_t* __cfi_init(uintptr_t shadow_base) { |
| 36 | shadow_base_storage.v = shadow_base; |
Steven Moreland | 8401230 | 2024-04-27 01:17:21 +0000 | [diff] [blame] | 37 | static_assert(sizeof(shadow_base_storage) == max_android_page_size(), ""); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 38 | return &shadow_base_storage.v; |
| 39 | } |
| 40 | |
Evgenii Stepanov | 97c16f8 | 2017-08-02 16:34:44 -0700 | [diff] [blame] | 41 | // Returns the size of the CFI shadow mapping, or 0 if CFI is not (yet) used in this process. |
| 42 | extern "C" size_t __cfi_shadow_size() { |
| 43 | return shadow_base_storage.v != 0 ? CFIShadow::kShadowSize : 0; |
| 44 | } |
| 45 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 46 | static uint16_t shadow_load(void* p) { |
Peter Collingbourne | 45f0a3b | 2019-07-16 11:18:07 -0700 | [diff] [blame] | 47 | // Untag the pointer to move it into the address space covered by the shadow. |
Peter Collingbourne | 191ecdc | 2019-08-07 19:06:00 -0700 | [diff] [blame] | 48 | uintptr_t addr = reinterpret_cast<uintptr_t>(untag_address(p)); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 49 | uintptr_t ofs = CFIShadow::MemToShadowOffset(addr); |
| 50 | if (ofs > CFIShadow::kShadowSize) return CFIShadow::kInvalidShadow; |
| 51 | return *reinterpret_cast<uint16_t*>(shadow_base_storage.v + ofs); |
| 52 | } |
| 53 | |
| 54 | static uintptr_t cfi_check_addr(uint16_t v, void* Ptr) { |
| 55 | uintptr_t addr = reinterpret_cast<uintptr_t>(Ptr); |
Evgenii Stepanov | ded4524 | 2017-09-15 13:58:58 -0700 | [diff] [blame] | 56 | // The aligned range of [0, kShadowAlign) uses a single shadow element, therefore all pointers in |
| 57 | // this range must get the same aligned_addr below. This matches CFIShadowWriter::Add; not the |
| 58 | // same as align_up(). |
| 59 | uintptr_t aligned_addr = align_down(addr, CFIShadow::kShadowAlign) + CFIShadow::kShadowAlign; |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 60 | uintptr_t p = aligned_addr - (static_cast<uintptr_t>(v - CFIShadow::kRegularShadowMin) |
| 61 | << CFIShadow::kCfiCheckGranularity); |
| 62 | #ifdef __arm__ |
| 63 | // Assume Thumb encoding. FIXME: force thumb at compile time? |
| 64 | p++; |
| 65 | #endif |
| 66 | return p; |
| 67 | } |
| 68 | |
| 69 | static inline void cfi_slowpath_common(uint64_t CallSiteTypeId, void* Ptr, void* DiagData) { |
| 70 | uint16_t v = shadow_load(Ptr); |
| 71 | switch (v) { |
| 72 | case CFIShadow::kInvalidShadow: |
| 73 | __loader_cfi_fail(CallSiteTypeId, Ptr, DiagData, __builtin_return_address(0)); |
| 74 | break; |
| 75 | case CFIShadow::kUncheckedShadow: |
| 76 | break; |
| 77 | default: |
| 78 | reinterpret_cast<CFIShadow::CFICheckFn>(cfi_check_addr(v, Ptr))(CallSiteTypeId, Ptr, DiagData); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | extern "C" void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr) { |
| 83 | cfi_slowpath_common(CallSiteTypeId, Ptr, nullptr); |
| 84 | } |
| 85 | |
| 86 | extern "C" void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData) { |
| 87 | cfi_slowpath_common(CallSiteTypeId, Ptr, DiagData); |
| 88 | } |