/
loader.c
383 lines (328 loc) · 10.9 KB
/
loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (C) 2019 Two Sigma Investments, LP.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* We use pieces of the musl loader to avoid rewriting a loader from scratch.
* musl loader is comprised of a few stages:
* - stage1 does relative relocations (useful for our data sections, e.g.,
* global variables referring to the addresses of other global variables).
* - stage2a does symbolic relocations (not that we need them as we don't
* export any symbols in our loader).
* - stage2b initializes the tls. This is useful as errno is setup in the tls.
* - stage3 does the rest (e.g., loading required libraries, invoking
* constructor, main).
*
* We replace the stage3 with our own. Note that we include musl .c files
* because we need access to map_library(), which is a static function.
*/
/*
* XXX stage2 invokes some syscalls:
* - arch_prctl(ARCH_SET_FS, 0x7fdf59509428)
* - set_tid_address(0x7fdf59509bd8)
* Also, calling sigaction unblocks RT_1 and RT_2:
* - rt_sigprocmask(SIG_UNBLOCK, [RT_1 RT_2], NULL, 8)
* Ideally, we would not have any of this behavior, as it's not our
* prerogative, but it seems to be fine.
*/
/* dlstart.c contains stage1 */
#include "../musl/ldso/dlstart.c"
/* dynlink.c contains stage2 and stage3 */
#undef _GNU_SOURCE /* dynlink.c defines it */
#define __dls3 orig_dls3 /* We override stage3 */
#include "../musl/ldso/dynlink.c"
#undef __dls3
#include "util.h"
#include "cpuid.h"
#include "config.h"
#include <err.h>
#include <stdbool.h>
#include <alloca.h>
/*
* stage2 and stage3 are resolved via symbol lookup in dynlink.c, so we have
* to export __dls2b and __dls3.
*/
LIB_EXPORT void __dls2b(size_t *sp);
LIB_EXPORT void __dls3(size_t *sp);
static void stage4(size_t *sp, size_t *auxv);
static void oom(void)
{
err(1, "out of memory");
}
static char *xstrdup(const char *s)
{
char *ret = strdup(s);
if (!ret)
oom();
return ret;
}
static size_t get_file_size(int fd)
{
struct stat stat;
if (fstat(fd, &stat) == -1)
err(1, "Can't stat file");
return stat.st_size;
}
static void read_all(int fd, char* buf, size_t len)
{
for (size_t i = 0; i < len;) {
ssize_t ret = read(fd, buf+i, len-i);
if (ret < 0)
err(1, "Can't read file");
i += ret;
}
}
/* XXX returns a string on the heap */
static char *unique_env_append(const char *name, const char *new_env, const char *sep)
{
char *prev_env = secure_getenv(name);
if (!prev_env)
return xstrdup(new_env);
char *env;
if (asprintf(&env, "%s%s%s", new_env, sep, prev_env) < 0)
oom();
/*
* env is of the form "/some/lib1.so:/some/lib2.so:/some/lib1.so"
* We have to take out duplicates, otherwise, we'll append the same
* variables over and over when forking from a child.
*/
char **items = NULL;
int num_items = 0;
char *saveptr;
for (char *tok = strtok_r(env, sep, &saveptr); tok; tok = strtok_r(NULL, sep, &saveptr)) {
bool duplicate = false;
for (int i = 0; i < num_items; i++) {
if (!strcmp(tok, items[i]))
duplicate |= true;
}
if (duplicate)
continue;
num_items++;
items = realloc(items, sizeof(*items)*num_items);
if (!items)
oom();
items[num_items-1] = tok;
}
/* Join the strings with the separator */
char *uenv = xstrdup(items[0]);
for (int i = 1; i < num_items; i++) {
char *tmp = uenv;
asprintf(&uenv, "%s%s%s", uenv, sep, items[i]);
free(tmp);
}
free(items);
free(env);
return uenv;
}
/* This does not include the NULL entry */
static int num_auxv_entries(size_t *auxv)
{
int i;
for (i=0; auxv[2*i]; i++);
return i;
}
static int num_pointers(char **envp)
{
int i;
for (i=0; envp[i]; i++);
return i;
}
static void inject_env_vars_heap(char *conf)
{
char *saveptr;
for (char *var = strtok_r(conf, "\n", &saveptr); var; var = strtok_r(NULL, "\n", &saveptr)) {
char *value;
char *eq = strchr(var, '=');
if (!eq)
err(1, "Incorrect env var: %s", var);
*eq = '\0';
value = eq+1;
/* Now, var and value point to a proper env var */
if (!strcmp(var, "LD_PRELOAD"))
value = unique_env_append("LD_PRELOAD", value, ":");
else if (!strcmp(var, "VIRT_CPUID_MASK"))
value = unique_env_append("VIRT_CPUID_MASK", value, ",");
/*
* Note that setenv() relocates the environ array on the heap,
* but that's fine as we copy it back on the stack later.
*/
setenv(var, value, 1);
}
}
static char *get_inject_env_file_content(void)
{
int fd = open(LD_INJECT_ENV_PATH, O_RDONLY);
if (fd < 0) {
if (errno != ENOENT)
err(1, "Can't open %s", LD_INJECT_ENV_PATH);
return NULL;
}
size_t conf_len = get_file_size(fd);
char *conf = malloc(conf_len+1);
if (!conf)
oom();
conf[conf_len] = '\0';
read_all(fd, conf, conf_len);
close(fd);
return conf;
}
static void init_libc(char **envp, size_t *auxv)
{
size_t aux[AUX_CNT];
__progname = LIB_NAME;
__environ = envp;
decode_vec(auxv, aux, AUX_CNT);
libc.page_size = aux[AT_PAGESZ];
libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
|| aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
}
void __dls3(size_t *sp)
{
/*
* We do the following:
* 1) Locate argv, envp, auxv, similar to musl's stage3.
* 2) Do nothing if LD_ENV_DISABLE is set
* 3) Load the configuration file at LD_INJECT_ENV_PATH.
* Do nothing if the file doesn't exists
* 4) The configuration file is a list of environment variables to
* set. Set all the variables in our environ. Note that
* LD_PRELOAD and VIRT_CPUID_MASK get special treatment of
* being appended to instead of overwritten
* 5) Relocate all env variables on the heap to the stack
* 6) Create the new argv, envp, auxv arrays on the stack
* 7) Continue with stage 4
*
* The reason why we inject a list of environment variables from a
* file is to prevent programs from manipulating the environment and
* avoid our LD_PRELOADs. This will make sure we are in.
*
* Note: In the following, we free() pointers, but it doesn't matter,
* because we'll use brk() to reset the heap, and never use our malloc
* again. Note that we need malloc() because we use setenv().
*/
int argc = *sp;
char **argv = (void *)(sp+1);
char **envp = argv+argc+1;
size_t *auxv;
int i;
uintptr_t orig_brk = __syscall(SYS_brk, 0);
for (i=argc+1; argv[i]; i++);
auxv = (void *)(argv+i+1);
init_libc(envp, auxv);
if (getenv("LD_ENV_DISABLE"))
stage4(sp, auxv); /* Never returns */
if (libc.secure) {
/*
* In setuid mode, we don't accept VIRT variables from the regular
* environment, only from the LD_INJECT_ENV_PATH file.
* Can't use secure_getenv() in cpuid.c, otherwise we won't get what
* we have in the inject-env file.
*/
unsetenv("VIRT_CPUID_MASK");
unsetenv("VIRT_CPUID_DEBUG");
}
char *conf = get_inject_env_file_content();
if (conf) {
inject_env_vars_heap(conf);
free(conf);
}
/* Relocate heap allocated environment variables on the stack */
for (char **var = environ; *var; var++) {
/*
* heap variables are necessarily at a lower address than
* the address of `var`.
*/
if ((void*)*var >= (void *)&var)
continue;
size_t len = strlen(*var);
char *stackvar = alloca(len);
memcpy(stackvar, *var, len+1);
free(*var);
*var = stackvar;
}
/*
* Finally, construct the argv, envp, and auxv arrays, which must be
* placed contiguously on the stack. It is what an ELF interpreter
* expects.
*/
struct __attribute__((aligned(sizeof(void *)), packed)) {
size_t argc;
char *argv[argc+1];
char *envp[num_pointers(environ)+1];
size_t auxv[2*(num_auxv_entries(auxv)+1)];
} new;
new.argc = argc;
memcpy(new.argv, argv, sizeof(new.argv));
memcpy(new.envp, environ, sizeof(new.envp));
memcpy(new.auxv, auxv, sizeof(new.auxv));
__environ = new.envp; /* We access the environ later */
__syscall(SYS_brk, orig_brk); /* Restore the heap like it was */
/*
* XXX at this point, malloc should no longer be used. we hand off the
* heap region to the interposed libc.
* Note, we forever leak the other stack variables we have in our frame.
*/
stage4((void *)&new, new.auxv);
}
static void fixup_elf_auxv(size_t *auxv, struct dso *ld_dso, Ehdr *ld_header)
{
size_t at_base;
if (search_vec(auxv, &at_base, AT_BASE) && !at_base) {
// AT_BASE==0: The ELF interpreter has been invoked as the program.
for (size_t *p = auxv; p[0]; p += 2) {
switch (p[0]) {
case AT_PHDR:
p[1] = (size_t)laddr(ld_dso, ld_header->e_phoff);
break;
case AT_PHENT:
p[1] = (size_t)ld_header->e_phnum;
break;
case AT_ENTRY:
p[1] = (size_t)laddr(ld_dso, ld_header->e_entry);
break;
}
}
} else {
// The ELF interpreter is invoked to load another program.
for (size_t *p = auxv; p[0]; p += 2) {
switch (p[0]) {
case AT_BASE:
p[1] = (size_t)laddr(ld_dso, 0);
break;
}
}
}
}
static void stage4(size_t *sp, size_t *auxv)
{
/*
* Install the SIGSEGV signal handler and enable faulting on the CPUID
* instruction.
*/
cpuid_init(libc.secure);
/* And finally load and jump to the interposed libc loader */
int fd = open(INTERPOSED_LD_PATH, O_RDONLY);
if (fd < 0)
err(1, "Can't open %s", INTERPOSED_LD_PATH);
struct dso ld_dso;
Ehdr *ld_header = map_library(fd, &ld_dso);
if (!ld_header)
errx(1, "Can't load %s", INTERPOSED_LD_PATH);
close(fd);
fixup_elf_auxv(auxv, &ld_dso, ld_header);
CRTJMP(laddr(&ld_dso, ld_header->e_entry), sp);
for(;;);
}