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