1 /*- 2 * Copyright (c) 2004 Tim J. Robbins 3 * Copyright (c) 2002 Doug Rabson 4 * Copyright (c) 2000 Marcel Moolenaar 5 * All rights reserved. 6 * Copyright (c) 2013 Dmitry Chagin <dchagin@FreeBSD.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer 13 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/capsicum.h> 37 #include <sys/clock.h> 38 #include <sys/dirent.h> 39 #include <sys/fcntl.h> 40 #include <sys/file.h> 41 #include <sys/filedesc.h> 42 #include <sys/imgact.h> 43 #include <sys/kernel.h> 44 #include <sys/ktr.h> 45 #include <sys/limits.h> 46 #include <sys/lock.h> 47 #include <sys/malloc.h> 48 #include <sys/mman.h> 49 #include <sys/mutex.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/ptrace.h> 53 #include <sys/resource.h> 54 #include <sys/resourcevar.h> 55 #include <sys/sched.h> 56 #include <sys/syscallsubr.h> 57 #include <sys/sysproto.h> 58 #include <sys/systm.h> 59 #include <sys/unistd.h> 60 #include <sys/vnode.h> 61 #include <sys/wait.h> 62 63 #include <security/mac/mac_framework.h> 64 65 #include <ufs/ufs/extattr.h> 66 #include <ufs/ufs/quota.h> 67 #include <ufs/ufs/ufsmount.h> 68 69 #include <machine/frame.h> 70 #include <machine/md_var.h> 71 #include <machine/pcb.h> 72 #include <machine/psl.h> 73 #include <machine/segments.h> 74 #include <machine/specialreg.h> 75 76 #include <vm/pmap.h> 77 #include <vm/vm.h> 78 #include <vm/vm_extern.h> 79 #include <vm/vm_kern.h> 80 #include <vm/vm_map.h> 81 82 #include <x86/ifunc.h> 83 #include <x86/reg.h> 84 #include <x86/sysarch.h> 85 86 #include <security/audit/audit.h> 87 88 #include <amd64/linux/linux.h> 89 #include <amd64/linux/linux_proto.h> 90 #include <compat/linux/linux_emul.h> 91 #include <compat/linux/linux_file.h> 92 #include <compat/linux/linux_fork.h> 93 #include <compat/linux/linux_ipc.h> 94 #include <compat/linux/linux_misc.h> 95 #include <compat/linux/linux_mmap.h> 96 #include <compat/linux/linux_signal.h> 97 #include <compat/linux/linux_util.h> 98 99 #define LINUX_ARCH_AMD64 0xc000003e 100 101 int 102 linux_execve(struct thread *td, struct linux_execve_args *args) 103 { 104 struct image_args eargs; 105 char *path; 106 int error; 107 108 LINUX_CTR(execve); 109 110 if (!LUSECONVPATH(td)) { 111 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE, 112 args->argp, args->envp); 113 } else { 114 LCONVPATHEXIST(args->path, &path); 115 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp, 116 args->envp); 117 LFREEPATH(path); 118 } 119 if (error == 0) 120 error = linux_common_execve(td, &eargs); 121 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 122 return (error); 123 } 124 125 int 126 linux_set_upcall(struct thread *td, register_t stack) 127 { 128 129 if (stack) 130 td->td_frame->tf_rsp = stack; 131 132 /* 133 * The newly created Linux thread returns 134 * to the user space by the same path that a parent does. 135 */ 136 td->td_frame->tf_rax = 0; 137 return (0); 138 } 139 140 int 141 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 142 { 143 144 return (linux_mmap_common(td, args->addr, args->len, args->prot, 145 args->flags, args->fd, args->pgoff)); 146 } 147 148 int 149 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 150 { 151 152 return (linux_mprotect_common(td, uap->addr, uap->len, uap->prot)); 153 } 154 155 int 156 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 157 { 158 159 return (linux_madvise_common(td, uap->addr, uap->len, uap->behav)); 160 } 161 162 int 163 linux_iopl(struct thread *td, struct linux_iopl_args *args) 164 { 165 int error; 166 167 LINUX_CTR(iopl); 168 169 if (args->level > 3) 170 return (EINVAL); 171 if ((error = priv_check(td, PRIV_IO)) != 0) 172 return (error); 173 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 174 return (error); 175 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 176 (args->level * (PSL_IOPL / 3)); 177 178 return (0); 179 } 180 181 int 182 linux_pause(struct thread *td, struct linux_pause_args *args) 183 { 184 struct proc *p = td->td_proc; 185 sigset_t sigmask; 186 187 LINUX_CTR(pause); 188 189 PROC_LOCK(p); 190 sigmask = td->td_sigmask; 191 PROC_UNLOCK(p); 192 return (kern_sigsuspend(td, sigmask)); 193 } 194 195 int 196 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args) 197 { 198 unsigned long long cet[3]; 199 struct pcb *pcb; 200 int error; 201 202 pcb = td->td_pcb; 203 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr); 204 205 switch (args->code) { 206 case LINUX_ARCH_SET_GS: 207 if (args->addr < VM_MAXUSER_ADDRESS) { 208 update_pcb_bases(pcb); 209 pcb->pcb_gsbase = args->addr; 210 td->td_frame->tf_gs = _ugssel; 211 error = 0; 212 } else 213 error = EPERM; 214 break; 215 case LINUX_ARCH_SET_FS: 216 if (args->addr < VM_MAXUSER_ADDRESS) { 217 update_pcb_bases(pcb); 218 pcb->pcb_fsbase = args->addr; 219 td->td_frame->tf_fs = _ufssel; 220 error = 0; 221 } else 222 error = EPERM; 223 break; 224 case LINUX_ARCH_GET_FS: 225 error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr), 226 sizeof(args->addr)); 227 break; 228 case LINUX_ARCH_GET_GS: 229 error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr), 230 sizeof(args->addr)); 231 break; 232 case LINUX_ARCH_CET_STATUS: 233 memset(cet, 0, sizeof(cet)); 234 error = copyout(&cet, PTRIN(args->addr), sizeof(cet)); 235 break; 236 default: 237 linux_msg(td, "unsupported arch_prctl code %#x", args->code); 238 error = EINVAL; 239 } 240 return (error); 241 } 242 243 int 244 linux_set_cloned_tls(struct thread *td, void *desc) 245 { 246 struct pcb *pcb; 247 248 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS) 249 return (EPERM); 250 251 pcb = td->td_pcb; 252 update_pcb_bases(pcb); 253 pcb->pcb_fsbase = (register_t)desc; 254 td->td_frame->tf_fs = _ufssel; 255 256 return (0); 257 } 258 259 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 260 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval); 261 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *)) 262 { 263 264 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 265 futex_xchgl_smap : futex_xchgl_nosmap); 266 } 267 268 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 269 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval); 270 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *)) 271 { 272 273 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 274 futex_addl_smap : futex_addl_nosmap); 275 } 276 277 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 278 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval); 279 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *)) 280 { 281 282 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 283 futex_orl_smap : futex_orl_nosmap); 284 } 285 286 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 287 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval); 288 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *)) 289 { 290 291 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 292 futex_andl_smap : futex_andl_nosmap); 293 } 294 295 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 296 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval); 297 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *)) 298 { 299 300 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 301 futex_xorl_smap : futex_xorl_nosmap); 302 } 303 304 void 305 bsd_to_linux_regset(const struct reg *b_reg, struct linux_pt_regset *l_regset) 306 { 307 308 l_regset->r15 = b_reg->r_r15; 309 l_regset->r14 = b_reg->r_r14; 310 l_regset->r13 = b_reg->r_r13; 311 l_regset->r12 = b_reg->r_r12; 312 l_regset->rbp = b_reg->r_rbp; 313 l_regset->rbx = b_reg->r_rbx; 314 l_regset->r11 = b_reg->r_r11; 315 l_regset->r10 = b_reg->r_r10; 316 l_regset->r9 = b_reg->r_r9; 317 l_regset->r8 = b_reg->r_r8; 318 l_regset->rax = b_reg->r_rax; 319 l_regset->rcx = b_reg->r_rcx; 320 l_regset->rdx = b_reg->r_rdx; 321 l_regset->rsi = b_reg->r_rsi; 322 l_regset->rdi = b_reg->r_rdi; 323 l_regset->orig_rax = b_reg->r_rax; 324 l_regset->rip = b_reg->r_rip; 325 l_regset->cs = b_reg->r_cs; 326 l_regset->eflags = b_reg->r_rflags; 327 l_regset->rsp = b_reg->r_rsp; 328 l_regset->ss = b_reg->r_ss; 329 l_regset->fs_base = 0; 330 l_regset->gs_base = 0; 331 l_regset->ds = b_reg->r_ds; 332 l_regset->es = b_reg->r_es; 333 l_regset->fs = b_reg->r_fs; 334 l_regset->gs = b_reg->r_gs; 335 } 336 337 void 338 linux_to_bsd_regset(struct reg *b_reg, const struct linux_pt_regset *l_regset) 339 { 340 341 b_reg->r_r15 = l_regset->r15; 342 b_reg->r_r14 = l_regset->r14; 343 b_reg->r_r13 = l_regset->r13; 344 b_reg->r_r12 = l_regset->r12; 345 b_reg->r_rbp = l_regset->rbp; 346 b_reg->r_rbx = l_regset->rbx; 347 b_reg->r_r11 = l_regset->r11; 348 b_reg->r_r10 = l_regset->r10; 349 b_reg->r_r9 = l_regset->r9; 350 b_reg->r_r8 = l_regset->r8; 351 b_reg->r_rax = l_regset->rax; 352 b_reg->r_rcx = l_regset->rcx; 353 b_reg->r_rdx = l_regset->rdx; 354 b_reg->r_rsi = l_regset->rsi; 355 b_reg->r_rdi = l_regset->rdi; 356 b_reg->r_rax = l_regset->orig_rax; 357 b_reg->r_rip = l_regset->rip; 358 b_reg->r_cs = l_regset->cs; 359 b_reg->r_rflags = l_regset->eflags; 360 b_reg->r_rsp = l_regset->rsp; 361 b_reg->r_ss = l_regset->ss; 362 b_reg->r_ds = l_regset->ds; 363 b_reg->r_es = l_regset->es; 364 b_reg->r_fs = l_regset->fs; 365 b_reg->r_gs = l_regset->gs; 366 } 367 368 void 369 linux_ptrace_get_syscall_info_machdep(const struct reg *reg, 370 struct syscall_info *si) 371 { 372 373 si->arch = LINUX_ARCH_AMD64; 374 si->instruction_pointer = reg->r_rip; 375 si->stack_pointer = reg->r_rsp; 376 } 377 378 int 379 linux_ptrace_getregs_machdep(struct thread *td, pid_t pid, 380 struct linux_pt_regset *l_regset) 381 { 382 struct ptrace_lwpinfo lwpinfo; 383 struct pcb *pcb; 384 int error; 385 386 pcb = td->td_pcb; 387 if (td == curthread) 388 update_pcb_bases(pcb); 389 390 l_regset->fs_base = pcb->pcb_fsbase; 391 l_regset->gs_base = pcb->pcb_gsbase; 392 393 error = kern_ptrace(td, PT_LWPINFO, pid, &lwpinfo, sizeof(lwpinfo)); 394 if (error != 0) { 395 linux_msg(td, "PT_LWPINFO failed with error %d", error); 396 return (error); 397 } 398 if ((lwpinfo.pl_flags & PL_FLAG_SCE) != 0) { 399 /* 400 * Undo the mangling done in exception.S:fast_syscall_common(). 401 */ 402 l_regset->r10 = l_regset->rcx; 403 } 404 if ((lwpinfo.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)) != 0) { 405 /* 406 * In Linux, the syscall number - passed to the syscall 407 * as rax - is preserved in orig_rax; rax gets overwritten 408 * with syscall return value. 409 */ 410 l_regset->orig_rax = lwpinfo.pl_syscall_code; 411 } 412 413 return (0); 414 } 415