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