1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 1994-1995 Søren Schmidt
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/fcntl.h>
34 #include <sys/jail.h>
35 #include <sys/imgact.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/msgbuf.h>
39 #include <sys/mqueue.h>
40 #include <sys/mutex.h>
41 #include <sys/poll.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/procctl.h>
45 #include <sys/reboot.h>
46 #include <sys/random.h>
47 #include <sys/resourcevar.h>
48 #include <sys/rtprio.h>
49 #include <sys/sched.h>
50 #include <sys/smp.h>
51 #include <sys/stat.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysent.h>
55 #include <sys/sysproto.h>
56 #include <sys/time.h>
57 #include <sys/unistd.h>
58 #include <sys/vmmeter.h>
59 #include <sys/vnode.h>
60
61 #include <security/audit/audit.h>
62 #include <security/mac/mac_framework.h>
63
64 #include <vm/pmap.h>
65 #include <vm/vm_map.h>
66 #include <vm/swap_pager.h>
67
68 #ifdef COMPAT_LINUX32
69 #include <machine/../linux32/linux.h>
70 #include <machine/../linux32/linux32_proto.h>
71 #else
72 #include <machine/../linux/linux.h>
73 #include <machine/../linux/linux_proto.h>
74 #endif
75
76 #include <compat/linux/linux_common.h>
77 #include <compat/linux/linux_dtrace.h>
78 #include <compat/linux/linux_file.h>
79 #include <compat/linux/linux_mib.h>
80 #include <compat/linux/linux_mmap.h>
81 #include <compat/linux/linux_signal.h>
82 #include <compat/linux/linux_time.h>
83 #include <compat/linux/linux_util.h>
84 #include <compat/linux/linux_emul.h>
85 #include <compat/linux/linux_misc.h>
86
87 int stclohz; /* Statistics clock frequency */
88
89 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
90 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
91 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
92 RLIMIT_MEMLOCK, RLIMIT_AS
93 };
94
95 struct l_sysinfo {
96 l_long uptime; /* Seconds since boot */
97 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
98 #define LINUX_SYSINFO_LOADS_SCALE 65536
99 l_ulong totalram; /* Total usable main memory size */
100 l_ulong freeram; /* Available memory size */
101 l_ulong sharedram; /* Amount of shared memory */
102 l_ulong bufferram; /* Memory used by buffers */
103 l_ulong totalswap; /* Total swap space size */
104 l_ulong freeswap; /* swap space still available */
105 l_ushort procs; /* Number of current processes */
106 l_ushort pads;
107 l_ulong totalhigh;
108 l_ulong freehigh;
109 l_uint mem_unit;
110 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
111 };
112
113 struct l_pselect6arg {
114 l_uintptr_t ss;
115 l_size_t ss_len;
116 };
117
118 static int linux_utimensat_lts_to_ts(struct l_timespec *,
119 struct timespec *);
120 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
121 static int linux_utimensat_lts64_to_ts(struct l_timespec64 *,
122 struct timespec *);
123 #endif
124 static int linux_common_utimensat(struct thread *, int,
125 const char *, struct timespec *, int);
126 static int linux_common_pselect6(struct thread *, l_int,
127 l_fd_set *, l_fd_set *, l_fd_set *,
128 struct timespec *, l_uintptr_t *);
129 static int linux_common_ppoll(struct thread *, struct pollfd *,
130 uint32_t, struct timespec *, l_sigset_t *,
131 l_size_t);
132 static int linux_pollin(struct thread *, struct pollfd *,
133 struct pollfd *, u_int);
134 static int linux_pollout(struct thread *, struct pollfd *,
135 struct pollfd *, u_int);
136
137 int
linux_sysinfo(struct thread * td,struct linux_sysinfo_args * args)138 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
139 {
140 struct l_sysinfo sysinfo;
141 int i, j;
142 struct timespec ts;
143
144 bzero(&sysinfo, sizeof(sysinfo));
145 getnanouptime(&ts);
146 if (ts.tv_nsec != 0)
147 ts.tv_sec++;
148 sysinfo.uptime = ts.tv_sec;
149
150 /* Use the information from the mib to get our load averages */
151 for (i = 0; i < 3; i++)
152 sysinfo.loads[i] = averunnable.ldavg[i] *
153 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
154
155 sysinfo.totalram = physmem * PAGE_SIZE;
156 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE;
157
158 /*
159 * sharedram counts pages allocated to named, swap-backed objects such
160 * as shared memory segments and tmpfs files. There is no cheap way to
161 * compute this, so just leave the field unpopulated. Linux itself only
162 * started setting this field in the 3.x timeframe.
163 */
164 sysinfo.sharedram = 0;
165 sysinfo.bufferram = 0;
166
167 swap_pager_status(&i, &j);
168 sysinfo.totalswap = i * PAGE_SIZE;
169 sysinfo.freeswap = (i - j) * PAGE_SIZE;
170
171 sysinfo.procs = nprocs;
172
173 /*
174 * Platforms supported by the emulation layer do not have a notion of
175 * high memory.
176 */
177 sysinfo.totalhigh = 0;
178 sysinfo.freehigh = 0;
179
180 sysinfo.mem_unit = 1;
181
182 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
183 }
184
185 #ifdef LINUX_LEGACY_SYSCALLS
186 int
linux_alarm(struct thread * td,struct linux_alarm_args * args)187 linux_alarm(struct thread *td, struct linux_alarm_args *args)
188 {
189 struct itimerval it, old_it;
190 u_int secs;
191 int error __diagused;
192
193 secs = args->secs;
194 /*
195 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
196 * to match kern_setitimer()'s limit to avoid error from it.
197 *
198 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
199 * platforms.
200 */
201 if (secs > INT32_MAX / 2)
202 secs = INT32_MAX / 2;
203
204 it.it_value.tv_sec = secs;
205 it.it_value.tv_usec = 0;
206 timevalclear(&it.it_interval);
207 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
208 KASSERT(error == 0, ("kern_setitimer returns %d", error));
209
210 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
211 old_it.it_value.tv_usec >= 500000)
212 old_it.it_value.tv_sec++;
213 td->td_retval[0] = old_it.it_value.tv_sec;
214 return (0);
215 }
216 #endif
217
218 int
linux_brk(struct thread * td,struct linux_brk_args * args)219 linux_brk(struct thread *td, struct linux_brk_args *args)
220 {
221 struct vmspace *vm = td->td_proc->p_vmspace;
222 uintptr_t new, old;
223
224 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
225 new = (uintptr_t)args->dsend;
226 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
227 td->td_retval[0] = (register_t)new;
228 else
229 td->td_retval[0] = (register_t)old;
230
231 return (0);
232 }
233
234 #ifdef LINUX_LEGACY_SYSCALLS
235 int
linux_select(struct thread * td,struct linux_select_args * args)236 linux_select(struct thread *td, struct linux_select_args *args)
237 {
238 l_timeval ltv;
239 struct timeval tv0, tv1, utv, *tvp;
240 int error;
241
242 /*
243 * Store current time for computation of the amount of
244 * time left.
245 */
246 if (args->timeout) {
247 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
248 goto select_out;
249 utv.tv_sec = ltv.tv_sec;
250 utv.tv_usec = ltv.tv_usec;
251
252 if (itimerfix(&utv)) {
253 /*
254 * The timeval was invalid. Convert it to something
255 * valid that will act as it does under Linux.
256 */
257 utv.tv_sec += utv.tv_usec / 1000000;
258 utv.tv_usec %= 1000000;
259 if (utv.tv_usec < 0) {
260 utv.tv_sec -= 1;
261 utv.tv_usec += 1000000;
262 }
263 if (utv.tv_sec < 0)
264 timevalclear(&utv);
265 }
266 microtime(&tv0);
267 tvp = &utv;
268 } else
269 tvp = NULL;
270
271 error = kern_select(td, args->nfds, args->readfds, args->writefds,
272 args->exceptfds, tvp, LINUX_NFDBITS);
273 if (error)
274 goto select_out;
275
276 if (args->timeout) {
277 if (td->td_retval[0]) {
278 /*
279 * Compute how much time was left of the timeout,
280 * by subtracting the current time and the time
281 * before we started the call, and subtracting
282 * that result from the user-supplied value.
283 */
284 microtime(&tv1);
285 timevalsub(&tv1, &tv0);
286 timevalsub(&utv, &tv1);
287 if (utv.tv_sec < 0)
288 timevalclear(&utv);
289 } else
290 timevalclear(&utv);
291 ltv.tv_sec = utv.tv_sec;
292 ltv.tv_usec = utv.tv_usec;
293 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
294 goto select_out;
295 }
296
297 select_out:
298 return (error);
299 }
300 #endif
301
302 int
linux_mremap(struct thread * td,struct linux_mremap_args * args)303 linux_mremap(struct thread *td, struct linux_mremap_args *args)
304 {
305 uintptr_t addr;
306 size_t len;
307 int error = 0;
308
309 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
310 td->td_retval[0] = 0;
311 return (EINVAL);
312 }
313
314 /*
315 * Check for the page alignment.
316 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
317 */
318 if (args->addr & PAGE_MASK) {
319 td->td_retval[0] = 0;
320 return (EINVAL);
321 }
322
323 args->new_len = round_page(args->new_len);
324 args->old_len = round_page(args->old_len);
325
326 if (args->new_len > args->old_len) {
327 td->td_retval[0] = 0;
328 return (ENOMEM);
329 }
330
331 if (args->new_len < args->old_len) {
332 addr = args->addr + args->new_len;
333 len = args->old_len - args->new_len;
334 error = kern_munmap(td, addr, len);
335 }
336
337 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
338 return (error);
339 }
340
341 #define LINUX_MS_ASYNC 0x0001
342 #define LINUX_MS_INVALIDATE 0x0002
343 #define LINUX_MS_SYNC 0x0004
344
345 int
linux_msync(struct thread * td,struct linux_msync_args * args)346 linux_msync(struct thread *td, struct linux_msync_args *args)
347 {
348
349 return (kern_msync(td, args->addr, args->len,
350 args->fl & ~LINUX_MS_SYNC));
351 }
352
353 int
linux_mprotect(struct thread * td,struct linux_mprotect_args * uap)354 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
355 {
356
357 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len,
358 uap->prot));
359 }
360
361 int
linux_madvise(struct thread * td,struct linux_madvise_args * uap)362 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
363 {
364
365 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len,
366 uap->behav));
367 }
368
369 int
linux_mmap2(struct thread * td,struct linux_mmap2_args * uap)370 linux_mmap2(struct thread *td, struct linux_mmap2_args *uap)
371 {
372 #if defined(LINUX_ARCHWANT_MMAP2PGOFF)
373 /*
374 * For architectures with sizeof (off_t) < sizeof (loff_t) mmap is
375 * implemented with mmap2 syscall and the offset is represented in
376 * multiples of page size.
377 */
378 return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot,
379 uap->flags, uap->fd, (uint64_t)(uint32_t)uap->pgoff * PAGE_SIZE));
380 #else
381 return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot,
382 uap->flags, uap->fd, uap->pgoff));
383 #endif
384 }
385
386 #ifdef LINUX_LEGACY_SYSCALLS
387 int
linux_time(struct thread * td,struct linux_time_args * args)388 linux_time(struct thread *td, struct linux_time_args *args)
389 {
390 struct timeval tv;
391 l_time_t tm;
392 int error;
393
394 microtime(&tv);
395 tm = tv.tv_sec;
396 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
397 return (error);
398 td->td_retval[0] = tm;
399 return (0);
400 }
401 #endif
402
403 struct l_times_argv {
404 l_clock_t tms_utime;
405 l_clock_t tms_stime;
406 l_clock_t tms_cutime;
407 l_clock_t tms_cstime;
408 };
409
410 /*
411 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
412 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
413 * auxiliary vector entry.
414 */
415 #define CLK_TCK 100
416
417 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
418 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
419
420 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER(2,4,0) ? \
421 CONVNTCK(r) : CONVOTCK(r))
422
423 int
linux_times(struct thread * td,struct linux_times_args * args)424 linux_times(struct thread *td, struct linux_times_args *args)
425 {
426 struct timeval tv, utime, stime, cutime, cstime;
427 struct l_times_argv tms;
428 struct proc *p;
429 int error;
430
431 if (args->buf != NULL) {
432 p = td->td_proc;
433 PROC_LOCK(p);
434 PROC_STATLOCK(p);
435 calcru(p, &utime, &stime);
436 PROC_STATUNLOCK(p);
437 calccru(p, &cutime, &cstime);
438 PROC_UNLOCK(p);
439
440 tms.tms_utime = CONVTCK(utime);
441 tms.tms_stime = CONVTCK(stime);
442
443 tms.tms_cutime = CONVTCK(cutime);
444 tms.tms_cstime = CONVTCK(cstime);
445
446 if ((error = copyout(&tms, args->buf, sizeof(tms))))
447 return (error);
448 }
449
450 microuptime(&tv);
451 td->td_retval[0] = (int)CONVTCK(tv);
452 return (0);
453 }
454
455 int
linux_newuname(struct thread * td,struct linux_newuname_args * args)456 linux_newuname(struct thread *td, struct linux_newuname_args *args)
457 {
458 struct l_new_utsname utsname;
459 char osname[LINUX_MAX_UTSNAME];
460 char osrelease[LINUX_MAX_UTSNAME];
461 char *p;
462
463 linux_get_osname(td, osname);
464 linux_get_osrelease(td, osrelease);
465
466 bzero(&utsname, sizeof(utsname));
467 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
468 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
469 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
470 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
471 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
472 for (p = utsname.version; *p != '\0'; ++p)
473 if (*p == '\n') {
474 *p = '\0';
475 break;
476 }
477 #if defined(__amd64__)
478 /*
479 * On amd64, Linux uname(2) needs to return "x86_64"
480 * for both 64-bit and 32-bit applications. On 32-bit,
481 * the string returned by getauxval(AT_PLATFORM) needs
482 * to remain "i686", though.
483 */
484 #if defined(COMPAT_LINUX32)
485 if (linux32_emulate_i386)
486 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
487 else
488 #endif
489 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
490 #elif defined(__aarch64__)
491 strlcpy(utsname.machine, "aarch64", LINUX_MAX_UTSNAME);
492 #elif defined(__i386__)
493 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
494 #endif
495
496 return (copyout(&utsname, args->buf, sizeof(utsname)));
497 }
498
499 struct l_utimbuf {
500 l_time_t l_actime;
501 l_time_t l_modtime;
502 };
503
504 #ifdef LINUX_LEGACY_SYSCALLS
505 int
linux_utime(struct thread * td,struct linux_utime_args * args)506 linux_utime(struct thread *td, struct linux_utime_args *args)
507 {
508 struct timeval tv[2], *tvp;
509 struct l_utimbuf lut;
510 int error;
511
512 if (args->times) {
513 if ((error = copyin(args->times, &lut, sizeof lut)) != 0)
514 return (error);
515 tv[0].tv_sec = lut.l_actime;
516 tv[0].tv_usec = 0;
517 tv[1].tv_sec = lut.l_modtime;
518 tv[1].tv_usec = 0;
519 tvp = tv;
520 } else
521 tvp = NULL;
522
523 return (kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
524 tvp, UIO_SYSSPACE));
525 }
526 #endif
527
528 #ifdef LINUX_LEGACY_SYSCALLS
529 int
linux_utimes(struct thread * td,struct linux_utimes_args * args)530 linux_utimes(struct thread *td, struct linux_utimes_args *args)
531 {
532 l_timeval ltv[2];
533 struct timeval tv[2], *tvp = NULL;
534 int error;
535
536 if (args->tptr != NULL) {
537 if ((error = copyin(args->tptr, ltv, sizeof ltv)) != 0)
538 return (error);
539 tv[0].tv_sec = ltv[0].tv_sec;
540 tv[0].tv_usec = ltv[0].tv_usec;
541 tv[1].tv_sec = ltv[1].tv_sec;
542 tv[1].tv_usec = ltv[1].tv_usec;
543 tvp = tv;
544 }
545
546 return (kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
547 tvp, UIO_SYSSPACE));
548 }
549 #endif
550
551 static int
linux_utimensat_lts_to_ts(struct l_timespec * l_times,struct timespec * times)552 linux_utimensat_lts_to_ts(struct l_timespec *l_times, struct timespec *times)
553 {
554
555 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
556 l_times->tv_nsec != LINUX_UTIME_NOW &&
557 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
558 return (EINVAL);
559
560 times->tv_sec = l_times->tv_sec;
561 switch (l_times->tv_nsec)
562 {
563 case LINUX_UTIME_OMIT:
564 times->tv_nsec = UTIME_OMIT;
565 break;
566 case LINUX_UTIME_NOW:
567 times->tv_nsec = UTIME_NOW;
568 break;
569 default:
570 times->tv_nsec = l_times->tv_nsec;
571 }
572
573 return (0);
574 }
575
576 static int
linux_common_utimensat(struct thread * td,int ldfd,const char * pathname,struct timespec * timesp,int lflags)577 linux_common_utimensat(struct thread *td, int ldfd, const char *pathname,
578 struct timespec *timesp, int lflags)
579 {
580 int dfd, flags = 0;
581
582 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
583
584 if (lflags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH))
585 return (EINVAL);
586
587 if (timesp != NULL) {
588 /* This breaks POSIX, but is what the Linux kernel does
589 * _on purpose_ (documented in the man page for utimensat(2)),
590 * so we must follow that behaviour. */
591 if (timesp[0].tv_nsec == UTIME_OMIT &&
592 timesp[1].tv_nsec == UTIME_OMIT)
593 return (0);
594 }
595
596 if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
597 flags |= AT_SYMLINK_NOFOLLOW;
598 if (lflags & LINUX_AT_EMPTY_PATH)
599 flags |= AT_EMPTY_PATH;
600
601 if (pathname != NULL)
602 return (kern_utimensat(td, dfd, pathname,
603 UIO_USERSPACE, timesp, UIO_SYSSPACE, flags));
604
605 if (lflags != 0)
606 return (EINVAL);
607
608 return (kern_futimens(td, dfd, timesp, UIO_SYSSPACE));
609 }
610
611 int
linux_utimensat(struct thread * td,struct linux_utimensat_args * args)612 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
613 {
614 struct l_timespec l_times[2];
615 struct timespec times[2], *timesp;
616 int error;
617
618 if (args->times != NULL) {
619 error = copyin(args->times, l_times, sizeof(l_times));
620 if (error != 0)
621 return (error);
622
623 error = linux_utimensat_lts_to_ts(&l_times[0], ×[0]);
624 if (error != 0)
625 return (error);
626 error = linux_utimensat_lts_to_ts(&l_times[1], ×[1]);
627 if (error != 0)
628 return (error);
629 timesp = times;
630 } else
631 timesp = NULL;
632
633 return (linux_common_utimensat(td, args->dfd, args->pathname,
634 timesp, args->flags));
635 }
636
637 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
638 static int
linux_utimensat_lts64_to_ts(struct l_timespec64 * l_times,struct timespec * times)639 linux_utimensat_lts64_to_ts(struct l_timespec64 *l_times, struct timespec *times)
640 {
641
642 /* Zero out the padding in compat mode. */
643 l_times->tv_nsec &= 0xFFFFFFFFUL;
644
645 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
646 l_times->tv_nsec != LINUX_UTIME_NOW &&
647 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
648 return (EINVAL);
649
650 times->tv_sec = l_times->tv_sec;
651 switch (l_times->tv_nsec)
652 {
653 case LINUX_UTIME_OMIT:
654 times->tv_nsec = UTIME_OMIT;
655 break;
656 case LINUX_UTIME_NOW:
657 times->tv_nsec = UTIME_NOW;
658 break;
659 default:
660 times->tv_nsec = l_times->tv_nsec;
661 }
662
663 return (0);
664 }
665
666 int
linux_utimensat_time64(struct thread * td,struct linux_utimensat_time64_args * args)667 linux_utimensat_time64(struct thread *td, struct linux_utimensat_time64_args *args)
668 {
669 struct l_timespec64 l_times[2];
670 struct timespec times[2], *timesp;
671 int error;
672
673 if (args->times64 != NULL) {
674 error = copyin(args->times64, l_times, sizeof(l_times));
675 if (error != 0)
676 return (error);
677
678 error = linux_utimensat_lts64_to_ts(&l_times[0], ×[0]);
679 if (error != 0)
680 return (error);
681 error = linux_utimensat_lts64_to_ts(&l_times[1], ×[1]);
682 if (error != 0)
683 return (error);
684 timesp = times;
685 } else
686 timesp = NULL;
687
688 return (linux_common_utimensat(td, args->dfd, args->pathname,
689 timesp, args->flags));
690 }
691 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
692
693 #ifdef LINUX_LEGACY_SYSCALLS
694 int
linux_futimesat(struct thread * td,struct linux_futimesat_args * args)695 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
696 {
697 l_timeval ltv[2];
698 struct timeval tv[2], *tvp = NULL;
699 int error, dfd;
700
701 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
702
703 if (args->utimes != NULL) {
704 if ((error = copyin(args->utimes, ltv, sizeof ltv)) != 0)
705 return (error);
706 tv[0].tv_sec = ltv[0].tv_sec;
707 tv[0].tv_usec = ltv[0].tv_usec;
708 tv[1].tv_sec = ltv[1].tv_sec;
709 tv[1].tv_usec = ltv[1].tv_usec;
710 tvp = tv;
711 }
712
713 return (kern_utimesat(td, dfd, args->filename, UIO_USERSPACE,
714 tvp, UIO_SYSSPACE));
715 }
716 #endif
717
718 static int
linux_common_wait(struct thread * td,idtype_t idtype,int id,int * statusp,int options,void * rup,l_siginfo_t * infop)719 linux_common_wait(struct thread *td, idtype_t idtype, int id, int *statusp,
720 int options, void *rup, l_siginfo_t *infop)
721 {
722 l_siginfo_t lsi;
723 siginfo_t siginfo;
724 struct __wrusage wru;
725 int error, status, tmpstat, sig;
726
727 error = kern_wait6(td, idtype, id, &status, options,
728 rup != NULL ? &wru : NULL, &siginfo);
729
730 if (error == 0 && statusp) {
731 tmpstat = status & 0xffff;
732 if (WIFSIGNALED(tmpstat)) {
733 tmpstat = (tmpstat & 0xffffff80) |
734 bsd_to_linux_signal(WTERMSIG(tmpstat));
735 } else if (WIFSTOPPED(tmpstat)) {
736 tmpstat = (tmpstat & 0xffff00ff) |
737 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
738 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32))
739 if (WSTOPSIG(status) == SIGTRAP) {
740 tmpstat = linux_ptrace_status(td,
741 siginfo.si_pid, tmpstat);
742 }
743 #endif
744 } else if (WIFCONTINUED(tmpstat)) {
745 tmpstat = 0xffff;
746 }
747 error = copyout(&tmpstat, statusp, sizeof(int));
748 }
749 if (error == 0 && rup != NULL)
750 error = linux_copyout_rusage(&wru.wru_self, rup);
751 if (error == 0 && infop != NULL && td->td_retval[0] != 0) {
752 sig = bsd_to_linux_signal(siginfo.si_signo);
753 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
754 error = copyout(&lsi, infop, sizeof(lsi));
755 }
756
757 return (error);
758 }
759
760 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
761 int
linux_waitpid(struct thread * td,struct linux_waitpid_args * args)762 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
763 {
764 struct linux_wait4_args wait4_args = {
765 .pid = args->pid,
766 .status = args->status,
767 .options = args->options,
768 .rusage = NULL,
769 };
770
771 return (linux_wait4(td, &wait4_args));
772 }
773 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
774
775 int
linux_wait4(struct thread * td,struct linux_wait4_args * args)776 linux_wait4(struct thread *td, struct linux_wait4_args *args)
777 {
778 struct proc *p;
779 int options, id, idtype;
780
781 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
782 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
783 return (EINVAL);
784
785 /* -INT_MIN is not defined. */
786 if (args->pid == INT_MIN)
787 return (ESRCH);
788
789 options = 0;
790 linux_to_bsd_waitopts(args->options, &options);
791
792 /*
793 * For backward compatibility we implicitly add flags WEXITED
794 * and WTRAPPED here.
795 */
796 options |= WEXITED | WTRAPPED;
797
798 if (args->pid == WAIT_ANY) {
799 idtype = P_ALL;
800 id = 0;
801 } else if (args->pid < 0) {
802 idtype = P_PGID;
803 id = (id_t)-args->pid;
804 } else if (args->pid == 0) {
805 idtype = P_PGID;
806 p = td->td_proc;
807 PROC_LOCK(p);
808 id = p->p_pgid;
809 PROC_UNLOCK(p);
810 } else {
811 idtype = P_PID;
812 id = (id_t)args->pid;
813 }
814
815 return (linux_common_wait(td, idtype, id, args->status, options,
816 args->rusage, NULL));
817 }
818
819 int
linux_waitid(struct thread * td,struct linux_waitid_args * args)820 linux_waitid(struct thread *td, struct linux_waitid_args *args)
821 {
822 idtype_t idtype;
823 int error, options;
824 struct proc *p;
825 pid_t id;
826
827 if (args->options & ~(LINUX_WNOHANG | LINUX_WNOWAIT | LINUX_WEXITED |
828 LINUX_WSTOPPED | LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
829 return (EINVAL);
830
831 options = 0;
832 linux_to_bsd_waitopts(args->options, &options);
833
834 id = args->id;
835 switch (args->idtype) {
836 case LINUX_P_ALL:
837 idtype = P_ALL;
838 break;
839 case LINUX_P_PID:
840 if (args->id <= 0)
841 return (EINVAL);
842 idtype = P_PID;
843 break;
844 case LINUX_P_PGID:
845 if (linux_kernver(td) >= LINUX_KERNVER(5,4,0) && args->id == 0) {
846 p = td->td_proc;
847 PROC_LOCK(p);
848 id = p->p_pgid;
849 PROC_UNLOCK(p);
850 } else if (args->id <= 0)
851 return (EINVAL);
852 idtype = P_PGID;
853 break;
854 case LINUX_P_PIDFD:
855 LINUX_RATELIMIT_MSG("unsupported waitid P_PIDFD idtype");
856 return (ENOSYS);
857 default:
858 return (EINVAL);
859 }
860
861 error = linux_common_wait(td, idtype, id, NULL, options,
862 args->rusage, args->info);
863 td->td_retval[0] = 0;
864
865 return (error);
866 }
867
868 #ifdef LINUX_LEGACY_SYSCALLS
869 int
linux_mknod(struct thread * td,struct linux_mknod_args * args)870 linux_mknod(struct thread *td, struct linux_mknod_args *args)
871 {
872 int error;
873
874 switch (args->mode & S_IFMT) {
875 case S_IFIFO:
876 case S_IFSOCK:
877 error = kern_mkfifoat(td, AT_FDCWD, args->path, UIO_USERSPACE,
878 args->mode);
879 break;
880
881 case S_IFCHR:
882 case S_IFBLK:
883 error = kern_mknodat(td, AT_FDCWD, args->path, UIO_USERSPACE,
884 args->mode, linux_decode_dev(args->dev));
885 break;
886
887 case S_IFDIR:
888 error = EPERM;
889 break;
890
891 case 0:
892 args->mode |= S_IFREG;
893 /* FALLTHROUGH */
894 case S_IFREG:
895 error = kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE,
896 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
897 if (error == 0)
898 kern_close(td, td->td_retval[0]);
899 break;
900
901 default:
902 error = EINVAL;
903 break;
904 }
905 return (error);
906 }
907 #endif
908
909 int
linux_mknodat(struct thread * td,struct linux_mknodat_args * args)910 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
911 {
912 int error, dfd;
913
914 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
915
916 switch (args->mode & S_IFMT) {
917 case S_IFIFO:
918 case S_IFSOCK:
919 error = kern_mkfifoat(td, dfd, args->filename, UIO_USERSPACE,
920 args->mode);
921 break;
922
923 case S_IFCHR:
924 case S_IFBLK:
925 error = kern_mknodat(td, dfd, args->filename, UIO_USERSPACE,
926 args->mode, linux_decode_dev(args->dev));
927 break;
928
929 case S_IFDIR:
930 error = EPERM;
931 break;
932
933 case 0:
934 args->mode |= S_IFREG;
935 /* FALLTHROUGH */
936 case S_IFREG:
937 error = kern_openat(td, dfd, args->filename, UIO_USERSPACE,
938 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
939 if (error == 0)
940 kern_close(td, td->td_retval[0]);
941 break;
942
943 default:
944 error = EINVAL;
945 break;
946 }
947 return (error);
948 }
949
950 /*
951 * UGH! This is just about the dumbest idea I've ever heard!!
952 */
953 int
linux_personality(struct thread * td,struct linux_personality_args * args)954 linux_personality(struct thread *td, struct linux_personality_args *args)
955 {
956 struct linux_pemuldata *pem;
957 struct proc *p = td->td_proc;
958 uint32_t old;
959
960 PROC_LOCK(p);
961 pem = pem_find(p);
962 old = pem->persona;
963 if (args->per != 0xffffffff)
964 pem->persona = args->per;
965 PROC_UNLOCK(p);
966
967 td->td_retval[0] = old;
968 return (0);
969 }
970
971 struct l_itimerval {
972 l_timeval it_interval;
973 l_timeval it_value;
974 };
975
976 #define B2L_ITIMERVAL(bip, lip) \
977 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
978 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
979 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
980 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
981
982 int
linux_setitimer(struct thread * td,struct linux_setitimer_args * uap)983 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
984 {
985 int error;
986 struct l_itimerval ls;
987 struct itimerval aitv, oitv;
988
989 if (uap->itv == NULL) {
990 uap->itv = uap->oitv;
991 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
992 }
993
994 error = copyin(uap->itv, &ls, sizeof(ls));
995 if (error != 0)
996 return (error);
997 B2L_ITIMERVAL(&aitv, &ls);
998 error = kern_setitimer(td, uap->which, &aitv, &oitv);
999 if (error != 0 || uap->oitv == NULL)
1000 return (error);
1001 B2L_ITIMERVAL(&ls, &oitv);
1002
1003 return (copyout(&ls, uap->oitv, sizeof(ls)));
1004 }
1005
1006 int
linux_getitimer(struct thread * td,struct linux_getitimer_args * uap)1007 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1008 {
1009 int error;
1010 struct l_itimerval ls;
1011 struct itimerval aitv;
1012
1013 error = kern_getitimer(td, uap->which, &aitv);
1014 if (error != 0)
1015 return (error);
1016 B2L_ITIMERVAL(&ls, &aitv);
1017 return (copyout(&ls, uap->itv, sizeof(ls)));
1018 }
1019
1020 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1021 int
linux_nice(struct thread * td,struct linux_nice_args * args)1022 linux_nice(struct thread *td, struct linux_nice_args *args)
1023 {
1024
1025 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc));
1026 }
1027 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1028
1029 int
linux_setgroups(struct thread * td,struct linux_setgroups_args * args)1030 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1031 {
1032 const int ngrp = args->gidsetsize;
1033 struct ucred *newcred, *oldcred;
1034 l_gid_t *linux_gidset;
1035 int error;
1036 struct proc *p;
1037
1038 if (ngrp < 0 || ngrp > ngroups_max)
1039 return (EINVAL);
1040 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1041 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1042 if (error)
1043 goto out;
1044
1045 newcred = crget();
1046 crextend(newcred, ngrp);
1047 p = td->td_proc;
1048 PROC_LOCK(p);
1049 oldcred = crcopysafe(p, newcred);
1050
1051 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1052 PROC_UNLOCK(p);
1053 crfree(newcred);
1054 goto out;
1055 }
1056
1057 newcred->cr_ngroups = ngrp;
1058 for (int i = 0; i < ngrp; i++)
1059 newcred->cr_groups[i] = linux_gidset[i];
1060 newcred->cr_flags |= CRED_FLAG_GROUPSET;
1061
1062 setsugid(p);
1063 proc_set_cred(p, newcred);
1064 PROC_UNLOCK(p);
1065 crfree(oldcred);
1066 error = 0;
1067 out:
1068 free(linux_gidset, M_LINUX);
1069 return (error);
1070 }
1071
1072 int
linux_getgroups(struct thread * td,struct linux_getgroups_args * args)1073 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1074 {
1075 const struct ucred *const cred = td->td_ucred;
1076 l_gid_t *linux_gidset;
1077 int ngrp, error;
1078
1079 ngrp = args->gidsetsize;
1080
1081 if (ngrp == 0) {
1082 td->td_retval[0] = cred->cr_ngroups;
1083 return (0);
1084 }
1085 if (ngrp < cred->cr_ngroups)
1086 return (EINVAL);
1087
1088 ngrp = cred->cr_ngroups;
1089
1090 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1091 for (int i = 0; i < ngrp; ++i)
1092 linux_gidset[i] = cred->cr_groups[i];
1093
1094 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1095 free(linux_gidset, M_LINUX);
1096
1097 if (error != 0)
1098 return (error);
1099
1100 td->td_retval[0] = ngrp;
1101 return (0);
1102 }
1103
1104 static bool
linux_get_dummy_limit(struct thread * td,l_uint resource,struct rlimit * rlim)1105 linux_get_dummy_limit(struct thread *td, l_uint resource, struct rlimit *rlim)
1106 {
1107 ssize_t size;
1108 int res, error;
1109
1110 if (linux_dummy_rlimits == 0)
1111 return (false);
1112
1113 switch (resource) {
1114 case LINUX_RLIMIT_LOCKS:
1115 case LINUX_RLIMIT_RTTIME:
1116 rlim->rlim_cur = LINUX_RLIM_INFINITY;
1117 rlim->rlim_max = LINUX_RLIM_INFINITY;
1118 return (true);
1119 case LINUX_RLIMIT_NICE:
1120 case LINUX_RLIMIT_RTPRIO:
1121 rlim->rlim_cur = 0;
1122 rlim->rlim_max = 0;
1123 return (true);
1124 case LINUX_RLIMIT_SIGPENDING:
1125 error = kernel_sysctlbyname(td,
1126 "kern.sigqueue.max_pending_per_proc",
1127 &res, &size, 0, 0, 0, 0);
1128 if (error != 0)
1129 return (false);
1130 rlim->rlim_cur = res;
1131 rlim->rlim_max = res;
1132 return (true);
1133 case LINUX_RLIMIT_MSGQUEUE:
1134 error = kernel_sysctlbyname(td,
1135 "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1136 if (error != 0)
1137 return (false);
1138 rlim->rlim_cur = res;
1139 rlim->rlim_max = res;
1140 return (true);
1141 default:
1142 return (false);
1143 }
1144 }
1145
1146 int
linux_setrlimit(struct thread * td,struct linux_setrlimit_args * args)1147 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1148 {
1149 struct rlimit bsd_rlim;
1150 struct l_rlimit rlim;
1151 u_int which;
1152 int error;
1153
1154 if (args->resource >= LINUX_RLIM_NLIMITS)
1155 return (EINVAL);
1156
1157 which = linux_to_bsd_resource[args->resource];
1158 if (which == -1)
1159 return (EINVAL);
1160
1161 error = copyin(args->rlim, &rlim, sizeof(rlim));
1162 if (error)
1163 return (error);
1164
1165 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1166 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1167 return (kern_setrlimit(td, which, &bsd_rlim));
1168 }
1169
1170 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1171 int
linux_old_getrlimit(struct thread * td,struct linux_old_getrlimit_args * args)1172 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1173 {
1174 struct l_rlimit rlim;
1175 struct rlimit bsd_rlim;
1176 u_int which;
1177
1178 if (linux_get_dummy_limit(td, args->resource, &bsd_rlim)) {
1179 rlim.rlim_cur = bsd_rlim.rlim_cur;
1180 rlim.rlim_max = bsd_rlim.rlim_max;
1181 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1182 }
1183
1184 if (args->resource >= LINUX_RLIM_NLIMITS)
1185 return (EINVAL);
1186
1187 which = linux_to_bsd_resource[args->resource];
1188 if (which == -1)
1189 return (EINVAL);
1190
1191 lim_rlimit(td, which, &bsd_rlim);
1192
1193 #ifdef COMPAT_LINUX32
1194 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1195 if (rlim.rlim_cur == UINT_MAX)
1196 rlim.rlim_cur = INT_MAX;
1197 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1198 if (rlim.rlim_max == UINT_MAX)
1199 rlim.rlim_max = INT_MAX;
1200 #else
1201 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1202 if (rlim.rlim_cur == ULONG_MAX)
1203 rlim.rlim_cur = LONG_MAX;
1204 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1205 if (rlim.rlim_max == ULONG_MAX)
1206 rlim.rlim_max = LONG_MAX;
1207 #endif
1208 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1209 }
1210 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1211
1212 int
linux_getrlimit(struct thread * td,struct linux_getrlimit_args * args)1213 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1214 {
1215 struct l_rlimit rlim;
1216 struct rlimit bsd_rlim;
1217 u_int which;
1218
1219 if (linux_get_dummy_limit(td, args->resource, &bsd_rlim)) {
1220 rlim.rlim_cur = bsd_rlim.rlim_cur;
1221 rlim.rlim_max = bsd_rlim.rlim_max;
1222 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1223 }
1224
1225 if (args->resource >= LINUX_RLIM_NLIMITS)
1226 return (EINVAL);
1227
1228 which = linux_to_bsd_resource[args->resource];
1229 if (which == -1)
1230 return (EINVAL);
1231
1232 lim_rlimit(td, which, &bsd_rlim);
1233
1234 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1235 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1236 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1237 }
1238
1239 int
linux_sched_setscheduler(struct thread * td,struct linux_sched_setscheduler_args * args)1240 linux_sched_setscheduler(struct thread *td,
1241 struct linux_sched_setscheduler_args *args)
1242 {
1243 struct sched_param sched_param;
1244 struct thread *tdt;
1245 int error, policy;
1246
1247 switch (args->policy) {
1248 case LINUX_SCHED_OTHER:
1249 policy = SCHED_OTHER;
1250 break;
1251 case LINUX_SCHED_FIFO:
1252 policy = SCHED_FIFO;
1253 break;
1254 case LINUX_SCHED_RR:
1255 policy = SCHED_RR;
1256 break;
1257 default:
1258 return (EINVAL);
1259 }
1260
1261 error = copyin(args->param, &sched_param, sizeof(sched_param));
1262 if (error)
1263 return (error);
1264
1265 if (linux_map_sched_prio) {
1266 switch (policy) {
1267 case SCHED_OTHER:
1268 if (sched_param.sched_priority != 0)
1269 return (EINVAL);
1270
1271 sched_param.sched_priority =
1272 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1273 break;
1274 case SCHED_FIFO:
1275 case SCHED_RR:
1276 if (sched_param.sched_priority < 1 ||
1277 sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1278 return (EINVAL);
1279
1280 /*
1281 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1282 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1283 */
1284 sched_param.sched_priority =
1285 (sched_param.sched_priority - 1) *
1286 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1287 (LINUX_MAX_RT_PRIO - 1);
1288 break;
1289 }
1290 }
1291
1292 tdt = linux_tdfind(td, args->pid, -1);
1293 if (tdt == NULL)
1294 return (ESRCH);
1295
1296 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1297 PROC_UNLOCK(tdt->td_proc);
1298 return (error);
1299 }
1300
1301 int
linux_sched_getscheduler(struct thread * td,struct linux_sched_getscheduler_args * args)1302 linux_sched_getscheduler(struct thread *td,
1303 struct linux_sched_getscheduler_args *args)
1304 {
1305 struct thread *tdt;
1306 int error, policy;
1307
1308 tdt = linux_tdfind(td, args->pid, -1);
1309 if (tdt == NULL)
1310 return (ESRCH);
1311
1312 error = kern_sched_getscheduler(td, tdt, &policy);
1313 PROC_UNLOCK(tdt->td_proc);
1314
1315 switch (policy) {
1316 case SCHED_OTHER:
1317 td->td_retval[0] = LINUX_SCHED_OTHER;
1318 break;
1319 case SCHED_FIFO:
1320 td->td_retval[0] = LINUX_SCHED_FIFO;
1321 break;
1322 case SCHED_RR:
1323 td->td_retval[0] = LINUX_SCHED_RR;
1324 break;
1325 }
1326 return (error);
1327 }
1328
1329 int
linux_sched_get_priority_max(struct thread * td,struct linux_sched_get_priority_max_args * args)1330 linux_sched_get_priority_max(struct thread *td,
1331 struct linux_sched_get_priority_max_args *args)
1332 {
1333 struct sched_get_priority_max_args bsd;
1334
1335 if (linux_map_sched_prio) {
1336 switch (args->policy) {
1337 case LINUX_SCHED_OTHER:
1338 td->td_retval[0] = 0;
1339 return (0);
1340 case LINUX_SCHED_FIFO:
1341 case LINUX_SCHED_RR:
1342 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1343 return (0);
1344 default:
1345 return (EINVAL);
1346 }
1347 }
1348
1349 switch (args->policy) {
1350 case LINUX_SCHED_OTHER:
1351 bsd.policy = SCHED_OTHER;
1352 break;
1353 case LINUX_SCHED_FIFO:
1354 bsd.policy = SCHED_FIFO;
1355 break;
1356 case LINUX_SCHED_RR:
1357 bsd.policy = SCHED_RR;
1358 break;
1359 default:
1360 return (EINVAL);
1361 }
1362 return (sys_sched_get_priority_max(td, &bsd));
1363 }
1364
1365 int
linux_sched_get_priority_min(struct thread * td,struct linux_sched_get_priority_min_args * args)1366 linux_sched_get_priority_min(struct thread *td,
1367 struct linux_sched_get_priority_min_args *args)
1368 {
1369 struct sched_get_priority_min_args bsd;
1370
1371 if (linux_map_sched_prio) {
1372 switch (args->policy) {
1373 case LINUX_SCHED_OTHER:
1374 td->td_retval[0] = 0;
1375 return (0);
1376 case LINUX_SCHED_FIFO:
1377 case LINUX_SCHED_RR:
1378 td->td_retval[0] = 1;
1379 return (0);
1380 default:
1381 return (EINVAL);
1382 }
1383 }
1384
1385 switch (args->policy) {
1386 case LINUX_SCHED_OTHER:
1387 bsd.policy = SCHED_OTHER;
1388 break;
1389 case LINUX_SCHED_FIFO:
1390 bsd.policy = SCHED_FIFO;
1391 break;
1392 case LINUX_SCHED_RR:
1393 bsd.policy = SCHED_RR;
1394 break;
1395 default:
1396 return (EINVAL);
1397 }
1398 return (sys_sched_get_priority_min(td, &bsd));
1399 }
1400
1401 #define REBOOT_CAD_ON 0x89abcdef
1402 #define REBOOT_CAD_OFF 0
1403 #define REBOOT_HALT 0xcdef0123
1404 #define REBOOT_RESTART 0x01234567
1405 #define REBOOT_RESTART2 0xA1B2C3D4
1406 #define REBOOT_POWEROFF 0x4321FEDC
1407 #define REBOOT_MAGIC1 0xfee1dead
1408 #define REBOOT_MAGIC2 0x28121969
1409 #define REBOOT_MAGIC2A 0x05121996
1410 #define REBOOT_MAGIC2B 0x16041998
1411
1412 int
linux_reboot(struct thread * td,struct linux_reboot_args * args)1413 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1414 {
1415 struct reboot_args bsd_args;
1416
1417 if (args->magic1 != REBOOT_MAGIC1)
1418 return (EINVAL);
1419
1420 switch (args->magic2) {
1421 case REBOOT_MAGIC2:
1422 case REBOOT_MAGIC2A:
1423 case REBOOT_MAGIC2B:
1424 break;
1425 default:
1426 return (EINVAL);
1427 }
1428
1429 switch (args->cmd) {
1430 case REBOOT_CAD_ON:
1431 case REBOOT_CAD_OFF:
1432 return (priv_check(td, PRIV_REBOOT));
1433 case REBOOT_HALT:
1434 bsd_args.opt = RB_HALT;
1435 break;
1436 case REBOOT_RESTART:
1437 case REBOOT_RESTART2:
1438 bsd_args.opt = 0;
1439 break;
1440 case REBOOT_POWEROFF:
1441 bsd_args.opt = RB_POWEROFF;
1442 break;
1443 default:
1444 return (EINVAL);
1445 }
1446 return (sys_reboot(td, &bsd_args));
1447 }
1448
1449 int
linux_getpid(struct thread * td,struct linux_getpid_args * args)1450 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1451 {
1452
1453 td->td_retval[0] = td->td_proc->p_pid;
1454
1455 return (0);
1456 }
1457
1458 int
linux_gettid(struct thread * td,struct linux_gettid_args * args)1459 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1460 {
1461 struct linux_emuldata *em;
1462
1463 em = em_find(td);
1464 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1465
1466 td->td_retval[0] = em->em_tid;
1467
1468 return (0);
1469 }
1470
1471 int
linux_getppid(struct thread * td,struct linux_getppid_args * args)1472 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1473 {
1474
1475 td->td_retval[0] = kern_getppid(td);
1476 return (0);
1477 }
1478
1479 int
linux_getgid(struct thread * td,struct linux_getgid_args * args)1480 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1481 {
1482
1483 td->td_retval[0] = td->td_ucred->cr_rgid;
1484 return (0);
1485 }
1486
1487 int
linux_getuid(struct thread * td,struct linux_getuid_args * args)1488 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1489 {
1490
1491 td->td_retval[0] = td->td_ucred->cr_ruid;
1492 return (0);
1493 }
1494
1495 int
linux_getsid(struct thread * td,struct linux_getsid_args * args)1496 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1497 {
1498
1499 return (kern_getsid(td, args->pid));
1500 }
1501
1502 int
linux_getpriority(struct thread * td,struct linux_getpriority_args * args)1503 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1504 {
1505 int error;
1506
1507 error = kern_getpriority(td, args->which, args->who);
1508 td->td_retval[0] = 20 - td->td_retval[0];
1509 return (error);
1510 }
1511
1512 int
linux_sethostname(struct thread * td,struct linux_sethostname_args * args)1513 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1514 {
1515 int name[2];
1516
1517 name[0] = CTL_KERN;
1518 name[1] = KERN_HOSTNAME;
1519 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1520 args->len, 0, 0));
1521 }
1522
1523 int
linux_setdomainname(struct thread * td,struct linux_setdomainname_args * args)1524 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1525 {
1526 int name[2];
1527
1528 name[0] = CTL_KERN;
1529 name[1] = KERN_NISDOMAINNAME;
1530 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1531 args->len, 0, 0));
1532 }
1533
1534 int
linux_exit_group(struct thread * td,struct linux_exit_group_args * args)1535 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1536 {
1537
1538 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1539 args->error_code);
1540
1541 /*
1542 * XXX: we should send a signal to the parent if
1543 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1544 * as it doesnt occur often.
1545 */
1546 exit1(td, args->error_code, 0);
1547 /* NOTREACHED */
1548 }
1549
1550 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1551 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1552 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1553
1554 struct l_user_cap_header {
1555 l_int version;
1556 l_int pid;
1557 };
1558
1559 struct l_user_cap_data {
1560 l_int effective;
1561 l_int permitted;
1562 l_int inheritable;
1563 };
1564
1565 int
linux_capget(struct thread * td,struct linux_capget_args * uap)1566 linux_capget(struct thread *td, struct linux_capget_args *uap)
1567 {
1568 struct l_user_cap_header luch;
1569 struct l_user_cap_data lucd[2];
1570 int error, u32s;
1571
1572 if (uap->hdrp == NULL)
1573 return (EFAULT);
1574
1575 error = copyin(uap->hdrp, &luch, sizeof(luch));
1576 if (error != 0)
1577 return (error);
1578
1579 switch (luch.version) {
1580 case _LINUX_CAPABILITY_VERSION_1:
1581 u32s = 1;
1582 break;
1583 case _LINUX_CAPABILITY_VERSION_2:
1584 case _LINUX_CAPABILITY_VERSION_3:
1585 u32s = 2;
1586 break;
1587 default:
1588 luch.version = _LINUX_CAPABILITY_VERSION_1;
1589 error = copyout(&luch, uap->hdrp, sizeof(luch));
1590 if (error)
1591 return (error);
1592 return (EINVAL);
1593 }
1594
1595 if (luch.pid)
1596 return (EPERM);
1597
1598 if (uap->datap) {
1599 /*
1600 * The current implementation doesn't support setting
1601 * a capability (it's essentially a stub) so indicate
1602 * that no capabilities are currently set or available
1603 * to request.
1604 */
1605 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1606 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1607 }
1608
1609 return (error);
1610 }
1611
1612 int
linux_capset(struct thread * td,struct linux_capset_args * uap)1613 linux_capset(struct thread *td, struct linux_capset_args *uap)
1614 {
1615 struct l_user_cap_header luch;
1616 struct l_user_cap_data lucd[2];
1617 int error, i, u32s;
1618
1619 if (uap->hdrp == NULL || uap->datap == NULL)
1620 return (EFAULT);
1621
1622 error = copyin(uap->hdrp, &luch, sizeof(luch));
1623 if (error != 0)
1624 return (error);
1625
1626 switch (luch.version) {
1627 case _LINUX_CAPABILITY_VERSION_1:
1628 u32s = 1;
1629 break;
1630 case _LINUX_CAPABILITY_VERSION_2:
1631 case _LINUX_CAPABILITY_VERSION_3:
1632 u32s = 2;
1633 break;
1634 default:
1635 luch.version = _LINUX_CAPABILITY_VERSION_1;
1636 error = copyout(&luch, uap->hdrp, sizeof(luch));
1637 if (error)
1638 return (error);
1639 return (EINVAL);
1640 }
1641
1642 if (luch.pid)
1643 return (EPERM);
1644
1645 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1646 if (error != 0)
1647 return (error);
1648
1649 /* We currently don't support setting any capabilities. */
1650 for (i = 0; i < u32s; i++) {
1651 if (lucd[i].effective || lucd[i].permitted ||
1652 lucd[i].inheritable) {
1653 linux_msg(td,
1654 "capset[%d] effective=0x%x, permitted=0x%x, "
1655 "inheritable=0x%x is not implemented", i,
1656 (int)lucd[i].effective, (int)lucd[i].permitted,
1657 (int)lucd[i].inheritable);
1658 return (EPERM);
1659 }
1660 }
1661
1662 return (0);
1663 }
1664
1665 int
linux_prctl(struct thread * td,struct linux_prctl_args * args)1666 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1667 {
1668 int error = 0, max_size, arg;
1669 struct proc *p = td->td_proc;
1670 char comm[LINUX_MAX_COMM_LEN];
1671 int pdeath_signal, trace_state;
1672
1673 switch (args->option) {
1674 case LINUX_PR_SET_PDEATHSIG:
1675 if (!LINUX_SIG_VALID(args->arg2))
1676 return (EINVAL);
1677 pdeath_signal = linux_to_bsd_signal(args->arg2);
1678 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
1679 &pdeath_signal));
1680 case LINUX_PR_GET_PDEATHSIG:
1681 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
1682 &pdeath_signal);
1683 if (error != 0)
1684 return (error);
1685 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
1686 return (copyout(&pdeath_signal,
1687 (void *)(register_t)args->arg2,
1688 sizeof(pdeath_signal)));
1689 /*
1690 * In Linux, this flag controls if set[gu]id processes can coredump.
1691 * There are additional semantics imposed on processes that cannot
1692 * coredump:
1693 * - Such processes can not be ptraced.
1694 * - There are some semantics around ownership of process-related files
1695 * in the /proc namespace.
1696 *
1697 * In FreeBSD, we can (and by default, do) disable setuid coredump
1698 * system-wide with 'sugid_coredump.' We control tracability on a
1699 * per-process basis with the procctl PROC_TRACE (=> P2_NOTRACE flag).
1700 * By happy coincidence, P2_NOTRACE also prevents coredumping. So the
1701 * procctl is roughly analogous to Linux's DUMPABLE.
1702 *
1703 * So, proxy these knobs to the corresponding PROC_TRACE setting.
1704 */
1705 case LINUX_PR_GET_DUMPABLE:
1706 error = kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_STATUS,
1707 &trace_state);
1708 if (error != 0)
1709 return (error);
1710 td->td_retval[0] = (trace_state != -1);
1711 return (0);
1712 case LINUX_PR_SET_DUMPABLE:
1713 /*
1714 * It is only valid for userspace to set one of these two
1715 * flags, and only one at a time.
1716 */
1717 switch (args->arg2) {
1718 case LINUX_SUID_DUMP_DISABLE:
1719 trace_state = PROC_TRACE_CTL_DISABLE_EXEC;
1720 break;
1721 case LINUX_SUID_DUMP_USER:
1722 trace_state = PROC_TRACE_CTL_ENABLE;
1723 break;
1724 default:
1725 return (EINVAL);
1726 }
1727 return (kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_CTL,
1728 &trace_state));
1729 case LINUX_PR_GET_KEEPCAPS:
1730 /*
1731 * Indicate that we always clear the effective and
1732 * permitted capability sets when the user id becomes
1733 * non-zero (actually the capability sets are simply
1734 * always zero in the current implementation).
1735 */
1736 td->td_retval[0] = 0;
1737 break;
1738 case LINUX_PR_SET_KEEPCAPS:
1739 /*
1740 * Ignore requests to keep the effective and permitted
1741 * capability sets when the user id becomes non-zero.
1742 */
1743 break;
1744 case LINUX_PR_SET_NAME:
1745 /*
1746 * To be on the safe side we need to make sure to not
1747 * overflow the size a Linux program expects. We already
1748 * do this here in the copyin, so that we don't need to
1749 * check on copyout.
1750 */
1751 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1752 error = copyinstr((void *)(register_t)args->arg2, comm,
1753 max_size, NULL);
1754
1755 /* Linux silently truncates the name if it is too long. */
1756 if (error == ENAMETOOLONG) {
1757 /*
1758 * XXX: copyinstr() isn't documented to populate the
1759 * array completely, so do a copyin() to be on the
1760 * safe side. This should be changed in case
1761 * copyinstr() is changed to guarantee this.
1762 */
1763 error = copyin((void *)(register_t)args->arg2, comm,
1764 max_size - 1);
1765 comm[max_size - 1] = '\0';
1766 }
1767 if (error)
1768 return (error);
1769
1770 PROC_LOCK(p);
1771 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1772 PROC_UNLOCK(p);
1773 break;
1774 case LINUX_PR_GET_NAME:
1775 PROC_LOCK(p);
1776 strlcpy(comm, p->p_comm, sizeof(comm));
1777 PROC_UNLOCK(p);
1778 error = copyout(comm, (void *)(register_t)args->arg2,
1779 strlen(comm) + 1);
1780 break;
1781 case LINUX_PR_GET_SECCOMP:
1782 case LINUX_PR_SET_SECCOMP:
1783 /*
1784 * Same as returned by Linux without CONFIG_SECCOMP enabled.
1785 */
1786 error = EINVAL;
1787 break;
1788 case LINUX_PR_CAPBSET_READ:
1789 #if 0
1790 /*
1791 * This makes too much noise with Ubuntu Focal.
1792 */
1793 linux_msg(td, "unsupported prctl PR_CAPBSET_READ %d",
1794 (int)args->arg2);
1795 #endif
1796 error = EINVAL;
1797 break;
1798 case LINUX_PR_SET_CHILD_SUBREAPER:
1799 if (args->arg2 == 0) {
1800 return (kern_procctl(td, P_PID, 0, PROC_REAP_RELEASE,
1801 NULL));
1802 }
1803
1804 return (kern_procctl(td, P_PID, 0, PROC_REAP_ACQUIRE,
1805 NULL));
1806 case LINUX_PR_SET_NO_NEW_PRIVS:
1807 arg = args->arg2 == 1 ?
1808 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
1809 error = kern_procctl(td, P_PID, p->p_pid,
1810 PROC_NO_NEW_PRIVS_CTL, &arg);
1811 break;
1812 case LINUX_PR_SET_PTRACER:
1813 linux_msg(td, "unsupported prctl PR_SET_PTRACER");
1814 error = EINVAL;
1815 break;
1816 default:
1817 linux_msg(td, "unsupported prctl option %d", args->option);
1818 error = EINVAL;
1819 break;
1820 }
1821
1822 return (error);
1823 }
1824
1825 int
linux_sched_setparam(struct thread * td,struct linux_sched_setparam_args * uap)1826 linux_sched_setparam(struct thread *td,
1827 struct linux_sched_setparam_args *uap)
1828 {
1829 struct sched_param sched_param;
1830 struct thread *tdt;
1831 int error, policy;
1832
1833 error = copyin(uap->param, &sched_param, sizeof(sched_param));
1834 if (error)
1835 return (error);
1836
1837 tdt = linux_tdfind(td, uap->pid, -1);
1838 if (tdt == NULL)
1839 return (ESRCH);
1840
1841 if (linux_map_sched_prio) {
1842 error = kern_sched_getscheduler(td, tdt, &policy);
1843 if (error)
1844 goto out;
1845
1846 switch (policy) {
1847 case SCHED_OTHER:
1848 if (sched_param.sched_priority != 0) {
1849 error = EINVAL;
1850 goto out;
1851 }
1852 sched_param.sched_priority =
1853 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1854 break;
1855 case SCHED_FIFO:
1856 case SCHED_RR:
1857 if (sched_param.sched_priority < 1 ||
1858 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
1859 error = EINVAL;
1860 goto out;
1861 }
1862 /*
1863 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1864 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1865 */
1866 sched_param.sched_priority =
1867 (sched_param.sched_priority - 1) *
1868 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1869 (LINUX_MAX_RT_PRIO - 1);
1870 break;
1871 }
1872 }
1873
1874 error = kern_sched_setparam(td, tdt, &sched_param);
1875 out: PROC_UNLOCK(tdt->td_proc);
1876 return (error);
1877 }
1878
1879 int
linux_sched_getparam(struct thread * td,struct linux_sched_getparam_args * uap)1880 linux_sched_getparam(struct thread *td,
1881 struct linux_sched_getparam_args *uap)
1882 {
1883 struct sched_param sched_param;
1884 struct thread *tdt;
1885 int error, policy;
1886
1887 tdt = linux_tdfind(td, uap->pid, -1);
1888 if (tdt == NULL)
1889 return (ESRCH);
1890
1891 error = kern_sched_getparam(td, tdt, &sched_param);
1892 if (error) {
1893 PROC_UNLOCK(tdt->td_proc);
1894 return (error);
1895 }
1896
1897 if (linux_map_sched_prio) {
1898 error = kern_sched_getscheduler(td, tdt, &policy);
1899 PROC_UNLOCK(tdt->td_proc);
1900 if (error)
1901 return (error);
1902
1903 switch (policy) {
1904 case SCHED_OTHER:
1905 sched_param.sched_priority = 0;
1906 break;
1907 case SCHED_FIFO:
1908 case SCHED_RR:
1909 /*
1910 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
1911 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
1912 */
1913 sched_param.sched_priority =
1914 (sched_param.sched_priority *
1915 (LINUX_MAX_RT_PRIO - 1) +
1916 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
1917 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
1918 break;
1919 }
1920 } else
1921 PROC_UNLOCK(tdt->td_proc);
1922
1923 error = copyout(&sched_param, uap->param, sizeof(sched_param));
1924 return (error);
1925 }
1926
1927 /*
1928 * Get affinity of a process.
1929 */
1930 int
linux_sched_getaffinity(struct thread * td,struct linux_sched_getaffinity_args * args)1931 linux_sched_getaffinity(struct thread *td,
1932 struct linux_sched_getaffinity_args *args)
1933 {
1934 struct thread *tdt;
1935 cpuset_t *mask;
1936 size_t size;
1937 int error;
1938 id_t tid;
1939
1940 tdt = linux_tdfind(td, args->pid, -1);
1941 if (tdt == NULL)
1942 return (ESRCH);
1943 tid = tdt->td_tid;
1944 PROC_UNLOCK(tdt->td_proc);
1945
1946 mask = malloc(sizeof(cpuset_t), M_LINUX, M_WAITOK | M_ZERO);
1947 size = min(args->len, sizeof(cpuset_t));
1948 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
1949 tid, size, mask);
1950 if (error == ERANGE)
1951 error = EINVAL;
1952 if (error == 0)
1953 error = copyout(mask, args->user_mask_ptr, size);
1954 if (error == 0)
1955 td->td_retval[0] = size;
1956 free(mask, M_LINUX);
1957 return (error);
1958 }
1959
1960 /*
1961 * Set affinity of a process.
1962 */
1963 int
linux_sched_setaffinity(struct thread * td,struct linux_sched_setaffinity_args * args)1964 linux_sched_setaffinity(struct thread *td,
1965 struct linux_sched_setaffinity_args *args)
1966 {
1967 struct thread *tdt;
1968 cpuset_t *mask;
1969 int cpu, error;
1970 size_t len;
1971 id_t tid;
1972
1973 tdt = linux_tdfind(td, args->pid, -1);
1974 if (tdt == NULL)
1975 return (ESRCH);
1976 tid = tdt->td_tid;
1977 PROC_UNLOCK(tdt->td_proc);
1978
1979 len = min(args->len, sizeof(cpuset_t));
1980 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);
1981 error = copyin(args->user_mask_ptr, mask, len);
1982 if (error != 0)
1983 goto out;
1984 /* Linux ignore high bits */
1985 CPU_FOREACH_ISSET(cpu, mask)
1986 if (cpu > mp_maxid)
1987 CPU_CLR(cpu, mask);
1988
1989 error = kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
1990 tid, mask);
1991 if (error == EDEADLK)
1992 error = EINVAL;
1993 out:
1994 free(mask, M_TEMP);
1995 return (error);
1996 }
1997
1998 struct linux_rlimit64 {
1999 uint64_t rlim_cur;
2000 uint64_t rlim_max;
2001 };
2002
2003 int
linux_prlimit64(struct thread * td,struct linux_prlimit64_args * args)2004 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2005 {
2006 struct rlimit rlim, nrlim;
2007 struct linux_rlimit64 lrlim;
2008 struct proc *p;
2009 u_int which;
2010 int flags;
2011 int error;
2012
2013 if (args->new == NULL && args->old != NULL) {
2014 if (linux_get_dummy_limit(td, args->resource, &rlim)) {
2015 lrlim.rlim_cur = rlim.rlim_cur;
2016 lrlim.rlim_max = rlim.rlim_max;
2017 return (copyout(&lrlim, args->old, sizeof(lrlim)));
2018 }
2019 }
2020
2021 if (args->resource >= LINUX_RLIM_NLIMITS)
2022 return (EINVAL);
2023
2024 which = linux_to_bsd_resource[args->resource];
2025 if (which == -1)
2026 return (EINVAL);
2027
2028 if (args->new != NULL) {
2029 /*
2030 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2031 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2032 * as INFINITY so we do not need a conversion even.
2033 */
2034 error = copyin(args->new, &nrlim, sizeof(nrlim));
2035 if (error != 0)
2036 return (error);
2037 }
2038
2039 flags = PGET_HOLD | PGET_NOTWEXIT;
2040 if (args->new != NULL)
2041 flags |= PGET_CANDEBUG;
2042 else
2043 flags |= PGET_CANSEE;
2044 if (args->pid == 0) {
2045 p = td->td_proc;
2046 PHOLD(p);
2047 } else {
2048 error = pget(args->pid, flags, &p);
2049 if (error != 0)
2050 return (error);
2051 }
2052 if (args->old != NULL) {
2053 PROC_LOCK(p);
2054 lim_rlimit_proc(p, which, &rlim);
2055 PROC_UNLOCK(p);
2056 if (rlim.rlim_cur == RLIM_INFINITY)
2057 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2058 else
2059 lrlim.rlim_cur = rlim.rlim_cur;
2060 if (rlim.rlim_max == RLIM_INFINITY)
2061 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2062 else
2063 lrlim.rlim_max = rlim.rlim_max;
2064 error = copyout(&lrlim, args->old, sizeof(lrlim));
2065 if (error != 0)
2066 goto out;
2067 }
2068
2069 if (args->new != NULL)
2070 error = kern_proc_setrlimit(td, p, which, &nrlim);
2071
2072 out:
2073 PRELE(p);
2074 return (error);
2075 }
2076
2077 int
linux_pselect6(struct thread * td,struct linux_pselect6_args * args)2078 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2079 {
2080 struct timespec ts, *tsp;
2081 int error;
2082
2083 if (args->tsp != NULL) {
2084 error = linux_get_timespec(&ts, args->tsp);
2085 if (error != 0)
2086 return (error);
2087 tsp = &ts;
2088 } else
2089 tsp = NULL;
2090
2091 error = linux_common_pselect6(td, args->nfds, args->readfds,
2092 args->writefds, args->exceptfds, tsp, args->sig);
2093
2094 if (args->tsp != NULL)
2095 linux_put_timespec(&ts, args->tsp);
2096 return (error);
2097 }
2098
2099 static int
linux_common_pselect6(struct thread * td,l_int nfds,l_fd_set * readfds,l_fd_set * writefds,l_fd_set * exceptfds,struct timespec * tsp,l_uintptr_t * sig)2100 linux_common_pselect6(struct thread *td, l_int nfds, l_fd_set *readfds,
2101 l_fd_set *writefds, l_fd_set *exceptfds, struct timespec *tsp,
2102 l_uintptr_t *sig)
2103 {
2104 struct timeval utv, tv0, tv1, *tvp;
2105 struct l_pselect6arg lpse6;
2106 sigset_t *ssp;
2107 sigset_t ss;
2108 int error;
2109
2110 ssp = NULL;
2111 if (sig != NULL) {
2112 error = copyin(sig, &lpse6, sizeof(lpse6));
2113 if (error != 0)
2114 return (error);
2115 error = linux_copyin_sigset(td, PTRIN(lpse6.ss),
2116 lpse6.ss_len, &ss, &ssp);
2117 if (error != 0)
2118 return (error);
2119 } else
2120 ssp = NULL;
2121
2122 /*
2123 * Currently glibc changes nanosecond number to microsecond.
2124 * This mean losing precision but for now it is hardly seen.
2125 */
2126 if (tsp != NULL) {
2127 TIMESPEC_TO_TIMEVAL(&utv, tsp);
2128 if (itimerfix(&utv))
2129 return (EINVAL);
2130
2131 microtime(&tv0);
2132 tvp = &utv;
2133 } else
2134 tvp = NULL;
2135
2136 error = kern_pselect(td, nfds, readfds, writefds,
2137 exceptfds, tvp, ssp, LINUX_NFDBITS);
2138
2139 if (tsp != NULL) {
2140 /*
2141 * Compute how much time was left of the timeout,
2142 * by subtracting the current time and the time
2143 * before we started the call, and subtracting
2144 * that result from the user-supplied value.
2145 */
2146 microtime(&tv1);
2147 timevalsub(&tv1, &tv0);
2148 timevalsub(&utv, &tv1);
2149 if (utv.tv_sec < 0)
2150 timevalclear(&utv);
2151 TIMEVAL_TO_TIMESPEC(&utv, tsp);
2152 }
2153 return (error);
2154 }
2155
2156 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2157 int
linux_pselect6_time64(struct thread * td,struct linux_pselect6_time64_args * args)2158 linux_pselect6_time64(struct thread *td,
2159 struct linux_pselect6_time64_args *args)
2160 {
2161 struct timespec ts, *tsp;
2162 int error;
2163
2164 if (args->tsp != NULL) {
2165 error = linux_get_timespec64(&ts, args->tsp);
2166 if (error != 0)
2167 return (error);
2168 tsp = &ts;
2169 } else
2170 tsp = NULL;
2171
2172 error = linux_common_pselect6(td, args->nfds, args->readfds,
2173 args->writefds, args->exceptfds, tsp, args->sig);
2174
2175 if (args->tsp != NULL)
2176 linux_put_timespec64(&ts, args->tsp);
2177 return (error);
2178 }
2179 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2180
2181 int
linux_ppoll(struct thread * td,struct linux_ppoll_args * args)2182 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2183 {
2184 struct timespec uts, *tsp;
2185 int error;
2186
2187 if (args->tsp != NULL) {
2188 error = linux_get_timespec(&uts, args->tsp);
2189 if (error != 0)
2190 return (error);
2191 tsp = &uts;
2192 } else
2193 tsp = NULL;
2194
2195 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2196 args->sset, args->ssize);
2197 if (error == 0 && args->tsp != NULL)
2198 error = linux_put_timespec(&uts, args->tsp);
2199 return (error);
2200 }
2201
2202 static int
linux_common_ppoll(struct thread * td,struct pollfd * fds,uint32_t nfds,struct timespec * tsp,l_sigset_t * sset,l_size_t ssize)2203 linux_common_ppoll(struct thread *td, struct pollfd *fds, uint32_t nfds,
2204 struct timespec *tsp, l_sigset_t *sset, l_size_t ssize)
2205 {
2206 struct timespec ts0, ts1;
2207 struct pollfd stackfds[32];
2208 struct pollfd *kfds;
2209 sigset_t *ssp;
2210 sigset_t ss;
2211 int error;
2212
2213 if (kern_poll_maxfds(nfds))
2214 return (EINVAL);
2215 if (sset != NULL) {
2216 error = linux_copyin_sigset(td, sset, ssize, &ss, &ssp);
2217 if (error != 0)
2218 return (error);
2219 } else
2220 ssp = NULL;
2221 if (tsp != NULL)
2222 nanotime(&ts0);
2223
2224 if (nfds > nitems(stackfds))
2225 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
2226 else
2227 kfds = stackfds;
2228 error = linux_pollin(td, kfds, fds, nfds);
2229 if (error != 0)
2230 goto out;
2231
2232 error = kern_poll_kfds(td, kfds, nfds, tsp, ssp);
2233 if (error == 0)
2234 error = linux_pollout(td, kfds, fds, nfds);
2235
2236 if (error == 0 && tsp != NULL) {
2237 if (td->td_retval[0]) {
2238 nanotime(&ts1);
2239 timespecsub(&ts1, &ts0, &ts1);
2240 timespecsub(tsp, &ts1, tsp);
2241 if (tsp->tv_sec < 0)
2242 timespecclear(tsp);
2243 } else
2244 timespecclear(tsp);
2245 }
2246
2247 out:
2248 if (nfds > nitems(stackfds))
2249 free(kfds, M_TEMP);
2250 return (error);
2251 }
2252
2253 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2254 int
linux_ppoll_time64(struct thread * td,struct linux_ppoll_time64_args * args)2255 linux_ppoll_time64(struct thread *td, struct linux_ppoll_time64_args *args)
2256 {
2257 struct timespec uts, *tsp;
2258 int error;
2259
2260 if (args->tsp != NULL) {
2261 error = linux_get_timespec64(&uts, args->tsp);
2262 if (error != 0)
2263 return (error);
2264 tsp = &uts;
2265 } else
2266 tsp = NULL;
2267 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2268 args->sset, args->ssize);
2269 if (error == 0 && args->tsp != NULL)
2270 error = linux_put_timespec64(&uts, args->tsp);
2271 return (error);
2272 }
2273 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2274
2275 static int
linux_pollin(struct thread * td,struct pollfd * fds,struct pollfd * ufds,u_int nfd)2276 linux_pollin(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2277 {
2278 int error;
2279 u_int i;
2280
2281 error = copyin(ufds, fds, nfd * sizeof(*fds));
2282 if (error != 0)
2283 return (error);
2284
2285 for (i = 0; i < nfd; i++) {
2286 if (fds->events != 0)
2287 linux_to_bsd_poll_events(td, fds->fd,
2288 fds->events, &fds->events);
2289 fds++;
2290 }
2291 return (0);
2292 }
2293
2294 static int
linux_pollout(struct thread * td,struct pollfd * fds,struct pollfd * ufds,u_int nfd)2295 linux_pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2296 {
2297 int error = 0;
2298 u_int i, n = 0;
2299
2300 for (i = 0; i < nfd; i++) {
2301 if (fds->revents != 0) {
2302 bsd_to_linux_poll_events(fds->revents,
2303 &fds->revents);
2304 n++;
2305 }
2306 error = copyout(&fds->revents, &ufds->revents,
2307 sizeof(ufds->revents));
2308 if (error)
2309 return (error);
2310 fds++;
2311 ufds++;
2312 }
2313 td->td_retval[0] = n;
2314 return (0);
2315 }
2316
2317 static int
linux_sched_rr_get_interval_common(struct thread * td,pid_t pid,struct timespec * ts)2318 linux_sched_rr_get_interval_common(struct thread *td, pid_t pid,
2319 struct timespec *ts)
2320 {
2321 struct thread *tdt;
2322 int error;
2323
2324 /*
2325 * According to man in case the invalid pid specified
2326 * EINVAL should be returned.
2327 */
2328 if (pid < 0)
2329 return (EINVAL);
2330
2331 tdt = linux_tdfind(td, pid, -1);
2332 if (tdt == NULL)
2333 return (ESRCH);
2334
2335 error = kern_sched_rr_get_interval_td(td, tdt, ts);
2336 PROC_UNLOCK(tdt->td_proc);
2337 return (error);
2338 }
2339
2340 int
linux_sched_rr_get_interval(struct thread * td,struct linux_sched_rr_get_interval_args * uap)2341 linux_sched_rr_get_interval(struct thread *td,
2342 struct linux_sched_rr_get_interval_args *uap)
2343 {
2344 struct timespec ts;
2345 int error;
2346
2347 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts);
2348 if (error != 0)
2349 return (error);
2350 return (linux_put_timespec(&ts, uap->interval));
2351 }
2352
2353 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2354 int
linux_sched_rr_get_interval_time64(struct thread * td,struct linux_sched_rr_get_interval_time64_args * uap)2355 linux_sched_rr_get_interval_time64(struct thread *td,
2356 struct linux_sched_rr_get_interval_time64_args *uap)
2357 {
2358 struct timespec ts;
2359 int error;
2360
2361 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts);
2362 if (error != 0)
2363 return (error);
2364 return (linux_put_timespec64(&ts, uap->interval));
2365 }
2366 #endif
2367
2368 /*
2369 * In case when the Linux thread is the initial thread in
2370 * the thread group thread id is equal to the process id.
2371 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2372 */
2373 struct thread *
linux_tdfind(struct thread * td,lwpid_t tid,pid_t pid)2374 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2375 {
2376 struct linux_emuldata *em;
2377 struct thread *tdt;
2378 struct proc *p;
2379
2380 tdt = NULL;
2381 if (tid == 0 || tid == td->td_tid) {
2382 if (pid != -1 && td->td_proc->p_pid != pid)
2383 return (NULL);
2384 PROC_LOCK(td->td_proc);
2385 return (td);
2386 } else if (tid > PID_MAX)
2387 return (tdfind(tid, pid));
2388
2389 /*
2390 * Initial thread where the tid equal to the pid.
2391 */
2392 p = pfind(tid);
2393 if (p != NULL) {
2394 if (SV_PROC_ABI(p) != SV_ABI_LINUX ||
2395 (pid != -1 && tid != pid)) {
2396 /*
2397 * p is not a Linuxulator process.
2398 */
2399 PROC_UNLOCK(p);
2400 return (NULL);
2401 }
2402 FOREACH_THREAD_IN_PROC(p, tdt) {
2403 em = em_find(tdt);
2404 if (tid == em->em_tid)
2405 return (tdt);
2406 }
2407 PROC_UNLOCK(p);
2408 }
2409 return (NULL);
2410 }
2411
2412 void
linux_to_bsd_waitopts(int options,int * bsdopts)2413 linux_to_bsd_waitopts(int options, int *bsdopts)
2414 {
2415
2416 if (options & LINUX_WNOHANG)
2417 *bsdopts |= WNOHANG;
2418 if (options & LINUX_WUNTRACED)
2419 *bsdopts |= WUNTRACED;
2420 if (options & LINUX_WEXITED)
2421 *bsdopts |= WEXITED;
2422 if (options & LINUX_WCONTINUED)
2423 *bsdopts |= WCONTINUED;
2424 if (options & LINUX_WNOWAIT)
2425 *bsdopts |= WNOWAIT;
2426
2427 if (options & __WCLONE)
2428 *bsdopts |= WLINUXCLONE;
2429 }
2430
2431 int
linux_getrandom(struct thread * td,struct linux_getrandom_args * args)2432 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2433 {
2434 struct uio uio;
2435 struct iovec iov;
2436 int error;
2437
2438 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2439 return (EINVAL);
2440 if (args->count > INT_MAX)
2441 args->count = INT_MAX;
2442
2443 iov.iov_base = args->buf;
2444 iov.iov_len = args->count;
2445
2446 uio.uio_iov = &iov;
2447 uio.uio_iovcnt = 1;
2448 uio.uio_resid = iov.iov_len;
2449 uio.uio_segflg = UIO_USERSPACE;
2450 uio.uio_rw = UIO_READ;
2451 uio.uio_td = td;
2452
2453 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2454 if (error == 0)
2455 td->td_retval[0] = args->count - uio.uio_resid;
2456 return (error);
2457 }
2458
2459 int
linux_mincore(struct thread * td,struct linux_mincore_args * args)2460 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2461 {
2462
2463 /* Needs to be page-aligned */
2464 if (args->start & PAGE_MASK)
2465 return (EINVAL);
2466 return (kern_mincore(td, args->start, args->len, args->vec));
2467 }
2468
2469 #define SYSLOG_TAG "<6>"
2470
2471 int
linux_syslog(struct thread * td,struct linux_syslog_args * args)2472 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2473 {
2474 char buf[128], *src, *dst;
2475 u_int seq;
2476 int buflen, error;
2477
2478 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2479 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2480 return (EINVAL);
2481 }
2482
2483 if (args->len < 6) {
2484 td->td_retval[0] = 0;
2485 return (0);
2486 }
2487
2488 error = priv_check(td, PRIV_MSGBUF);
2489 if (error)
2490 return (error);
2491
2492 mtx_lock(&msgbuf_lock);
2493 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2494 mtx_unlock(&msgbuf_lock);
2495
2496 dst = args->buf;
2497 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2498 /* The -1 is to skip the trailing '\0'. */
2499 dst += sizeof(SYSLOG_TAG) - 1;
2500
2501 while (error == 0) {
2502 mtx_lock(&msgbuf_lock);
2503 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2504 mtx_unlock(&msgbuf_lock);
2505
2506 if (buflen == 0)
2507 break;
2508
2509 for (src = buf; src < buf + buflen && error == 0; src++) {
2510 if (*src == '\0')
2511 continue;
2512
2513 if (dst >= args->buf + args->len)
2514 goto out;
2515
2516 error = copyout(src, dst, 1);
2517 dst++;
2518
2519 if (*src == '\n' && *(src + 1) != '<' &&
2520 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2521 error = copyout(&SYSLOG_TAG,
2522 dst, sizeof(SYSLOG_TAG));
2523 dst += sizeof(SYSLOG_TAG) - 1;
2524 }
2525 }
2526 }
2527 out:
2528 td->td_retval[0] = dst - args->buf;
2529 return (error);
2530 }
2531
2532 int
linux_getcpu(struct thread * td,struct linux_getcpu_args * args)2533 linux_getcpu(struct thread *td, struct linux_getcpu_args *args)
2534 {
2535 int cpu, error, node;
2536
2537 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */
2538 error = 0;
2539 node = cpuid_to_pcpu[cpu]->pc_domain;
2540
2541 if (args->cpu != NULL)
2542 error = copyout(&cpu, args->cpu, sizeof(l_int));
2543 if (args->node != NULL)
2544 error = copyout(&node, args->node, sizeof(l_int));
2545 return (error);
2546 }
2547
2548 #if defined(__i386__) || defined(__amd64__)
2549 int
linux_poll(struct thread * td,struct linux_poll_args * args)2550 linux_poll(struct thread *td, struct linux_poll_args *args)
2551 {
2552 struct timespec ts, *tsp;
2553
2554 if (args->timeout != INFTIM) {
2555 if (args->timeout < 0)
2556 return (EINVAL);
2557 ts.tv_sec = args->timeout / 1000;
2558 ts.tv_nsec = (args->timeout % 1000) * 1000000;
2559 tsp = &ts;
2560 } else
2561 tsp = NULL;
2562
2563 return (linux_common_ppoll(td, args->fds, args->nfds,
2564 tsp, NULL, 0));
2565 }
2566 #endif /* __i386__ || __amd64__ */
2567
2568 int
linux_seccomp(struct thread * td,struct linux_seccomp_args * args)2569 linux_seccomp(struct thread *td, struct linux_seccomp_args *args)
2570 {
2571
2572 switch (args->op) {
2573 case LINUX_SECCOMP_GET_ACTION_AVAIL:
2574 return (EOPNOTSUPP);
2575 default:
2576 /*
2577 * Ignore unknown operations, just like Linux kernel built
2578 * without CONFIG_SECCOMP.
2579 */
2580 return (EINVAL);
2581 }
2582 }
2583
2584 /*
2585 * Custom version of exec_copyin_args(), to copy out argument and environment
2586 * strings from the old process address space into the temporary string buffer.
2587 * Based on freebsd32_exec_copyin_args.
2588 */
2589 static int
linux_exec_copyin_args(struct image_args * args,const char * fname,l_uintptr_t * argv,l_uintptr_t * envv)2590 linux_exec_copyin_args(struct image_args *args, const char *fname,
2591 l_uintptr_t *argv, l_uintptr_t *envv)
2592 {
2593 char *argp, *envp;
2594 l_uintptr_t *ptr, arg;
2595 int error;
2596
2597 bzero(args, sizeof(*args));
2598 if (argv == NULL)
2599 return (EFAULT);
2600
2601 /*
2602 * Allocate demand-paged memory for the file name, argument, and
2603 * environment strings.
2604 */
2605 error = exec_alloc_args(args);
2606 if (error != 0)
2607 return (error);
2608
2609 /*
2610 * Copy the file name.
2611 */
2612 error = exec_args_add_fname(args, fname, UIO_USERSPACE);
2613 if (error != 0)
2614 goto err_exit;
2615
2616 /*
2617 * extract arguments first
2618 */
2619 ptr = argv;
2620 for (;;) {
2621 error = copyin(ptr++, &arg, sizeof(arg));
2622 if (error)
2623 goto err_exit;
2624 if (arg == 0)
2625 break;
2626 argp = PTRIN(arg);
2627 error = exec_args_add_arg(args, argp, UIO_USERSPACE);
2628 if (error != 0)
2629 goto err_exit;
2630 }
2631
2632 /*
2633 * This comment is from Linux do_execveat_common:
2634 * When argv is empty, add an empty string ("") as argv[0] to
2635 * ensure confused userspace programs that start processing
2636 * from argv[1] won't end up walking envp.
2637 */
2638 if (args->argc == 0 &&
2639 (error = exec_args_add_arg(args, "", UIO_SYSSPACE) != 0))
2640 goto err_exit;
2641
2642 /*
2643 * extract environment strings
2644 */
2645 if (envv) {
2646 ptr = envv;
2647 for (;;) {
2648 error = copyin(ptr++, &arg, sizeof(arg));
2649 if (error)
2650 goto err_exit;
2651 if (arg == 0)
2652 break;
2653 envp = PTRIN(arg);
2654 error = exec_args_add_env(args, envp, UIO_USERSPACE);
2655 if (error != 0)
2656 goto err_exit;
2657 }
2658 }
2659
2660 return (0);
2661
2662 err_exit:
2663 exec_free_args(args);
2664 return (error);
2665 }
2666
2667 int
linux_execve(struct thread * td,struct linux_execve_args * args)2668 linux_execve(struct thread *td, struct linux_execve_args *args)
2669 {
2670 struct image_args eargs;
2671 int error;
2672
2673 LINUX_CTR(execve);
2674
2675 error = linux_exec_copyin_args(&eargs, args->path, args->argp,
2676 args->envp);
2677 if (error == 0)
2678 error = linux_common_execve(td, &eargs);
2679 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
2680 return (error);
2681 }
2682
2683 static void
linux_up_rtprio_if(struct thread * td1,struct rtprio * rtp)2684 linux_up_rtprio_if(struct thread *td1, struct rtprio *rtp)
2685 {
2686 struct rtprio rtp2;
2687
2688 pri_to_rtp(td1, &rtp2);
2689 if (rtp2.type < rtp->type ||
2690 (rtp2.type == rtp->type &&
2691 rtp2.prio < rtp->prio)) {
2692 rtp->type = rtp2.type;
2693 rtp->prio = rtp2.prio;
2694 }
2695 }
2696
2697 #define LINUX_PRIO_DIVIDER RTP_PRIO_MAX / LINUX_IOPRIO_MAX
2698
2699 static int
linux_rtprio2ioprio(struct rtprio * rtp)2700 linux_rtprio2ioprio(struct rtprio *rtp)
2701 {
2702 int ioprio, prio;
2703
2704 switch (rtp->type) {
2705 case RTP_PRIO_IDLE:
2706 prio = RTP_PRIO_MIN;
2707 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_IDLE, prio);
2708 break;
2709 case RTP_PRIO_NORMAL:
2710 prio = rtp->prio / LINUX_PRIO_DIVIDER;
2711 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_BE, prio);
2712 break;
2713 case RTP_PRIO_REALTIME:
2714 prio = rtp->prio / LINUX_PRIO_DIVIDER;
2715 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_RT, prio);
2716 break;
2717 default:
2718 prio = RTP_PRIO_MIN;
2719 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_NONE, prio);
2720 break;
2721 }
2722 return (ioprio);
2723 }
2724
2725 static int
linux_ioprio2rtprio(int ioprio,struct rtprio * rtp)2726 linux_ioprio2rtprio(int ioprio, struct rtprio *rtp)
2727 {
2728
2729 switch (LINUX_IOPRIO_PRIO_CLASS(ioprio)) {
2730 case LINUX_IOPRIO_CLASS_IDLE:
2731 rtp->prio = RTP_PRIO_MIN;
2732 rtp->type = RTP_PRIO_IDLE;
2733 break;
2734 case LINUX_IOPRIO_CLASS_BE:
2735 rtp->prio = LINUX_IOPRIO_PRIO_DATA(ioprio) * LINUX_PRIO_DIVIDER;
2736 rtp->type = RTP_PRIO_NORMAL;
2737 break;
2738 case LINUX_IOPRIO_CLASS_RT:
2739 rtp->prio = LINUX_IOPRIO_PRIO_DATA(ioprio) * LINUX_PRIO_DIVIDER;
2740 rtp->type = RTP_PRIO_REALTIME;
2741 break;
2742 default:
2743 return (EINVAL);
2744 }
2745 return (0);
2746 }
2747 #undef LINUX_PRIO_DIVIDER
2748
2749 int
linux_ioprio_get(struct thread * td,struct linux_ioprio_get_args * args)2750 linux_ioprio_get(struct thread *td, struct linux_ioprio_get_args *args)
2751 {
2752 struct thread *td1;
2753 struct rtprio rtp;
2754 struct pgrp *pg;
2755 struct proc *p;
2756 int error, found;
2757
2758 p = NULL;
2759 td1 = NULL;
2760 error = 0;
2761 found = 0;
2762 rtp.type = RTP_PRIO_IDLE;
2763 rtp.prio = RTP_PRIO_MAX;
2764 switch (args->which) {
2765 case LINUX_IOPRIO_WHO_PROCESS:
2766 if (args->who == 0) {
2767 td1 = td;
2768 p = td1->td_proc;
2769 PROC_LOCK(p);
2770 } else if (args->who > PID_MAX) {
2771 td1 = linux_tdfind(td, args->who, -1);
2772 if (td1 != NULL)
2773 p = td1->td_proc;
2774 } else
2775 p = pfind(args->who);
2776 if (p == NULL)
2777 return (ESRCH);
2778 if ((error = p_cansee(td, p))) {
2779 PROC_UNLOCK(p);
2780 break;
2781 }
2782 if (td1 != NULL) {
2783 pri_to_rtp(td1, &rtp);
2784 } else {
2785 FOREACH_THREAD_IN_PROC(p, td1) {
2786 linux_up_rtprio_if(td1, &rtp);
2787 }
2788 }
2789 found++;
2790 PROC_UNLOCK(p);
2791 break;
2792 case LINUX_IOPRIO_WHO_PGRP:
2793 sx_slock(&proctree_lock);
2794 if (args->who == 0) {
2795 pg = td->td_proc->p_pgrp;
2796 PGRP_LOCK(pg);
2797 } else {
2798 pg = pgfind(args->who);
2799 if (pg == NULL) {
2800 sx_sunlock(&proctree_lock);
2801 error = ESRCH;
2802 break;
2803 }
2804 }
2805 sx_sunlock(&proctree_lock);
2806 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
2807 PROC_LOCK(p);
2808 if (p->p_state == PRS_NORMAL &&
2809 p_cansee(td, p) == 0) {
2810 FOREACH_THREAD_IN_PROC(p, td1) {
2811 linux_up_rtprio_if(td1, &rtp);
2812 found++;
2813 }
2814 }
2815 PROC_UNLOCK(p);
2816 }
2817 PGRP_UNLOCK(pg);
2818 break;
2819 case LINUX_IOPRIO_WHO_USER:
2820 if (args->who == 0)
2821 args->who = td->td_ucred->cr_uid;
2822 sx_slock(&allproc_lock);
2823 FOREACH_PROC_IN_SYSTEM(p) {
2824 PROC_LOCK(p);
2825 if (p->p_state == PRS_NORMAL &&
2826 p->p_ucred->cr_uid == args->who &&
2827 p_cansee(td, p) == 0) {
2828 FOREACH_THREAD_IN_PROC(p, td1) {
2829 linux_up_rtprio_if(td1, &rtp);
2830 found++;
2831 }
2832 }
2833 PROC_UNLOCK(p);
2834 }
2835 sx_sunlock(&allproc_lock);
2836 break;
2837 default:
2838 error = EINVAL;
2839 break;
2840 }
2841 if (error == 0) {
2842 if (found != 0)
2843 td->td_retval[0] = linux_rtprio2ioprio(&rtp);
2844 else
2845 error = ESRCH;
2846 }
2847 return (error);
2848 }
2849
2850 int
linux_ioprio_set(struct thread * td,struct linux_ioprio_set_args * args)2851 linux_ioprio_set(struct thread *td, struct linux_ioprio_set_args *args)
2852 {
2853 struct thread *td1;
2854 struct rtprio rtp;
2855 struct pgrp *pg;
2856 struct proc *p;
2857 int error;
2858
2859 if ((error = linux_ioprio2rtprio(args->ioprio, &rtp)) != 0)
2860 return (error);
2861 /* Attempts to set high priorities (REALTIME) require su privileges. */
2862 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME &&
2863 (error = priv_check(td, PRIV_SCHED_RTPRIO)) != 0)
2864 return (error);
2865
2866 p = NULL;
2867 td1 = NULL;
2868 switch (args->which) {
2869 case LINUX_IOPRIO_WHO_PROCESS:
2870 if (args->who == 0) {
2871 td1 = td;
2872 p = td1->td_proc;
2873 PROC_LOCK(p);
2874 } else if (args->who > PID_MAX) {
2875 td1 = linux_tdfind(td, args->who, -1);
2876 if (td1 != NULL)
2877 p = td1->td_proc;
2878 } else
2879 p = pfind(args->who);
2880 if (p == NULL)
2881 return (ESRCH);
2882 if ((error = p_cansched(td, p))) {
2883 PROC_UNLOCK(p);
2884 break;
2885 }
2886 if (td1 != NULL) {
2887 error = rtp_to_pri(&rtp, td1);
2888 } else {
2889 FOREACH_THREAD_IN_PROC(p, td1) {
2890 if ((error = rtp_to_pri(&rtp, td1)) != 0)
2891 break;
2892 }
2893 }
2894 PROC_UNLOCK(p);
2895 break;
2896 case LINUX_IOPRIO_WHO_PGRP:
2897 sx_slock(&proctree_lock);
2898 if (args->who == 0) {
2899 pg = td->td_proc->p_pgrp;
2900 PGRP_LOCK(pg);
2901 } else {
2902 pg = pgfind(args->who);
2903 if (pg == NULL) {
2904 sx_sunlock(&proctree_lock);
2905 error = ESRCH;
2906 break;
2907 }
2908 }
2909 sx_sunlock(&proctree_lock);
2910 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
2911 PROC_LOCK(p);
2912 if (p->p_state == PRS_NORMAL &&
2913 p_cansched(td, p) == 0) {
2914 FOREACH_THREAD_IN_PROC(p, td1) {
2915 if ((error = rtp_to_pri(&rtp, td1)) != 0)
2916 break;
2917 }
2918 }
2919 PROC_UNLOCK(p);
2920 if (error != 0)
2921 break;
2922 }
2923 PGRP_UNLOCK(pg);
2924 break;
2925 case LINUX_IOPRIO_WHO_USER:
2926 if (args->who == 0)
2927 args->who = td->td_ucred->cr_uid;
2928 sx_slock(&allproc_lock);
2929 FOREACH_PROC_IN_SYSTEM(p) {
2930 PROC_LOCK(p);
2931 if (p->p_state == PRS_NORMAL &&
2932 p->p_ucred->cr_uid == args->who &&
2933 p_cansched(td, p) == 0) {
2934 FOREACH_THREAD_IN_PROC(p, td1) {
2935 if ((error = rtp_to_pri(&rtp, td1)) != 0)
2936 break;
2937 }
2938 }
2939 PROC_UNLOCK(p);
2940 if (error != 0)
2941 break;
2942 }
2943 sx_sunlock(&allproc_lock);
2944 break;
2945 default:
2946 error = EINVAL;
2947 break;
2948 }
2949 return (error);
2950 }
2951
2952 /* The only flag is O_NONBLOCK */
2953 #define B2L_MQ_FLAGS(bflags) ((bflags) != 0 ? LINUX_O_NONBLOCK : 0)
2954 #define L2B_MQ_FLAGS(lflags) ((lflags) != 0 ? O_NONBLOCK : 0)
2955
2956 int
linux_mq_open(struct thread * td,struct linux_mq_open_args * args)2957 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
2958 {
2959 struct mq_attr attr;
2960 int error, flags;
2961
2962 flags = linux_common_openflags(args->oflag);
2963 if ((flags & O_ACCMODE) == O_ACCMODE || (flags & O_EXEC) != 0)
2964 return (EINVAL);
2965 flags = FFLAGS(flags);
2966 if ((flags & O_CREAT) != 0 && args->attr != NULL) {
2967 error = copyin(args->attr, &attr, sizeof(attr));
2968 if (error != 0)
2969 return (error);
2970 attr.mq_flags = L2B_MQ_FLAGS(attr.mq_flags);
2971 }
2972
2973 return (kern_kmq_open(td, args->name, flags, args->mode,
2974 args->attr != NULL ? &attr : NULL));
2975 }
2976
2977 int
linux_mq_unlink(struct thread * td,struct linux_mq_unlink_args * args)2978 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
2979 {
2980 struct kmq_unlink_args bsd_args = {
2981 .path = PTRIN(args->name)
2982 };
2983
2984 return (sys_kmq_unlink(td, &bsd_args));
2985 }
2986
2987 int
linux_mq_timedsend(struct thread * td,struct linux_mq_timedsend_args * args)2988 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
2989 {
2990 struct timespec ts, *abs_timeout;
2991 int error;
2992
2993 if (args->abs_timeout == NULL)
2994 abs_timeout = NULL;
2995 else {
2996 error = linux_get_timespec(&ts, args->abs_timeout);
2997 if (error != 0)
2998 return (error);
2999 abs_timeout = &ts;
3000 }
3001
3002 return (kern_kmq_timedsend(td, args->mqd, PTRIN(args->msg_ptr),
3003 args->msg_len, args->msg_prio, abs_timeout));
3004 }
3005
3006 int
linux_mq_timedreceive(struct thread * td,struct linux_mq_timedreceive_args * args)3007 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
3008 {
3009 struct timespec ts, *abs_timeout;
3010 int error;
3011
3012 if (args->abs_timeout == NULL)
3013 abs_timeout = NULL;
3014 else {
3015 error = linux_get_timespec(&ts, args->abs_timeout);
3016 if (error != 0)
3017 return (error);
3018 abs_timeout = &ts;
3019 }
3020
3021 return (kern_kmq_timedreceive(td, args->mqd, PTRIN(args->msg_ptr),
3022 args->msg_len, args->msg_prio, abs_timeout));
3023 }
3024
3025 int
linux_mq_notify(struct thread * td,struct linux_mq_notify_args * args)3026 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
3027 {
3028 struct sigevent ev, *evp;
3029 struct l_sigevent l_ev;
3030 int error;
3031
3032 if (args->sevp == NULL)
3033 evp = NULL;
3034 else {
3035 error = copyin(args->sevp, &l_ev, sizeof(l_ev));
3036 if (error != 0)
3037 return (error);
3038 error = linux_convert_l_sigevent(&l_ev, &ev);
3039 if (error != 0)
3040 return (error);
3041 evp = &ev;
3042 }
3043
3044 return (kern_kmq_notify(td, args->mqd, evp));
3045 }
3046
3047 int
linux_mq_getsetattr(struct thread * td,struct linux_mq_getsetattr_args * args)3048 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
3049 {
3050 struct mq_attr attr, oattr;
3051 int error;
3052
3053 if (args->attr != NULL) {
3054 error = copyin(args->attr, &attr, sizeof(attr));
3055 if (error != 0)
3056 return (error);
3057 attr.mq_flags = L2B_MQ_FLAGS(attr.mq_flags);
3058 }
3059
3060 error = kern_kmq_setattr(td, args->mqd, args->attr != NULL ? &attr : NULL,
3061 &oattr);
3062 if (error == 0 && args->oattr != NULL) {
3063 oattr.mq_flags = B2L_MQ_FLAGS(oattr.mq_flags);
3064 bzero(oattr.__reserved, sizeof(oattr.__reserved));
3065 error = copyout(&oattr, args->oattr, sizeof(oattr));
3066 }
3067
3068 return (error);
3069 }
3070
3071 int
linux_kcmp(struct thread * td,struct linux_kcmp_args * args)3072 linux_kcmp(struct thread *td, struct linux_kcmp_args *args)
3073 {
3074 int type;
3075
3076 switch (args->type) {
3077 case LINUX_KCMP_FILE:
3078 type = KCMP_FILE;
3079 break;
3080 case LINUX_KCMP_FILES:
3081 type = KCMP_FILES;
3082 break;
3083 case LINUX_KCMP_SIGHAND:
3084 type = KCMP_SIGHAND;
3085 break;
3086 case LINUX_KCMP_VM:
3087 type = KCMP_VM;
3088 break;
3089 default:
3090 return (EINVAL);
3091 }
3092
3093 return (kern_kcmp(td, args->pid1, args->pid2, type, args->idx1,
3094 args->idx));
3095 }
3096
3097 MODULE_DEPEND(linux, mqueuefs, 1, 1, 1);
3098