Debian Codegraph @

build/unix/elfhack/inject.c

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/mman.h>
9 #include <elf.h>
10
11 /* The Android NDK headers define those */
12 #undef Elf_Ehdr
13 #undef Elf_Addr
14
15 #if defined(__LP64__)
16 # define Elf_Ehdr Elf64_Ehdr
17 # define Elf_Phdr Elf64_Phdr
18 # define Elf_Addr Elf64_Addr
19 # define Elf_Word Elf64_Word
20 # define Elf_Dyn Elf64_Dyn
21 #else
22 # define Elf_Phdr Elf32_Phdr
23 # define Elf_Ehdr Elf32_Ehdr
24 # define Elf_Addr Elf32_Addr
25 # define Elf_Word Elf32_Word
26 # define Elf_Dyn Elf32_Dyn
27 #endif
28
29 #ifdef RELRHACK
30 # include "relrhack.h"
31 # define mprotect_cb mprotect
32 # define sysconf_cb sysconf
33
34 #else
35 // On ARM, PC-relative function calls have a limit in how far they can jump,
36 // which might not be enough for e.g. libxul.so. The easy way out would be
37 // to use the long_call attribute, which forces the compiler to generate code
38 // that can call anywhere, but clang doesn't support the attribute yet
39 // (https://bugs.llvm.org/show_bug.cgi?id=40623), and while the command-line
40 // equivalent does exist, it's currently broken
41 // (https://bugs.llvm.org/show_bug.cgi?id=40624). So we create a manual
42 // trampoline, corresponding to the code GCC generates with long_call.
43 # ifdef __arm__
44 __attribute__((section(".text._init_trampoline"), naked)) int init_trampoline(
45 int argc, char** argv, char** env) {
46 __asm__ __volatile__(
47 // thumb doesn't allow to use r12/ip with ldr, and thus would require an
48 // additional push/pop to save/restore the modified register, which would
49 // also change the call into a blx. It's simpler to switch to arm.
50 ".arm\n"
51 " ldr ip, .LADDR\n"
52 ".LAFTER:\n"
53 " add ip, pc, ip\n"
54 " bx ip\n"
55 ".LADDR:\n"
56 " .word real_original_init-(.LAFTER+8)\n");
57 }
58 # endif
59
60 // On aarch64, a similar problem exists, but long_call is not an option at all
61 // (even GCC doesn't support them on aarch64).
62 # ifdef __aarch64__
63 __attribute__((section(".text._init_trampoline"), naked)) int init_trampoline(
64 int argc, char** argv, char** env) {
65 __asm__ __volatile__(
66 " adrp x8, .LADDR\n"
67 " add x8, x8, :lo12:.LADDR\n" // adrp + add gives us the full address
68 // for .LADDR
69 " ldr x0, [x8]\n" // Load the address of real_original_init relative to
70 // .LADDR
71 " add x0, x8, x0\n" // Add the address of .LADDR
72 " br x0\n" // Branch to real_original_init
73 ".LADDR:\n"
74 " .xword real_original_init-.LADDR\n");
75 }
76 # endif
77
78 extern __attribute__((visibility("hidden"))) void original_init(int argc,
79 char** argv,
80 char** env);
81
82 extern __attribute__((visibility("hidden"))) Elf_Addr relhack[];
83 extern __attribute__((visibility("hidden"))) Elf_Addr relhack_end[];
84
85 extern __attribute__((visibility("hidden"))) int (*mprotect_cb)(void* addr,
86 size_t len,
87 int prot);
88 extern __attribute__((visibility("hidden"))) long (*sysconf_cb)(int name);
89 extern __attribute__((visibility("hidden"))) char relro_start[];
90 extern __attribute__((visibility("hidden"))) char relro_end[];
91 #endif
92
93 extern __attribute__((visibility("hidden"))) Elf_Ehdr __ehdr_start;
94
95 static inline __attribute__((always_inline)) void do_relocations(
96 Elf_Addr* relhack, Elf_Addr* relhack_end) {
97 Elf_Addr* ptr;
98 for (Elf_Addr* entry = relhack; entry < relhack_end; entry++) {
99 if ((*entry & 1) == 0) {
100 ptr = (Elf_Addr*)((intptr_t)&__ehdr_start + *entry);
101 *ptr += (intptr_t)&__ehdr_start;
102 } else {
103 Elf_Addr bits = *entry;
104 Elf_Addr* end = ptr + 8 * sizeof(Elf_Addr) - 1;
105 do {
106 ptr++;
107 bits >>= 1;
108 if (bits & 1) {
109 *ptr += (intptr_t)&__ehdr_start;
110 }
111 } while (ptr < end);
112 }
113 }
114 }
115
116 #ifndef RELRHACK
117 __attribute__((section(".text._init_noinit"))) int init_noinit(int argc,
118 char** argv,
119 char** env) {
120 do_relocations(relhack, relhack_end);
121 return 0;
122 }
123
124 __attribute__((section(".text._init"))) int init(int argc, char** argv,
125 char** env) {
126 do_relocations(relhack, relhack_end);
127 original_init(argc, argv, env);
128 // Ensure there is no tail-call optimization, avoiding the use of the
129 // B.W instruction in Thumb for the call above.
130 return 0;
131 }
132 #endif
133
134 static inline __attribute__((always_inline)) void do_relocations_with_relro(
135 Elf_Addr* relhack, Elf_Addr* relhack_end, char* relro_start,
136 char* relro_end) {
137 long page_size = sysconf_cb(_SC_PAGESIZE);
138 uintptr_t aligned_relro_start = ((uintptr_t)relro_start) & ~(page_size - 1);
139 // The relro segment may not end at a page boundary. If that's the case, the
140 // remainder of the page needs to stay read-write, so the last page is never
141 // set read-only. Thus the aligned relro end is page-rounded down.
142 uintptr_t aligned_relro_end = ((uintptr_t)relro_end) & ~(page_size - 1);
143 // By the time the injected code runs, the relro segment is read-only. But
144 // we want to apply relocations in it, so we set it r/w first. We'll restore
145 // it to read-only in relro_post.
146 mprotect_cb((void*)aligned_relro_start,
147 aligned_relro_end - aligned_relro_start, PROT_READ | PROT_WRITE);
148
149 do_relocations(relhack, relhack_end);
150
151 mprotect_cb((void*)aligned_relro_start,
152 aligned_relro_end - aligned_relro_start, PROT_READ);
153 #ifndef RELRHACK
154 // mprotect_cb and sysconf_cb are allocated in .bss, so we need to restore
155 // them to a NULL value.
156 mprotect_cb = NULL;
157 sysconf_cb = NULL;
158 #endif
159 }
160
161 #ifndef RELRHACK
162 __attribute__((section(".text._init_noinit_relro"))) int init_noinit_relro(
163 int argc, char** argv, char** env) {
164 do_relocations_with_relro(relhack, relhack_end, relro_start, relro_end);
165 return 0;
166 }
167
168 __attribute__((section(".text._init_relro"))) int init_relro(int argc,
169 char** argv,
170 char** env) {
171 do_relocations_with_relro(relhack, relhack_end, relro_start, relro_end);
172 original_init(argc, argv, env);
173 return 0;
174 }
175 #else
176
177 extern __attribute__((visibility("hidden"))) Elf_Dyn _DYNAMIC[];
178
179 static void _relrhack_init(void) {
180 // Get the location of the SHT_RELR data from the PT_DYNAMIC segment.
181 uintptr_t elf_header = (uintptr_t)&__ehdr_start;
182 Elf_Addr* relhack = NULL;
183 Elf_Word size = 0;
184 for (Elf_Dyn* dyn = _DYNAMIC; dyn->d_tag != DT_NULL; dyn++) {
185 if ((dyn->d_tag & ~DT_RELRHACK_BIT) == DT_RELR) {
186 relhack = (Elf_Addr*)(elf_header + dyn->d_un.d_ptr);
187 } else if ((dyn->d_tag & ~DT_RELRHACK_BIT) == DT_RELRSZ) {
188 size = dyn->d_un.d_val;
189 }
190 }
191
192 Elf_Addr* relhack_end = (Elf_Addr*)((uintptr_t)relhack + size);
193
194 // Find the location of the PT_GNU_RELRO segment in the program headers.
195 Elf_Phdr* phdr = (Elf_Phdr*)(elf_header + __ehdr_start.e_phoff);
196 char* relro_start = NULL;
197 char* relro_end = NULL;
198 for (int i = 0; i < __ehdr_start.e_phnum; i++) {
199 if (phdr[i].p_type == PT_GNU_RELRO) {
200 relro_start = (char*)(elf_header + phdr[i].p_vaddr);
201 relro_end = (char*)(relro_start + phdr[i].p_memsz);
202 break;
203 }
204 }
205
206 if (relro_start != relro_end) {
207 do_relocations_with_relro(relhack, relhack_end, relro_start, relro_end);
208 } else {
209 do_relocations(relhack, relhack_end);
210 }
211 }
212
213 # ifdef ANDROID
214 // This creates a value that uses a relative relocation.
215 // In the case we've not set DT_RELRHACK_BIT on the RELR tags in the
216 // dynamic section, if the dynamic loader applied the relocation,
217 // the value will correctly point to the function address in memory.
218 __attribute__((visibility("hidden"))) void (*__relrhack_init)(void) =
219 _relrhack_init;
220 # endif
221
222 // The Android CRT doesn't contain an init function.
223 # ifndef ANDROID
224 extern __attribute__((visibility("hidden"))) void _init(int argc, char** argv,
225 char** env);
226 # endif
227
228 void _relrhack_wrap_init(int argc, char** argv, char** env) {
229 # ifdef ANDROID
230 // When the dynamic loader has applied the relocations itself, do nothing.
231 // (see comment above __relrhack_init)
232 if (__relrhack_init == _relrhack_init) {
233 return;
234 }
235 # endif
236 _relrhack_init();
237 # ifndef ANDROID
238 _init(argc, argv, env);
239 # endif
240 }
241 #endif