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