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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/* $NetBSD: linux_exec_elf32.c,v 1.102 2021/11/26 09:05:05 ryo Exp $ */
/*-
* Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
* Emmanuel Dreyfus.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* based on exec_aout.c, sunos_exec.c and svr4_exec.c
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.102 2021/11/26 09:05:05 ryo Exp $");
#ifndef ELFSIZE
/* XXX should die */
#define ELFSIZE 32
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/namei.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>
#include <sys/stat.h>
#include <sys/kauth.h>
#include <sys/cprng.h>
#include <sys/compat_stub.h>
#include <sys/mman.h>
#include <sys/syscallargs.h>
#include <sys/cpu.h>
#include <machine/reg.h>
#include <compat/linux/common/linux_types.h>
#include <compat/linux/common/linux_signal.h>
#include <compat/linux/common/linux_util.h>
#include <compat/linux/common/linux_exec.h>
#include <compat/linux/common/linux_machdep.h>
#include <compat/linux/common/linux_ipc.h>
#include <compat/linux/common/linux_sem.h>
#include <compat/linux/linux_syscallargs.h>
#include <compat/linux/linux_syscall.h>
#ifdef DEBUG_LINUX
#define DPRINTF(a) uprintf a
#else
#define DPRINTF(a) do {} while (0)
#endif
#ifdef LINUX_ATEXIT_SIGNATURE
/*
* On the PowerPC, statically linked Linux binaries are not recognized
* by linux_signature nor by linux_gcc_signature. Fortunately, thoses
* binaries features a __libc_atexit ELF section. We therefore assume we
* have a Linux binary if we find this section.
*/
int
ELFNAME2(linux,atexit_signature)(
struct lwp *l,
struct exec_package *epp,
Elf_Ehdr *eh)
{
Elf_Shdr *sh;
size_t shsize;
u_int shstrndx;
size_t i;
static const char signature[] = "__libc_atexit";
const size_t sigsz = sizeof(signature);
char tbuf[sizeof(signature)];
int error;
/* Load the section header table. */
shsize = eh->e_shnum * sizeof(Elf_Shdr);
sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
IO_NODELOCKED);
if (error)
goto out;
/* Now let's find the string table. If it does not exist, give up. */
shstrndx = eh->e_shstrndx;
if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
error = ENOEXEC;
goto out;
}
/* Check if any section has the name we're looking for. */
const off_t stroff = sh[shstrndx].sh_offset;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
if (s->sh_name + sigsz > sh[shstrndx].sh_size)
continue;
error = exec_read(l, epp->ep_vp, stroff + s->sh_name, tbuf,
sigsz, IO_NODELOCKED);
if (error)
goto out;
if (!memcmp(tbuf, signature, sigsz)) {
DPRINTF(("linux_atexit_sig=%s\n", tbuf));
error = 0;
goto out;
}
}
error = ENOEXEC;
out:
free(sh, M_TEMP);
return (error);
}
#endif
#ifdef LINUX_GCC_SIGNATURE
/*
* Take advantage of the fact that all the linux binaries are compiled
* with gcc, and gcc sticks in the comment field a signature. Note that
* on SVR4 binaries, the gcc signature will follow the OS name signature,
* that will not be a problem. We don't bother to read in the string table,
* but we check all the progbits headers.
*
* XXX This only works in the i386. On the alpha (at least)
* XXX we have the same gcc signature which incorrectly identifies
* XXX NetBSD binaries as Linux.
*/
int
ELFNAME2(linux,gcc_signature)(
struct lwp *l,
struct exec_package *epp,
Elf_Ehdr *eh)
{
size_t shsize;
size_t i;
static const char signature[] = "\0GCC: (GNU) ";
char tbuf[sizeof(signature) - 1];
Elf_Shdr *sh;
int error;
shsize = eh->e_shnum * sizeof(Elf_Shdr);
sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
IO_NODELOCKED);
if (error)
goto out;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
/*
* Identify candidates for the comment header;
* Header cannot have a load address, or flags and
* it must be large enough.
*/
if (s->sh_type != SHT_PROGBITS ||
s->sh_addr != 0 ||
s->sh_flags != 0 ||
s->sh_size < sizeof(signature) - 1)
continue;
error = exec_read(l, epp->ep_vp, s->sh_offset, tbuf,
sizeof(signature) - 1, IO_NODELOCKED);
if (error)
continue;
/*
* error is 0, if the signatures match we are done.
*/
DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf));
if (!memcmp(tbuf, signature, sizeof(signature) - 1)) {
error = 0;
goto out;
}
}
error = ENOEXEC;
out:
free(sh, M_TEMP);
return (error);
}
#endif
#ifdef LINUX_DEBUGLINK_SIGNATURE
/*
* Look for a .gnu_debuglink, specific to x86_64 interpreter
*/
int
ELFNAME2(linux,debuglink_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
{
Elf_Shdr *sh;
size_t shsize;
u_int shstrndx;
size_t i;
static const char signature[] = ".gnu_debuglink";
const size_t sigsz = sizeof(signature);
char tbuf[sizeof(signature)];
int error;
/* Load the section header table. */
shsize = eh->e_shnum * sizeof(Elf_Shdr);
sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
IO_NODELOCKED);
if (error)
goto out;
/* Now let's find the string table. If it does not exist, give up. */
shstrndx = eh->e_shstrndx;
if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
error = ENOEXEC;
goto out;
}
/* Check if any section has the name we're looking for. */
const off_t stroff = sh[shstrndx].sh_offset;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
if (s->sh_name + sigsz > sh[shstrndx].sh_size)
continue;
error = exec_read(l, epp->ep_vp, stroff + s->sh_name, tbuf,
sigsz, IO_NODELOCKED);
if (error)
goto out;
if (!memcmp(tbuf, signature, sigsz)) {
DPRINTF(("linux_debuglink_sig=%s\n", tbuf));
error = 0;
goto out;
}
}
error = ENOEXEC;
out:
free(sh, M_TEMP);
return (error);
}
#endif
#ifdef LINUX_GO_RT0_SIGNATURE
/*
* Look for a .gopclntab, specific to go binaries
* in it look for a symbol called _rt0_<cpu>_linux
*/
int
ELFNAME2(linux,go_rt0_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
{
Elf_Shdr *sh;
size_t shsize;
u_int shstrndx;
size_t i;
static const char signature[] = ".gopclntab";
const size_t sigsz = sizeof(signature);
char tbuf[sizeof(signature)], *tmp = NULL;
char mbuf[64];
const char *m;
int mlen;
int error;
/* Load the section header table. */
shsize = eh->e_shnum * sizeof(Elf_Shdr);
sh = malloc(shsize, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
IO_NODELOCKED);
if (error)
goto out;
/* Now let's find the string table. If it does not exist, give up. */
shstrndx = eh->e_shstrndx;
if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
error = ENOEXEC;
goto out;
}
/* Check if any section has the name we're looking for. */
const off_t stroff = sh[shstrndx].sh_offset;
for (i = 0; i < eh->e_shnum; i++) {
Elf_Shdr *s = &sh[i];
if (s->sh_name + sigsz > sh[shstrndx].sh_size)
continue;
error = exec_read(l, epp->ep_vp, stroff + s->sh_name, tbuf,
sigsz, IO_NODELOCKED);
if (error)
goto out;
if (!memcmp(tbuf, signature, sigsz)) {
DPRINTF(("linux_goplcntab_sig=%s\n", tbuf));
break;
}
}
if (i == eh->e_shnum) {
error = ENOEXEC;
goto out;
}
// Don't scan more than 1MB
if (sh[i].sh_size > 1024 * 1024)
sh[i].sh_size = 1024 * 1024;
tmp = malloc(sh[i].sh_size, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, sh[i].sh_offset, tmp,
sh[i].sh_size, IO_NODELOCKED);
if (error)
goto out;
#if (ELFSIZE == 32)
#ifdef LINUX_GO_RT0_SIGNATURE_ARCH32
m = LINUX_GO_RT0_SIGNATURE_ARCH32;
#else
extern struct netbsd32_machine32_hook_t netbsd32_machine32_hook;
MODULE_HOOK_CALL(netbsd32_machine32_hook, (), machine, m);
#endif
#else /* (ELFSIZE == 32) */
#ifdef LINUX_GO_RT0_SIGNATURE_ARCH64
m = LINUX_GO_RT0_SIGNATURE_ARCH64;
#else
m = machine;
#endif
#endif /* (ELFSIZE == 32) */
mlen = snprintf(mbuf, sizeof(mbuf), "_rt0_%s_linux", m);
if (memmem(tmp, sh[i].sh_size, mbuf, mlen) == NULL)
error = ENOEXEC;
else
DPRINTF(("linux_rt0_sig=%s\n", mbuf));
out:
if (tmp)
free(tmp, M_TEMP);
free(sh, M_TEMP);
return error;
}
#endif
int
ELFNAME2(linux,signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh, char *itp)
{
size_t i;
Elf_Phdr *ph;
size_t phsize;
int error;
static const char linux[] = "Linux";
if (eh->e_ident[EI_OSABI] == ELFOSABI_LINUX ||
memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
return 0;
phsize = eh->e_phnum * sizeof(Elf_Phdr);
ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, eh->e_phoff, ph, phsize,
IO_NODELOCKED);
if (error)
goto out;
for (i = 0; i < eh->e_phnum; i++) {
Elf_Phdr *ephp = &ph[i];
Elf_Nhdr *np;
u_int32_t *abi;
if (ephp->p_type != PT_NOTE ||
ephp->p_filesz > 1024 ||
ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
continue;
np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
error = exec_read(l, epp->ep_vp, ephp->p_offset, np,
ephp->p_filesz, IO_NODELOCKED);
if (error)
goto next;
if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
memcmp((void *)(np + 1), ELF_NOTE_ABI_NAME,
ELF_NOTE_ABI_NAMESZ))
goto next;
/* Make sure the OS is Linux. */
abi = (u_int32_t *)((char *)np + sizeof(Elf_Nhdr) +
np->n_namesz);
if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
error = 0;
else
error = ENOEXEC;
free(np, M_TEMP);
goto out;
next:
free(np, M_TEMP);
continue;
}
/* Check for certain interpreter names. */
if (itp) {
if (!strncmp(itp, "/lib/ld-linux", 13) ||
#if (ELFSIZE == 64)
!strncmp(itp, "/lib64/ld-linux", 15) ||
#endif
!strncmp(itp, "/lib/ld.so.", 11))
error = 0;
else
error = ENOEXEC;
goto out;
}
error = ENOEXEC;
out:
free(ph, M_TEMP);
return (error);
}
int
ELFNAME2(linux,probe)(struct lwp *l, struct exec_package *epp, void *eh,
char *itp, vaddr_t *pos)
{
int error;
if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
#ifdef LINUX_GCC_SIGNATURE
((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
#endif
#ifdef LINUX_ATEXIT_SIGNATURE
((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
#endif
#ifdef LINUX_DEBUGLINK_SIGNATURE
((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
#endif
#ifdef LINUX_GO_RT0_SIGNATURE
((error = ELFNAME2(linux,go_rt0_signature)(l, epp, eh)) != 0) &&
#endif
1) {
DPRINTF(("linux_probe: returning %d\n", error));
return error;
}
if (itp) {
if ((error = emul_find_interp(l, epp, itp)))
return (error);
}
epp->ep_flags |= EXEC_FORCEAUX;
DPRINTF(("linux_probe: returning 0\n"));
return 0;
}
#ifndef LINUX_MACHDEP_ELF_COPYARGS
/*
* Copy arguments onto the stack in the normal way, but add some
* extra information in case of dynamic binding.
*/
int
ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
struct ps_strings *arginfo, char **stackp, void *argp)
{
size_t len;
AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
struct elf_args *ap;
int error;
struct vattr *vap;
uint32_t randbytes[4];
if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
return error;
a = ai;
memset(ai, 0, sizeof(ai));
/*
* Push extra arguments used by glibc on the stack.
*/
a->a_type = AT_PAGESZ;
a->a_v = PAGE_SIZE;
a++;
if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
a->a_type = AT_PHDR;
a->a_v = ap->arg_phaddr;
a++;
a->a_type = AT_PHENT;
a->a_v = ap->arg_phentsize;
a++;
a->a_type = AT_PHNUM;
a->a_v = ap->arg_phnum;
a++;
a->a_type = AT_BASE;
a->a_v = ap->arg_interp;
a++;
a->a_type = AT_FLAGS;
a->a_v = 0;
a++;
a->a_type = AT_ENTRY;
a->a_v = ap->arg_entry;
a++;
exec_free_emul_arg(pack);
}
/* Linux-specific items */
a->a_type = LINUX_AT_CLKTCK;
a->a_v = hz;
a++;
vap = pack->ep_vap;
a->a_type = LINUX_AT_UID;
a->a_v = kauth_cred_getuid(l->l_cred);
a++;
a->a_type = LINUX_AT_EUID;
if (vap->va_mode & S_ISUID)
a->a_v = vap->va_uid;
else
a->a_v = kauth_cred_geteuid(l->l_cred);
a++;
a->a_type = LINUX_AT_GID;
a->a_v = kauth_cred_getgid(l->l_cred);
a++;
a->a_type = LINUX_AT_EGID;
if (vap->va_mode & S_ISGID)
a->a_v = vap->va_gid;
else
a->a_v = kauth_cred_getegid(l->l_cred);
a++;
a->a_type = LINUX_AT_RANDOM;
a->a_v = (Elf_Addr)(uintptr_t)*stackp;
a++;
a->a_type = AT_NULL;
a->a_v = 0;
a++;
randbytes[0] = cprng_strong32();
randbytes[1] = cprng_strong32();
randbytes[2] = cprng_strong32();
randbytes[3] = cprng_strong32();
len = sizeof(randbytes);
if ((error = copyout(randbytes, *stackp, len)) != 0)
return error;
*stackp += len;
len = (a - ai) * sizeof(AuxInfo);
KASSERT(len <= LINUX_ELF_AUX_ENTRIES * sizeof(AuxInfo));
if ((error = copyout(ai, *stackp, len)) != 0)
return error;
*stackp += len;
return 0;
}
#endif /* !LINUX_MACHDEP_ELF_COPYARGS */
|