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