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