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->original_code = sa->code; 701 sa->args[0] = frame->tf_ebx; 702 sa->args[1] = frame->tf_ecx; 703 sa->args[2] = frame->tf_edx; 704 sa->args[3] = frame->tf_esi; 705 sa->args[4] = frame->tf_edi; 706 sa->args[5] = frame->tf_ebp; /* Unconfirmed */ 707 708 if (sa->code >= p->p_sysent->sv_size) 709 /* nosys */ 710 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 711 else 712 sa->callp = &p->p_sysent->sv_table[sa->code]; 713 714 td->td_retval[0] = 0; 715 td->td_retval[1] = frame->tf_edx; 716 717 return (0); 718 } 719 720 static void 721 linux_set_syscall_retval(struct thread *td, int error) 722 { 723 struct trapframe *frame = td->td_frame; 724 725 cpu_set_syscall_retval(td, error); 726 727 if (__predict_false(error != 0)) { 728 if (error != ERESTART && error != EJUSTRETURN) 729 frame->tf_eax = bsd_to_linux_errno(error); 730 } 731 } 732 733 static void 734 linux_set_fork_retval(struct thread *td) 735 { 736 struct trapframe *frame = td->td_frame; 737 738 frame->tf_eax = 0; 739 } 740 741 /* 742 * exec_setregs may initialize some registers differently than Linux 743 * does, thus potentially confusing Linux binaries. If necessary, we 744 * override the exec_setregs default(s) here. 745 */ 746 static void 747 linux_exec_setregs(struct thread *td, struct image_params *imgp, 748 uintptr_t stack) 749 { 750 struct pcb *pcb = td->td_pcb; 751 752 exec_setregs(td, imgp, stack); 753 754 /* Linux sets %gs to 0, we default to _udatasel. */ 755 pcb->pcb_gs = 0; 756 load_gs(0); 757 758 pcb->pcb_initial_npxcw = __LINUX_NPXCW__; 759 } 760 761 struct sysentvec linux_sysvec = { 762 .sv_size = LINUX_SYS_MAXSYSCALL, 763 .sv_table = linux_sysent, 764 .sv_fixup = linux_fixup, 765 .sv_sendsig = linux_sendsig, 766 .sv_sigcode = &_binary_linux_vdso_so_o_start, 767 .sv_szsigcode = &linux_szsigcode, 768 .sv_name = "Linux a.out", 769 .sv_coredump = NULL, 770 .sv_imgact_try = linux_exec_imgact_try, 771 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 772 .sv_minuser = VM_MIN_ADDRESS, 773 .sv_maxuser = VM_MAXUSER_ADDRESS, 774 .sv_usrstack = LINUX_USRSTACK, 775 .sv_psstrings = PS_STRINGS, 776 .sv_psstringssz = sizeof(struct ps_strings), 777 .sv_stackprot = VM_PROT_ALL, 778 .sv_copyout_strings = exec_copyout_strings, 779 .sv_setregs = linux_exec_setregs, 780 .sv_fixlimit = NULL, 781 .sv_maxssiz = NULL, 782 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 | 783 SV_SIG_DISCIGN | SV_SIG_WAITNDQ, 784 .sv_set_syscall_retval = linux_set_syscall_retval, 785 .sv_fetch_syscall_args = linux_fetch_syscall_args, 786 .sv_syscallnames = linux_syscallnames, 787 .sv_schedtail = linux_schedtail, 788 .sv_thread_detach = linux_thread_detach, 789 .sv_trap = NULL, 790 .sv_onexec = linux_on_exec_vmspace, 791 .sv_onexit = linux_on_exit, 792 .sv_ontdexit = linux_thread_dtor, 793 .sv_setid_allowed = &linux_setid_allowed_query, 794 .sv_set_fork_retval = linux_set_fork_retval, 795 }; 796 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec); 797 798 struct sysentvec elf_linux_sysvec = { 799 .sv_size = LINUX_SYS_MAXSYSCALL, 800 .sv_table = linux_sysent, 801 .sv_fixup = linux_fixup_elf, 802 .sv_sendsig = linux_sendsig, 803 .sv_sigcode = &_binary_linux_vdso_so_o_start, 804 .sv_szsigcode = &linux_szsigcode, 805 .sv_name = "Linux ELF32", 806 .sv_coredump = elf32_coredump, 807 .sv_elf_core_osabi = ELFOSABI_FREEBSD, 808 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR, 809 .sv_elf_core_prepare_notes = elf32_prepare_notes, 810 .sv_imgact_try = linux_exec_imgact_try, 811 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 812 .sv_minuser = VM_MIN_ADDRESS, 813 .sv_maxuser = VM_MAXUSER_ADDRESS, 814 .sv_usrstack = LINUX_USRSTACK, 815 .sv_psstrings = LINUX_PS_STRINGS, 816 .sv_psstringssz = sizeof(struct ps_strings), 817 .sv_stackprot = VM_PROT_ALL, 818 .sv_copyout_auxargs = linux_copyout_auxargs, 819 .sv_copyout_strings = linux_copyout_strings, 820 .sv_setregs = linux_exec_setregs, 821 .sv_fixlimit = NULL, 822 .sv_maxssiz = NULL, 823 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP | 824 SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP, 825 .sv_set_syscall_retval = linux_set_syscall_retval, 826 .sv_fetch_syscall_args = linux_fetch_syscall_args, 827 .sv_syscallnames = NULL, 828 .sv_shared_page_base = LINUX_SHAREDPAGE, 829 .sv_shared_page_len = PAGE_SIZE, 830 .sv_schedtail = linux_schedtail, 831 .sv_thread_detach = linux_thread_detach, 832 .sv_trap = NULL, 833 .sv_onexec = linux_on_exec_vmspace, 834 .sv_onexit = linux_on_exit, 835 .sv_ontdexit = linux_thread_dtor, 836 .sv_setid_allowed = &linux_setid_allowed_query, 837 .sv_set_fork_retval = linux_set_fork_retval, 838 }; 839 840 static int 841 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 842 { 843 int error = 0; 844 845 if (SV_PROC_FLAG(p, SV_SHP) != 0) 846 error = linux_map_vdso(p, linux_vdso_obj, 847 linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp); 848 if (error == 0) 849 linux_on_exec(p, imgp); 850 return (error); 851 } 852 853 /* 854 * linux_vdso_install() and linux_exec_sysvec_init() must be called 855 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 856 */ 857 static void 858 linux_exec_sysvec_init(void *param) 859 { 860 l_uintptr_t *ktimekeep_base, *ktsc_selector; 861 struct sysentvec *sv; 862 ptrdiff_t tkoff; 863 864 sv = param; 865 /* Fill timekeep_base */ 866 exec_sysvec_init(sv); 867 868 tkoff = kern_timekeep_base - linux_vdso_base; 869 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 870 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; 871 872 tkoff = kern_tsc_selector - linux_vdso_base; 873 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 874 *ktsc_selector = linux_vdso_tsc_selector_idx(); 875 if (bootverbose) 876 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector); 877 878 tkoff = kern_cpu_selector - linux_vdso_base; 879 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 880 *ktsc_selector = linux_vdso_cpu_selector_idx(); 881 if (bootverbose) 882 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector); 883 } 884 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 885 linux_exec_sysvec_init, &elf_linux_sysvec); 886 887 static void 888 linux_vdso_install(const void *param) 889 { 890 char *vdso_start = &_binary_linux_vdso_so_o_start; 891 char *vdso_end = &_binary_linux_vdso_so_o_end; 892 893 linux_szsigcode = vdso_end - vdso_start; 894 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 895 896 linux_vdso_base = LINUX_VDSOPAGE; 897 898 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 899 900 linux_vdso_obj = __elfN(linux_shared_page_init) 901 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 902 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 903 904 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 905 } 906 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 907 linux_vdso_install, NULL); 908 909 static void 910 linux_vdso_deinstall(const void *param) 911 { 912 913 __elfN(linux_shared_page_fini)(linux_vdso_obj, 914 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 915 } 916 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 917 linux_vdso_deinstall, NULL); 918 919 static void 920 linux_vdso_reloc(char *mapping, Elf_Addr offset) 921 { 922 const Elf_Shdr *shdr; 923 const Elf_Rel *rel; 924 const Elf_Ehdr *ehdr; 925 Elf_Addr *where; 926 Elf_Size rtype, symidx; 927 Elf_Addr addr, addend; 928 int i, relcnt; 929 930 MPASS(offset != 0); 931 932 relcnt = 0; 933 ehdr = (const Elf_Ehdr *)mapping; 934 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 935 for (i = 0; i < ehdr->e_shnum; i++) 936 { 937 switch (shdr[i].sh_type) { 938 case SHT_REL: 939 rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset); 940 relcnt = shdr[i].sh_size / sizeof(*rel); 941 break; 942 case SHT_RELA: 943 printf("Linux i386 vDSO: unexpected Rela section\n"); 944 break; 945 } 946 } 947 948 for (i = 0; i < relcnt; i++, rel++) { 949 where = (Elf_Addr *)(mapping + rel->r_offset); 950 addend = *where; 951 rtype = ELF_R_TYPE(rel->r_info); 952 symidx = ELF_R_SYM(rel->r_info); 953 954 switch (rtype) { 955 case R_386_NONE: /* none */ 956 break; 957 958 case R_386_RELATIVE: /* B + A */ 959 addr = (Elf_Addr)PTROUT(offset + addend); 960 if (*where != addr) 961 *where = addr; 962 break; 963 964 case R_386_IRELATIVE: 965 printf("Linux i386 vDSO: unexpected ifunc relocation, " 966 "symbol index %d\n", symidx); 967 break; 968 default: 969 printf("Linux i386 vDSO: unexpected relocation type %d, " 970 "symbol index %d\n", rtype, symidx); 971 } 972 } 973 } 974 975 static char GNU_ABI_VENDOR[] = "GNU"; 976 static int GNULINUX_ABI_DESC = 0; 977 978 static bool 979 linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 980 { 981 const Elf32_Word *desc; 982 uintptr_t p; 983 984 p = (uintptr_t)(note + 1); 985 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 986 987 desc = (const Elf32_Word *)p; 988 if (desc[0] != GNULINUX_ABI_DESC) 989 return (false); 990 991 /* 992 * For Linux we encode osrel using the Linux convention of 993 * (version << 16) | (major << 8) | (minor) 994 * See macro in linux_mib.h 995 */ 996 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 997 998 return (true); 999 } 1000 1001 static Elf_Brandnote linux_brandnote = { 1002 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 1003 .hdr.n_descsz = 16, /* XXX at least 16 */ 1004 .hdr.n_type = 1, 1005 .vendor = GNU_ABI_VENDOR, 1006 .flags = BN_TRANSLATE_OSREL, 1007 .trans_osrel = linux_trans_osrel 1008 }; 1009 1010 static Elf32_Brandinfo linux_brand = { 1011 .brand = ELFOSABI_LINUX, 1012 .machine = EM_386, 1013 .compat_3_brand = "Linux", 1014 .emul_path = linux_emul_path, 1015 .interp_path = "/lib/ld-linux.so.1", 1016 .sysvec = &elf_linux_sysvec, 1017 .interp_newpath = NULL, 1018 .brand_note = &linux_brandnote, 1019 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1020 }; 1021 1022 static Elf32_Brandinfo linux_glibc2brand = { 1023 .brand = ELFOSABI_LINUX, 1024 .machine = EM_386, 1025 .compat_3_brand = "Linux", 1026 .emul_path = linux_emul_path, 1027 .interp_path = "/lib/ld-linux.so.2", 1028 .sysvec = &elf_linux_sysvec, 1029 .interp_newpath = NULL, 1030 .brand_note = &linux_brandnote, 1031 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1032 }; 1033 1034 static Elf32_Brandinfo linux_muslbrand = { 1035 .brand = ELFOSABI_LINUX, 1036 .machine = EM_386, 1037 .compat_3_brand = "Linux", 1038 .emul_path = linux_emul_path, 1039 .interp_path = "/lib/ld-musl-i386.so.1", 1040 .sysvec = &elf_linux_sysvec, 1041 .interp_newpath = NULL, 1042 .brand_note = &linux_brandnote, 1043 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE | 1044 LINUX_BI_FUTEX_REQUEUE 1045 }; 1046 1047 Elf32_Brandinfo *linux_brandlist[] = { 1048 &linux_brand, 1049 &linux_glibc2brand, 1050 &linux_muslbrand, 1051 NULL 1052 }; 1053 1054 static int 1055 linux_elf_modevent(module_t mod, int type, void *data) 1056 { 1057 Elf32_Brandinfo **brandinfo; 1058 int error; 1059 struct linux_ioctl_handler **lihp; 1060 1061 error = 0; 1062 1063 switch(type) { 1064 case MOD_LOAD: 1065 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1066 ++brandinfo) 1067 if (elf32_insert_brand_entry(*brandinfo) < 0) 1068 error = EINVAL; 1069 if (error == 0) { 1070 SET_FOREACH(lihp, linux_ioctl_handler_set) 1071 linux_ioctl_register_handler(*lihp); 1072 linux_dev_shm_create(); 1073 linux_osd_jail_register(); 1074 stclohz = (stathz ? stathz : hz); 1075 if (bootverbose) 1076 printf("Linux ELF exec handler installed\n"); 1077 } else 1078 printf("cannot insert Linux ELF brand handler\n"); 1079 break; 1080 case MOD_UNLOAD: 1081 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1082 ++brandinfo) 1083 if (elf32_brand_inuse(*brandinfo)) 1084 error = EBUSY; 1085 if (error == 0) { 1086 for (brandinfo = &linux_brandlist[0]; 1087 *brandinfo != NULL; ++brandinfo) 1088 if (elf32_remove_brand_entry(*brandinfo) < 0) 1089 error = EINVAL; 1090 } 1091 if (error == 0) { 1092 SET_FOREACH(lihp, linux_ioctl_handler_set) 1093 linux_ioctl_unregister_handler(*lihp); 1094 linux_dev_shm_destroy(); 1095 linux_osd_jail_deregister(); 1096 if (bootverbose) 1097 printf("Linux ELF exec handler removed\n"); 1098 } else 1099 printf("Could not deinstall ELF interpreter entry\n"); 1100 break; 1101 default: 1102 return (EOPNOTSUPP); 1103 } 1104 return (error); 1105 } 1106 1107 static moduledata_t linux_elf_mod = { 1108 "linuxelf", 1109 linux_elf_modevent, 1110 0 1111 }; 1112 1113 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 1114 FEATURE(linux, "Linux 32bit support"); 1115