xref: /aosp_15_r20/bionic/libdl/libdl_cfi.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  *
4*8d67ca89SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker  *
8*8d67ca89SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker  *
10*8d67ca89SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker  * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker  */
16*8d67ca89SAndroid Build Coastguard Worker 
17*8d67ca89SAndroid Build Coastguard Worker #include <sys/mman.h>
18*8d67ca89SAndroid Build Coastguard Worker 
19*8d67ca89SAndroid Build Coastguard Worker #include "private/CFIShadow.h"
20*8d67ca89SAndroid Build Coastguard Worker 
21*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default"))) extern "C" void __loader_cfi_fail(
22*8d67ca89SAndroid Build Coastguard Worker     uint64_t CallSiteTypeId, void* Ptr, void* DiagData, void* CallerPc);
23*8d67ca89SAndroid Build Coastguard Worker 
24*8d67ca89SAndroid Build Coastguard Worker // Base address of the CFI shadow. Passed down from the linker in __cfi_init()
25*8d67ca89SAndroid Build Coastguard Worker // and does not change after that. The contents of the shadow change in
26*8d67ca89SAndroid Build Coastguard Worker // dlopen/dlclose.
27*8d67ca89SAndroid Build Coastguard Worker static struct {
28*8d67ca89SAndroid Build Coastguard Worker   uintptr_t v;
29*8d67ca89SAndroid Build Coastguard Worker   char padding[max_android_page_size() - sizeof(v)];
30*8d67ca89SAndroid Build Coastguard Worker } shadow_base_storage alignas(max_android_page_size());
31*8d67ca89SAndroid Build Coastguard Worker 
32*8d67ca89SAndroid Build Coastguard Worker // __cfi_init is called by the loader as soon as the shadow is mapped. This may happen very early
33*8d67ca89SAndroid Build Coastguard Worker // during startup, before libdl.so global constructors, and, on i386, even before __libc_sysinfo is
34*8d67ca89SAndroid Build Coastguard Worker // initialized. This function should not do any system calls.
__cfi_init(uintptr_t shadow_base)35*8d67ca89SAndroid Build Coastguard Worker extern "C" uintptr_t* __cfi_init(uintptr_t shadow_base) {
36*8d67ca89SAndroid Build Coastguard Worker   shadow_base_storage.v = shadow_base;
37*8d67ca89SAndroid Build Coastguard Worker   static_assert(sizeof(shadow_base_storage) == max_android_page_size(), "");
38*8d67ca89SAndroid Build Coastguard Worker   return &shadow_base_storage.v;
39*8d67ca89SAndroid Build Coastguard Worker }
40*8d67ca89SAndroid Build Coastguard Worker 
41*8d67ca89SAndroid Build Coastguard Worker // Returns the size of the CFI shadow mapping, or 0 if CFI is not (yet) used in this process.
__cfi_shadow_size()42*8d67ca89SAndroid Build Coastguard Worker extern "C" size_t __cfi_shadow_size() {
43*8d67ca89SAndroid Build Coastguard Worker   return shadow_base_storage.v != 0 ? CFIShadow::kShadowSize : 0;
44*8d67ca89SAndroid Build Coastguard Worker }
45*8d67ca89SAndroid Build Coastguard Worker 
shadow_load(void * p)46*8d67ca89SAndroid Build Coastguard Worker static uint16_t shadow_load(void* p) {
47*8d67ca89SAndroid Build Coastguard Worker   // Untag the pointer to move it into the address space covered by the shadow.
48*8d67ca89SAndroid Build Coastguard Worker   uintptr_t addr = reinterpret_cast<uintptr_t>(untag_address(p));
49*8d67ca89SAndroid Build Coastguard Worker   uintptr_t ofs = CFIShadow::MemToShadowOffset(addr);
50*8d67ca89SAndroid Build Coastguard Worker   if (ofs > CFIShadow::kShadowSize) return CFIShadow::kInvalidShadow;
51*8d67ca89SAndroid Build Coastguard Worker   return *reinterpret_cast<uint16_t*>(shadow_base_storage.v + ofs);
52*8d67ca89SAndroid Build Coastguard Worker }
53*8d67ca89SAndroid Build Coastguard Worker 
cfi_check_addr(uint16_t v,void * Ptr)54*8d67ca89SAndroid Build Coastguard Worker static uintptr_t cfi_check_addr(uint16_t v, void* Ptr) {
55*8d67ca89SAndroid Build Coastguard Worker   uintptr_t addr = reinterpret_cast<uintptr_t>(Ptr);
56*8d67ca89SAndroid Build Coastguard Worker   // The aligned range of [0, kShadowAlign) uses a single shadow element, therefore all pointers in
57*8d67ca89SAndroid Build Coastguard Worker   // this range must get the same aligned_addr below. This matches CFIShadowWriter::Add; not the
58*8d67ca89SAndroid Build Coastguard Worker   // same as align_up().
59*8d67ca89SAndroid Build Coastguard Worker   uintptr_t aligned_addr = align_down(addr, CFIShadow::kShadowAlign) + CFIShadow::kShadowAlign;
60*8d67ca89SAndroid Build Coastguard Worker   uintptr_t p = aligned_addr - (static_cast<uintptr_t>(v - CFIShadow::kRegularShadowMin)
61*8d67ca89SAndroid Build Coastguard Worker                                 << CFIShadow::kCfiCheckGranularity);
62*8d67ca89SAndroid Build Coastguard Worker #ifdef __arm__
63*8d67ca89SAndroid Build Coastguard Worker   // Assume Thumb encoding. FIXME: force thumb at compile time?
64*8d67ca89SAndroid Build Coastguard Worker   p++;
65*8d67ca89SAndroid Build Coastguard Worker #endif
66*8d67ca89SAndroid Build Coastguard Worker   return p;
67*8d67ca89SAndroid Build Coastguard Worker }
68*8d67ca89SAndroid Build Coastguard Worker 
cfi_slowpath_common(uint64_t CallSiteTypeId,void * Ptr,void * DiagData)69*8d67ca89SAndroid Build Coastguard Worker static inline void cfi_slowpath_common(uint64_t CallSiteTypeId, void* Ptr, void* DiagData) {
70*8d67ca89SAndroid Build Coastguard Worker   uint16_t v = shadow_load(Ptr);
71*8d67ca89SAndroid Build Coastguard Worker   switch (v) {
72*8d67ca89SAndroid Build Coastguard Worker     case CFIShadow::kInvalidShadow:
73*8d67ca89SAndroid Build Coastguard Worker       __loader_cfi_fail(CallSiteTypeId, Ptr, DiagData, __builtin_return_address(0));
74*8d67ca89SAndroid Build Coastguard Worker       break;
75*8d67ca89SAndroid Build Coastguard Worker     case CFIShadow::kUncheckedShadow:
76*8d67ca89SAndroid Build Coastguard Worker       break;
77*8d67ca89SAndroid Build Coastguard Worker     default:
78*8d67ca89SAndroid Build Coastguard Worker       reinterpret_cast<CFIShadow::CFICheckFn>(cfi_check_addr(v, Ptr))(CallSiteTypeId, Ptr, DiagData);
79*8d67ca89SAndroid Build Coastguard Worker   }
80*8d67ca89SAndroid Build Coastguard Worker }
81*8d67ca89SAndroid Build Coastguard Worker 
__cfi_slowpath(uint64_t CallSiteTypeId,void * Ptr)82*8d67ca89SAndroid Build Coastguard Worker extern "C" void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr) {
83*8d67ca89SAndroid Build Coastguard Worker   cfi_slowpath_common(CallSiteTypeId, Ptr, nullptr);
84*8d67ca89SAndroid Build Coastguard Worker }
85*8d67ca89SAndroid Build Coastguard Worker 
__cfi_slowpath_diag(uint64_t CallSiteTypeId,void * Ptr,void * DiagData)86*8d67ca89SAndroid Build Coastguard Worker extern "C" void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData) {
87*8d67ca89SAndroid Build Coastguard Worker   cfi_slowpath_common(CallSiteTypeId, Ptr, DiagData);
88*8d67ca89SAndroid Build Coastguard Worker }
89