1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2000 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/capsicum.h> 34 #include <sys/fcntl.h> 35 #include <sys/file.h> 36 #include <sys/imgact.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mman.h> 40 #include <sys/mutex.h> 41 #include <sys/priv.h> 42 #include <sys/proc.h> 43 #include <sys/queue.h> 44 #include <sys/resource.h> 45 #include <sys/resourcevar.h> 46 #include <sys/sched.h> 47 #include <sys/signalvar.h> 48 #include <sys/syscallsubr.h> 49 #include <sys/sysproto.h> 50 #include <sys/systm.h> 51 #include <sys/sx.h> 52 #include <sys/unistd.h> 53 #include <sys/wait.h> 54 55 #include <machine/frame.h> 56 #include <machine/psl.h> 57 #include <machine/segments.h> 58 #include <machine/sysarch.h> 59 60 #include <vm/pmap.h> 61 #include <vm/vm.h> 62 #include <vm/vm_map.h> 63 64 #include <security/audit/audit.h> 65 66 #include <i386/linux/linux.h> 67 #include <i386/linux/linux_proto.h> 68 #include <compat/linux/linux_emul.h> 69 #include <compat/linux/linux_fork.h> 70 #include <compat/linux/linux_ipc.h> 71 #include <compat/linux/linux_misc.h> 72 #include <compat/linux/linux_mmap.h> 73 #include <compat/linux/linux_signal.h> 74 #include <compat/linux/linux_util.h> 75 76 #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */ 77 78 #include "opt_posix.h" 79 80 struct l_descriptor { 81 l_uint entry_number; 82 l_ulong base_addr; 83 l_uint limit; 84 l_uint seg_32bit:1; 85 l_uint contents:2; 86 l_uint read_exec_only:1; 87 l_uint limit_in_pages:1; 88 l_uint seg_not_present:1; 89 l_uint useable:1; 90 }; 91 92 struct l_old_select_argv { 93 l_int nfds; 94 l_fd_set *readfds; 95 l_fd_set *writefds; 96 l_fd_set *exceptfds; 97 struct l_timeval *timeout; 98 }; 99 100 int 101 linux_execve(struct thread *td, struct linux_execve_args *args) 102 { 103 struct image_args eargs; 104 char *newpath; 105 int error; 106 107 if (!LUSECONVPATH(td)) { 108 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE, 109 args->argp, args->envp); 110 } else { 111 LCONVPATHEXIST(args->path, &newpath); 112 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE, 113 args->argp, args->envp); 114 LFREEPATH(newpath); 115 } 116 if (error == 0) 117 error = linux_common_execve(td, &eargs); 118 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 119 return (error); 120 } 121 122 struct l_ipc_kludge { 123 struct l_msgbuf *msgp; 124 l_long msgtyp; 125 }; 126 127 int 128 linux_ipc(struct thread *td, struct linux_ipc_args *args) 129 { 130 131 switch (args->what & 0xFFFF) { 132 case LINUX_SEMOP: { 133 134 return (kern_semop(td, args->arg1, PTRIN(args->ptr), 135 args->arg2, NULL)); 136 } 137 case LINUX_SEMGET: { 138 struct linux_semget_args a; 139 140 a.key = args->arg1; 141 a.nsems = args->arg2; 142 a.semflg = args->arg3; 143 return (linux_semget(td, &a)); 144 } 145 case LINUX_SEMCTL: { 146 struct linux_semctl_args a; 147 int error; 148 149 a.semid = args->arg1; 150 a.semnum = args->arg2; 151 a.cmd = args->arg3; 152 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg)); 153 if (error) 154 return (error); 155 return (linux_semctl(td, &a)); 156 } 157 case LINUX_MSGSND: { 158 struct linux_msgsnd_args a; 159 160 a.msqid = args->arg1; 161 a.msgp = PTRIN(args->ptr); 162 a.msgsz = args->arg2; 163 a.msgflg = args->arg3; 164 return (linux_msgsnd(td, &a)); 165 } 166 case LINUX_MSGRCV: { 167 struct linux_msgrcv_args a; 168 169 a.msqid = args->arg1; 170 a.msgsz = args->arg2; 171 a.msgflg = args->arg3; 172 if ((args->what >> 16) == 0) { 173 struct l_ipc_kludge tmp; 174 int error; 175 176 if (args->ptr == 0) 177 return (EINVAL); 178 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp)); 179 if (error) 180 return (error); 181 a.msgp = PTRIN(tmp.msgp); 182 a.msgtyp = tmp.msgtyp; 183 } else { 184 a.msgp = PTRIN(args->ptr); 185 a.msgtyp = args->arg5; 186 } 187 return (linux_msgrcv(td, &a)); 188 } 189 case LINUX_MSGGET: { 190 struct linux_msgget_args a; 191 192 a.key = args->arg1; 193 a.msgflg = args->arg2; 194 return (linux_msgget(td, &a)); 195 } 196 case LINUX_MSGCTL: { 197 struct linux_msgctl_args a; 198 199 a.msqid = args->arg1; 200 a.cmd = args->arg2; 201 a.buf = PTRIN(args->ptr); 202 return (linux_msgctl(td, &a)); 203 } 204 case LINUX_SHMAT: { 205 struct linux_shmat_args a; 206 l_uintptr_t addr; 207 int error; 208 209 a.shmid = args->arg1; 210 a.shmaddr = PTRIN(args->ptr); 211 a.shmflg = args->arg2; 212 error = linux_shmat(td, &a); 213 if (error != 0) 214 return (error); 215 addr = td->td_retval[0]; 216 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr)); 217 td->td_retval[0] = 0; 218 return (error); 219 } 220 case LINUX_SHMDT: { 221 struct linux_shmdt_args a; 222 223 a.shmaddr = PTRIN(args->ptr); 224 return (linux_shmdt(td, &a)); 225 } 226 case LINUX_SHMGET: { 227 struct linux_shmget_args a; 228 229 a.key = args->arg1; 230 a.size = args->arg2; 231 a.shmflg = args->arg3; 232 return (linux_shmget(td, &a)); 233 } 234 case LINUX_SHMCTL: { 235 struct linux_shmctl_args a; 236 237 a.shmid = args->arg1; 238 a.cmd = args->arg2; 239 a.buf = PTRIN(args->ptr); 240 return (linux_shmctl(td, &a)); 241 } 242 default: 243 break; 244 } 245 246 return (EINVAL); 247 } 248 249 int 250 linux_old_select(struct thread *td, struct linux_old_select_args *args) 251 { 252 struct l_old_select_argv linux_args; 253 struct linux_select_args newsel; 254 int error; 255 256 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 257 if (error) 258 return (error); 259 260 newsel.nfds = linux_args.nfds; 261 newsel.readfds = linux_args.readfds; 262 newsel.writefds = linux_args.writefds; 263 newsel.exceptfds = linux_args.exceptfds; 264 newsel.timeout = linux_args.timeout; 265 return (linux_select(td, &newsel)); 266 } 267 268 int 269 linux_set_cloned_tls(struct thread *td, void *desc) 270 { 271 struct segment_descriptor sd; 272 struct l_user_desc info; 273 int idx, error; 274 int a[2]; 275 276 error = copyin(desc, &info, sizeof(struct l_user_desc)); 277 if (error) { 278 linux_msg(td, "set_cloned_tls copyin failed!"); 279 } else { 280 idx = info.entry_number; 281 282 /* 283 * looks like we're getting the idx we returned 284 * in the set_thread_area() syscall 285 */ 286 if (idx != 6 && idx != 3) { 287 linux_msg(td, "set_cloned_tls resetting idx!"); 288 idx = 3; 289 } 290 291 /* this doesnt happen in practice */ 292 if (idx == 6) { 293 /* we might copy out the entry_number as 3 */ 294 info.entry_number = 3; 295 error = copyout(&info, desc, sizeof(struct l_user_desc)); 296 if (error) 297 linux_msg(td, "set_cloned_tls copyout failed!"); 298 } 299 300 a[0] = LINUX_LDT_entry_a(&info); 301 a[1] = LINUX_LDT_entry_b(&info); 302 303 memcpy(&sd, &a, sizeof(a)); 304 /* set %gs */ 305 td->td_pcb->pcb_gsd = sd; 306 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL); 307 } 308 309 return (error); 310 } 311 312 int 313 linux_set_upcall(struct thread *td, register_t stack) 314 { 315 316 if (stack) 317 td->td_frame->tf_esp = stack; 318 319 /* 320 * The newly created Linux thread returns 321 * to the user space by the same path that a parent do. 322 */ 323 td->td_frame->tf_eax = 0; 324 return (0); 325 } 326 327 int 328 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 329 { 330 331 return (linux_mmap_common(td, args->addr, args->len, args->prot, 332 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff * 333 PAGE_SIZE)); 334 } 335 336 int 337 linux_mmap(struct thread *td, struct linux_mmap_args *args) 338 { 339 int error; 340 struct l_mmap_argv linux_args; 341 342 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 343 if (error) 344 return (error); 345 346 return (linux_mmap_common(td, linux_args.addr, linux_args.len, 347 linux_args.prot, linux_args.flags, linux_args.fd, 348 (uint32_t)linux_args.pgoff)); 349 } 350 351 int 352 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 353 { 354 355 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot)); 356 } 357 358 int 359 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 360 { 361 362 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav)); 363 } 364 365 int 366 linux_ioperm(struct thread *td, struct linux_ioperm_args *args) 367 { 368 int error; 369 struct i386_ioperm_args iia; 370 371 iia.start = args->start; 372 iia.length = args->length; 373 iia.enable = args->enable; 374 error = i386_set_ioperm(td, &iia); 375 return (error); 376 } 377 378 int 379 linux_iopl(struct thread *td, struct linux_iopl_args *args) 380 { 381 int error; 382 383 if (args->level < 0 || args->level > 3) 384 return (EINVAL); 385 if ((error = priv_check(td, PRIV_IO)) != 0) 386 return (error); 387 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 388 return (error); 389 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) | 390 (args->level * (PSL_IOPL / 3)); 391 return (0); 392 } 393 394 int 395 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap) 396 { 397 int error; 398 struct i386_ldt_args ldt; 399 struct l_descriptor ld; 400 union descriptor desc; 401 int size, written; 402 403 switch (uap->func) { 404 case 0x00: /* read_ldt */ 405 ldt.start = 0; 406 ldt.descs = uap->ptr; 407 ldt.num = uap->bytecount / sizeof(union descriptor); 408 error = i386_get_ldt(td, &ldt); 409 td->td_retval[0] *= sizeof(union descriptor); 410 break; 411 case 0x02: /* read_default_ldt = 0 */ 412 size = 5*sizeof(struct l_desc_struct); 413 if (size > uap->bytecount) 414 size = uap->bytecount; 415 for (written = error = 0; written < size && error == 0; written++) 416 error = subyte((char *)uap->ptr + written, 0); 417 td->td_retval[0] = written; 418 break; 419 case 0x01: /* write_ldt */ 420 case 0x11: /* write_ldt */ 421 if (uap->bytecount != sizeof(ld)) 422 return (EINVAL); 423 424 error = copyin(uap->ptr, &ld, sizeof(ld)); 425 if (error) 426 return (error); 427 428 ldt.start = ld.entry_number; 429 ldt.descs = &desc; 430 ldt.num = 1; 431 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff); 432 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16; 433 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff); 434 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24; 435 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) | 436 (ld.contents << 2); 437 desc.sd.sd_dpl = 3; 438 desc.sd.sd_p = (ld.seg_not_present ^ 1); 439 desc.sd.sd_xx = 0; 440 desc.sd.sd_def32 = ld.seg_32bit; 441 desc.sd.sd_gran = ld.limit_in_pages; 442 error = i386_set_ldt(td, &ldt, &desc); 443 break; 444 default: 445 error = ENOSYS; 446 break; 447 } 448 449 if (error == EOPNOTSUPP) { 450 linux_msg(td, "modify_ldt needs kernel option USER_LDT"); 451 error = ENOSYS; 452 } 453 454 return (error); 455 } 456 457 int 458 linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 459 { 460 l_osigaction_t osa; 461 l_sigaction_t act, oact; 462 int error; 463 464 if (args->nsa != NULL) { 465 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 466 if (error) 467 return (error); 468 act.lsa_handler = osa.lsa_handler; 469 act.lsa_flags = osa.lsa_flags; 470 act.lsa_restorer = osa.lsa_restorer; 471 LINUX_SIGEMPTYSET(act.lsa_mask); 472 act.lsa_mask.__mask = osa.lsa_mask; 473 } 474 475 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 476 args->osa ? &oact : NULL); 477 478 if (args->osa != NULL && !error) { 479 osa.lsa_handler = oact.lsa_handler; 480 osa.lsa_flags = oact.lsa_flags; 481 osa.lsa_restorer = oact.lsa_restorer; 482 osa.lsa_mask = oact.lsa_mask.__mask; 483 error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 484 } 485 486 return (error); 487 } 488 489 /* 490 * Linux has two extra args, restart and oldmask. We dont use these, 491 * but it seems that "restart" is actually a context pointer that 492 * enables the signal to happen with a different register set. 493 */ 494 int 495 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 496 { 497 sigset_t sigmask; 498 l_sigset_t mask; 499 500 LINUX_SIGEMPTYSET(mask); 501 mask.__mask = args->mask; 502 linux_to_bsd_sigset(&mask, &sigmask); 503 return (kern_sigsuspend(td, sigmask)); 504 } 505 506 int 507 linux_pause(struct thread *td, struct linux_pause_args *args) 508 { 509 struct proc *p = td->td_proc; 510 sigset_t sigmask; 511 512 PROC_LOCK(p); 513 sigmask = td->td_sigmask; 514 PROC_UNLOCK(p); 515 return (kern_sigsuspend(td, sigmask)); 516 } 517 518 int 519 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args) 520 { 521 struct l_user_desc info; 522 int error; 523 int idx; 524 int a[2]; 525 struct segment_descriptor sd; 526 527 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 528 if (error) 529 return (error); 530 531 idx = info.entry_number; 532 /* 533 * Semantics of Linux version: every thread in the system has array of 534 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This 535 * syscall loads one of the selected tls decriptors with a value and 536 * also loads GDT descriptors 6, 7 and 8 with the content of the 537 * per-thread descriptors. 538 * 539 * Semantics of FreeBSD version: I think we can ignore that Linux has 3 540 * per-thread descriptors and use just the 1st one. The tls_array[] 541 * is used only in set/get-thread_area() syscalls and for loading the 542 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS 543 * so we will load just one. 544 * 545 * XXX: this doesn't work when a user space process tries to use more 546 * than 1 TLS segment. Comment in the Linux sources says wine might do 547 * this. 548 */ 549 550 /* 551 * we support just GLIBC TLS now 552 * we should let 3 proceed as well because we use this segment so 553 * if code does two subsequent calls it should succeed 554 */ 555 if (idx != 6 && idx != -1 && idx != 3) 556 return (EINVAL); 557 558 /* 559 * we have to copy out the GDT entry we use 560 * FreeBSD uses GDT entry #3 for storing %gs so load that 561 * 562 * XXX: what if a user space program doesn't check this value and tries 563 * to use 6, 7 or 8? 564 */ 565 idx = info.entry_number = 3; 566 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 567 if (error) 568 return (error); 569 570 if (LINUX_LDT_empty(&info)) { 571 a[0] = 0; 572 a[1] = 0; 573 } else { 574 a[0] = LINUX_LDT_entry_a(&info); 575 a[1] = LINUX_LDT_entry_b(&info); 576 } 577 578 memcpy(&sd, &a, sizeof(a)); 579 /* this is taken from i386 version of cpu_set_user_tls() */ 580 critical_enter(); 581 /* set %gs */ 582 td->td_pcb->pcb_gsd = sd; 583 PCPU_GET(fsgs_gdt)[1] = sd; 584 load_gs(GSEL(GUGS_SEL, SEL_UPL)); 585 critical_exit(); 586 587 return (0); 588 } 589 590 int 591 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args) 592 { 593 594 struct l_user_desc info; 595 int error; 596 int idx; 597 struct l_desc_struct desc; 598 struct segment_descriptor sd; 599 600 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 601 if (error) 602 return (error); 603 604 idx = info.entry_number; 605 /* XXX: I am not sure if we want 3 to be allowed too. */ 606 if (idx != 6 && idx != 3) 607 return (EINVAL); 608 609 idx = 3; 610 611 memset(&info, 0, sizeof(info)); 612 613 sd = PCPU_GET(fsgs_gdt)[1]; 614 615 memcpy(&desc, &sd, sizeof(desc)); 616 617 info.entry_number = idx; 618 info.base_addr = LINUX_GET_BASE(&desc); 619 info.limit = LINUX_GET_LIMIT(&desc); 620 info.seg_32bit = LINUX_GET_32BIT(&desc); 621 info.contents = LINUX_GET_CONTENTS(&desc); 622 info.read_exec_only = !LINUX_GET_WRITABLE(&desc); 623 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc); 624 info.seg_not_present = !LINUX_GET_PRESENT(&desc); 625 info.useable = LINUX_GET_USEABLE(&desc); 626 627 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 628 if (error) 629 return (EFAULT); 630 631 return (0); 632 } 633 634 /* XXX: this wont work with module - convert it */ 635 int 636 linux_mq_open(struct thread *td, struct linux_mq_open_args *args) 637 { 638 #ifdef P1003_1B_MQUEUE 639 return (sys_kmq_open(td, (struct kmq_open_args *)args)); 640 #else 641 return (ENOSYS); 642 #endif 643 } 644 645 int 646 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args) 647 { 648 #ifdef P1003_1B_MQUEUE 649 return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args)); 650 #else 651 return (ENOSYS); 652 #endif 653 } 654 655 int 656 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args) 657 { 658 #ifdef P1003_1B_MQUEUE 659 return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args)); 660 #else 661 return (ENOSYS); 662 #endif 663 } 664 665 int 666 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args) 667 { 668 #ifdef P1003_1B_MQUEUE 669 return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args)); 670 #else 671 return (ENOSYS); 672 #endif 673 } 674 675 int 676 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args) 677 { 678 #ifdef P1003_1B_MQUEUE 679 return (sys_kmq_notify(td, (struct kmq_notify_args *)args)); 680 #else 681 return (ENOSYS); 682 #endif 683 } 684 685 int 686 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args) 687 { 688 #ifdef P1003_1B_MQUEUE 689 return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args)); 690 #else 691 return (ENOSYS); 692 #endif 693 } 694