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