1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1994-1996 Søren Schmidt
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/proc.h>
43 #include <sys/stddef.h>
44 #include <sys/signalvar.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysctl.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
49 #include <sys/vnode.h>
50
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_param.h>
58
59 #include <machine/cpu.h>
60 #include <machine/cputypes.h>
61 #include <machine/md_var.h>
62 #include <machine/pcb.h>
63 #include <machine/trap.h>
64
65 #include <x86/linux/linux_x86.h>
66 #include <i386/linux/linux.h>
67 #include <i386/linux/linux_proto.h>
68 #include <compat/linux/linux_emul.h>
69 #include <compat/linux/linux_fork.h>
70 #include <compat/linux/linux_ioctl.h>
71 #include <compat/linux/linux_mib.h>
72 #include <compat/linux/linux_misc.h>
73 #include <compat/linux/linux_signal.h>
74 #include <compat/linux/linux_util.h>
75 #include <compat/linux/linux_vdso.h>
76
77 #include <x86/linux/linux_x86_sigframe.h>
78
79 MODULE_VERSION(linux, 1);
80
81 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
82 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE)
83 #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE)
84 /*
85 * PAGE_SIZE - the size
86 * of the native SHAREDPAGE
87 */
88 #define LINUX_USRSTACK LINUX_SHAREDPAGE
89 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings))
90
91 static int linux_szsigcode;
92 static vm_object_t linux_vdso_obj;
93 static char *linux_vdso_mapping;
94 extern char _binary_linux_vdso_so_o_start;
95 extern char _binary_linux_vdso_so_o_end;
96 static vm_offset_t linux_vdso_base;
97
98 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
99 extern const char *linux_syscallnames[];
100
101 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
102
103 static int linux_fixup(uintptr_t *stack_base,
104 struct image_params *iparams);
105 static int linux_fixup_elf(uintptr_t *stack_base,
106 struct image_params *iparams);
107 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
108 static void linux_exec_setregs(struct thread *td,
109 struct image_params *imgp, uintptr_t stack);
110 static void linux_exec_sysvec_init(void *param);
111 static int linux_on_exec_vmspace(struct proc *p,
112 struct image_params *imgp);
113 static int linux_copyout_strings(struct image_params *imgp,
114 uintptr_t *stack_base);
115 static void linux_set_fork_retval(struct thread *td);
116 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
117 static void linux_vdso_install(const void *param);
118 static void linux_vdso_deinstall(const void *param);
119 static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
120
121 LINUX_VDSO_SYM_CHAR(linux_platform);
122 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
123 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode);
124 LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode);
125 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
126 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
127 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
128
129 static int
130 linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
131 {
132 register_t *base, *argv, *envp;
133
134 base = (register_t *)*stack_base;
135 argv = base;
136 envp = base + (imgp->args->argc + 1);
137 base--;
138 suword(base, (intptr_t)envp);
139 base--;
140 suword(base, (intptr_t)argv);
141 base--;
142 suword(base, imgp->args->argc);
143 *stack_base = (uintptr_t)base;
144 return (0);
145 }
146
147 static int
148 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
149 {
150 Elf32_Auxargs *args;
151 Elf32_Auxinfo *argarray, *pos;
152 int error, issetugid;
153
154 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
155 args = (Elf32_Auxargs *)imgp->auxargs;
156 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
157 M_WAITOK | M_ZERO);
158
159 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
160 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
161 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
162
163 /*
164 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
165 * as it has appeared in the 2.4.0-rc7 first time.
166 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
167 * glibc falls back to the hard-coded CLK_TCK value when aux entry
168 * is not present.
169 * Also see linux_times() implementation.
170 */
171 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
172 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
173 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
174 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
175 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
176 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
177 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
178 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
179 AUXARGS_ENTRY(pos, AT_BASE, args->base);
180 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
181 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
182 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
183 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
184 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
185 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
186 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
187 if (imgp->execpathp != 0)
188 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
189 if (args->execfd != -1)
190 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
191 AUXARGS_ENTRY(pos, AT_NULL, 0);
192
193 free(imgp->auxargs, M_TEMP);
194 imgp->auxargs = NULL;
195 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
196
197 error = copyout(argarray, (void *)base,
198 sizeof(*argarray) * LINUX_AT_COUNT);
199 free(argarray, M_TEMP);
200 return (error);
201 }
202
203 static int
204 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
205 {
206 register_t *base;
207
208 base = (register_t *)*stack_base;
209 base--;
210 if (suword(base, (register_t)imgp->args->argc) == -1)
211 return (EFAULT);
212 *stack_base = (uintptr_t)base;
213 return (0);
214 }
215
216 /*
217 * Copied from kern/kern_exec.c
218 */
219 static int
220 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
221 {
222 int argc, envc, error;
223 char **vectp;
224 char *stringp;
225 uintptr_t destp, ustringp;
226 struct ps_strings *arginfo;
227 char canary[LINUX_AT_RANDOM_LEN];
228 size_t execpath_len;
229 struct proc *p;
230
231 p = imgp->proc;
232 arginfo = (struct ps_strings *)PROC_PS_STRINGS(p);
233 destp = (uintptr_t)arginfo;
234
235 if (imgp->execpath != NULL && imgp->auxargs != NULL) {
236 execpath_len = strlen(imgp->execpath) + 1;
237 destp -= execpath_len;
238 destp = rounddown2(destp, sizeof(void *));
239 imgp->execpathp = (void *)destp;
240 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
241 if (error != 0)
242 return (error);
243 }
244
245 /* Prepare the canary for SSP. */
246 arc4rand(canary, sizeof(canary), 0);
247 destp -= roundup(sizeof(canary), sizeof(void *));
248 imgp->canary = (void *)destp;
249 error = copyout(canary, imgp->canary, sizeof(canary));
250 if (error != 0)
251 return (error);
252
253 /* Allocate room for the argument and environment strings. */
254 destp -= ARG_MAX - imgp->args->stringspace;
255 destp = rounddown2(destp, sizeof(void *));
256 ustringp = destp;
257
258 if (imgp->auxargs) {
259 /*
260 * Allocate room on the stack for the ELF auxargs
261 * array. It has LINUX_AT_COUNT entries.
262 */
263 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
264 destp = rounddown2(destp, sizeof(void *));
265 }
266
267 vectp = (char **)destp;
268
269 /*
270 * Allocate room for the argv[] and env vectors including the
271 * terminating NULL pointers.
272 */
273 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
274
275 /* vectp also becomes our initial stack base. */
276 *stack_base = (uintptr_t)vectp;
277
278 stringp = imgp->args->begin_argv;
279 argc = imgp->args->argc;
280 envc = imgp->args->envc;
281
282 /* Copy out strings - arguments and environment. */
283 error = copyout(stringp, (void *)ustringp,
284 ARG_MAX - imgp->args->stringspace);
285 if (error != 0)
286 return (error);
287
288 /* Fill in "ps_strings" struct for ps, w, etc. */
289 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
290 suword(&arginfo->ps_nargvstr, argc) != 0)
291 return (EFAULT);
292
293 /* Fill in argument portion of vector table. */
294 for (; argc > 0; --argc) {
295 if (suword(vectp++, ustringp) != 0)
296 return (EFAULT);
297 while (*stringp++ != 0)
298 ustringp++;
299 ustringp++;
300 }
301
302 /* A null vector table pointer separates the argp's from the envp's. */
303 if (suword(vectp++, 0) != 0)
304 return (EFAULT);
305
306 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
307 suword(&arginfo->ps_nenvstr, envc) != 0)
308 return (EFAULT);
309
310 /* Fill in environment portion of vector table. */
311 for (; envc > 0; --envc) {
312 if (suword(vectp++, ustringp) != 0)
313 return (EFAULT);
314 while (*stringp++ != 0)
315 ustringp++;
316 ustringp++;
317 }
318
319 /* The end of the vector table is a null pointer. */
320 if (suword(vectp, 0) != 0)
321 return (EFAULT);
322
323 if (imgp->auxargs) {
324 vectp++;
325 error = imgp->sysent->sv_copyout_auxargs(imgp,
326 (uintptr_t)vectp);
327 if (error != 0)
328 return (error);
329 }
330
331 return (0);
332 }
333
334 static void
335 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
336 {
337 struct thread *td = curthread;
338 struct proc *p = td->td_proc;
339 struct sigacts *psp;
340 struct trapframe *regs;
341 struct l_rt_sigframe *fp, frame;
342 int sig, code;
343 int oonstack;
344
345 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
346 code = ksi->ksi_code;
347 PROC_LOCK_ASSERT(p, MA_OWNED);
348 psp = p->p_sigacts;
349 mtx_assert(&psp->ps_mtx, MA_OWNED);
350 regs = td->td_frame;
351 oonstack = sigonstack(regs->tf_esp);
352
353 /* Allocate space for the signal handler context. */
354 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
355 SIGISMEMBER(psp->ps_sigonstack, sig)) {
356 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
357 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
358 } else
359 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
360 mtx_unlock(&psp->ps_mtx);
361
362 /* Build the argument list for the signal handler. */
363 sig = bsd_to_linux_signal(sig);
364
365 bzero(&frame, sizeof(frame));
366
367 frame.sf_sig = sig;
368 frame.sf_siginfo = PTROUT(&fp->sf_si);
369 frame.sf_ucontext = PTROUT(&fp->sf_uc);
370
371 /* Fill in POSIX parts. */
372 siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
373
374 /* Build the signal context to be used by sigreturn. */
375 frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
376 frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
377 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
378 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
379 PROC_UNLOCK(p);
380
381 bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
382
383 frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask;
384 frame.sf_uc.uc_mcontext.sc_gs = rgs();
385 frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs;
386 frame.sf_uc.uc_mcontext.sc_es = regs->tf_es;
387 frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds;
388 frame.sf_uc.uc_mcontext.sc_edi = regs->tf_edi;
389 frame.sf_uc.uc_mcontext.sc_esi = regs->tf_esi;
390 frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_ebp;
391 frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_ebx;
392 frame.sf_uc.uc_mcontext.sc_esp = regs->tf_esp;
393 frame.sf_uc.uc_mcontext.sc_edx = regs->tf_edx;
394 frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_ecx;
395 frame.sf_uc.uc_mcontext.sc_eax = regs->tf_eax;
396 frame.sf_uc.uc_mcontext.sc_eip = regs->tf_eip;
397 frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;
398 frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags;
399 frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
400 frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss;
401 frame.sf_uc.uc_mcontext.sc_err = regs->tf_err;
402 frame.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
403 frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
404
405 if (copyout(&frame, fp, sizeof(frame)) != 0) {
406 /*
407 * Process has trashed its stack; give it an illegal
408 * instruction to halt it in its tracks.
409 */
410 PROC_LOCK(p);
411 sigexit(td, SIGILL);
412 }
413
414 /* Build context to run handler in. */
415 regs->tf_esp = PTROUT(fp);
416 regs->tf_eip = linux_vdso_rt_sigcode;
417 regs->tf_edi = PTROUT(catcher);
418 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
419 regs->tf_cs = _ucodesel;
420 regs->tf_ds = _udatasel;
421 regs->tf_es = _udatasel;
422 regs->tf_fs = _udatasel;
423 regs->tf_ss = _udatasel;
424 PROC_LOCK(p);
425 mtx_lock(&psp->ps_mtx);
426 }
427
428 /*
429 * Send an interrupt to process.
430 *
431 * Stack is set up to allow sigcode stored
432 * in u. to call routine, followed by kcall
433 * to sigreturn routine below. After sigreturn
434 * resets the signal mask, the stack, and the
435 * frame pointer, it returns to the user
436 * specified pc, psl.
437 */
438 static void
439 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
440 {
441 struct thread *td = curthread;
442 struct proc *p = td->td_proc;
443 struct sigacts *psp;
444 struct trapframe *regs;
445 struct l_sigframe *fp, frame;
446 l_sigset_t lmask;
447 int sig;
448 int oonstack;
449
450 PROC_LOCK_ASSERT(p, MA_OWNED);
451 psp = p->p_sigacts;
452 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
453 mtx_assert(&psp->ps_mtx, MA_OWNED);
454 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
455 /* Signal handler installed with SA_SIGINFO. */
456 linux_rt_sendsig(catcher, ksi, mask);
457 return;
458 }
459 regs = td->td_frame;
460 oonstack = sigonstack(regs->tf_esp);
461
462 /* Allocate space for the signal handler context. */
463 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
464 SIGISMEMBER(psp->ps_sigonstack, sig)) {
465 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
466 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
467 } else
468 fp = (struct l_sigframe *)regs->tf_esp - 1;
469 mtx_unlock(&psp->ps_mtx);
470 PROC_UNLOCK(p);
471
472 /* Build the argument list for the signal handler. */
473 sig = bsd_to_linux_signal(sig);
474
475 bzero(&frame, sizeof(frame));
476
477 frame.sf_sig = sig;
478 frame.sf_sigmask = *mask;
479 bsd_to_linux_sigset(mask, &lmask);
480
481 /* Build the signal context to be used by sigreturn. */
482 frame.sf_sc.sc_mask = lmask.__mask;
483 frame.sf_sc.sc_gs = rgs();
484 frame.sf_sc.sc_fs = regs->tf_fs;
485 frame.sf_sc.sc_es = regs->tf_es;
486 frame.sf_sc.sc_ds = regs->tf_ds;
487 frame.sf_sc.sc_edi = regs->tf_edi;
488 frame.sf_sc.sc_esi = regs->tf_esi;
489 frame.sf_sc.sc_ebp = regs->tf_ebp;
490 frame.sf_sc.sc_ebx = regs->tf_ebx;
491 frame.sf_sc.sc_esp = regs->tf_esp;
492 frame.sf_sc.sc_edx = regs->tf_edx;
493 frame.sf_sc.sc_ecx = regs->tf_ecx;
494 frame.sf_sc.sc_eax = regs->tf_eax;
495 frame.sf_sc.sc_eip = regs->tf_eip;
496 frame.sf_sc.sc_cs = regs->tf_cs;
497 frame.sf_sc.sc_eflags = regs->tf_eflags;
498 frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
499 frame.sf_sc.sc_ss = regs->tf_ss;
500 frame.sf_sc.sc_err = regs->tf_err;
501 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr;
502 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
503
504 if (copyout(&frame, fp, sizeof(frame)) != 0) {
505 /*
506 * Process has trashed its stack; give it an illegal
507 * instruction to halt it in its tracks.
508 */
509 PROC_LOCK(p);
510 sigexit(td, SIGILL);
511 }
512
513 /* Build context to run handler in. */
514 regs->tf_esp = PTROUT(fp);
515 regs->tf_eip = linux_vdso_sigcode;
516 regs->tf_edi = PTROUT(catcher);
517 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
518 regs->tf_cs = _ucodesel;
519 regs->tf_ds = _udatasel;
520 regs->tf_es = _udatasel;
521 regs->tf_fs = _udatasel;
522 regs->tf_ss = _udatasel;
523 PROC_LOCK(p);
524 mtx_lock(&psp->ps_mtx);
525 }
526
527 /*
528 * System call to cleanup state after a signal
529 * has been taken. Reset signal mask and
530 * stack state from context left by sendsig (above).
531 * Return to previous pc and psl as specified by
532 * context left by sendsig. Check carefully to
533 * make sure that the user has not modified the
534 * psl to gain improper privileges or to cause
535 * a machine fault.
536 */
537 int
538 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
539 {
540 struct l_sigframe frame;
541 struct trapframe *regs;
542 int eflags;
543 ksiginfo_t ksi;
544
545 regs = td->td_frame;
546
547 /*
548 * The trampoline code hands us the sigframe.
549 * It is unsafe to keep track of it ourselves, in the event that a
550 * program jumps out of a signal handler.
551 */
552 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
553 return (EFAULT);
554
555 /* Check for security violations. */
556 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
557 eflags = frame.sf_sc.sc_eflags;
558 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
559 return (EINVAL);
560
561 /*
562 * Don't allow users to load a valid privileged %cs. Let the
563 * hardware check for invalid selectors, excess privilege in
564 * other selectors, invalid %eip's and invalid %esp's.
565 */
566 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
567 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
568 ksiginfo_init_trap(&ksi);
569 ksi.ksi_signo = SIGBUS;
570 ksi.ksi_code = BUS_OBJERR;
571 ksi.ksi_trapno = T_PROTFLT;
572 ksi.ksi_addr = (void *)regs->tf_eip;
573 trapsignal(td, &ksi);
574 return (EINVAL);
575 }
576
577 kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
578
579 /* Restore signal context. */
580 /* %gs was restored by the trampoline. */
581 regs->tf_fs = frame.sf_sc.sc_fs;
582 regs->tf_es = frame.sf_sc.sc_es;
583 regs->tf_ds = frame.sf_sc.sc_ds;
584 regs->tf_edi = frame.sf_sc.sc_edi;
585 regs->tf_esi = frame.sf_sc.sc_esi;
586 regs->tf_ebp = frame.sf_sc.sc_ebp;
587 regs->tf_ebx = frame.sf_sc.sc_ebx;
588 regs->tf_edx = frame.sf_sc.sc_edx;
589 regs->tf_ecx = frame.sf_sc.sc_ecx;
590 regs->tf_eax = frame.sf_sc.sc_eax;
591 regs->tf_eip = frame.sf_sc.sc_eip;
592 regs->tf_cs = frame.sf_sc.sc_cs;
593 regs->tf_eflags = eflags;
594 regs->tf_esp = frame.sf_sc.sc_esp_at_signal;
595 regs->tf_ss = frame.sf_sc.sc_ss;
596
597 return (EJUSTRETURN);
598 }
599
600 /*
601 * System call to cleanup state after a signal
602 * has been taken. Reset signal mask and
603 * stack state from context left by rt_sendsig (above).
604 * Return to previous pc and psl as specified by
605 * context left by sendsig. Check carefully to
606 * make sure that the user has not modified the
607 * psl to gain improper privileges or to cause
608 * a machine fault.
609 */
610 int
611 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
612 {
613 struct l_ucontext uc;
614 struct l_sigcontext *context;
615 sigset_t bmask;
616 l_stack_t *lss;
617 stack_t ss;
618 struct trapframe *regs;
619 int eflags;
620 ksiginfo_t ksi;
621
622 regs = td->td_frame;
623
624 /*
625 * The trampoline code hands us the ucontext.
626 * It is unsafe to keep track of it ourselves, in the event that a
627 * program jumps out of a signal handler.
628 */
629 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
630 return (EFAULT);
631
632 context = &uc.uc_mcontext;
633
634 /* Check for security violations. */
635 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
636 eflags = context->sc_eflags;
637 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
638 return (EINVAL);
639
640 /*
641 * Don't allow users to load a valid privileged %cs. Let the
642 * hardware check for invalid selectors, excess privilege in
643 * other selectors, invalid %eip's and invalid %esp's.
644 */
645 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
646 if (!CS_SECURE(context->sc_cs)) {
647 ksiginfo_init_trap(&ksi);
648 ksi.ksi_signo = SIGBUS;
649 ksi.ksi_code = BUS_OBJERR;
650 ksi.ksi_trapno = T_PROTFLT;
651 ksi.ksi_addr = (void *)regs->tf_eip;
652 trapsignal(td, &ksi);
653 return (EINVAL);
654 }
655
656 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
657 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
658
659 /* Restore signal context. */
660 /* %gs was restored by the trampoline. */
661 regs->tf_fs = context->sc_fs;
662 regs->tf_es = context->sc_es;
663 regs->tf_ds = context->sc_ds;
664 regs->tf_edi = context->sc_edi;
665 regs->tf_esi = context->sc_esi;
666 regs->tf_ebp = context->sc_ebp;
667 regs->tf_ebx = context->sc_ebx;
668 regs->tf_edx = context->sc_edx;
669 regs->tf_ecx = context->sc_ecx;
670 regs->tf_eax = context->sc_eax;
671 regs->tf_eip = context->sc_eip;
672 regs->tf_cs = context->sc_cs;
673 regs->tf_eflags = eflags;
674 regs->tf_esp = context->sc_esp_at_signal;
675 regs->tf_ss = context->sc_ss;
676
677 /* Call sigaltstack & ignore results. */
678 lss = &uc.uc_stack;
679 ss.ss_sp = PTRIN(lss->ss_sp);
680 ss.ss_size = lss->ss_size;
681 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
682
683 (void)kern_sigaltstack(td, &ss, NULL);
684
685 return (EJUSTRETURN);
686 }
687
688 static int
689 linux_fetch_syscall_args(struct thread *td)
690 {
691 struct proc *p;
692 struct trapframe *frame;
693 struct syscall_args *sa;
694
695 p = td->td_proc;
696 frame = td->td_frame;
697 sa = &td->td_sa;
698
699 sa->code = frame->tf_eax;
700 sa->args[0] = frame->tf_ebx;
701 sa->args[1] = frame->tf_ecx;
702 sa->args[2] = frame->tf_edx;
703 sa->args[3] = frame->tf_esi;
704 sa->args[4] = frame->tf_edi;
705 sa->args[5] = frame->tf_ebp; /* Unconfirmed */
706
707 if (sa->code >= p->p_sysent->sv_size)
708 /* nosys */
709 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
710 else
711 sa->callp = &p->p_sysent->sv_table[sa->code];
712
713 td->td_retval[0] = 0;
714 td->td_retval[1] = frame->tf_edx;
715
716 return (0);
717 }
718
719 static void
720 linux_set_syscall_retval(struct thread *td, int error)
721 {
722 struct trapframe *frame = td->td_frame;
723
724 cpu_set_syscall_retval(td, error);
725
726 if (__predict_false(error != 0)) {
727 if (error != ERESTART && error != EJUSTRETURN)
728 frame->tf_eax = bsd_to_linux_errno(error);
729 }
730 }
731
732 static void
733 linux_set_fork_retval(struct thread *td)
734 {
735 struct trapframe *frame = td->td_frame;
736
737 frame->tf_eax = 0;
738 }
739
740 /*
741 * exec_setregs may initialize some registers differently than Linux
742 * does, thus potentially confusing Linux binaries. If necessary, we
743 * override the exec_setregs default(s) here.
744 */
745 static void
746 linux_exec_setregs(struct thread *td, struct image_params *imgp,
747 uintptr_t stack)
748 {
749 struct pcb *pcb = td->td_pcb;
750
751 exec_setregs(td, imgp, stack);
752
753 /* Linux sets %gs to 0, we default to _udatasel. */
754 pcb->pcb_gs = 0;
755 load_gs(0);
756
757 pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
758 }
759
760 struct sysentvec linux_sysvec = {
761 .sv_size = LINUX_SYS_MAXSYSCALL,
762 .sv_table = linux_sysent,
763 .sv_fixup = linux_fixup,
764 .sv_sendsig = linux_sendsig,
765 .sv_sigcode = &_binary_linux_vdso_so_o_start,
766 .sv_szsigcode = &linux_szsigcode,
767 .sv_name = "Linux a.out",
768 .sv_coredump = NULL,
769 .sv_imgact_try = linux_exec_imgact_try,
770 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
771 .sv_minuser = VM_MIN_ADDRESS,
772 .sv_maxuser = VM_MAXUSER_ADDRESS,
773 .sv_usrstack = LINUX_USRSTACK,
774 .sv_psstrings = PS_STRINGS,
775 .sv_psstringssz = sizeof(struct ps_strings),
776 .sv_stackprot = VM_PROT_ALL,
777 .sv_copyout_strings = exec_copyout_strings,
778 .sv_setregs = linux_exec_setregs,
779 .sv_fixlimit = NULL,
780 .sv_maxssiz = NULL,
781 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 |
782 SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
783 .sv_set_syscall_retval = linux_set_syscall_retval,
784 .sv_fetch_syscall_args = linux_fetch_syscall_args,
785 .sv_syscallnames = linux_syscallnames,
786 .sv_schedtail = linux_schedtail,
787 .sv_thread_detach = linux_thread_detach,
788 .sv_trap = NULL,
789 .sv_onexec = linux_on_exec_vmspace,
790 .sv_onexit = linux_on_exit,
791 .sv_ontdexit = linux_thread_dtor,
792 .sv_setid_allowed = &linux_setid_allowed_query,
793 .sv_set_fork_retval = linux_set_fork_retval,
794 };
795 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
796
797 struct sysentvec elf_linux_sysvec = {
798 .sv_size = LINUX_SYS_MAXSYSCALL,
799 .sv_table = linux_sysent,
800 .sv_fixup = linux_fixup_elf,
801 .sv_sendsig = linux_sendsig,
802 .sv_sigcode = &_binary_linux_vdso_so_o_start,
803 .sv_szsigcode = &linux_szsigcode,
804 .sv_name = "Linux ELF32",
805 .sv_coredump = elf32_coredump,
806 .sv_elf_core_osabi = ELFOSABI_FREEBSD,
807 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
808 .sv_elf_core_prepare_notes = elf32_prepare_notes,
809 .sv_imgact_try = linux_exec_imgact_try,
810 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
811 .sv_minuser = VM_MIN_ADDRESS,
812 .sv_maxuser = VM_MAXUSER_ADDRESS,
813 .sv_usrstack = LINUX_USRSTACK,
814 .sv_psstrings = LINUX_PS_STRINGS,
815 .sv_psstringssz = sizeof(struct ps_strings),
816 .sv_stackprot = VM_PROT_ALL,
817 .sv_copyout_auxargs = linux_copyout_auxargs,
818 .sv_copyout_strings = linux_copyout_strings,
819 .sv_setregs = linux_exec_setregs,
820 .sv_fixlimit = NULL,
821 .sv_maxssiz = NULL,
822 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP |
823 SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
824 .sv_set_syscall_retval = linux_set_syscall_retval,
825 .sv_fetch_syscall_args = linux_fetch_syscall_args,
826 .sv_syscallnames = NULL,
827 .sv_shared_page_base = LINUX_SHAREDPAGE,
828 .sv_shared_page_len = PAGE_SIZE,
829 .sv_schedtail = linux_schedtail,
830 .sv_thread_detach = linux_thread_detach,
831 .sv_trap = NULL,
832 .sv_onexec = linux_on_exec_vmspace,
833 .sv_onexit = linux_on_exit,
834 .sv_ontdexit = linux_thread_dtor,
835 .sv_setid_allowed = &linux_setid_allowed_query,
836 .sv_set_fork_retval = linux_set_fork_retval,
837 };
838
839 static int
840 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
841 {
842 int error = 0;
843
844 if (SV_PROC_FLAG(p, SV_SHP) != 0)
845 error = linux_map_vdso(p, linux_vdso_obj,
846 linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
847 if (error == 0)
848 linux_on_exec(p, imgp);
849 return (error);
850 }
851
852 /*
853 * linux_vdso_install() and linux_exec_sysvec_init() must be called
854 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
855 */
856 static void
857 linux_exec_sysvec_init(void *param)
858 {
859 l_uintptr_t *ktimekeep_base, *ktsc_selector;
860 struct sysentvec *sv;
861 ptrdiff_t tkoff;
862
863 sv = param;
864 /* Fill timekeep_base */
865 exec_sysvec_init(sv);
866
867 tkoff = kern_timekeep_base - linux_vdso_base;
868 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
869 *ktimekeep_base = sv->sv_timekeep_base;
870
871 tkoff = kern_tsc_selector - linux_vdso_base;
872 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
873 *ktsc_selector = linux_vdso_tsc_selector_idx();
874 if (bootverbose)
875 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
876
877 tkoff = kern_cpu_selector - linux_vdso_base;
878 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
879 *ktsc_selector = linux_vdso_cpu_selector_idx();
880 if (bootverbose)
881 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
882 }
883 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
884 linux_exec_sysvec_init, &elf_linux_sysvec);
885
886 static void
887 linux_vdso_install(const void *param)
888 {
889 char *vdso_start = &_binary_linux_vdso_so_o_start;
890 char *vdso_end = &_binary_linux_vdso_so_o_end;
891
892 linux_szsigcode = vdso_end - vdso_start;
893 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
894
895 linux_vdso_base = LINUX_VDSOPAGE;
896
897 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
898
899 linux_vdso_obj = __elfN(linux_shared_page_init)
900 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
901 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
902
903 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
904 }
905 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
906 linux_vdso_install, NULL);
907
908 static void
909 linux_vdso_deinstall(const void *param)
910 {
911
912 __elfN(linux_shared_page_fini)(linux_vdso_obj,
913 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
914 }
915 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
916 linux_vdso_deinstall, NULL);
917
918 static void
919 linux_vdso_reloc(char *mapping, Elf_Addr offset)
920 {
921 const Elf_Shdr *shdr;
922 const Elf_Rel *rel;
923 const Elf_Ehdr *ehdr;
924 Elf_Addr *where;
925 Elf_Size rtype, symidx;
926 Elf_Addr addr, addend;
927 int i, relcnt;
928
929 MPASS(offset != 0);
930
931 relcnt = 0;
932 ehdr = (const Elf_Ehdr *)mapping;
933 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
934 for (i = 0; i < ehdr->e_shnum; i++)
935 {
936 switch (shdr[i].sh_type) {
937 case SHT_REL:
938 rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
939 relcnt = shdr[i].sh_size / sizeof(*rel);
940 break;
941 case SHT_RELA:
942 printf("Linux i386 vDSO: unexpected Rela section\n");
943 break;
944 }
945 }
946
947 for (i = 0; i < relcnt; i++, rel++) {
948 where = (Elf_Addr *)(mapping + rel->r_offset);
949 addend = *where;
950 rtype = ELF_R_TYPE(rel->r_info);
951 symidx = ELF_R_SYM(rel->r_info);
952
953 switch (rtype) {
954 case R_386_NONE: /* none */
955 break;
956
957 case R_386_RELATIVE: /* B + A */
958 addr = (Elf_Addr)PTROUT(offset + addend);
959 if (*where != addr)
960 *where = addr;
961 break;
962
963 case R_386_IRELATIVE:
964 printf("Linux i386 vDSO: unexpected ifunc relocation, "
965 "symbol index %d\n", symidx);
966 break;
967 default:
968 printf("Linux i386 vDSO: unexpected relocation type %d, "
969 "symbol index %d\n", rtype, symidx);
970 }
971 }
972 }
973
974 static char GNU_ABI_VENDOR[] = "GNU";
975 static int GNULINUX_ABI_DESC = 0;
976
977 static bool
978 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
979 {
980 const Elf32_Word *desc;
981 uintptr_t p;
982
983 p = (uintptr_t)(note + 1);
984 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
985
986 desc = (const Elf32_Word *)p;
987 if (desc[0] != GNULINUX_ABI_DESC)
988 return (false);
989
990 /*
991 * For Linux we encode osrel using the Linux convention of
992 * (version << 16) | (major << 8) | (minor)
993 * See macro in linux_mib.h
994 */
995 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
996
997 return (true);
998 }
999
1000 static Elf_Brandnote linux_brandnote = {
1001 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
1002 .hdr.n_descsz = 16, /* XXX at least 16 */
1003 .hdr.n_type = 1,
1004 .vendor = GNU_ABI_VENDOR,
1005 .flags = BN_TRANSLATE_OSREL,
1006 .trans_osrel = linux_trans_osrel
1007 };
1008
1009 static Elf32_Brandinfo linux_brand = {
1010 .brand = ELFOSABI_LINUX,
1011 .machine = EM_386,
1012 .compat_3_brand = "Linux",
1013 .emul_path = linux_emul_path,
1014 .interp_path = "/lib/ld-linux.so.1",
1015 .sysvec = &elf_linux_sysvec,
1016 .interp_newpath = NULL,
1017 .brand_note = &linux_brandnote,
1018 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1019 };
1020
1021 static Elf32_Brandinfo linux_glibc2brand = {
1022 .brand = ELFOSABI_LINUX,
1023 .machine = EM_386,
1024 .compat_3_brand = "Linux",
1025 .emul_path = linux_emul_path,
1026 .interp_path = "/lib/ld-linux.so.2",
1027 .sysvec = &elf_linux_sysvec,
1028 .interp_newpath = NULL,
1029 .brand_note = &linux_brandnote,
1030 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1031 };
1032
1033 static Elf32_Brandinfo linux_muslbrand = {
1034 .brand = ELFOSABI_LINUX,
1035 .machine = EM_386,
1036 .compat_3_brand = "Linux",
1037 .emul_path = linux_emul_path,
1038 .interp_path = "/lib/ld-musl-i386.so.1",
1039 .sysvec = &elf_linux_sysvec,
1040 .interp_newpath = NULL,
1041 .brand_note = &linux_brandnote,
1042 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
1043 LINUX_BI_FUTEX_REQUEUE
1044 };
1045
1046 Elf32_Brandinfo *linux_brandlist[] = {
1047 &linux_brand,
1048 &linux_glibc2brand,
1049 &linux_muslbrand,
1050 NULL
1051 };
1052
1053 static int
1054 linux_elf_modevent(module_t mod, int type, void *data)
1055 {
1056 Elf32_Brandinfo **brandinfo;
1057 int error;
1058 struct linux_ioctl_handler **lihp;
1059
1060 error = 0;
1061
1062 switch(type) {
1063 case MOD_LOAD:
1064 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1065 ++brandinfo)
1066 if (elf32_insert_brand_entry(*brandinfo) < 0)
1067 error = EINVAL;
1068 if (error == 0) {
1069 SET_FOREACH(lihp, linux_ioctl_handler_set)
1070 linux_ioctl_register_handler(*lihp);
1071 linux_dev_shm_create();
1072 linux_osd_jail_register();
1073 stclohz = (stathz ? stathz : hz);
1074 if (bootverbose)
1075 printf("Linux ELF exec handler installed\n");
1076 } else
1077 printf("cannot insert Linux ELF brand handler\n");
1078 break;
1079 case MOD_UNLOAD:
1080 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1081 ++brandinfo)
1082 if (elf32_brand_inuse(*brandinfo))
1083 error = EBUSY;
1084 if (error == 0) {
1085 for (brandinfo = &linux_brandlist[0];
1086 *brandinfo != NULL; ++brandinfo)
1087 if (elf32_remove_brand_entry(*brandinfo) < 0)
1088 error = EINVAL;
1089 }
1090 if (error == 0) {
1091 SET_FOREACH(lihp, linux_ioctl_handler_set)
1092 linux_ioctl_unregister_handler(*lihp);
1093 linux_dev_shm_destroy();
1094 linux_osd_jail_deregister();
1095 if (bootverbose)
1096 printf("Linux ELF exec handler removed\n");
1097 } else
1098 printf("Could not deinstall ELF interpreter entry\n");
1099 break;
1100 default:
1101 return (EOPNOTSUPP);
1102 }
1103 return (error);
1104 }
1105
1106 static moduledata_t linux_elf_mod = {
1107 "linuxelf",
1108 linux_elf_modevent,
1109 0
1110 };
1111
1112 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1113 FEATURE(linux, "Linux 32bit support");
Cache object: e597ca66156587d31b9dd72ad642bfd5
|