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