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 666 if (sa->code >= p->p_sysent->sv_size) 667 /* nosys */ 668 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 669 else 670 sa->callp = &p->p_sysent->sv_table[sa->code]; 671 672 td->td_retval[0] = 0; 673 td->td_retval[1] = frame->tf_rdx; 674 675 return (0); 676 } 677 678 static void 679 linux32_set_syscall_retval(struct thread *td, int error) 680 { 681 struct trapframe *frame = td->td_frame; 682 683 cpu_set_syscall_retval(td, error); 684 685 if (__predict_false(error != 0)) { 686 if (error != ERESTART && error != EJUSTRETURN) 687 frame->tf_rax = bsd_to_linux_errno(error); 688 } 689 } 690 691 /* 692 * Clear registers on exec 693 * XXX copied from ia32_signal.c. 694 */ 695 static void 696 linux_exec_setregs(struct thread *td, struct image_params *imgp, 697 uintptr_t stack) 698 { 699 struct trapframe *regs = td->td_frame; 700 struct pcb *pcb = td->td_pcb; 701 register_t saved_rflags; 702 703 regs = td->td_frame; 704 pcb = td->td_pcb; 705 706 if (td->td_proc->p_md.md_ldt != NULL) 707 user_ldt_free(td); 708 709 critical_enter(); 710 wrmsr(MSR_FSBASE, 0); 711 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */ 712 pcb->pcb_fsbase = 0; 713 pcb->pcb_gsbase = 0; 714 critical_exit(); 715 pcb->pcb_initial_fpucw = __LINUX_NPXCW__; 716 717 saved_rflags = regs->tf_rflags & PSL_T; 718 bzero((char *)regs, sizeof(struct trapframe)); 719 regs->tf_rip = imgp->entry_addr; 720 regs->tf_rsp = stack; 721 regs->tf_rflags = PSL_USER | saved_rflags; 722 regs->tf_gs = _ugssel; 723 regs->tf_fs = _ufssel; 724 regs->tf_es = _udatasel; 725 regs->tf_ds = _udatasel; 726 regs->tf_ss = _udatasel; 727 regs->tf_flags = TF_HASSEGS; 728 regs->tf_cs = _ucode32sel; 729 regs->tf_rbx = (register_t)imgp->ps_strings; 730 731 x86_clear_dbregs(pcb); 732 733 fpstate_drop(td); 734 735 /* Do full restore on return so that we can change to a different %cs */ 736 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET); 737 } 738 739 /* 740 * XXX copied from ia32_sysvec.c. 741 */ 742 static int 743 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 744 { 745 int argc, envc, error; 746 u_int32_t *vectp; 747 char *stringp; 748 uintptr_t destp, ustringp; 749 struct linux32_ps_strings *arginfo; 750 char canary[LINUX_AT_RANDOM_LEN]; 751 size_t execpath_len; 752 753 /* Calculate string base and vector table pointers. */ 754 if (imgp->execpath != NULL && imgp->auxargs != NULL) 755 execpath_len = strlen(imgp->execpath) + 1; 756 else 757 execpath_len = 0; 758 759 arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS; 760 destp = (uintptr_t)arginfo; 761 762 if (execpath_len != 0) { 763 destp -= execpath_len; 764 destp = rounddown2(destp, sizeof(uint32_t)); 765 imgp->execpathp = (void *)destp; 766 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 767 if (error != 0) 768 return (error); 769 } 770 771 /* Prepare the canary for SSP. */ 772 arc4rand(canary, sizeof(canary), 0); 773 destp -= roundup(sizeof(canary), sizeof(uint32_t)); 774 imgp->canary = (void *)destp; 775 error = copyout(canary, imgp->canary, sizeof(canary)); 776 if (error != 0) 777 return (error); 778 779 /* Allocate room for the argument and environment strings. */ 780 destp -= ARG_MAX - imgp->args->stringspace; 781 destp = rounddown2(destp, sizeof(uint32_t)); 782 ustringp = destp; 783 784 if (imgp->auxargs) { 785 /* 786 * Allocate room on the stack for the ELF auxargs 787 * array. It has LINUX_AT_COUNT entries. 788 */ 789 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo); 790 destp = rounddown2(destp, sizeof(uint32_t)); 791 } 792 793 vectp = (uint32_t *)destp; 794 795 /* 796 * Allocate room for the argv[] and env vectors including the 797 * terminating NULL pointers. 798 */ 799 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 800 801 /* vectp also becomes our initial stack base. */ 802 *stack_base = (uintptr_t)vectp; 803 804 stringp = imgp->args->begin_argv; 805 argc = imgp->args->argc; 806 envc = imgp->args->envc; 807 808 /* Copy out strings - arguments and environment. */ 809 error = copyout(stringp, (void *)ustringp, 810 ARG_MAX - imgp->args->stringspace); 811 if (error != 0) 812 return (error); 813 814 /* Fill in "ps_strings" struct for ps, w, etc. */ 815 if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 || 816 suword32(&arginfo->ps_nargvstr, argc) != 0) 817 return (EFAULT); 818 819 /* Fill in argument portion of vector table. */ 820 for (; argc > 0; --argc) { 821 if (suword32(vectp++, ustringp) != 0) 822 return (EFAULT); 823 while (*stringp++ != 0) 824 ustringp++; 825 ustringp++; 826 } 827 828 /* A null vector table pointer separates the argp's from the envp's. */ 829 if (suword32(vectp++, 0) != 0) 830 return (EFAULT); 831 832 if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 || 833 suword32(&arginfo->ps_nenvstr, envc) != 0) 834 return (EFAULT); 835 836 /* Fill in environment portion of vector table. */ 837 for (; envc > 0; --envc) { 838 if (suword32(vectp++, ustringp) != 0) 839 return (EFAULT); 840 while (*stringp++ != 0) 841 ustringp++; 842 ustringp++; 843 } 844 845 /* The end of the vector table is a null pointer. */ 846 if (suword32(vectp, 0) != 0) 847 return (EFAULT); 848 849 if (imgp->auxargs) { 850 vectp++; 851 error = imgp->sysent->sv_copyout_auxargs(imgp, 852 (uintptr_t)vectp); 853 if (error != 0) 854 return (error); 855 } 856 857 return (0); 858 } 859 860 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 861 "32-bit Linux emulation"); 862 863 static u_long linux32_maxdsiz = LINUX32_MAXDSIZ; 864 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW, 865 &linux32_maxdsiz, 0, ""); 866 static u_long linux32_maxssiz = LINUX32_MAXSSIZ; 867 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW, 868 &linux32_maxssiz, 0, ""); 869 static u_long linux32_maxvmem = LINUX32_MAXVMEM; 870 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW, 871 &linux32_maxvmem, 0, ""); 872 873 static void 874 linux32_fixlimit(struct rlimit *rl, int which) 875 { 876 877 switch (which) { 878 case RLIMIT_DATA: 879 if (linux32_maxdsiz != 0) { 880 if (rl->rlim_cur > linux32_maxdsiz) 881 rl->rlim_cur = linux32_maxdsiz; 882 if (rl->rlim_max > linux32_maxdsiz) 883 rl->rlim_max = linux32_maxdsiz; 884 } 885 break; 886 case RLIMIT_STACK: 887 if (linux32_maxssiz != 0) { 888 if (rl->rlim_cur > linux32_maxssiz) 889 rl->rlim_cur = linux32_maxssiz; 890 if (rl->rlim_max > linux32_maxssiz) 891 rl->rlim_max = linux32_maxssiz; 892 } 893 break; 894 case RLIMIT_VMEM: 895 if (linux32_maxvmem != 0) { 896 if (rl->rlim_cur > linux32_maxvmem) 897 rl->rlim_cur = linux32_maxvmem; 898 if (rl->rlim_max > linux32_maxvmem) 899 rl->rlim_max = linux32_maxvmem; 900 } 901 break; 902 } 903 } 904 905 struct sysentvec elf_linux_sysvec = { 906 .sv_size = LINUX32_SYS_MAXSYSCALL, 907 .sv_table = linux32_sysent, 908 .sv_transtrap = linux_translate_traps, 909 .sv_fixup = linux_fixup_elf, 910 .sv_sendsig = linux_sendsig, 911 .sv_sigcode = &_binary_linux32_locore_o_start, 912 .sv_szsigcode = &linux_szsigcode, 913 .sv_name = "Linux ELF32", 914 .sv_coredump = elf32_coredump, 915 .sv_elf_core_osabi = ELFOSABI_FREEBSD, 916 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR, 917 .sv_elf_core_prepare_notes = elf32_prepare_notes, 918 .sv_imgact_try = linux_exec_imgact_try, 919 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 920 .sv_minuser = VM_MIN_ADDRESS, 921 .sv_maxuser = LINUX32_MAXUSER, 922 .sv_usrstack = LINUX32_USRSTACK, 923 .sv_psstrings = LINUX32_PS_STRINGS, 924 .sv_stackprot = VM_PROT_ALL, 925 .sv_copyout_auxargs = linux_copyout_auxargs, 926 .sv_copyout_strings = linux_copyout_strings, 927 .sv_setregs = linux_exec_setregs, 928 .sv_fixlimit = linux32_fixlimit, 929 .sv_maxssiz = &linux32_maxssiz, 930 .sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP | 931 SV_SIG_DISCIGN | SV_SIG_WAITNDQ, 932 .sv_set_syscall_retval = linux32_set_syscall_retval, 933 .sv_fetch_syscall_args = linux32_fetch_syscall_args, 934 .sv_syscallnames = NULL, 935 .sv_shared_page_base = LINUX32_SHAREDPAGE, 936 .sv_shared_page_len = PAGE_SIZE, 937 .sv_schedtail = linux_schedtail, 938 .sv_thread_detach = linux_thread_detach, 939 .sv_trap = NULL, 940 .sv_onexec = linux_on_exec, 941 .sv_onexit = linux_on_exit, 942 .sv_ontdexit = linux_thread_dtor, 943 .sv_setid_allowed = &linux_setid_allowed_query, 944 }; 945 946 static void 947 linux_vdso_install(void *param) 948 { 949 950 linux_szsigcode = (&_binary_linux32_locore_o_end - 951 &_binary_linux32_locore_o_start); 952 953 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 954 panic("Linux invalid vdso size\n"); 955 956 __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 957 958 linux_shared_page_obj = __elfN(linux_shared_page_init) 959 (&linux_shared_page_mapping); 960 961 __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 962 963 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping, 964 linux_szsigcode); 965 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 966 } 967 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 968 linux_vdso_install, NULL); 969 970 static void 971 linux_vdso_deinstall(void *param) 972 { 973 974 __elfN(linux_shared_page_fini)(linux_shared_page_obj, 975 linux_shared_page_mapping); 976 } 977 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 978 linux_vdso_deinstall, NULL); 979 980 static char GNU_ABI_VENDOR[] = "GNU"; 981 static int GNULINUX_ABI_DESC = 0; 982 983 static bool 984 linux32_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 linux32_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 = linux32_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 = &linux32_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 = &linux32_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 = &linux32_brandnote, 1048 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1049 }; 1050 1051 Elf32_Brandinfo *linux_brandlist[] = { 1052 &linux_brand, 1053 &linux_glibc2brand, 1054 &linux_muslbrand, 1055 NULL 1056 }; 1057 1058 static int 1059 linux_elf_modevent(module_t mod, int type, void *data) 1060 { 1061 Elf32_Brandinfo **brandinfo; 1062 int error; 1063 struct linux_ioctl_handler **lihp; 1064 1065 error = 0; 1066 1067 switch(type) { 1068 case MOD_LOAD: 1069 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1070 ++brandinfo) 1071 if (elf32_insert_brand_entry(*brandinfo) < 0) 1072 error = EINVAL; 1073 if (error == 0) { 1074 SET_FOREACH(lihp, linux_ioctl_handler_set) 1075 linux32_ioctl_register_handler(*lihp); 1076 stclohz = (stathz ? stathz : hz); 1077 if (bootverbose) 1078 printf("Linux ELF exec handler installed\n"); 1079 } else 1080 printf("cannot insert Linux ELF brand handler\n"); 1081 break; 1082 case MOD_UNLOAD: 1083 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1084 ++brandinfo) 1085 if (elf32_brand_inuse(*brandinfo)) 1086 error = EBUSY; 1087 if (error == 0) { 1088 for (brandinfo = &linux_brandlist[0]; 1089 *brandinfo != NULL; ++brandinfo) 1090 if (elf32_remove_brand_entry(*brandinfo) < 0) 1091 error = EINVAL; 1092 } 1093 if (error == 0) { 1094 SET_FOREACH(lihp, linux_ioctl_handler_set) 1095 linux32_ioctl_unregister_handler(*lihp); 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 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1); 1115 FEATURE(linux, "Linux 32bit support"); 1116