1 /*- 2 * Copyright (c) 2013 Dmitry Chagin 3 * Copyright (c) 2004 Tim J. Robbins 4 * Copyright (c) 2002 Doug Rabson 5 * Copyright (c) 2000 Marcel Moolenaar 6 * All rights reserved. 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/resource.h> 53 #include <sys/resourcevar.h> 54 #include <sys/sched.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysproto.h> 57 #include <sys/systm.h> 58 #include <sys/unistd.h> 59 #include <sys/vnode.h> 60 #include <sys/wait.h> 61 62 #include <security/mac/mac_framework.h> 63 64 #include <ufs/ufs/extattr.h> 65 #include <ufs/ufs/quota.h> 66 #include <ufs/ufs/ufsmount.h> 67 68 #include <machine/frame.h> 69 #include <machine/md_var.h> 70 #include <machine/pcb.h> 71 #include <machine/psl.h> 72 #include <machine/segments.h> 73 #include <machine/specialreg.h> 74 75 #include <vm/pmap.h> 76 #include <vm/vm.h> 77 #include <vm/vm_extern.h> 78 #include <vm/vm_kern.h> 79 #include <vm/vm_map.h> 80 81 #include <x86/ifunc.h> 82 #include <x86/reg.h> 83 #include <x86/sysarch.h> 84 85 #include <security/audit/audit.h> 86 87 #include <amd64/linux/linux.h> 88 #include <amd64/linux/linux_proto.h> 89 #include <compat/linux/linux_emul.h> 90 #include <compat/linux/linux_file.h> 91 #include <compat/linux/linux_ipc.h> 92 #include <compat/linux/linux_misc.h> 93 #include <compat/linux/linux_mmap.h> 94 #include <compat/linux/linux_signal.h> 95 #include <compat/linux/linux_util.h> 96 97 int 98 linux_execve(struct thread *td, struct linux_execve_args *args) 99 { 100 struct image_args eargs; 101 char *path; 102 int error; 103 104 LINUX_CTR(execve); 105 106 if (!LUSECONVPATH(td)) { 107 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE, 108 args->argp, args->envp); 109 } else { 110 LCONVPATHEXIST(td, args->path, &path); 111 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp, 112 args->envp); 113 LFREEPATH(path); 114 } 115 if (error == 0) 116 error = linux_common_execve(td, &eargs); 117 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 118 return (error); 119 } 120 121 int 122 linux_set_upcall(struct thread *td, register_t stack) 123 { 124 125 if (stack) 126 td->td_frame->tf_rsp = stack; 127 128 /* 129 * The newly created Linux thread returns 130 * to the user space by the same path that a parent does. 131 */ 132 td->td_frame->tf_rax = 0; 133 return (0); 134 } 135 136 int 137 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 138 { 139 140 return (linux_mmap_common(td, args->addr, args->len, args->prot, 141 args->flags, args->fd, args->pgoff)); 142 } 143 144 int 145 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 146 { 147 148 return (linux_mprotect_common(td, uap->addr, uap->len, uap->prot)); 149 } 150 151 int 152 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 153 { 154 155 return (linux_madvise_common(td, uap->addr, uap->len, uap->behav)); 156 } 157 158 int 159 linux_iopl(struct thread *td, struct linux_iopl_args *args) 160 { 161 int error; 162 163 LINUX_CTR(iopl); 164 165 if (args->level > 3) 166 return (EINVAL); 167 if ((error = priv_check(td, PRIV_IO)) != 0) 168 return (error); 169 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 170 return (error); 171 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 172 (args->level * (PSL_IOPL / 3)); 173 174 return (0); 175 } 176 177 int 178 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 179 { 180 l_sigset_t lmask; 181 sigset_t sigmask; 182 int error; 183 184 LINUX_CTR2(rt_sigsuspend, "%p, %ld", 185 uap->newset, uap->sigsetsize); 186 187 if (uap->sigsetsize != sizeof(l_sigset_t)) 188 return (EINVAL); 189 190 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t)); 191 if (error) 192 return (error); 193 194 linux_to_bsd_sigset(&lmask, &sigmask); 195 return (kern_sigsuspend(td, sigmask)); 196 } 197 198 int 199 linux_pause(struct thread *td, struct linux_pause_args *args) 200 { 201 struct proc *p = td->td_proc; 202 sigset_t sigmask; 203 204 LINUX_CTR(pause); 205 206 PROC_LOCK(p); 207 sigmask = td->td_sigmask; 208 PROC_UNLOCK(p); 209 return (kern_sigsuspend(td, sigmask)); 210 } 211 212 int 213 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 214 { 215 stack_t ss, oss; 216 l_stack_t lss; 217 int error; 218 219 memset(&lss, 0, sizeof(lss)); 220 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss); 221 222 if (uap->uss != NULL) { 223 error = copyin(uap->uss, &lss, sizeof(l_stack_t)); 224 if (error) 225 return (error); 226 227 ss.ss_sp = PTRIN(lss.ss_sp); 228 ss.ss_size = lss.ss_size; 229 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 230 } 231 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 232 (uap->uoss != NULL) ? &oss : NULL); 233 if (!error && uap->uoss != NULL) { 234 lss.ss_sp = PTROUT(oss.ss_sp); 235 lss.ss_size = oss.ss_size; 236 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 237 error = copyout(&lss, uap->uoss, sizeof(l_stack_t)); 238 } 239 240 return (error); 241 } 242 243 int 244 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args) 245 { 246 unsigned long long cet[3]; 247 struct pcb *pcb; 248 int error; 249 250 pcb = td->td_pcb; 251 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr); 252 253 switch (args->code) { 254 case LINUX_ARCH_SET_GS: 255 if (args->addr < VM_MAXUSER_ADDRESS) { 256 update_pcb_bases(pcb); 257 pcb->pcb_gsbase = args->addr; 258 td->td_frame->tf_gs = _ugssel; 259 error = 0; 260 } else 261 error = EPERM; 262 break; 263 case LINUX_ARCH_SET_FS: 264 if (args->addr < VM_MAXUSER_ADDRESS) { 265 update_pcb_bases(pcb); 266 pcb->pcb_fsbase = args->addr; 267 td->td_frame->tf_fs = _ufssel; 268 error = 0; 269 } else 270 error = EPERM; 271 break; 272 case LINUX_ARCH_GET_FS: 273 error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr), 274 sizeof(args->addr)); 275 break; 276 case LINUX_ARCH_GET_GS: 277 error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr), 278 sizeof(args->addr)); 279 break; 280 case LINUX_ARCH_CET_STATUS: 281 memset(cet, 0, sizeof(cet)); 282 error = copyout(&cet, PTRIN(args->addr), sizeof(cet)); 283 break; 284 default: 285 linux_msg(td, "unsupported arch_prctl code %#x", args->code); 286 error = EINVAL; 287 } 288 return (error); 289 } 290 291 int 292 linux_set_cloned_tls(struct thread *td, void *desc) 293 { 294 struct pcb *pcb; 295 296 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS) 297 return (EPERM); 298 299 pcb = td->td_pcb; 300 update_pcb_bases(pcb); 301 pcb->pcb_fsbase = (register_t)desc; 302 td->td_frame->tf_fs = _ufssel; 303 304 return (0); 305 } 306 307 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 308 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval); 309 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *)) 310 { 311 312 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 313 futex_xchgl_smap : futex_xchgl_nosmap); 314 } 315 316 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 317 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval); 318 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *)) 319 { 320 321 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 322 futex_addl_smap : futex_addl_nosmap); 323 } 324 325 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 326 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval); 327 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *)) 328 { 329 330 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 331 futex_orl_smap : futex_orl_nosmap); 332 } 333 334 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 335 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval); 336 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *)) 337 { 338 339 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 340 futex_andl_smap : futex_andl_nosmap); 341 } 342 343 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 344 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval); 345 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *)) 346 { 347 348 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 349 futex_xorl_smap : futex_xorl_nosmap); 350 } 351 352 void 353 bsd_to_linux_regset(struct reg *b_reg, struct linux_pt_regset *l_regset) 354 { 355 356 l_regset->r15 = b_reg->r_r15; 357 l_regset->r14 = b_reg->r_r14; 358 l_regset->r13 = b_reg->r_r13; 359 l_regset->r12 = b_reg->r_r12; 360 l_regset->rbp = b_reg->r_rbp; 361 l_regset->rbx = b_reg->r_rbx; 362 l_regset->r11 = b_reg->r_r11; 363 l_regset->r10 = b_reg->r_r10; 364 l_regset->r9 = b_reg->r_r9; 365 l_regset->r8 = b_reg->r_r8; 366 l_regset->rax = b_reg->r_rax; 367 l_regset->rcx = b_reg->r_rcx; 368 l_regset->rdx = b_reg->r_rdx; 369 l_regset->rsi = b_reg->r_rsi; 370 l_regset->rdi = b_reg->r_rdi; 371 l_regset->orig_rax = b_reg->r_rax; 372 l_regset->rip = b_reg->r_rip; 373 l_regset->cs = b_reg->r_cs; 374 l_regset->eflags = b_reg->r_rflags; 375 l_regset->rsp = b_reg->r_rsp; 376 l_regset->ss = b_reg->r_ss; 377 l_regset->fs_base = 0; 378 l_regset->gs_base = 0; 379 l_regset->ds = b_reg->r_ds; 380 l_regset->es = b_reg->r_es; 381 l_regset->fs = b_reg->r_fs; 382 l_regset->gs = b_reg->r_gs; 383 } 384