1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1994-1995 Søren Schmidt 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/param.h> 30 #include <sys/systm.h> 31 #include <sys/dirent.h> 32 #include <sys/fcntl.h> 33 #include <sys/file.h> 34 #include <sys/filedesc.h> 35 #include <sys/lock.h> 36 #include <sys/mman.h> 37 #include <sys/selinfo.h> 38 #include <sys/pipe.h> 39 #include <sys/proc.h> 40 #include <sys/stat.h> 41 #include <sys/sx.h> 42 #include <sys/syscallsubr.h> 43 #include <sys/sysproto.h> 44 #include <sys/tty.h> 45 #include <sys/unistd.h> 46 #include <sys/vnode.h> 47 48 #ifdef COMPAT_LINUX32 49 #include <compat/freebsd32/freebsd32_misc.h> 50 #include <compat/freebsd32/freebsd32_util.h> 51 #include <machine/../linux32/linux.h> 52 #include <machine/../linux32/linux32_proto.h> 53 #else 54 #include <machine/../linux/linux.h> 55 #include <machine/../linux/linux_proto.h> 56 #endif 57 #include <compat/linux/linux_misc.h> 58 #include <compat/linux/linux_util.h> 59 #include <compat/linux/linux_file.h> 60 61 static int linux_common_open(struct thread *, int, const char *, int, int, 62 enum uio_seg); 63 static int linux_do_accessat(struct thread *t, int, const char *, int, int); 64 static int linux_getdents_error(struct thread *, int, int); 65 66 static struct bsd_to_linux_bitmap seal_bitmap[] = { 67 BITMAP_1t1_LINUX(F_SEAL_SEAL), 68 BITMAP_1t1_LINUX(F_SEAL_SHRINK), 69 BITMAP_1t1_LINUX(F_SEAL_GROW), 70 BITMAP_1t1_LINUX(F_SEAL_WRITE), 71 }; 72 73 #define MFD_HUGETLB_ENTRY(_size) \ 74 { \ 75 .bsd_value = MFD_HUGE_##_size, \ 76 .linux_value = LINUX_HUGETLB_FLAG_ENCODE_##_size \ 77 } 78 static struct bsd_to_linux_bitmap mfd_bitmap[] = { 79 BITMAP_1t1_LINUX(MFD_CLOEXEC), 80 BITMAP_1t1_LINUX(MFD_ALLOW_SEALING), 81 BITMAP_1t1_LINUX(MFD_HUGETLB), 82 MFD_HUGETLB_ENTRY(64KB), 83 MFD_HUGETLB_ENTRY(512KB), 84 MFD_HUGETLB_ENTRY(1MB), 85 MFD_HUGETLB_ENTRY(2MB), 86 MFD_HUGETLB_ENTRY(8MB), 87 MFD_HUGETLB_ENTRY(16MB), 88 MFD_HUGETLB_ENTRY(32MB), 89 MFD_HUGETLB_ENTRY(256MB), 90 MFD_HUGETLB_ENTRY(512MB), 91 MFD_HUGETLB_ENTRY(1GB), 92 MFD_HUGETLB_ENTRY(2GB), 93 MFD_HUGETLB_ENTRY(16GB), 94 }; 95 #undef MFD_HUGETLB_ENTRY 96 97 #ifdef LINUX_LEGACY_SYSCALLS 98 int 99 linux_creat(struct thread *td, struct linux_creat_args *args) 100 { 101 102 return (kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE, 103 O_WRONLY | O_CREAT | O_TRUNC, args->mode)); 104 } 105 #endif 106 107 static int 108 linux_common_openflags(int l_flags) 109 { 110 int bsd_flags; 111 112 bsd_flags = 0; 113 switch (l_flags & LINUX_O_ACCMODE) { 114 case LINUX_O_WRONLY: 115 bsd_flags |= O_WRONLY; 116 break; 117 case LINUX_O_RDWR: 118 bsd_flags |= O_RDWR; 119 break; 120 default: 121 bsd_flags |= O_RDONLY; 122 } 123 if (l_flags & LINUX_O_NDELAY) 124 bsd_flags |= O_NONBLOCK; 125 if (l_flags & LINUX_O_APPEND) 126 bsd_flags |= O_APPEND; 127 if (l_flags & LINUX_O_SYNC) 128 bsd_flags |= O_FSYNC; 129 if (l_flags & LINUX_O_CLOEXEC) 130 bsd_flags |= O_CLOEXEC; 131 if (l_flags & LINUX_O_NONBLOCK) 132 bsd_flags |= O_NONBLOCK; 133 if (l_flags & LINUX_O_ASYNC) 134 bsd_flags |= O_ASYNC; 135 if (l_flags & LINUX_O_CREAT) 136 bsd_flags |= O_CREAT; 137 if (l_flags & LINUX_O_TRUNC) 138 bsd_flags |= O_TRUNC; 139 if (l_flags & LINUX_O_EXCL) 140 bsd_flags |= O_EXCL; 141 if (l_flags & LINUX_O_NOCTTY) 142 bsd_flags |= O_NOCTTY; 143 if (l_flags & LINUX_O_DIRECT) 144 bsd_flags |= O_DIRECT; 145 if (l_flags & LINUX_O_NOFOLLOW) 146 bsd_flags |= O_NOFOLLOW; 147 if (l_flags & LINUX_O_DIRECTORY) 148 bsd_flags |= O_DIRECTORY; 149 if (l_flags & LINUX_O_PATH) 150 bsd_flags |= O_PATH; 151 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */ 152 return (bsd_flags); 153 } 154 155 static int 156 linux_common_open(struct thread *td, int dirfd, const char *path, int l_flags, 157 int mode, enum uio_seg seg) 158 { 159 struct proc *p = td->td_proc; 160 struct file *fp; 161 int fd; 162 int bsd_flags, error; 163 164 bsd_flags = linux_common_openflags(l_flags); 165 error = kern_openat(td, dirfd, path, seg, bsd_flags, mode); 166 if (error != 0) { 167 if (error == EMLINK) 168 error = ELOOP; 169 goto done; 170 } 171 if (p->p_flag & P_CONTROLT) 172 goto done; 173 if (bsd_flags & O_NOCTTY) 174 goto done; 175 176 /* 177 * XXX In between kern_openat() and fget(), another process 178 * having the same filedesc could use that fd without 179 * checking below. 180 */ 181 fd = td->td_retval[0]; 182 if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) { 183 if (fp->f_type != DTYPE_VNODE) { 184 fdrop(fp, td); 185 goto done; 186 } 187 sx_slock(&proctree_lock); 188 PROC_LOCK(p); 189 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 190 PROC_UNLOCK(p); 191 sx_sunlock(&proctree_lock); 192 /* XXXPJD: Verify if TIOCSCTTY is allowed. */ 193 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, 194 td->td_ucred, td); 195 } else { 196 PROC_UNLOCK(p); 197 sx_sunlock(&proctree_lock); 198 } 199 fdrop(fp, td); 200 } 201 202 done: 203 return (error); 204 } 205 206 int 207 linux_openat(struct thread *td, struct linux_openat_args *args) 208 { 209 int dfd; 210 211 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 212 return (linux_common_open(td, dfd, args->filename, args->flags, 213 args->mode, UIO_USERSPACE)); 214 } 215 216 #ifdef LINUX_LEGACY_SYSCALLS 217 int 218 linux_open(struct thread *td, struct linux_open_args *args) 219 { 220 221 return (linux_common_open(td, AT_FDCWD, args->path, args->flags, 222 args->mode, UIO_USERSPACE)); 223 } 224 #endif 225 226 int 227 linux_name_to_handle_at(struct thread *td, 228 struct linux_name_to_handle_at_args *args) 229 { 230 static const l_int valid_flags = (LINUX_AT_SYMLINK_FOLLOW | 231 LINUX_AT_EMPTY_PATH); 232 static const l_uint fh_size = sizeof(fhandle_t); 233 234 fhandle_t fh; 235 l_uint fh_bytes; 236 l_int mount_id; 237 int error, fd, bsd_flags; 238 239 if (args->flags & ~valid_flags) 240 return (EINVAL); 241 242 fd = args->dirfd; 243 if (fd == LINUX_AT_FDCWD) 244 fd = AT_FDCWD; 245 246 bsd_flags = 0; 247 if (!(args->flags & LINUX_AT_SYMLINK_FOLLOW)) 248 bsd_flags |= AT_SYMLINK_NOFOLLOW; 249 if ((args->flags & LINUX_AT_EMPTY_PATH) != 0) 250 bsd_flags |= AT_EMPTY_PATH; 251 252 error = kern_getfhat(td, bsd_flags, fd, args->name, 253 UIO_USERSPACE, &fh, UIO_SYSSPACE); 254 if (error != 0) 255 return (error); 256 257 /* Emit mount_id -- required before EOVERFLOW case. */ 258 mount_id = (fh.fh_fsid.val[0] ^ fh.fh_fsid.val[1]); 259 error = copyout(&mount_id, args->mnt_id, sizeof(mount_id)); 260 if (error != 0) 261 return (error); 262 263 /* Check if there is room for handle. */ 264 error = copyin(&args->handle->handle_bytes, &fh_bytes, 265 sizeof(fh_bytes)); 266 if (error != 0) 267 return (error); 268 269 if (fh_bytes < fh_size) { 270 error = copyout(&fh_size, &args->handle->handle_bytes, 271 sizeof(fh_size)); 272 if (error == 0) 273 error = EOVERFLOW; 274 return (error); 275 } 276 277 /* Emit handle. */ 278 mount_id = 0; 279 /* 280 * We don't use handle_type for anything yet, but initialize a known 281 * value. 282 */ 283 error = copyout(&mount_id, &args->handle->handle_type, 284 sizeof(mount_id)); 285 if (error != 0) 286 return (error); 287 288 error = copyout(&fh, &args->handle->f_handle, 289 sizeof(fh)); 290 return (error); 291 } 292 293 int 294 linux_open_by_handle_at(struct thread *td, 295 struct linux_open_by_handle_at_args *args) 296 { 297 l_uint fh_bytes; 298 int bsd_flags, error; 299 300 error = copyin(&args->handle->handle_bytes, &fh_bytes, 301 sizeof(fh_bytes)); 302 if (error != 0) 303 return (error); 304 305 if (fh_bytes < sizeof(fhandle_t)) 306 return (EINVAL); 307 308 bsd_flags = linux_common_openflags(args->flags); 309 return (kern_fhopen(td, (void *)&args->handle->f_handle, bsd_flags)); 310 } 311 312 int 313 linux_lseek(struct thread *td, struct linux_lseek_args *args) 314 { 315 316 return (kern_lseek(td, args->fdes, args->off, args->whence)); 317 } 318 319 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 320 int 321 linux_llseek(struct thread *td, struct linux_llseek_args *args) 322 { 323 int error; 324 off_t off; 325 326 off = (args->olow) | (((off_t) args->ohigh) << 32); 327 328 error = kern_lseek(td, args->fd, off, args->whence); 329 if (error != 0) 330 return (error); 331 332 error = copyout(td->td_retval, args->res, sizeof(off_t)); 333 if (error != 0) 334 return (error); 335 336 td->td_retval[0] = 0; 337 return (0); 338 } 339 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 340 341 /* 342 * Note that linux_getdents(2) and linux_getdents64(2) have the same 343 * arguments. They only differ in the definition of struct dirent they 344 * operate on. 345 * Note that linux_readdir(2) is a special case of linux_getdents(2) 346 * where count is always equals 1, meaning that the buffer is one 347 * dirent-structure in size and that the code can't handle more anyway. 348 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2) 349 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will 350 * trash user stack. 351 */ 352 353 static int 354 linux_getdents_error(struct thread *td, int fd, int err) 355 { 356 struct vnode *vp; 357 struct file *fp; 358 int error; 359 360 /* Linux return ENOTDIR in case when fd is not a directory. */ 361 error = getvnode(td, fd, &cap_read_rights, &fp); 362 if (error != 0) 363 return (error); 364 vp = fp->f_vnode; 365 if (vp->v_type != VDIR) { 366 fdrop(fp, td); 367 return (ENOTDIR); 368 } 369 fdrop(fp, td); 370 return (err); 371 } 372 373 struct l_dirent { 374 l_ulong d_ino; 375 l_off_t d_off; 376 l_ushort d_reclen; 377 char d_name[LINUX_NAME_MAX + 1]; 378 }; 379 380 struct l_dirent64 { 381 uint64_t d_ino; 382 int64_t d_off; 383 l_ushort d_reclen; 384 u_char d_type; 385 char d_name[LINUX_NAME_MAX + 1]; 386 }; 387 388 /* 389 * Linux uses the last byte in the dirent buffer to store d_type, 390 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes. 391 */ 392 #define LINUX_RECLEN(namlen) \ 393 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong)) 394 395 #define LINUX_RECLEN64(namlen) \ 396 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \ 397 sizeof(uint64_t)) 398 399 #ifdef LINUX_LEGACY_SYSCALLS 400 int 401 linux_getdents(struct thread *td, struct linux_getdents_args *args) 402 { 403 struct dirent *bdp; 404 caddr_t inp, buf; /* BSD-format */ 405 int len, reclen; /* BSD-format */ 406 caddr_t outp; /* Linux-format */ 407 int resid, linuxreclen; /* Linux-format */ 408 caddr_t lbuf; /* Linux-format */ 409 off_t base; 410 struct l_dirent *linux_dirent; 411 int buflen, error; 412 size_t retval; 413 414 buflen = min(args->count, MAXBSIZE); 415 buf = malloc(buflen, M_LINUX, M_WAITOK); 416 417 error = kern_getdirentries(td, args->fd, buf, buflen, 418 &base, NULL, UIO_SYSSPACE); 419 if (error != 0) { 420 error = linux_getdents_error(td, args->fd, error); 421 goto out1; 422 } 423 424 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX, M_WAITOK | M_ZERO); 425 426 len = td->td_retval[0]; 427 inp = buf; 428 outp = (caddr_t)args->dent; 429 resid = args->count; 430 retval = 0; 431 432 while (len > 0) { 433 bdp = (struct dirent *) inp; 434 reclen = bdp->d_reclen; 435 linuxreclen = LINUX_RECLEN(bdp->d_namlen); 436 /* 437 * No more space in the user supplied dirent buffer. 438 * Return EINVAL. 439 */ 440 if (resid < linuxreclen) { 441 error = EINVAL; 442 goto out; 443 } 444 445 linux_dirent = (struct l_dirent*)lbuf; 446 linux_dirent->d_ino = bdp->d_fileno; 447 linux_dirent->d_off = bdp->d_off; 448 linux_dirent->d_reclen = linuxreclen; 449 /* 450 * Copy d_type to last byte of l_dirent buffer 451 */ 452 lbuf[linuxreclen - 1] = bdp->d_type; 453 strlcpy(linux_dirent->d_name, bdp->d_name, 454 linuxreclen - offsetof(struct l_dirent, d_name)-1); 455 error = copyout(linux_dirent, outp, linuxreclen); 456 if (error != 0) 457 goto out; 458 459 inp += reclen; 460 base += reclen; 461 len -= reclen; 462 463 retval += linuxreclen; 464 outp += linuxreclen; 465 resid -= linuxreclen; 466 } 467 td->td_retval[0] = retval; 468 469 out: 470 free(lbuf, M_LINUX); 471 out1: 472 free(buf, M_LINUX); 473 return (error); 474 } 475 #endif 476 477 int 478 linux_getdents64(struct thread *td, struct linux_getdents64_args *args) 479 { 480 struct dirent *bdp; 481 caddr_t inp, buf; /* BSD-format */ 482 int len, reclen; /* BSD-format */ 483 caddr_t outp; /* Linux-format */ 484 int resid, linuxreclen; /* Linux-format */ 485 off_t base; 486 struct l_dirent64 *linux_dirent64; 487 int buflen, error; 488 size_t retval; 489 490 buflen = min(args->count, MAXBSIZE); 491 buf = malloc(buflen, M_LINUX, M_WAITOK); 492 493 error = kern_getdirentries(td, args->fd, buf, buflen, 494 &base, NULL, UIO_SYSSPACE); 495 if (error != 0) { 496 error = linux_getdents_error(td, args->fd, error); 497 goto out1; 498 } 499 500 linux_dirent64 = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_LINUX, 501 M_WAITOK | M_ZERO); 502 503 len = td->td_retval[0]; 504 inp = buf; 505 outp = (caddr_t)args->dirent; 506 resid = args->count; 507 retval = 0; 508 509 while (len > 0) { 510 bdp = (struct dirent *) inp; 511 reclen = bdp->d_reclen; 512 linuxreclen = LINUX_RECLEN64(bdp->d_namlen); 513 /* 514 * No more space in the user supplied dirent buffer. 515 * Return EINVAL. 516 */ 517 if (resid < linuxreclen) { 518 error = EINVAL; 519 goto out; 520 } 521 522 linux_dirent64->d_ino = bdp->d_fileno; 523 linux_dirent64->d_off = bdp->d_off; 524 linux_dirent64->d_reclen = linuxreclen; 525 linux_dirent64->d_type = bdp->d_type; 526 strlcpy(linux_dirent64->d_name, bdp->d_name, 527 linuxreclen - offsetof(struct l_dirent64, d_name)); 528 error = copyout(linux_dirent64, outp, linuxreclen); 529 if (error != 0) 530 goto out; 531 532 inp += reclen; 533 base += reclen; 534 len -= reclen; 535 536 retval += linuxreclen; 537 outp += linuxreclen; 538 resid -= linuxreclen; 539 } 540 td->td_retval[0] = retval; 541 542 out: 543 free(linux_dirent64, M_LINUX); 544 out1: 545 free(buf, M_LINUX); 546 return (error); 547 } 548 549 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 550 int 551 linux_readdir(struct thread *td, struct linux_readdir_args *args) 552 { 553 struct dirent *bdp; 554 caddr_t buf; /* BSD-format */ 555 int linuxreclen; /* Linux-format */ 556 off_t base; 557 struct l_dirent *linux_dirent; /* Linux-format */ 558 int buflen, error; 559 560 buflen = sizeof(*bdp); 561 buf = malloc(buflen, M_LINUX, M_WAITOK); 562 563 error = kern_getdirentries(td, args->fd, buf, buflen, 564 &base, NULL, UIO_SYSSPACE); 565 if (error != 0) { 566 error = linux_getdents_error(td, args->fd, error); 567 goto out; 568 } 569 if (td->td_retval[0] == 0) 570 goto out; 571 572 linux_dirent = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX, 573 M_WAITOK | M_ZERO); 574 575 bdp = (struct dirent *) buf; 576 linuxreclen = LINUX_RECLEN(bdp->d_namlen); 577 578 linux_dirent->d_ino = bdp->d_fileno; 579 linux_dirent->d_off = bdp->d_off; 580 linux_dirent->d_reclen = bdp->d_namlen; 581 strlcpy(linux_dirent->d_name, bdp->d_name, 582 linuxreclen - offsetof(struct l_dirent, d_name)); 583 error = copyout(linux_dirent, args->dent, linuxreclen); 584 if (error == 0) 585 td->td_retval[0] = linuxreclen; 586 587 free(linux_dirent, M_LINUX); 588 out: 589 free(buf, M_LINUX); 590 return (error); 591 } 592 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 593 594 /* 595 * These exist mainly for hooks for doing /compat/linux translation. 596 */ 597 598 #ifdef LINUX_LEGACY_SYSCALLS 599 int 600 linux_access(struct thread *td, struct linux_access_args *args) 601 { 602 603 /* Linux convention. */ 604 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 605 return (EINVAL); 606 607 return (kern_accessat(td, AT_FDCWD, args->path, UIO_USERSPACE, 0, 608 args->amode)); 609 } 610 #endif 611 612 static int 613 linux_do_accessat(struct thread *td, int ldfd, const char *filename, 614 int amode, int flags) 615 { 616 int dfd; 617 618 /* Linux convention. */ 619 if (amode & ~(F_OK | X_OK | W_OK | R_OK)) 620 return (EINVAL); 621 622 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd; 623 return (kern_accessat(td, dfd, filename, UIO_USERSPACE, flags, amode)); 624 } 625 626 int 627 linux_faccessat(struct thread *td, struct linux_faccessat_args *args) 628 { 629 630 return (linux_do_accessat(td, args->dfd, args->filename, args->amode, 631 0)); 632 } 633 634 int 635 linux_faccessat2(struct thread *td, struct linux_faccessat2_args *args) 636 { 637 int flags, unsupported; 638 639 /* XXX. AT_SYMLINK_NOFOLLOW is not supported by kern_accessat */ 640 unsupported = args->flags & ~(LINUX_AT_EACCESS | LINUX_AT_EMPTY_PATH); 641 if (unsupported != 0) { 642 linux_msg(td, "faccessat2 unsupported flag 0x%x", unsupported); 643 return (EINVAL); 644 } 645 646 flags = (args->flags & LINUX_AT_EACCESS) == 0 ? 0 : 647 AT_EACCESS; 648 flags |= (args->flags & LINUX_AT_EMPTY_PATH) == 0 ? 0 : 649 AT_EMPTY_PATH; 650 return (linux_do_accessat(td, args->dfd, args->filename, args->amode, 651 flags)); 652 } 653 654 655 #ifdef LINUX_LEGACY_SYSCALLS 656 int 657 linux_unlink(struct thread *td, struct linux_unlink_args *args) 658 { 659 int error; 660 struct stat st; 661 662 error = kern_funlinkat(td, AT_FDCWD, args->path, FD_NONE, 663 UIO_USERSPACE, 0, 0); 664 if (error == EPERM) { 665 /* Introduce POSIX noncompliant behaviour of Linux */ 666 if (kern_statat(td, 0, AT_FDCWD, args->path, 667 UIO_USERSPACE, &st) == 0) { 668 if (S_ISDIR(st.st_mode)) 669 error = EISDIR; 670 } 671 } 672 673 return (error); 674 } 675 #endif 676 677 static int 678 linux_unlinkat_impl(struct thread *td, enum uio_seg pathseg, const char *path, 679 int dfd, struct linux_unlinkat_args *args) 680 { 681 struct stat st; 682 int error; 683 684 if (args->flag & LINUX_AT_REMOVEDIR) 685 error = kern_frmdirat(td, dfd, path, FD_NONE, pathseg, 0); 686 else 687 error = kern_funlinkat(td, dfd, path, FD_NONE, pathseg, 0, 0); 688 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) { 689 /* Introduce POSIX noncompliant behaviour of Linux */ 690 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path, 691 pathseg, &st) == 0 && S_ISDIR(st.st_mode)) 692 error = EISDIR; 693 } 694 return (error); 695 } 696 697 int 698 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args) 699 { 700 int dfd; 701 702 if (args->flag & ~LINUX_AT_REMOVEDIR) 703 return (EINVAL); 704 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 705 return (linux_unlinkat_impl(td, UIO_USERSPACE, args->pathname, 706 dfd, args)); 707 } 708 709 int 710 linux_chdir(struct thread *td, struct linux_chdir_args *args) 711 { 712 713 return (kern_chdir(td, args->path, UIO_USERSPACE)); 714 } 715 716 #ifdef LINUX_LEGACY_SYSCALLS 717 int 718 linux_chmod(struct thread *td, struct linux_chmod_args *args) 719 { 720 721 return (kern_fchmodat(td, AT_FDCWD, args->path, UIO_USERSPACE, 722 args->mode, 0)); 723 } 724 #endif 725 726 int 727 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args) 728 { 729 int dfd; 730 731 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 732 return (kern_fchmodat(td, dfd, args->filename, UIO_USERSPACE, 733 args->mode, 0)); 734 } 735 736 #ifdef LINUX_LEGACY_SYSCALLS 737 int 738 linux_mkdir(struct thread *td, struct linux_mkdir_args *args) 739 { 740 741 return (kern_mkdirat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->mode)); 742 } 743 #endif 744 745 int 746 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args) 747 { 748 int dfd; 749 750 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 751 return (kern_mkdirat(td, dfd, args->pathname, UIO_USERSPACE, args->mode)); 752 } 753 754 #ifdef LINUX_LEGACY_SYSCALLS 755 int 756 linux_rmdir(struct thread *td, struct linux_rmdir_args *args) 757 { 758 759 return (kern_frmdirat(td, AT_FDCWD, args->path, FD_NONE, 760 UIO_USERSPACE, 0)); 761 } 762 763 int 764 linux_rename(struct thread *td, struct linux_rename_args *args) 765 { 766 767 return (kern_renameat(td, AT_FDCWD, args->from, AT_FDCWD, 768 args->to, UIO_USERSPACE)); 769 } 770 #endif 771 772 int 773 linux_renameat(struct thread *td, struct linux_renameat_args *args) 774 { 775 struct linux_renameat2_args renameat2_args = { 776 .olddfd = args->olddfd, 777 .oldname = args->oldname, 778 .newdfd = args->newdfd, 779 .newname = args->newname, 780 .flags = 0 781 }; 782 783 return (linux_renameat2(td, &renameat2_args)); 784 } 785 786 int 787 linux_renameat2(struct thread *td, struct linux_renameat2_args *args) 788 { 789 int olddfd, newdfd; 790 791 if (args->flags != 0) { 792 if (args->flags & ~(LINUX_RENAME_EXCHANGE | 793 LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT)) 794 return (EINVAL); 795 if (args->flags & LINUX_RENAME_EXCHANGE && 796 args->flags & (LINUX_RENAME_NOREPLACE | 797 LINUX_RENAME_WHITEOUT)) 798 return (EINVAL); 799 #if 0 800 /* 801 * This spams the console on Ubuntu Focal. 802 * 803 * What's needed here is a general mechanism to let users know 804 * about missing features without hogging the system. 805 */ 806 linux_msg(td, "renameat2 unsupported flags 0x%x", 807 args->flags); 808 #endif 809 return (EINVAL); 810 } 811 812 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 813 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 814 return (kern_renameat(td, olddfd, args->oldname, newdfd, 815 args->newname, UIO_USERSPACE)); 816 } 817 818 #ifdef LINUX_LEGACY_SYSCALLS 819 int 820 linux_symlink(struct thread *td, struct linux_symlink_args *args) 821 { 822 823 return (kern_symlinkat(td, args->path, AT_FDCWD, args->to, 824 UIO_USERSPACE)); 825 } 826 #endif 827 828 int 829 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args) 830 { 831 int dfd; 832 833 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 834 return (kern_symlinkat(td, args->oldname, dfd, args->newname, 835 UIO_USERSPACE)); 836 } 837 838 #ifdef LINUX_LEGACY_SYSCALLS 839 int 840 linux_readlink(struct thread *td, struct linux_readlink_args *args) 841 { 842 843 if (args->count <= 0) 844 return (EINVAL); 845 846 return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE, 847 args->buf, UIO_USERSPACE, args->count)); 848 } 849 #endif 850 851 int 852 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args) 853 { 854 int dfd; 855 856 if (args->bufsiz <= 0) 857 return (EINVAL); 858 859 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 860 return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE, 861 args->buf, UIO_USERSPACE, args->bufsiz)); 862 } 863 864 int 865 linux_truncate(struct thread *td, struct linux_truncate_args *args) 866 { 867 868 return (kern_truncate(td, args->path, UIO_USERSPACE, args->length)); 869 } 870 871 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 872 int 873 linux_truncate64(struct thread *td, struct linux_truncate64_args *args) 874 { 875 off_t length; 876 877 #if defined(__amd64__) && defined(COMPAT_LINUX32) 878 length = PAIR32TO64(off_t, args->length); 879 #else 880 length = args->length; 881 #endif 882 883 return (kern_truncate(td, args->path, UIO_USERSPACE, length)); 884 } 885 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 886 887 int 888 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args) 889 { 890 891 return (kern_ftruncate(td, args->fd, args->length)); 892 } 893 894 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 895 int 896 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args) 897 { 898 off_t length; 899 900 #if defined(__amd64__) && defined(COMPAT_LINUX32) 901 length = PAIR32TO64(off_t, args->length); 902 #else 903 length = args->length; 904 #endif 905 906 return (kern_ftruncate(td, args->fd, length)); 907 } 908 #endif 909 910 #ifdef LINUX_LEGACY_SYSCALLS 911 int 912 linux_link(struct thread *td, struct linux_link_args *args) 913 { 914 915 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to, 916 UIO_USERSPACE, AT_SYMLINK_FOLLOW)); 917 } 918 #endif 919 920 int 921 linux_linkat(struct thread *td, struct linux_linkat_args *args) 922 { 923 int olddfd, newdfd, flag; 924 925 if (args->flag & ~(LINUX_AT_SYMLINK_FOLLOW | LINUX_AT_EMPTY_PATH)) 926 return (EINVAL); 927 928 flag = (args->flag & LINUX_AT_SYMLINK_FOLLOW) != 0 ? AT_SYMLINK_FOLLOW : 929 0; 930 flag |= (args->flag & LINUX_AT_EMPTY_PATH) != 0 ? AT_EMPTY_PATH : 0; 931 932 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 933 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 934 return (kern_linkat(td, olddfd, newdfd, args->oldname, 935 args->newname, UIO_USERSPACE, flag)); 936 } 937 938 int 939 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap) 940 { 941 942 return (kern_fsync(td, uap->fd, false)); 943 } 944 945 int 946 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap) 947 { 948 off_t nbytes, offset; 949 950 #if defined(__amd64__) && defined(COMPAT_LINUX32) 951 nbytes = PAIR32TO64(off_t, uap->nbytes); 952 offset = PAIR32TO64(off_t, uap->offset); 953 #else 954 nbytes = uap->nbytes; 955 offset = uap->offset; 956 #endif 957 958 if (offset < 0 || nbytes < 0 || 959 (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE | 960 LINUX_SYNC_FILE_RANGE_WRITE | 961 LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) { 962 return (EINVAL); 963 } 964 965 return (kern_fsync(td, uap->fd, false)); 966 } 967 968 int 969 linux_pread(struct thread *td, struct linux_pread_args *uap) 970 { 971 struct vnode *vp; 972 off_t offset; 973 int error; 974 975 #if defined(__amd64__) && defined(COMPAT_LINUX32) 976 offset = PAIR32TO64(off_t, uap->offset); 977 #else 978 offset = uap->offset; 979 #endif 980 981 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset); 982 if (error == 0) { 983 /* This seems to violate POSIX but Linux does it. */ 984 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp); 985 if (error != 0) 986 return (error); 987 if (vp->v_type == VDIR) 988 error = EISDIR; 989 vrele(vp); 990 } 991 return (error); 992 } 993 994 int 995 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap) 996 { 997 off_t offset; 998 999 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1000 offset = PAIR32TO64(off_t, uap->offset); 1001 #else 1002 offset = uap->offset; 1003 #endif 1004 1005 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset)); 1006 } 1007 1008 #define HALF_LONG_BITS ((sizeof(l_long) * NBBY / 2)) 1009 1010 static inline off_t 1011 pos_from_hilo(unsigned long high, unsigned long low) 1012 { 1013 1014 return (((off_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; 1015 } 1016 1017 int 1018 linux_preadv(struct thread *td, struct linux_preadv_args *uap) 1019 { 1020 struct uio *auio; 1021 int error; 1022 off_t offset; 1023 1024 /* 1025 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES 1026 * pos_l and pos_h, respectively, contain the 1027 * low order and high order 32 bits of offset. 1028 */ 1029 offset = pos_from_hilo(uap->pos_h, uap->pos_l); 1030 if (offset < 0) 1031 return (EINVAL); 1032 #ifdef COMPAT_LINUX32 1033 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio); 1034 #else 1035 error = copyinuio(uap->vec, uap->vlen, &auio); 1036 #endif 1037 if (error != 0) 1038 return (error); 1039 error = kern_preadv(td, uap->fd, auio, offset); 1040 free(auio, M_IOV); 1041 return (error); 1042 } 1043 1044 int 1045 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap) 1046 { 1047 struct uio *auio; 1048 int error; 1049 off_t offset; 1050 1051 /* 1052 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES 1053 * pos_l and pos_h, respectively, contain the 1054 * low order and high order 32 bits of offset. 1055 */ 1056 offset = pos_from_hilo(uap->pos_h, uap->pos_l); 1057 if (offset < 0) 1058 return (EINVAL); 1059 #ifdef COMPAT_LINUX32 1060 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio); 1061 #else 1062 error = copyinuio(uap->vec, uap->vlen, &auio); 1063 #endif 1064 if (error != 0) 1065 return (error); 1066 error = kern_pwritev(td, uap->fd, auio, offset); 1067 free(auio, M_IOV); 1068 return (linux_enobufs2eagain(td, uap->fd, error)); 1069 } 1070 1071 int 1072 linux_mount(struct thread *td, struct linux_mount_args *args) 1073 { 1074 struct mntarg *ma = NULL; 1075 char *fstypename, *mntonname, *mntfromname, *data; 1076 int error, fsflags; 1077 1078 fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1079 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1080 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1081 data = NULL; 1082 error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1, 1083 NULL); 1084 if (error != 0) 1085 goto out; 1086 if (args->specialfile != NULL) { 1087 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL); 1088 if (error != 0) 1089 goto out; 1090 } else { 1091 mntfromname[0] = '\0'; 1092 } 1093 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL); 1094 if (error != 0) 1095 goto out; 1096 1097 if (strcmp(fstypename, "ext2") == 0) { 1098 strcpy(fstypename, "ext2fs"); 1099 } else if (strcmp(fstypename, "proc") == 0) { 1100 strcpy(fstypename, "linprocfs"); 1101 } else if (strcmp(fstypename, "vfat") == 0) { 1102 strcpy(fstypename, "msdosfs"); 1103 } else if (strcmp(fstypename, "fuse") == 0 || 1104 strncmp(fstypename, "fuse.", 5) == 0) { 1105 char *fuse_options, *fuse_option, *fuse_name; 1106 1107 strcpy(mntfromname, "/dev/fuse"); 1108 strcpy(fstypename, "fusefs"); 1109 data = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1110 error = copyinstr(args->data, data, MNAMELEN - 1, NULL); 1111 if (error != 0) 1112 goto out; 1113 1114 fuse_options = data; 1115 while ((fuse_option = strsep(&fuse_options, ",")) != NULL) { 1116 fuse_name = strsep(&fuse_option, "="); 1117 if (fuse_name == NULL || fuse_option == NULL) 1118 goto out; 1119 ma = mount_arg(ma, fuse_name, fuse_option, -1); 1120 } 1121 1122 /* 1123 * The FUSE server uses Linux errno values instead of FreeBSD 1124 * ones; add a flag to tell fuse(4) to do errno translation. 1125 */ 1126 ma = mount_arg(ma, "linux_errnos", "1", -1); 1127 } 1128 1129 fsflags = 0; 1130 1131 /* 1132 * Linux SYNC flag is not included; the closest equivalent 1133 * FreeBSD has is !ASYNC, which is our default. 1134 */ 1135 if (args->rwflag & LINUX_MS_RDONLY) 1136 fsflags |= MNT_RDONLY; 1137 if (args->rwflag & LINUX_MS_NOSUID) 1138 fsflags |= MNT_NOSUID; 1139 if (args->rwflag & LINUX_MS_NOEXEC) 1140 fsflags |= MNT_NOEXEC; 1141 if (args->rwflag & LINUX_MS_REMOUNT) 1142 fsflags |= MNT_UPDATE; 1143 1144 ma = mount_arg(ma, "fstype", fstypename, -1); 1145 ma = mount_arg(ma, "fspath", mntonname, -1); 1146 ma = mount_arg(ma, "from", mntfromname, -1); 1147 error = kernel_mount(ma, fsflags); 1148 out: 1149 free(fstypename, M_TEMP); 1150 free(mntonname, M_TEMP); 1151 free(mntfromname, M_TEMP); 1152 free(data, M_TEMP); 1153 return (error); 1154 } 1155 1156 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1157 int 1158 linux_oldumount(struct thread *td, struct linux_oldumount_args *args) 1159 { 1160 1161 return (kern_unmount(td, args->path, 0)); 1162 } 1163 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1164 1165 #ifdef LINUX_LEGACY_SYSCALLS 1166 int 1167 linux_umount(struct thread *td, struct linux_umount_args *args) 1168 { 1169 int flags; 1170 1171 flags = 0; 1172 if ((args->flags & LINUX_MNT_FORCE) != 0) { 1173 args->flags &= ~LINUX_MNT_FORCE; 1174 flags |= MNT_FORCE; 1175 } 1176 if (args->flags != 0) { 1177 linux_msg(td, "unsupported umount2 flags %#x", args->flags); 1178 return (EINVAL); 1179 } 1180 1181 return (kern_unmount(td, args->path, flags)); 1182 } 1183 #endif 1184 1185 /* 1186 * fcntl family of syscalls 1187 */ 1188 1189 struct l_flock { 1190 l_short l_type; 1191 l_short l_whence; 1192 l_off_t l_start; 1193 l_off_t l_len; 1194 l_pid_t l_pid; 1195 } 1196 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1197 __packed 1198 #endif 1199 ; 1200 1201 static void 1202 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock) 1203 { 1204 switch (linux_flock->l_type) { 1205 case LINUX_F_RDLCK: 1206 bsd_flock->l_type = F_RDLCK; 1207 break; 1208 case LINUX_F_WRLCK: 1209 bsd_flock->l_type = F_WRLCK; 1210 break; 1211 case LINUX_F_UNLCK: 1212 bsd_flock->l_type = F_UNLCK; 1213 break; 1214 default: 1215 bsd_flock->l_type = -1; 1216 break; 1217 } 1218 bsd_flock->l_whence = linux_flock->l_whence; 1219 bsd_flock->l_start = (off_t)linux_flock->l_start; 1220 bsd_flock->l_len = (off_t)linux_flock->l_len; 1221 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1222 bsd_flock->l_sysid = 0; 1223 } 1224 1225 static void 1226 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock) 1227 { 1228 switch (bsd_flock->l_type) { 1229 case F_RDLCK: 1230 linux_flock->l_type = LINUX_F_RDLCK; 1231 break; 1232 case F_WRLCK: 1233 linux_flock->l_type = LINUX_F_WRLCK; 1234 break; 1235 case F_UNLCK: 1236 linux_flock->l_type = LINUX_F_UNLCK; 1237 break; 1238 } 1239 linux_flock->l_whence = bsd_flock->l_whence; 1240 linux_flock->l_start = (l_off_t)bsd_flock->l_start; 1241 linux_flock->l_len = (l_off_t)bsd_flock->l_len; 1242 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1243 } 1244 1245 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1246 struct l_flock64 { 1247 l_short l_type; 1248 l_short l_whence; 1249 l_loff_t l_start; 1250 l_loff_t l_len; 1251 l_pid_t l_pid; 1252 } 1253 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1254 __packed 1255 #endif 1256 ; 1257 1258 static void 1259 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock) 1260 { 1261 switch (linux_flock->l_type) { 1262 case LINUX_F_RDLCK: 1263 bsd_flock->l_type = F_RDLCK; 1264 break; 1265 case LINUX_F_WRLCK: 1266 bsd_flock->l_type = F_WRLCK; 1267 break; 1268 case LINUX_F_UNLCK: 1269 bsd_flock->l_type = F_UNLCK; 1270 break; 1271 default: 1272 bsd_flock->l_type = -1; 1273 break; 1274 } 1275 bsd_flock->l_whence = linux_flock->l_whence; 1276 bsd_flock->l_start = (off_t)linux_flock->l_start; 1277 bsd_flock->l_len = (off_t)linux_flock->l_len; 1278 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1279 bsd_flock->l_sysid = 0; 1280 } 1281 1282 static void 1283 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock) 1284 { 1285 switch (bsd_flock->l_type) { 1286 case F_RDLCK: 1287 linux_flock->l_type = LINUX_F_RDLCK; 1288 break; 1289 case F_WRLCK: 1290 linux_flock->l_type = LINUX_F_WRLCK; 1291 break; 1292 case F_UNLCK: 1293 linux_flock->l_type = LINUX_F_UNLCK; 1294 break; 1295 } 1296 linux_flock->l_whence = bsd_flock->l_whence; 1297 linux_flock->l_start = (l_loff_t)bsd_flock->l_start; 1298 linux_flock->l_len = (l_loff_t)bsd_flock->l_len; 1299 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1300 } 1301 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1302 1303 static int 1304 fcntl_common(struct thread *td, struct linux_fcntl_args *args) 1305 { 1306 struct l_flock linux_flock; 1307 struct flock bsd_flock; 1308 struct pipe *fpipe; 1309 struct file *fp; 1310 long arg; 1311 int error, result; 1312 1313 switch (args->cmd) { 1314 case LINUX_F_DUPFD: 1315 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg)); 1316 1317 case LINUX_F_GETFD: 1318 return (kern_fcntl(td, args->fd, F_GETFD, 0)); 1319 1320 case LINUX_F_SETFD: 1321 return (kern_fcntl(td, args->fd, F_SETFD, args->arg)); 1322 1323 case LINUX_F_GETFL: 1324 error = kern_fcntl(td, args->fd, F_GETFL, 0); 1325 result = td->td_retval[0]; 1326 td->td_retval[0] = 0; 1327 if (result & O_RDONLY) 1328 td->td_retval[0] |= LINUX_O_RDONLY; 1329 if (result & O_WRONLY) 1330 td->td_retval[0] |= LINUX_O_WRONLY; 1331 if (result & O_RDWR) 1332 td->td_retval[0] |= LINUX_O_RDWR; 1333 if (result & O_NDELAY) 1334 td->td_retval[0] |= LINUX_O_NONBLOCK; 1335 if (result & O_APPEND) 1336 td->td_retval[0] |= LINUX_O_APPEND; 1337 if (result & O_FSYNC) 1338 td->td_retval[0] |= LINUX_O_SYNC; 1339 if (result & O_ASYNC) 1340 td->td_retval[0] |= LINUX_O_ASYNC; 1341 #ifdef LINUX_O_NOFOLLOW 1342 if (result & O_NOFOLLOW) 1343 td->td_retval[0] |= LINUX_O_NOFOLLOW; 1344 #endif 1345 #ifdef LINUX_O_DIRECT 1346 if (result & O_DIRECT) 1347 td->td_retval[0] |= LINUX_O_DIRECT; 1348 #endif 1349 return (error); 1350 1351 case LINUX_F_SETFL: 1352 arg = 0; 1353 if (args->arg & LINUX_O_NDELAY) 1354 arg |= O_NONBLOCK; 1355 if (args->arg & LINUX_O_APPEND) 1356 arg |= O_APPEND; 1357 if (args->arg & LINUX_O_SYNC) 1358 arg |= O_FSYNC; 1359 if (args->arg & LINUX_O_ASYNC) 1360 arg |= O_ASYNC; 1361 #ifdef LINUX_O_NOFOLLOW 1362 if (args->arg & LINUX_O_NOFOLLOW) 1363 arg |= O_NOFOLLOW; 1364 #endif 1365 #ifdef LINUX_O_DIRECT 1366 if (args->arg & LINUX_O_DIRECT) 1367 arg |= O_DIRECT; 1368 #endif 1369 return (kern_fcntl(td, args->fd, F_SETFL, arg)); 1370 1371 case LINUX_F_GETLK: 1372 error = copyin((void *)args->arg, &linux_flock, 1373 sizeof(linux_flock)); 1374 if (error) 1375 return (error); 1376 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1377 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1378 if (error) 1379 return (error); 1380 bsd_to_linux_flock(&bsd_flock, &linux_flock); 1381 return (copyout(&linux_flock, (void *)args->arg, 1382 sizeof(linux_flock))); 1383 1384 case LINUX_F_SETLK: 1385 error = copyin((void *)args->arg, &linux_flock, 1386 sizeof(linux_flock)); 1387 if (error) 1388 return (error); 1389 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1390 return (kern_fcntl(td, args->fd, F_SETLK, 1391 (intptr_t)&bsd_flock)); 1392 1393 case LINUX_F_SETLKW: 1394 error = copyin((void *)args->arg, &linux_flock, 1395 sizeof(linux_flock)); 1396 if (error) 1397 return (error); 1398 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1399 return (kern_fcntl(td, args->fd, F_SETLKW, 1400 (intptr_t)&bsd_flock)); 1401 1402 case LINUX_F_GETOWN: 1403 return (kern_fcntl(td, args->fd, F_GETOWN, 0)); 1404 1405 case LINUX_F_SETOWN: 1406 /* 1407 * XXX some Linux applications depend on F_SETOWN having no 1408 * significant effect for pipes (SIGIO is not delivered for 1409 * pipes under Linux-2.2.35 at least). 1410 */ 1411 error = fget(td, args->fd, 1412 &cap_fcntl_rights, &fp); 1413 if (error) 1414 return (error); 1415 if (fp->f_type == DTYPE_PIPE) { 1416 fdrop(fp, td); 1417 return (EINVAL); 1418 } 1419 fdrop(fp, td); 1420 1421 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg)); 1422 1423 case LINUX_F_DUPFD_CLOEXEC: 1424 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg)); 1425 /* 1426 * Our F_SEAL_* values match Linux one for maximum compatibility. So we 1427 * only needed to account for different values for fcntl(2) commands. 1428 */ 1429 case LINUX_F_GET_SEALS: 1430 error = kern_fcntl(td, args->fd, F_GET_SEALS, 0); 1431 if (error != 0) 1432 return (error); 1433 td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0], 1434 seal_bitmap, 0); 1435 return (0); 1436 1437 case LINUX_F_ADD_SEALS: 1438 return (kern_fcntl(td, args->fd, F_ADD_SEALS, 1439 linux_to_bsd_bits(args->arg, seal_bitmap, 0))); 1440 1441 case LINUX_F_GETPIPE_SZ: 1442 error = fget(td, args->fd, 1443 &cap_fcntl_rights, &fp); 1444 if (error != 0) 1445 return (error); 1446 if (fp->f_type != DTYPE_PIPE) { 1447 fdrop(fp, td); 1448 return (EINVAL); 1449 } 1450 fpipe = fp->f_data; 1451 td->td_retval[0] = fpipe->pipe_buffer.size; 1452 fdrop(fp, td); 1453 return (0); 1454 1455 default: 1456 linux_msg(td, "unsupported fcntl cmd %d", args->cmd); 1457 return (EINVAL); 1458 } 1459 } 1460 1461 int 1462 linux_fcntl(struct thread *td, struct linux_fcntl_args *args) 1463 { 1464 1465 return (fcntl_common(td, args)); 1466 } 1467 1468 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1469 int 1470 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args) 1471 { 1472 struct l_flock64 linux_flock; 1473 struct flock bsd_flock; 1474 struct linux_fcntl_args fcntl_args; 1475 int error; 1476 1477 switch (args->cmd) { 1478 case LINUX_F_GETLK64: 1479 error = copyin((void *)args->arg, &linux_flock, 1480 sizeof(linux_flock)); 1481 if (error) 1482 return (error); 1483 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1484 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1485 if (error) 1486 return (error); 1487 bsd_to_linux_flock64(&bsd_flock, &linux_flock); 1488 return (copyout(&linux_flock, (void *)args->arg, 1489 sizeof(linux_flock))); 1490 1491 case LINUX_F_SETLK64: 1492 error = copyin((void *)args->arg, &linux_flock, 1493 sizeof(linux_flock)); 1494 if (error) 1495 return (error); 1496 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1497 return (kern_fcntl(td, args->fd, F_SETLK, 1498 (intptr_t)&bsd_flock)); 1499 1500 case LINUX_F_SETLKW64: 1501 error = copyin((void *)args->arg, &linux_flock, 1502 sizeof(linux_flock)); 1503 if (error) 1504 return (error); 1505 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1506 return (kern_fcntl(td, args->fd, F_SETLKW, 1507 (intptr_t)&bsd_flock)); 1508 } 1509 1510 fcntl_args.fd = args->fd; 1511 fcntl_args.cmd = args->cmd; 1512 fcntl_args.arg = args->arg; 1513 return (fcntl_common(td, &fcntl_args)); 1514 } 1515 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1516 1517 #ifdef LINUX_LEGACY_SYSCALLS 1518 int 1519 linux_chown(struct thread *td, struct linux_chown_args *args) 1520 { 1521 1522 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, 1523 args->uid, args->gid, 0)); 1524 } 1525 #endif 1526 1527 int 1528 linux_fchownat(struct thread *td, struct linux_fchownat_args *args) 1529 { 1530 int dfd, flag, unsupported; 1531 1532 unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH); 1533 if (unsupported != 0) { 1534 linux_msg(td, "fchownat unsupported flag 0x%x", unsupported); 1535 return (EINVAL); 1536 } 1537 1538 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 : 1539 AT_SYMLINK_NOFOLLOW; 1540 flag |= (args->flag & LINUX_AT_EMPTY_PATH) == 0 ? 0 : 1541 AT_EMPTY_PATH; 1542 1543 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1544 return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE, 1545 args->uid, args->gid, flag)); 1546 } 1547 1548 #ifdef LINUX_LEGACY_SYSCALLS 1549 int 1550 linux_lchown(struct thread *td, struct linux_lchown_args *args) 1551 { 1552 1553 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid, 1554 args->gid, AT_SYMLINK_NOFOLLOW)); 1555 } 1556 #endif 1557 1558 static int 1559 convert_fadvice(int advice) 1560 { 1561 switch (advice) { 1562 case LINUX_POSIX_FADV_NORMAL: 1563 return (POSIX_FADV_NORMAL); 1564 case LINUX_POSIX_FADV_RANDOM: 1565 return (POSIX_FADV_RANDOM); 1566 case LINUX_POSIX_FADV_SEQUENTIAL: 1567 return (POSIX_FADV_SEQUENTIAL); 1568 case LINUX_POSIX_FADV_WILLNEED: 1569 return (POSIX_FADV_WILLNEED); 1570 case LINUX_POSIX_FADV_DONTNEED: 1571 return (POSIX_FADV_DONTNEED); 1572 case LINUX_POSIX_FADV_NOREUSE: 1573 return (POSIX_FADV_NOREUSE); 1574 default: 1575 return (-1); 1576 } 1577 } 1578 1579 int 1580 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args) 1581 { 1582 off_t offset; 1583 int advice; 1584 1585 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1586 offset = PAIR32TO64(off_t, args->offset); 1587 #else 1588 offset = args->offset; 1589 #endif 1590 1591 advice = convert_fadvice(args->advice); 1592 if (advice == -1) 1593 return (EINVAL); 1594 return (kern_posix_fadvise(td, args->fd, offset, args->len, advice)); 1595 } 1596 1597 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1598 int 1599 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args) 1600 { 1601 off_t len, offset; 1602 int advice; 1603 1604 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1605 len = PAIR32TO64(off_t, args->len); 1606 offset = PAIR32TO64(off_t, args->offset); 1607 #else 1608 len = args->len; 1609 offset = args->offset; 1610 #endif 1611 1612 advice = convert_fadvice(args->advice); 1613 if (advice == -1) 1614 return (EINVAL); 1615 return (kern_posix_fadvise(td, args->fd, offset, len, advice)); 1616 } 1617 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1618 1619 #ifdef LINUX_LEGACY_SYSCALLS 1620 int 1621 linux_pipe(struct thread *td, struct linux_pipe_args *args) 1622 { 1623 int fildes[2]; 1624 int error; 1625 1626 error = kern_pipe(td, fildes, 0, NULL, NULL); 1627 if (error != 0) 1628 return (error); 1629 1630 error = copyout(fildes, args->pipefds, sizeof(fildes)); 1631 if (error != 0) { 1632 (void)kern_close(td, fildes[0]); 1633 (void)kern_close(td, fildes[1]); 1634 } 1635 1636 return (error); 1637 } 1638 #endif 1639 1640 int 1641 linux_pipe2(struct thread *td, struct linux_pipe2_args *args) 1642 { 1643 int fildes[2]; 1644 int error, flags; 1645 1646 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0) 1647 return (EINVAL); 1648 1649 flags = 0; 1650 if ((args->flags & LINUX_O_NONBLOCK) != 0) 1651 flags |= O_NONBLOCK; 1652 if ((args->flags & LINUX_O_CLOEXEC) != 0) 1653 flags |= O_CLOEXEC; 1654 error = kern_pipe(td, fildes, flags, NULL, NULL); 1655 if (error != 0) 1656 return (error); 1657 1658 error = copyout(fildes, args->pipefds, sizeof(fildes)); 1659 if (error != 0) { 1660 (void)kern_close(td, fildes[0]); 1661 (void)kern_close(td, fildes[1]); 1662 } 1663 1664 return (error); 1665 } 1666 1667 int 1668 linux_dup3(struct thread *td, struct linux_dup3_args *args) 1669 { 1670 int cmd; 1671 intptr_t newfd; 1672 1673 if (args->oldfd == args->newfd) 1674 return (EINVAL); 1675 if ((args->flags & ~LINUX_O_CLOEXEC) != 0) 1676 return (EINVAL); 1677 if (args->flags & LINUX_O_CLOEXEC) 1678 cmd = F_DUP2FD_CLOEXEC; 1679 else 1680 cmd = F_DUP2FD; 1681 1682 newfd = args->newfd; 1683 return (kern_fcntl(td, args->oldfd, cmd, newfd)); 1684 } 1685 1686 int 1687 linux_fallocate(struct thread *td, struct linux_fallocate_args *args) 1688 { 1689 off_t len, offset; 1690 1691 /* 1692 * We emulate only posix_fallocate system call for which 1693 * mode should be 0. 1694 */ 1695 if (args->mode != 0) 1696 return (EOPNOTSUPP); 1697 1698 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1699 len = PAIR32TO64(off_t, args->len); 1700 offset = PAIR32TO64(off_t, args->offset); 1701 #else 1702 len = args->len; 1703 offset = args->offset; 1704 #endif 1705 1706 return (kern_posix_fallocate(td, args->fd, offset, len)); 1707 } 1708 1709 int 1710 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args 1711 *args) 1712 { 1713 l_loff_t inoff, outoff, *inoffp, *outoffp; 1714 int error, flags; 1715 1716 /* 1717 * copy_file_range(2) on Linux doesn't define any flags (yet), so is 1718 * the native implementation. Enforce it. 1719 */ 1720 if (args->flags != 0) { 1721 linux_msg(td, "copy_file_range unsupported flags 0x%x", 1722 args->flags); 1723 return (EINVAL); 1724 } 1725 flags = 0; 1726 inoffp = outoffp = NULL; 1727 if (args->off_in != NULL) { 1728 error = copyin(args->off_in, &inoff, sizeof(l_loff_t)); 1729 if (error != 0) 1730 return (error); 1731 inoffp = &inoff; 1732 } 1733 if (args->off_out != NULL) { 1734 error = copyin(args->off_out, &outoff, sizeof(l_loff_t)); 1735 if (error != 0) 1736 return (error); 1737 outoffp = &outoff; 1738 } 1739 1740 error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out, 1741 outoffp, args->len, flags); 1742 if (error == 0 && args->off_in != NULL) 1743 error = copyout(inoffp, args->off_in, sizeof(l_loff_t)); 1744 if (error == 0 && args->off_out != NULL) 1745 error = copyout(outoffp, args->off_out, sizeof(l_loff_t)); 1746 return (error); 1747 } 1748 1749 #define LINUX_MEMFD_PREFIX "memfd:" 1750 1751 int 1752 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args) 1753 { 1754 char memfd_name[LINUX_NAME_MAX + 1]; 1755 int error, flags, shmflags, oflags; 1756 1757 /* 1758 * This is our clever trick to avoid the heap allocation to copy in the 1759 * uname. We don't really need to go this far out of our way, but it 1760 * does keep the rest of this function fairly clean as they don't have 1761 * to worry about cleanup on the way out. 1762 */ 1763 error = copyinstr(args->uname_ptr, 1764 memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1, 1765 LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL); 1766 if (error != 0) { 1767 if (error == ENAMETOOLONG) 1768 error = EINVAL; 1769 return (error); 1770 } 1771 1772 memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1); 1773 flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0); 1774 if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | 1775 MFD_HUGE_MASK)) != 0) 1776 return (EINVAL); 1777 /* Size specified but no HUGETLB. */ 1778 if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0) 1779 return (EINVAL); 1780 /* We don't actually support HUGETLB. */ 1781 if ((flags & MFD_HUGETLB) != 0) 1782 return (ENOSYS); 1783 oflags = O_RDWR; 1784 shmflags = SHM_GROW_ON_WRITE; 1785 if ((flags & MFD_CLOEXEC) != 0) 1786 oflags |= O_CLOEXEC; 1787 if ((flags & MFD_ALLOW_SEALING) != 0) 1788 shmflags |= SHM_ALLOW_SEALING; 1789 return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL, 1790 memfd_name)); 1791 } 1792 1793 int 1794 linux_splice(struct thread *td, struct linux_splice_args *args) 1795 { 1796 1797 linux_msg(td, "syscall splice not really implemented"); 1798 1799 /* 1800 * splice(2) is documented to return EINVAL in various circumstances; 1801 * returning it instead of ENOSYS should hint the caller to use fallback 1802 * instead. 1803 */ 1804 return (EINVAL); 1805 } 1806 1807 int 1808 linux_close_range(struct thread *td, struct linux_close_range_args *args) 1809 { 1810 u_int flags = 0; 1811 1812 /* 1813 * Implementing close_range(CLOSE_RANGE_UNSHARE) allows Linux to 1814 * unshare filedesc table of the calling thread from others threads 1815 * in a thread group (i.e., process in the FreeBSD) or others processes, 1816 * which shares the same table, before closing the files. FreeBSD does 1817 * not have compatible unsharing mechanism due to the fact that sharing 1818 * process resources, including filedesc table, is at thread level in the 1819 * Linux, while in the FreeBSD it is at the process level. 1820 * Return EINVAL for now if the CLOSE_RANGE_UNSHARE flag is specified 1821 * until this new Linux API stabilizes. 1822 */ 1823 1824 if ((args->flags & ~(LINUX_CLOSE_RANGE_CLOEXEC)) != 0) 1825 return (EINVAL); 1826 if (args->first > args->last) 1827 return (EINVAL); 1828 if ((args->flags & LINUX_CLOSE_RANGE_CLOEXEC) != 0) 1829 flags |= CLOSE_RANGE_CLOEXEC; 1830 return (kern_close_range(td, flags, args->first, args->last)); 1831 } 1832 1833 int 1834 linux_enobufs2eagain(struct thread *td, int fd, int error) 1835 { 1836 struct file *fp; 1837 1838 if (error != ENOBUFS) 1839 return (error); 1840 if (fget(td, fd, &cap_no_rights, &fp) != 0) 1841 return (error); 1842 if (fp->f_type == DTYPE_SOCKET && (fp->f_flag & FNONBLOCK) != 0) 1843 error = EAGAIN; 1844 fdrop(fp, td); 1845 return (error); 1846 } 1847 1848 int 1849 linux_write(struct thread *td, struct linux_write_args *args) 1850 { 1851 struct write_args bargs = { 1852 .fd = args->fd, 1853 .buf = args->buf, 1854 .nbyte = args->nbyte, 1855 }; 1856 1857 return (linux_enobufs2eagain(td, args->fd, sys_write(td, &bargs))); 1858 } 1859 1860 int 1861 linux_writev(struct thread *td, struct linux_writev_args *args) 1862 { 1863 struct uio *auio; 1864 int error; 1865 1866 #ifdef COMPAT_LINUX32 1867 error = freebsd32_copyinuio(PTRIN(args->iovp), args->iovcnt, &auio); 1868 #else 1869 error = copyinuio(args->iovp, args->iovcnt, &auio); 1870 #endif 1871 if (error != 0) 1872 return (error); 1873 error = kern_writev(td, args->fd, auio); 1874 free(auio, M_IOV); 1875 return (linux_enobufs2eagain(td, args->fd, error)); 1876 } 1877