xref: /aosp_15_r20/bionic/linker/linker_cfi.h (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  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #pragma once
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker #include "linker.h"
32*8d67ca89SAndroid Build Coastguard Worker #include "linker_debug.h"
33*8d67ca89SAndroid Build Coastguard Worker 
34*8d67ca89SAndroid Build Coastguard Worker #include <algorithm>
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker #include "private/CFIShadow.h"
37*8d67ca89SAndroid Build Coastguard Worker 
38*8d67ca89SAndroid Build Coastguard Worker // This class keeps the contents of CFI shadow up-to-date with the current set of loaded libraries.
39*8d67ca89SAndroid Build Coastguard Worker // See the comment in CFIShadow.h for more context.
40*8d67ca89SAndroid Build Coastguard Worker // See documentation in http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html#shared-library-support.
41*8d67ca89SAndroid Build Coastguard Worker //
42*8d67ca89SAndroid Build Coastguard Worker // Shadow is mapped and initialized lazily as soon as the first CFI-enabled DSO is loaded.
43*8d67ca89SAndroid Build Coastguard Worker // It is updated after any library is loaded (but before any constructors are ran), and
44*8d67ca89SAndroid Build Coastguard Worker // before any library is unloaded.
45*8d67ca89SAndroid Build Coastguard Worker class CFIShadowWriter : private CFIShadow {
46*8d67ca89SAndroid Build Coastguard Worker   // Returns pointer to the shadow element for an address.
MemToShadow(uintptr_t x)47*8d67ca89SAndroid Build Coastguard Worker   uint16_t* MemToShadow(uintptr_t x) {
48*8d67ca89SAndroid Build Coastguard Worker     return reinterpret_cast<uint16_t*>(*shadow_start + MemToShadowOffset(x));
49*8d67ca89SAndroid Build Coastguard Worker   }
50*8d67ca89SAndroid Build Coastguard Worker 
51*8d67ca89SAndroid Build Coastguard Worker   // Update shadow for the address range to the given constant value.
52*8d67ca89SAndroid Build Coastguard Worker   void AddConstant(uintptr_t begin, uintptr_t end, uint16_t v);
53*8d67ca89SAndroid Build Coastguard Worker 
54*8d67ca89SAndroid Build Coastguard Worker   // Update shadow for the address range to kUncheckedShadow.
55*8d67ca89SAndroid Build Coastguard Worker   void AddUnchecked(uintptr_t begin, uintptr_t end);
56*8d67ca89SAndroid Build Coastguard Worker 
57*8d67ca89SAndroid Build Coastguard Worker   // Update shadow for the address range to kInvalidShadow.
58*8d67ca89SAndroid Build Coastguard Worker   void AddInvalid(uintptr_t begin, uintptr_t end);
59*8d67ca89SAndroid Build Coastguard Worker 
60*8d67ca89SAndroid Build Coastguard Worker   // Update shadow for the address range to the given __cfi_check value.
61*8d67ca89SAndroid Build Coastguard Worker   void Add(uintptr_t begin, uintptr_t end, uintptr_t cfi_check);
62*8d67ca89SAndroid Build Coastguard Worker 
63*8d67ca89SAndroid Build Coastguard Worker   // Add a DSO to CFI shadow.
64*8d67ca89SAndroid Build Coastguard Worker   bool AddLibrary(soinfo* si);
65*8d67ca89SAndroid Build Coastguard Worker 
66*8d67ca89SAndroid Build Coastguard Worker   // Map CFI shadow.
67*8d67ca89SAndroid Build Coastguard Worker   uintptr_t MapShadow();
68*8d67ca89SAndroid Build Coastguard Worker 
69*8d67ca89SAndroid Build Coastguard Worker   // Initialize CFI shadow and update its contents for everything in solist if any loaded library is
70*8d67ca89SAndroid Build Coastguard Worker   // CFI-enabled. If new_si != nullptr, do an incremental check by looking only at new_si; otherwise
71*8d67ca89SAndroid Build Coastguard Worker   // look at the entire solist.
72*8d67ca89SAndroid Build Coastguard Worker   bool MaybeInit(soinfo *new_si, soinfo *solist);
73*8d67ca89SAndroid Build Coastguard Worker 
74*8d67ca89SAndroid Build Coastguard Worker   // Set a human readable name for the entire shadow region.
75*8d67ca89SAndroid Build Coastguard Worker   void FixupVmaName();
76*8d67ca89SAndroid Build Coastguard Worker 
77*8d67ca89SAndroid Build Coastguard Worker   // Pass the pointer to the mapped shadow region to libdl. Must only be called once.
78*8d67ca89SAndroid Build Coastguard Worker   // Flips shadow_start to a non-nullptr value.
79*8d67ca89SAndroid Build Coastguard Worker   bool NotifyLibDl(soinfo *solist, uintptr_t p);
80*8d67ca89SAndroid Build Coastguard Worker 
81*8d67ca89SAndroid Build Coastguard Worker   // Pointer to the shadow start address.
82*8d67ca89SAndroid Build Coastguard Worker   uintptr_t *shadow_start;
83*8d67ca89SAndroid Build Coastguard Worker 
84*8d67ca89SAndroid Build Coastguard Worker   bool initial_link_done;
85*8d67ca89SAndroid Build Coastguard Worker 
86*8d67ca89SAndroid Build Coastguard Worker  public:
87*8d67ca89SAndroid Build Coastguard Worker   // Update shadow after loading a DSO.
88*8d67ca89SAndroid Build Coastguard Worker   // This function will initialize the shadow if it sees a CFI-enabled DSO for the first time.
89*8d67ca89SAndroid Build Coastguard Worker   // In that case it will retroactively update shadow for all previously loaded DSOs. "solist" is a
90*8d67ca89SAndroid Build Coastguard Worker   // pointer to the global list.
91*8d67ca89SAndroid Build Coastguard Worker   // This function must be called before any user code has observed the newly loaded DSO.
92*8d67ca89SAndroid Build Coastguard Worker   bool AfterLoad(soinfo* si, soinfo *solist);
93*8d67ca89SAndroid Build Coastguard Worker 
94*8d67ca89SAndroid Build Coastguard Worker   // Update shadow before unloading a DSO.
95*8d67ca89SAndroid Build Coastguard Worker   void BeforeUnload(soinfo* si);
96*8d67ca89SAndroid Build Coastguard Worker 
97*8d67ca89SAndroid Build Coastguard Worker   // This is called as soon as the initial set of libraries is linked.
98*8d67ca89SAndroid Build Coastguard Worker   bool InitialLinkDone(soinfo *solist);
99*8d67ca89SAndroid Build Coastguard Worker 
100*8d67ca89SAndroid Build Coastguard Worker   // Handle failure to locate __cfi_check for a target address.
101*8d67ca89SAndroid Build Coastguard Worker   static void CfiFail(uint64_t CallSiteTypeId, void* Ptr, void* DiagData, void *caller_pc);
102*8d67ca89SAndroid Build Coastguard Worker };
103*8d67ca89SAndroid Build Coastguard Worker 
104*8d67ca89SAndroid Build Coastguard Worker CFIShadowWriter* get_cfi_shadow();
105