linux_exec_elf32.c revision 1.94.12.5 1 1.94.12.5 pgoyette /* $NetBSD: linux_exec_elf32.c,v 1.94.12.5 2019/01/24 04:41:34 pgoyette Exp $ */
2 1.29 christos
3 1.29 christos /*-
4 1.51 manu * Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
5 1.29 christos * All rights reserved.
6 1.29 christos *
7 1.29 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.51 manu * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
9 1.51 manu * Emmanuel Dreyfus.
10 1.29 christos *
11 1.29 christos * Redistribution and use in source and binary forms, with or without
12 1.29 christos * modification, are permitted provided that the following conditions
13 1.29 christos * are met:
14 1.29 christos * 1. Redistributions of source code must retain the above copyright
15 1.29 christos * notice, this list of conditions and the following disclaimer.
16 1.29 christos * 2. Redistributions in binary form must reproduce the above copyright
17 1.29 christos * notice, this list of conditions and the following disclaimer in the
18 1.29 christos * documentation and/or other materials provided with the distribution.
19 1.29 christos *
20 1.29 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.29 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.29 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.29 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.29 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.29 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.29 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.29 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.29 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.29 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.29 christos * POSSIBILITY OF SUCH DAMAGE.
31 1.29 christos */
32 1.1 fvdl
33 1.1 fvdl /*
34 1.7 fvdl * based on exec_aout.c, sunos_exec.c and svr4_exec.c
35 1.1 fvdl */
36 1.54 lukem
37 1.54 lukem #include <sys/cdefs.h>
38 1.94.12.5 pgoyette __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.94.12.5 2019/01/24 04:41:34 pgoyette Exp $");
39 1.1 fvdl
40 1.31 erh #ifndef ELFSIZE
41 1.58 christos /* XXX should die */
42 1.58 christos #define ELFSIZE 32
43 1.31 erh #endif
44 1.17 cgd
45 1.1 fvdl #include <sys/param.h>
46 1.1 fvdl #include <sys/systm.h>
47 1.1 fvdl #include <sys/kernel.h>
48 1.1 fvdl #include <sys/proc.h>
49 1.1 fvdl #include <sys/malloc.h>
50 1.1 fvdl #include <sys/namei.h>
51 1.1 fvdl #include <sys/vnode.h>
52 1.13 christos #include <sys/mount.h>
53 1.25 christos #include <sys/exec.h>
54 1.8 fvdl #include <sys/exec_elf.h>
55 1.59 jdolecek #include <sys/stat.h>
56 1.72 elad #include <sys/kauth.h>
57 1.88 chs #include <sys/cprng.h>
58 1.94.12.2 pgoyette #include <sys/compat_stub.h>
59 1.1 fvdl
60 1.1 fvdl #include <sys/mman.h>
61 1.13 christos #include <sys/syscallargs.h>
62 1.1 fvdl
63 1.80 ad #include <sys/cpu.h>
64 1.1 fvdl #include <machine/reg.h>
65 1.1 fvdl
66 1.32 christos #include <compat/linux/common/linux_types.h>
67 1.32 christos #include <compat/linux/common/linux_signal.h>
68 1.32 christos #include <compat/linux/common/linux_util.h>
69 1.32 christos #include <compat/linux/common/linux_exec.h>
70 1.32 christos #include <compat/linux/common/linux_machdep.h>
71 1.82 ad #include <compat/linux/common/linux_ipc.h>
72 1.82 ad #include <compat/linux/common/linux_sem.h>
73 1.32 christos
74 1.32 christos #include <compat/linux/linux_syscallargs.h>
75 1.4 christos #include <compat/linux/linux_syscall.h>
76 1.94.12.5 pgoyette #include <compat/netbsd32/netbsd32.h>
77 1.31 erh
78 1.94.12.1 pgoyette #define LINUX_GO_RT0_SIGNATURE
79 1.94.12.1 pgoyette
80 1.56 christos #ifdef DEBUG_LINUX
81 1.56 christos #define DPRINTF(a) uprintf a
82 1.56 christos #else
83 1.94.12.1 pgoyette #define DPRINTF(a) do {} while (0)
84 1.56 christos #endif
85 1.56 christos
86 1.51 manu #ifdef LINUX_ATEXIT_SIGNATURE
87 1.51 manu /*
88 1.51 manu * On the PowerPC, statically linked Linux binaries are not recognized
89 1.51 manu * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
90 1.51 manu * binaries features a __libc_atexit ELF section. We therefore assume we
91 1.51 manu * have a Linux binary if we find this section.
92 1.51 manu */
93 1.71 manu int
94 1.86 matt ELFNAME2(linux,atexit_signature)(
95 1.86 matt struct lwp *l,
96 1.86 matt struct exec_package *epp,
97 1.86 matt Elf_Ehdr *eh)
98 1.51 manu {
99 1.91 maxv Elf_Shdr *sh;
100 1.51 manu size_t shsize;
101 1.91 maxv u_int shstrndx;
102 1.51 manu size_t i;
103 1.51 manu static const char signature[] = "__libc_atexit";
104 1.91 maxv const size_t sigsz = sizeof(signature);
105 1.91 maxv char tbuf[sizeof(signature)];
106 1.51 manu int error;
107 1.51 manu
108 1.91 maxv /* Load the section header table. */
109 1.51 manu shsize = eh->e_shnum * sizeof(Elf_Shdr);
110 1.51 manu sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
111 1.70 christos error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
112 1.51 manu if (error)
113 1.51 manu goto out;
114 1.51 manu
115 1.91 maxv /* Now let's find the string table. If it does not exist, give up. */
116 1.91 maxv shstrndx = eh->e_shstrndx;
117 1.91 maxv if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
118 1.51 manu error = ENOEXEC;
119 1.51 manu goto out;
120 1.51 manu }
121 1.51 manu
122 1.91 maxv /* Check if any section has the name we're looking for. */
123 1.91 maxv const off_t stroff = sh[shstrndx].sh_offset;
124 1.51 manu for (i = 0; i < eh->e_shnum; i++) {
125 1.51 manu Elf_Shdr *s = &sh[i];
126 1.91 maxv
127 1.91 maxv if (s->sh_name + sigsz > sh[shstrndx].sh_size)
128 1.91 maxv continue;
129 1.91 maxv
130 1.91 maxv error = exec_read_from(l, epp->ep_vp, stroff + s->sh_name, tbuf,
131 1.91 maxv sigsz);
132 1.91 maxv if (error)
133 1.91 maxv goto out;
134 1.91 maxv if (!memcmp(tbuf, signature, sigsz)) {
135 1.91 maxv DPRINTF(("linux_atexit_sig=%s\n", tbuf));
136 1.51 manu error = 0;
137 1.51 manu goto out;
138 1.51 manu }
139 1.51 manu }
140 1.51 manu error = ENOEXEC;
141 1.51 manu
142 1.51 manu out:
143 1.51 manu free(sh, M_TEMP);
144 1.51 manu return (error);
145 1.51 manu }
146 1.51 manu #endif
147 1.4 christos
148 1.31 erh #ifdef LINUX_GCC_SIGNATURE
149 1.14 christos /*
150 1.14 christos * Take advantage of the fact that all the linux binaries are compiled
151 1.14 christos * with gcc, and gcc sticks in the comment field a signature. Note that
152 1.14 christos * on SVR4 binaries, the gcc signature will follow the OS name signature,
153 1.14 christos * that will not be a problem. We don't bother to read in the string table,
154 1.14 christos * but we check all the progbits headers.
155 1.31 erh *
156 1.31 erh * XXX This only works in the i386. On the alpha (at least)
157 1.31 erh * XXX we have the same gcc signature which incorrectly identifies
158 1.31 erh * XXX NetBSD binaries as Linux.
159 1.14 christos */
160 1.71 manu int
161 1.86 matt ELFNAME2(linux,gcc_signature)(
162 1.86 matt struct lwp *l,
163 1.86 matt struct exec_package *epp,
164 1.86 matt Elf_Ehdr *eh)
165 1.14 christos {
166 1.50 mycroft size_t shsize;
167 1.14 christos size_t i;
168 1.14 christos static const char signature[] = "\0GCC: (GNU) ";
169 1.69 christos char tbuf[sizeof(signature) - 1];
170 1.31 erh Elf_Shdr *sh;
171 1.14 christos int error;
172 1.14 christos
173 1.50 mycroft shsize = eh->e_shnum * sizeof(Elf_Shdr);
174 1.31 erh sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
175 1.70 christos error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
176 1.50 mycroft if (error)
177 1.14 christos goto out;
178 1.14 christos
179 1.14 christos for (i = 0; i < eh->e_shnum; i++) {
180 1.31 erh Elf_Shdr *s = &sh[i];
181 1.14 christos
182 1.14 christos /*
183 1.14 christos * Identify candidates for the comment header;
184 1.15 christos * Header cannot have a load address, or flags and
185 1.14 christos * it must be large enough.
186 1.14 christos */
187 1.43 kleink if (s->sh_type != SHT_PROGBITS ||
188 1.14 christos s->sh_addr != 0 ||
189 1.14 christos s->sh_flags != 0 ||
190 1.14 christos s->sh_size < sizeof(signature) - 1)
191 1.14 christos continue;
192 1.14 christos
193 1.70 christos error = exec_read_from(l, epp->ep_vp, s->sh_offset, tbuf,
194 1.52 christos sizeof(signature) - 1);
195 1.50 mycroft if (error)
196 1.50 mycroft continue;
197 1.14 christos
198 1.15 christos /*
199 1.15 christos * error is 0, if the signatures match we are done.
200 1.15 christos */
201 1.69 christos DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf));
202 1.69 christos if (!memcmp(tbuf, signature, sizeof(signature) - 1)) {
203 1.50 mycroft error = 0;
204 1.14 christos goto out;
205 1.50 mycroft }
206 1.14 christos }
207 1.50 mycroft error = ENOEXEC;
208 1.14 christos
209 1.14 christos out:
210 1.14 christos free(sh, M_TEMP);
211 1.50 mycroft return (error);
212 1.14 christos }
213 1.31 erh #endif
214 1.31 erh
215 1.74 manu #ifdef LINUX_DEBUGLINK_SIGNATURE
216 1.74 manu /*
217 1.91 maxv * Look for a .gnu_debuglink, specific to x86_64 interpreter
218 1.74 manu */
219 1.74 manu int
220 1.83 cegger ELFNAME2(linux,debuglink_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
221 1.74 manu {
222 1.91 maxv Elf_Shdr *sh;
223 1.74 manu size_t shsize;
224 1.91 maxv u_int shstrndx;
225 1.74 manu size_t i;
226 1.74 manu static const char signature[] = ".gnu_debuglink";
227 1.91 maxv const size_t sigsz = sizeof(signature);
228 1.91 maxv char tbuf[sizeof(signature)];
229 1.74 manu int error;
230 1.74 manu
231 1.91 maxv /* Load the section header table. */
232 1.74 manu shsize = eh->e_shnum * sizeof(Elf_Shdr);
233 1.74 manu sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
234 1.74 manu error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
235 1.74 manu if (error)
236 1.74 manu goto out;
237 1.74 manu
238 1.91 maxv /* Now let's find the string table. If it does not exist, give up. */
239 1.91 maxv shstrndx = eh->e_shstrndx;
240 1.91 maxv if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
241 1.74 manu error = ENOEXEC;
242 1.74 manu goto out;
243 1.74 manu }
244 1.74 manu
245 1.91 maxv /* Check if any section has the name we're looking for. */
246 1.91 maxv const off_t stroff = sh[shstrndx].sh_offset;
247 1.74 manu for (i = 0; i < eh->e_shnum; i++) {
248 1.74 manu Elf_Shdr *s = &sh[i];
249 1.74 manu
250 1.91 maxv if (s->sh_name + sigsz > sh[shstrndx].sh_size)
251 1.91 maxv continue;
252 1.91 maxv
253 1.91 maxv error = exec_read_from(l, epp->ep_vp, stroff + s->sh_name, tbuf,
254 1.91 maxv sigsz);
255 1.91 maxv if (error)
256 1.91 maxv goto out;
257 1.91 maxv if (!memcmp(tbuf, signature, sigsz)) {
258 1.91 maxv DPRINTF(("linux_debuglink_sig=%s\n", tbuf));
259 1.74 manu error = 0;
260 1.74 manu goto out;
261 1.74 manu }
262 1.74 manu }
263 1.74 manu error = ENOEXEC;
264 1.74 manu
265 1.74 manu out:
266 1.74 manu free(sh, M_TEMP);
267 1.74 manu return (error);
268 1.74 manu }
269 1.74 manu #endif
270 1.74 manu
271 1.94.12.1 pgoyette #ifdef LINUX_GO_RT0_SIGNATURE
272 1.94.12.1 pgoyette /*
273 1.94.12.1 pgoyette * Look for a .gopclntab, specific to go binaries
274 1.94.12.1 pgoyette * in it look for a symbol called _rt0_<cpu>_linux
275 1.94.12.1 pgoyette */
276 1.94.12.1 pgoyette static int
277 1.94.12.1 pgoyette ELFNAME2(linux,go_rt0_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
278 1.94.12.1 pgoyette {
279 1.94.12.1 pgoyette Elf_Shdr *sh;
280 1.94.12.1 pgoyette size_t shsize;
281 1.94.12.1 pgoyette u_int shstrndx;
282 1.94.12.1 pgoyette size_t i;
283 1.94.12.1 pgoyette static const char signature[] = ".gopclntab";
284 1.94.12.1 pgoyette const size_t sigsz = sizeof(signature);
285 1.94.12.1 pgoyette char tbuf[sizeof(signature)], *tmp = NULL;
286 1.94.12.1 pgoyette char mbuf[64];
287 1.94.12.1 pgoyette const char *m;
288 1.94.12.1 pgoyette int mlen;
289 1.94.12.1 pgoyette int error;
290 1.94.12.1 pgoyette
291 1.94.12.1 pgoyette /* Load the section header table. */
292 1.94.12.1 pgoyette shsize = eh->e_shnum * sizeof(Elf_Shdr);
293 1.94.12.1 pgoyette sh = malloc(shsize, M_TEMP, M_WAITOK);
294 1.94.12.1 pgoyette error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
295 1.94.12.1 pgoyette if (error)
296 1.94.12.1 pgoyette goto out;
297 1.94.12.1 pgoyette
298 1.94.12.1 pgoyette /* Now let's find the string table. If it does not exist, give up. */
299 1.94.12.1 pgoyette shstrndx = eh->e_shstrndx;
300 1.94.12.1 pgoyette if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
301 1.94.12.1 pgoyette error = ENOEXEC;
302 1.94.12.1 pgoyette goto out;
303 1.94.12.1 pgoyette }
304 1.94.12.1 pgoyette
305 1.94.12.1 pgoyette /* Check if any section has the name we're looking for. */
306 1.94.12.1 pgoyette const off_t stroff = sh[shstrndx].sh_offset;
307 1.94.12.1 pgoyette for (i = 0; i < eh->e_shnum; i++) {
308 1.94.12.1 pgoyette Elf_Shdr *s = &sh[i];
309 1.94.12.1 pgoyette
310 1.94.12.1 pgoyette if (s->sh_name + sigsz > sh[shstrndx].sh_size)
311 1.94.12.1 pgoyette continue;
312 1.94.12.1 pgoyette
313 1.94.12.1 pgoyette error = exec_read_from(l, epp->ep_vp, stroff + s->sh_name, tbuf,
314 1.94.12.1 pgoyette sigsz);
315 1.94.12.1 pgoyette if (error)
316 1.94.12.1 pgoyette goto out;
317 1.94.12.1 pgoyette if (!memcmp(tbuf, signature, sigsz)) {
318 1.94.12.1 pgoyette DPRINTF(("linux_goplcntab_sig=%s\n", tbuf));
319 1.94.12.1 pgoyette break;
320 1.94.12.1 pgoyette }
321 1.94.12.1 pgoyette }
322 1.94.12.1 pgoyette
323 1.94.12.1 pgoyette if (i == eh->e_shnum) {
324 1.94.12.1 pgoyette error = ENOEXEC;
325 1.94.12.1 pgoyette goto out;
326 1.94.12.1 pgoyette }
327 1.94.12.1 pgoyette
328 1.94.12.1 pgoyette // Don't scan more than 1MB
329 1.94.12.1 pgoyette if (sh[i].sh_size > 1024 * 1024)
330 1.94.12.1 pgoyette sh[i].sh_size = 1024 * 1024;
331 1.94.12.1 pgoyette
332 1.94.12.1 pgoyette tmp = malloc(sh[i].sh_size, M_TEMP, M_WAITOK);
333 1.94.12.1 pgoyette error = exec_read_from(l, epp->ep_vp, sh[i].sh_offset, tmp,
334 1.94.12.1 pgoyette sh[i].sh_size);
335 1.94.12.1 pgoyette if (error)
336 1.94.12.1 pgoyette goto out;
337 1.94.12.1 pgoyette
338 1.94.12.1 pgoyette #if (ELFSIZE == 32)
339 1.94.12.4 pgoyette extern struct netbsd32_machine32_hook_t netbsd32_machine32_hook;
340 1.94.12.3 pgoyette MODULE_CALL_HOOK(netbsd32_machine32_hook, (), machine, m);
341 1.94.12.1 pgoyette #else
342 1.94.12.1 pgoyette m = machine;
343 1.94.12.1 pgoyette #endif
344 1.94.12.1 pgoyette mlen = snprintf(mbuf, sizeof(mbuf), "_rt0_%s_linux", m);
345 1.94.12.1 pgoyette if (memmem(tmp, sh[i].sh_size, mbuf, mlen) == NULL)
346 1.94.12.1 pgoyette error = ENOEXEC;
347 1.94.12.1 pgoyette else
348 1.94.12.1 pgoyette DPRINTF(("linux_rt0_sig=%s\n", mbuf));
349 1.94.12.1 pgoyette out:
350 1.94.12.1 pgoyette if (tmp)
351 1.94.12.1 pgoyette free(tmp, M_TEMP);
352 1.94.12.1 pgoyette free(sh, M_TEMP);
353 1.94.12.1 pgoyette return error;
354 1.94.12.1 pgoyette }
355 1.94.12.1 pgoyette #endif
356 1.94.12.1 pgoyette
357 1.71 manu int
358 1.83 cegger ELFNAME2(linux,signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh, char *itp)
359 1.31 erh {
360 1.31 erh size_t i;
361 1.31 erh Elf_Phdr *ph;
362 1.31 erh size_t phsize;
363 1.50 mycroft int error;
364 1.57 christos static const char linux[] = "Linux";
365 1.57 christos
366 1.94 uwe if (eh->e_ident[EI_OSABI] == ELFOSABI_LINUX ||
367 1.57 christos memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
368 1.57 christos return 0;
369 1.31 erh
370 1.31 erh phsize = eh->e_phnum * sizeof(Elf_Phdr);
371 1.31 erh ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
372 1.70 christos error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
373 1.50 mycroft if (error)
374 1.50 mycroft goto out;
375 1.31 erh
376 1.31 erh for (i = 0; i < eh->e_phnum; i++) {
377 1.31 erh Elf_Phdr *ephp = &ph[i];
378 1.50 mycroft Elf_Nhdr *np;
379 1.50 mycroft u_int32_t *abi;
380 1.31 erh
381 1.50 mycroft if (ephp->p_type != PT_NOTE ||
382 1.50 mycroft ephp->p_filesz > 1024 ||
383 1.50 mycroft ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
384 1.31 erh continue;
385 1.31 erh
386 1.50 mycroft np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
387 1.70 christos error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np,
388 1.52 christos ephp->p_filesz);
389 1.50 mycroft if (error)
390 1.50 mycroft goto next;
391 1.50 mycroft
392 1.50 mycroft if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
393 1.50 mycroft np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
394 1.50 mycroft np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
395 1.78 christos memcmp((void *)(np + 1), ELF_NOTE_ABI_NAME,
396 1.50 mycroft ELF_NOTE_ABI_NAMESZ))
397 1.50 mycroft goto next;
398 1.50 mycroft
399 1.50 mycroft /* Make sure the OS is Linux. */
400 1.78 christos abi = (u_int32_t *)((char *)np + sizeof(Elf_Nhdr) +
401 1.50 mycroft np->n_namesz);
402 1.50 mycroft if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
403 1.50 mycroft error = 0;
404 1.50 mycroft else
405 1.40 erh error = ENOEXEC;
406 1.50 mycroft free(np, M_TEMP);
407 1.50 mycroft goto out;
408 1.31 erh
409 1.50 mycroft next:
410 1.50 mycroft free(np, M_TEMP);
411 1.50 mycroft continue;
412 1.50 mycroft }
413 1.31 erh
414 1.91 maxv /* Check for certain interpreter names. */
415 1.66 drochner if (itp) {
416 1.50 mycroft if (!strncmp(itp, "/lib/ld-linux", 13) ||
417 1.68 manu #if (ELFSIZE == 64)
418 1.68 manu !strncmp(itp, "/lib64/ld-linux", 15) ||
419 1.68 manu #endif
420 1.50 mycroft !strncmp(itp, "/lib/ld.so.", 11))
421 1.50 mycroft error = 0;
422 1.50 mycroft else
423 1.50 mycroft error = ENOEXEC;
424 1.50 mycroft goto out;
425 1.31 erh }
426 1.31 erh
427 1.31 erh error = ENOEXEC;
428 1.50 mycroft out:
429 1.31 erh free(ph, M_TEMP);
430 1.50 mycroft return (error);
431 1.31 erh }
432 1.14 christos
433 1.8 fvdl int
434 1.75 christos ELFNAME2(linux,probe)(struct lwp *l, struct exec_package *epp, void *eh,
435 1.76 christos char *itp, vaddr_t *pos)
436 1.6 fvdl {
437 1.8 fvdl int error;
438 1.14 christos
439 1.70 christos if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
440 1.51 manu #ifdef LINUX_GCC_SIGNATURE
441 1.70 christos ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
442 1.53 manu #endif
443 1.51 manu #ifdef LINUX_ATEXIT_SIGNATURE
444 1.70 christos ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
445 1.53 manu #endif
446 1.74 manu #ifdef LINUX_DEBUGLINK_SIGNATURE
447 1.74 manu ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
448 1.74 manu #endif
449 1.94.12.1 pgoyette #ifdef LINUX_GO_RT0_SIGNATURE
450 1.94.12.1 pgoyette ((error = ELFNAME2(linux,go_rt0_signature)(l, epp, eh)) != 0) &&
451 1.94.12.1 pgoyette #endif
452 1.74 manu 1) {
453 1.74 manu DPRINTF(("linux_probe: returning %d\n", error));
454 1.51 manu return error;
455 1.74 manu }
456 1.6 fvdl
457 1.66 drochner if (itp) {
458 1.79 dsl if ((error = emul_find_interp(l, epp, itp)))
459 1.60 jdolecek return (error);
460 1.6 fvdl }
461 1.84 chs epp->ep_flags |= EXEC_FORCEAUX;
462 1.56 christos DPRINTF(("linux_probe: returning 0\n"));
463 1.6 fvdl return 0;
464 1.6 fvdl }
465 1.6 fvdl
466 1.59 jdolecek #ifndef LINUX_MACHDEP_ELF_COPYARGS
467 1.59 jdolecek /*
468 1.59 jdolecek * Copy arguments onto the stack in the normal way, but add some
469 1.59 jdolecek * extra information in case of dynamic binding.
470 1.59 jdolecek */
471 1.59 jdolecek int
472 1.70 christos ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
473 1.59 jdolecek struct ps_strings *arginfo, char **stackp, void *argp)
474 1.59 jdolecek {
475 1.59 jdolecek size_t len;
476 1.59 jdolecek AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
477 1.59 jdolecek struct elf_args *ap;
478 1.59 jdolecek int error;
479 1.59 jdolecek struct vattr *vap;
480 1.87 chs uint32_t randbytes[4];
481 1.59 jdolecek
482 1.70 christos if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
483 1.59 jdolecek return error;
484 1.59 jdolecek
485 1.59 jdolecek a = ai;
486 1.59 jdolecek
487 1.92 maxv memset(ai, 0, sizeof(ai));
488 1.92 maxv
489 1.59 jdolecek /*
490 1.59 jdolecek * Push extra arguments used by glibc on the stack.
491 1.59 jdolecek */
492 1.59 jdolecek
493 1.59 jdolecek a->a_type = AT_PAGESZ;
494 1.59 jdolecek a->a_v = PAGE_SIZE;
495 1.59 jdolecek a++;
496 1.59 jdolecek
497 1.59 jdolecek if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
498 1.59 jdolecek
499 1.59 jdolecek a->a_type = AT_PHDR;
500 1.59 jdolecek a->a_v = ap->arg_phaddr;
501 1.59 jdolecek a++;
502 1.59 jdolecek
503 1.59 jdolecek a->a_type = AT_PHENT;
504 1.59 jdolecek a->a_v = ap->arg_phentsize;
505 1.59 jdolecek a++;
506 1.59 jdolecek
507 1.59 jdolecek a->a_type = AT_PHNUM;
508 1.59 jdolecek a->a_v = ap->arg_phnum;
509 1.59 jdolecek a++;
510 1.59 jdolecek
511 1.59 jdolecek a->a_type = AT_BASE;
512 1.59 jdolecek a->a_v = ap->arg_interp;
513 1.59 jdolecek a++;
514 1.59 jdolecek
515 1.59 jdolecek a->a_type = AT_FLAGS;
516 1.59 jdolecek a->a_v = 0;
517 1.59 jdolecek a++;
518 1.59 jdolecek
519 1.59 jdolecek a->a_type = AT_ENTRY;
520 1.59 jdolecek a->a_v = ap->arg_entry;
521 1.59 jdolecek a++;
522 1.59 jdolecek
523 1.85 matt exec_free_emul_arg(pack);
524 1.59 jdolecek }
525 1.59 jdolecek
526 1.59 jdolecek /* Linux-specific items */
527 1.59 jdolecek a->a_type = LINUX_AT_CLKTCK;
528 1.59 jdolecek a->a_v = hz;
529 1.59 jdolecek a++;
530 1.59 jdolecek
531 1.59 jdolecek vap = pack->ep_vap;
532 1.59 jdolecek
533 1.59 jdolecek a->a_type = LINUX_AT_UID;
534 1.73 ad a->a_v = kauth_cred_getuid(l->l_cred);
535 1.59 jdolecek a++;
536 1.59 jdolecek
537 1.59 jdolecek a->a_type = LINUX_AT_EUID;
538 1.59 jdolecek if (vap->va_mode & S_ISUID)
539 1.59 jdolecek a->a_v = vap->va_uid;
540 1.59 jdolecek else
541 1.73 ad a->a_v = kauth_cred_geteuid(l->l_cred);
542 1.59 jdolecek a++;
543 1.59 jdolecek
544 1.59 jdolecek a->a_type = LINUX_AT_GID;
545 1.73 ad a->a_v = kauth_cred_getgid(l->l_cred);
546 1.59 jdolecek a++;
547 1.59 jdolecek
548 1.59 jdolecek a->a_type = LINUX_AT_EGID;
549 1.59 jdolecek if (vap->va_mode & S_ISGID)
550 1.59 jdolecek a->a_v = vap->va_gid;
551 1.59 jdolecek else
552 1.73 ad a->a_v = kauth_cred_getegid(l->l_cred);
553 1.59 jdolecek a++;
554 1.59 jdolecek
555 1.87 chs a->a_type = LINUX_AT_RANDOM;
556 1.93 matt a->a_v = (Elf_Addr)(uintptr_t)*stackp;
557 1.87 chs a++;
558 1.87 chs
559 1.59 jdolecek a->a_type = AT_NULL;
560 1.59 jdolecek a->a_v = 0;
561 1.59 jdolecek a++;
562 1.59 jdolecek
563 1.88 chs randbytes[0] = cprng_strong32();
564 1.88 chs randbytes[1] = cprng_strong32();
565 1.88 chs randbytes[2] = cprng_strong32();
566 1.88 chs randbytes[3] = cprng_strong32();
567 1.87 chs
568 1.87 chs len = sizeof(randbytes);
569 1.87 chs if ((error = copyout(randbytes, *stackp, len)) != 0)
570 1.87 chs return error;
571 1.87 chs *stackp += len;
572 1.87 chs
573 1.59 jdolecek len = (a - ai) * sizeof(AuxInfo);
574 1.90 njoly KASSERT(len <= LINUX_ELF_AUX_ENTRIES * sizeof(AuxInfo));
575 1.59 jdolecek if ((error = copyout(ai, *stackp, len)) != 0)
576 1.59 jdolecek return error;
577 1.59 jdolecek *stackp += len;
578 1.59 jdolecek
579 1.59 jdolecek return 0;
580 1.59 jdolecek }
581 1.59 jdolecek #endif /* !LINUX_MACHDEP_ELF_COPYARGS */
582