1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_ktrace.h"
35
36 #define __ELF_WORD_SIZE 32
37
38 #ifdef COMPAT_FREEBSD11
39 #define _WANT_FREEBSD11_KEVENT
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/capsicum.h>
45 #include <sys/clock.h>
46 #include <sys/exec.h>
47 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/imgact.h>
50 #include <sys/jail.h>
51 #include <sys/kernel.h>
52 #include <sys/limits.h>
53 #include <sys/linker.h>
54 #include <sys/lock.h>
55 #include <sys/malloc.h>
56 #include <sys/file.h> /* Must come after sys/malloc.h */
57 #include <sys/imgact.h>
58 #include <sys/mbuf.h>
59 #include <sys/mman.h>
60 #include <sys/module.h>
61 #include <sys/mount.h>
62 #include <sys/mutex.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/procctl.h>
66 #include <sys/reboot.h>
67 #include <sys/resource.h>
68 #include <sys/resourcevar.h>
69 #include <sys/selinfo.h>
70 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
71 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
72 #include <sys/signal.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/stat.h>
77 #include <sys/syscall.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysent.h>
81 #include <sys/sysproto.h>
82 #include <sys/systm.h>
83 #include <sys/thr.h>
84 #include <sys/unistd.h>
85 #include <sys/ucontext.h>
86 #include <sys/vnode.h>
87 #include <sys/wait.h>
88 #include <sys/ipc.h>
89 #include <sys/msg.h>
90 #include <sys/sem.h>
91 #include <sys/shm.h>
92 #ifdef KTRACE
93 #include <sys/ktrace.h>
94 #endif
95
96 #ifdef INET
97 #include <netinet/in.h>
98 #endif
99
100 #include <vm/vm.h>
101 #include <vm/vm_param.h>
102 #include <vm/pmap.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_extern.h>
106
107 #include <machine/cpu.h>
108 #include <machine/elf.h>
109
110 #include <security/audit/audit.h>
111
112 #include <compat/freebsd32/freebsd32_util.h>
113 #include <compat/freebsd32/freebsd32.h>
114 #include <compat/freebsd32/freebsd32_ipc.h>
115 #include <compat/freebsd32/freebsd32_misc.h>
116 #include <compat/freebsd32/freebsd32_signal.h>
117 #include <compat/freebsd32/freebsd32_proto.h>
118
119 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
120
121 #ifdef __amd64__
122 CTASSERT(sizeof(struct timeval32) == 8);
123 CTASSERT(sizeof(struct timespec32) == 8);
124 CTASSERT(sizeof(struct itimerval32) == 16);
125 CTASSERT(sizeof(struct bintime32) == 12);
126 #endif
127 CTASSERT(sizeof(struct statfs32) == 256);
128 #ifdef __amd64__
129 CTASSERT(sizeof(struct rusage32) == 72);
130 #endif
131 CTASSERT(sizeof(struct sigaltstack32) == 12);
132 #ifdef __amd64__
133 CTASSERT(sizeof(struct kevent32) == 56);
134 #else
135 CTASSERT(sizeof(struct kevent32) == 64);
136 #endif
137 CTASSERT(sizeof(struct iovec32) == 8);
138 CTASSERT(sizeof(struct msghdr32) == 28);
139 #ifdef __amd64__
140 CTASSERT(sizeof(struct stat32) == 208);
141 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
142 #endif
143 CTASSERT(sizeof(struct sigaction32) == 24);
144
145 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
146 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
147 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
148 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
149
150 void
freebsd32_rusage_out(const struct rusage * s,struct rusage32 * s32)151 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
152 {
153
154 TV_CP(*s, *s32, ru_utime);
155 TV_CP(*s, *s32, ru_stime);
156 CP(*s, *s32, ru_maxrss);
157 CP(*s, *s32, ru_ixrss);
158 CP(*s, *s32, ru_idrss);
159 CP(*s, *s32, ru_isrss);
160 CP(*s, *s32, ru_minflt);
161 CP(*s, *s32, ru_majflt);
162 CP(*s, *s32, ru_nswap);
163 CP(*s, *s32, ru_inblock);
164 CP(*s, *s32, ru_oublock);
165 CP(*s, *s32, ru_msgsnd);
166 CP(*s, *s32, ru_msgrcv);
167 CP(*s, *s32, ru_nsignals);
168 CP(*s, *s32, ru_nvcsw);
169 CP(*s, *s32, ru_nivcsw);
170 }
171
172 int
freebsd32_wait4(struct thread * td,struct freebsd32_wait4_args * uap)173 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
174 {
175 int error, status;
176 struct rusage32 ru32;
177 struct rusage ru, *rup;
178
179 if (uap->rusage != NULL)
180 rup = &ru;
181 else
182 rup = NULL;
183 error = kern_wait(td, uap->pid, &status, uap->options, rup);
184 if (error)
185 return (error);
186 if (uap->status != NULL)
187 error = copyout(&status, uap->status, sizeof(status));
188 if (uap->rusage != NULL && error == 0) {
189 freebsd32_rusage_out(&ru, &ru32);
190 error = copyout(&ru32, uap->rusage, sizeof(ru32));
191 }
192 return (error);
193 }
194
195 int
freebsd32_wait6(struct thread * td,struct freebsd32_wait6_args * uap)196 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
197 {
198 struct wrusage32 wru32;
199 struct __wrusage wru, *wrup;
200 struct siginfo32 si32;
201 struct __siginfo si, *sip;
202 int error, status;
203
204 if (uap->wrusage != NULL)
205 wrup = &wru;
206 else
207 wrup = NULL;
208 if (uap->info != NULL) {
209 sip = &si;
210 bzero(sip, sizeof(*sip));
211 } else
212 sip = NULL;
213 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
214 &status, uap->options, wrup, sip);
215 if (error != 0)
216 return (error);
217 if (uap->status != NULL)
218 error = copyout(&status, uap->status, sizeof(status));
219 if (uap->wrusage != NULL && error == 0) {
220 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
221 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
222 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
223 }
224 if (uap->info != NULL && error == 0) {
225 siginfo_to_siginfo32 (&si, &si32);
226 error = copyout(&si32, uap->info, sizeof(si32));
227 }
228 return (error);
229 }
230
231 #ifdef COMPAT_FREEBSD4
232 static void
copy_statfs(struct statfs * in,struct statfs32 * out)233 copy_statfs(struct statfs *in, struct statfs32 *out)
234 {
235
236 statfs_scale_blocks(in, INT32_MAX);
237 bzero(out, sizeof(*out));
238 CP(*in, *out, f_bsize);
239 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
240 CP(*in, *out, f_blocks);
241 CP(*in, *out, f_bfree);
242 CP(*in, *out, f_bavail);
243 out->f_files = MIN(in->f_files, INT32_MAX);
244 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
245 CP(*in, *out, f_fsid);
246 CP(*in, *out, f_owner);
247 CP(*in, *out, f_type);
248 CP(*in, *out, f_flags);
249 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
250 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
251 strlcpy(out->f_fstypename,
252 in->f_fstypename, MFSNAMELEN);
253 strlcpy(out->f_mntonname,
254 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
255 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
256 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
257 strlcpy(out->f_mntfromname,
258 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
259 }
260 #endif
261
262 #ifdef COMPAT_FREEBSD4
263 int
freebsd4_freebsd32_getfsstat(struct thread * td,struct freebsd4_freebsd32_getfsstat_args * uap)264 freebsd4_freebsd32_getfsstat(struct thread *td,
265 struct freebsd4_freebsd32_getfsstat_args *uap)
266 {
267 struct statfs *buf, *sp;
268 struct statfs32 stat32;
269 size_t count, size, copycount;
270 int error;
271
272 count = uap->bufsize / sizeof(struct statfs32);
273 size = count * sizeof(struct statfs);
274 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
275 if (size > 0) {
276 sp = buf;
277 copycount = count;
278 while (copycount > 0 && error == 0) {
279 copy_statfs(sp, &stat32);
280 error = copyout(&stat32, uap->buf, sizeof(stat32));
281 sp++;
282 uap->buf++;
283 copycount--;
284 }
285 free(buf, M_STATFS);
286 }
287 if (error == 0)
288 td->td_retval[0] = count;
289 return (error);
290 }
291 #endif
292
293 #ifdef COMPAT_FREEBSD10
294 int
freebsd10_freebsd32_pipe(struct thread * td,struct freebsd10_freebsd32_pipe_args * uap)295 freebsd10_freebsd32_pipe(struct thread *td,
296 struct freebsd10_freebsd32_pipe_args *uap) {
297
298 return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
299 }
300 #endif
301
302 int
freebsd32_sigaltstack(struct thread * td,struct freebsd32_sigaltstack_args * uap)303 freebsd32_sigaltstack(struct thread *td,
304 struct freebsd32_sigaltstack_args *uap)
305 {
306 struct sigaltstack32 s32;
307 struct sigaltstack ss, oss, *ssp;
308 int error;
309
310 if (uap->ss != NULL) {
311 error = copyin(uap->ss, &s32, sizeof(s32));
312 if (error)
313 return (error);
314 PTRIN_CP(s32, ss, ss_sp);
315 CP(s32, ss, ss_size);
316 CP(s32, ss, ss_flags);
317 ssp = &ss;
318 } else
319 ssp = NULL;
320 error = kern_sigaltstack(td, ssp, &oss);
321 if (error == 0 && uap->oss != NULL) {
322 PTROUT_CP(oss, s32, ss_sp);
323 CP(oss, s32, ss_size);
324 CP(oss, s32, ss_flags);
325 error = copyout(&s32, uap->oss, sizeof(s32));
326 }
327 return (error);
328 }
329
330 /*
331 * Custom version of exec_copyin_args() so that we can translate
332 * the pointers.
333 */
334 int
freebsd32_exec_copyin_args(struct image_args * args,char * fname,enum uio_seg segflg,u_int32_t * argv,u_int32_t * envv)335 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
336 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
337 {
338 char *argp, *envp;
339 u_int32_t *p32, arg;
340 size_t length;
341 int error;
342
343 bzero(args, sizeof(*args));
344 if (argv == NULL)
345 return (EFAULT);
346
347 /*
348 * Allocate demand-paged memory for the file name, argument, and
349 * environment strings.
350 */
351 error = exec_alloc_args(args);
352 if (error != 0)
353 return (error);
354
355 /*
356 * Copy the file name.
357 */
358 if (fname != NULL) {
359 args->fname = args->buf;
360 error = (segflg == UIO_SYSSPACE) ?
361 copystr(fname, args->fname, PATH_MAX, &length) :
362 copyinstr(fname, args->fname, PATH_MAX, &length);
363 if (error != 0)
364 goto err_exit;
365 } else
366 length = 0;
367
368 args->begin_argv = args->buf + length;
369 args->endp = args->begin_argv;
370 args->stringspace = ARG_MAX;
371
372 /*
373 * extract arguments first
374 */
375 p32 = argv;
376 for (;;) {
377 error = copyin(p32++, &arg, sizeof(arg));
378 if (error)
379 goto err_exit;
380 if (arg == 0)
381 break;
382 argp = PTRIN(arg);
383 error = copyinstr(argp, args->endp, args->stringspace, &length);
384 if (error) {
385 if (error == ENAMETOOLONG)
386 error = E2BIG;
387 goto err_exit;
388 }
389 args->stringspace -= length;
390 args->endp += length;
391 args->argc++;
392 }
393
394 args->begin_envv = args->endp;
395
396 /*
397 * extract environment strings
398 */
399 if (envv) {
400 p32 = envv;
401 for (;;) {
402 error = copyin(p32++, &arg, sizeof(arg));
403 if (error)
404 goto err_exit;
405 if (arg == 0)
406 break;
407 envp = PTRIN(arg);
408 error = copyinstr(envp, args->endp, args->stringspace,
409 &length);
410 if (error) {
411 if (error == ENAMETOOLONG)
412 error = E2BIG;
413 goto err_exit;
414 }
415 args->stringspace -= length;
416 args->endp += length;
417 args->envc++;
418 }
419 }
420
421 return (0);
422
423 err_exit:
424 exec_free_args(args);
425 return (error);
426 }
427
428 int
freebsd32_execve(struct thread * td,struct freebsd32_execve_args * uap)429 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
430 {
431 struct image_args eargs;
432 struct vmspace *oldvmspace;
433 int error;
434
435 error = pre_execve(td, &oldvmspace);
436 if (error != 0)
437 return (error);
438 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
439 uap->argv, uap->envv);
440 if (error == 0)
441 error = kern_execve(td, &eargs, NULL);
442 post_execve(td, error, oldvmspace);
443 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
444 return (error);
445 }
446
447 int
freebsd32_fexecve(struct thread * td,struct freebsd32_fexecve_args * uap)448 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
449 {
450 struct image_args eargs;
451 struct vmspace *oldvmspace;
452 int error;
453
454 error = pre_execve(td, &oldvmspace);
455 if (error != 0)
456 return (error);
457 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
458 uap->argv, uap->envv);
459 if (error == 0) {
460 eargs.fd = uap->fd;
461 error = kern_execve(td, &eargs, NULL);
462 }
463 post_execve(td, error, oldvmspace);
464 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
465 return (error);
466 }
467
468
469 int
freebsd32_mknodat(struct thread * td,struct freebsd32_mknodat_args * uap)470 freebsd32_mknodat(struct thread *td, struct freebsd32_mknodat_args *uap)
471 {
472
473 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE,
474 uap->mode, PAIR32TO64(dev_t, uap->dev)));
475 }
476
477 int
freebsd32_mprotect(struct thread * td,struct freebsd32_mprotect_args * uap)478 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
479 {
480 int prot;
481
482 prot = uap->prot;
483 #if defined(__amd64__)
484 if (i386_read_exec && (prot & PROT_READ) != 0)
485 prot |= PROT_EXEC;
486 #endif
487 return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
488 prot));
489 }
490
491 int
freebsd32_mmap(struct thread * td,struct freebsd32_mmap_args * uap)492 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
493 {
494 int prot;
495
496 prot = uap->prot;
497 #if defined(__amd64__)
498 if (i386_read_exec && (prot & PROT_READ))
499 prot |= PROT_EXEC;
500 #endif
501
502 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
503 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
504 }
505
506 #ifdef COMPAT_FREEBSD6
507 int
freebsd6_freebsd32_mmap(struct thread * td,struct freebsd6_freebsd32_mmap_args * uap)508 freebsd6_freebsd32_mmap(struct thread *td,
509 struct freebsd6_freebsd32_mmap_args *uap)
510 {
511 int prot;
512
513 prot = uap->prot;
514 #if defined(__amd64__)
515 if (i386_read_exec && (prot & PROT_READ))
516 prot |= PROT_EXEC;
517 #endif
518
519 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
520 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
521 }
522 #endif
523
524 int
freebsd32_setitimer(struct thread * td,struct freebsd32_setitimer_args * uap)525 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
526 {
527 struct itimerval itv, oitv, *itvp;
528 struct itimerval32 i32;
529 int error;
530
531 if (uap->itv != NULL) {
532 error = copyin(uap->itv, &i32, sizeof(i32));
533 if (error)
534 return (error);
535 TV_CP(i32, itv, it_interval);
536 TV_CP(i32, itv, it_value);
537 itvp = &itv;
538 } else
539 itvp = NULL;
540 error = kern_setitimer(td, uap->which, itvp, &oitv);
541 if (error || uap->oitv == NULL)
542 return (error);
543 TV_CP(oitv, i32, it_interval);
544 TV_CP(oitv, i32, it_value);
545 return (copyout(&i32, uap->oitv, sizeof(i32)));
546 }
547
548 int
freebsd32_getitimer(struct thread * td,struct freebsd32_getitimer_args * uap)549 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
550 {
551 struct itimerval itv;
552 struct itimerval32 i32;
553 int error;
554
555 error = kern_getitimer(td, uap->which, &itv);
556 if (error || uap->itv == NULL)
557 return (error);
558 TV_CP(itv, i32, it_interval);
559 TV_CP(itv, i32, it_value);
560 return (copyout(&i32, uap->itv, sizeof(i32)));
561 }
562
563 int
freebsd32_select(struct thread * td,struct freebsd32_select_args * uap)564 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
565 {
566 struct timeval32 tv32;
567 struct timeval tv, *tvp;
568 int error;
569
570 if (uap->tv != NULL) {
571 error = copyin(uap->tv, &tv32, sizeof(tv32));
572 if (error)
573 return (error);
574 CP(tv32, tv, tv_sec);
575 CP(tv32, tv, tv_usec);
576 tvp = &tv;
577 } else
578 tvp = NULL;
579 /*
580 * XXX Do pointers need PTRIN()?
581 */
582 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
583 sizeof(int32_t) * 8));
584 }
585
586 int
freebsd32_pselect(struct thread * td,struct freebsd32_pselect_args * uap)587 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
588 {
589 struct timespec32 ts32;
590 struct timespec ts;
591 struct timeval tv, *tvp;
592 sigset_t set, *uset;
593 int error;
594
595 if (uap->ts != NULL) {
596 error = copyin(uap->ts, &ts32, sizeof(ts32));
597 if (error != 0)
598 return (error);
599 CP(ts32, ts, tv_sec);
600 CP(ts32, ts, tv_nsec);
601 TIMESPEC_TO_TIMEVAL(&tv, &ts);
602 tvp = &tv;
603 } else
604 tvp = NULL;
605 if (uap->sm != NULL) {
606 error = copyin(uap->sm, &set, sizeof(set));
607 if (error != 0)
608 return (error);
609 uset = &set;
610 } else
611 uset = NULL;
612 /*
613 * XXX Do pointers need PTRIN()?
614 */
615 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
616 uset, sizeof(int32_t) * 8);
617 return (error);
618 }
619
620 /*
621 * Copy 'count' items into the destination list pointed to by uap->eventlist.
622 */
623 static int
freebsd32_kevent_copyout(void * arg,struct kevent * kevp,int count)624 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
625 {
626 struct freebsd32_kevent_args *uap;
627 struct kevent32 ks32[KQ_NEVENTS];
628 uint64_t e;
629 int i, j, error;
630
631 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
632 uap = (struct freebsd32_kevent_args *)arg;
633
634 for (i = 0; i < count; i++) {
635 CP(kevp[i], ks32[i], ident);
636 CP(kevp[i], ks32[i], filter);
637 CP(kevp[i], ks32[i], flags);
638 CP(kevp[i], ks32[i], fflags);
639 #if BYTE_ORDER == LITTLE_ENDIAN
640 ks32[i].data1 = kevp[i].data;
641 ks32[i].data2 = kevp[i].data >> 32;
642 #else
643 ks32[i].data1 = kevp[i].data >> 32;
644 ks32[i].data2 = kevp[i].data;
645 #endif
646 PTROUT_CP(kevp[i], ks32[i], udata);
647 for (j = 0; j < nitems(kevp->ext); j++) {
648 e = kevp[i].ext[j];
649 #if BYTE_ORDER == LITTLE_ENDIAN
650 ks32[i].ext64[2 * j] = e;
651 ks32[i].ext64[2 * j + 1] = e >> 32;
652 #else
653 ks32[i].ext64[2 * j] = e >> 32;
654 ks32[i].ext64[2 * j + 1] = e;
655 #endif
656 }
657 }
658 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
659 if (error == 0)
660 uap->eventlist += count;
661 return (error);
662 }
663
664 /*
665 * Copy 'count' items from the list pointed to by uap->changelist.
666 */
667 static int
freebsd32_kevent_copyin(void * arg,struct kevent * kevp,int count)668 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
669 {
670 struct freebsd32_kevent_args *uap;
671 struct kevent32 ks32[KQ_NEVENTS];
672 uint64_t e;
673 int i, j, error;
674
675 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
676 uap = (struct freebsd32_kevent_args *)arg;
677
678 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
679 if (error)
680 goto done;
681 uap->changelist += count;
682
683 for (i = 0; i < count; i++) {
684 CP(ks32[i], kevp[i], ident);
685 CP(ks32[i], kevp[i], filter);
686 CP(ks32[i], kevp[i], flags);
687 CP(ks32[i], kevp[i], fflags);
688 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
689 PTRIN_CP(ks32[i], kevp[i], udata);
690 for (j = 0; j < nitems(kevp->ext); j++) {
691 #if BYTE_ORDER == LITTLE_ENDIAN
692 e = ks32[i].ext64[2 * j + 1];
693 e <<= 32;
694 e += ks32[i].ext64[2 * j];
695 #else
696 e = ks32[i].ext64[2 * j];
697 e <<= 32;
698 e += ks32[i].ext64[2 * j + 1];
699 #endif
700 kevp[i].ext[j] = e;
701 }
702 }
703 done:
704 return (error);
705 }
706
707 int
freebsd32_kevent(struct thread * td,struct freebsd32_kevent_args * uap)708 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
709 {
710 struct timespec32 ts32;
711 struct timespec ts, *tsp;
712 struct kevent_copyops k_ops = {
713 .arg = uap,
714 .k_copyout = freebsd32_kevent_copyout,
715 .k_copyin = freebsd32_kevent_copyin,
716 };
717 #ifdef KTRACE
718 struct kevent32 *eventlist = uap->eventlist;
719 #endif
720 int error;
721
722 if (uap->timeout) {
723 error = copyin(uap->timeout, &ts32, sizeof(ts32));
724 if (error)
725 return (error);
726 CP(ts32, ts, tv_sec);
727 CP(ts32, ts, tv_nsec);
728 tsp = &ts;
729 } else
730 tsp = NULL;
731 #ifdef KTRACE
732 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
733 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
734 uap->nchanges, sizeof(struct kevent32));
735 #endif
736 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
737 &k_ops, tsp);
738 #ifdef KTRACE
739 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
740 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
741 td->td_retval[0], sizeof(struct kevent32));
742 #endif
743 return (error);
744 }
745
746 #ifdef COMPAT_FREEBSD11
747 static int
freebsd32_kevent11_copyout(void * arg,struct kevent * kevp,int count)748 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
749 {
750 struct freebsd11_freebsd32_kevent_args *uap;
751 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
752 int i, error;
753
754 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
755 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
756
757 for (i = 0; i < count; i++) {
758 CP(kevp[i], ks32[i], ident);
759 CP(kevp[i], ks32[i], filter);
760 CP(kevp[i], ks32[i], flags);
761 CP(kevp[i], ks32[i], fflags);
762 CP(kevp[i], ks32[i], data);
763 PTROUT_CP(kevp[i], ks32[i], udata);
764 }
765 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
766 if (error == 0)
767 uap->eventlist += count;
768 return (error);
769 }
770
771 /*
772 * Copy 'count' items from the list pointed to by uap->changelist.
773 */
774 static int
freebsd32_kevent11_copyin(void * arg,struct kevent * kevp,int count)775 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
776 {
777 struct freebsd11_freebsd32_kevent_args *uap;
778 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
779 int i, j, error;
780
781 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
782 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
783
784 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
785 if (error)
786 goto done;
787 uap->changelist += count;
788
789 for (i = 0; i < count; i++) {
790 CP(ks32[i], kevp[i], ident);
791 CP(ks32[i], kevp[i], filter);
792 CP(ks32[i], kevp[i], flags);
793 CP(ks32[i], kevp[i], fflags);
794 CP(ks32[i], kevp[i], data);
795 PTRIN_CP(ks32[i], kevp[i], udata);
796 for (j = 0; j < nitems(kevp->ext); j++)
797 kevp[i].ext[j] = 0;
798 }
799 done:
800 return (error);
801 }
802
803 int
freebsd11_freebsd32_kevent(struct thread * td,struct freebsd11_freebsd32_kevent_args * uap)804 freebsd11_freebsd32_kevent(struct thread *td,
805 struct freebsd11_freebsd32_kevent_args *uap)
806 {
807 struct timespec32 ts32;
808 struct timespec ts, *tsp;
809 struct kevent_copyops k_ops = {
810 .arg = uap,
811 .k_copyout = freebsd32_kevent11_copyout,
812 .k_copyin = freebsd32_kevent11_copyin,
813 };
814 #ifdef KTRACE
815 struct kevent32_freebsd11 *eventlist = uap->eventlist;
816 #endif
817 int error;
818
819 if (uap->timeout) {
820 error = copyin(uap->timeout, &ts32, sizeof(ts32));
821 if (error)
822 return (error);
823 CP(ts32, ts, tv_sec);
824 CP(ts32, ts, tv_nsec);
825 tsp = &ts;
826 } else
827 tsp = NULL;
828 #ifdef KTRACE
829 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
830 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
831 uap->changelist, uap->nchanges,
832 sizeof(struct kevent32_freebsd11));
833 #endif
834 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
835 &k_ops, tsp);
836 #ifdef KTRACE
837 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
838 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
839 eventlist, td->td_retval[0],
840 sizeof(struct kevent32_freebsd11));
841 #endif
842 return (error);
843 }
844 #endif
845
846 int
freebsd32_gettimeofday(struct thread * td,struct freebsd32_gettimeofday_args * uap)847 freebsd32_gettimeofday(struct thread *td,
848 struct freebsd32_gettimeofday_args *uap)
849 {
850 struct timeval atv;
851 struct timeval32 atv32;
852 struct timezone rtz;
853 int error = 0;
854
855 if (uap->tp) {
856 microtime(&atv);
857 CP(atv, atv32, tv_sec);
858 CP(atv, atv32, tv_usec);
859 error = copyout(&atv32, uap->tp, sizeof (atv32));
860 }
861 if (error == 0 && uap->tzp != NULL) {
862 rtz.tz_minuteswest = tz_minuteswest;
863 rtz.tz_dsttime = tz_dsttime;
864 error = copyout(&rtz, uap->tzp, sizeof (rtz));
865 }
866 return (error);
867 }
868
869 int
freebsd32_getrusage(struct thread * td,struct freebsd32_getrusage_args * uap)870 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
871 {
872 struct rusage32 s32;
873 struct rusage s;
874 int error;
875
876 error = kern_getrusage(td, uap->who, &s);
877 if (error == 0) {
878 freebsd32_rusage_out(&s, &s32);
879 error = copyout(&s32, uap->rusage, sizeof(s32));
880 }
881 return (error);
882 }
883
884 static int
freebsd32_copyinuio(struct iovec32 * iovp,u_int iovcnt,struct uio ** uiop)885 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
886 {
887 struct iovec32 iov32;
888 struct iovec *iov;
889 struct uio *uio;
890 u_int iovlen;
891 int error, i;
892
893 *uiop = NULL;
894 if (iovcnt > UIO_MAXIOV)
895 return (EINVAL);
896 iovlen = iovcnt * sizeof(struct iovec);
897 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
898 iov = (struct iovec *)(uio + 1);
899 for (i = 0; i < iovcnt; i++) {
900 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
901 if (error) {
902 free(uio, M_IOV);
903 return (error);
904 }
905 iov[i].iov_base = PTRIN(iov32.iov_base);
906 iov[i].iov_len = iov32.iov_len;
907 }
908 uio->uio_iov = iov;
909 uio->uio_iovcnt = iovcnt;
910 uio->uio_segflg = UIO_USERSPACE;
911 uio->uio_offset = -1;
912 uio->uio_resid = 0;
913 for (i = 0; i < iovcnt; i++) {
914 if (iov->iov_len > INT_MAX - uio->uio_resid) {
915 free(uio, M_IOV);
916 return (EINVAL);
917 }
918 uio->uio_resid += iov->iov_len;
919 iov++;
920 }
921 *uiop = uio;
922 return (0);
923 }
924
925 int
freebsd32_readv(struct thread * td,struct freebsd32_readv_args * uap)926 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
927 {
928 struct uio *auio;
929 int error;
930
931 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
932 if (error)
933 return (error);
934 error = kern_readv(td, uap->fd, auio);
935 free(auio, M_IOV);
936 return (error);
937 }
938
939 int
freebsd32_writev(struct thread * td,struct freebsd32_writev_args * uap)940 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
941 {
942 struct uio *auio;
943 int error;
944
945 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
946 if (error)
947 return (error);
948 error = kern_writev(td, uap->fd, auio);
949 free(auio, M_IOV);
950 return (error);
951 }
952
953 int
freebsd32_preadv(struct thread * td,struct freebsd32_preadv_args * uap)954 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
955 {
956 struct uio *auio;
957 int error;
958
959 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
960 if (error)
961 return (error);
962 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
963 free(auio, M_IOV);
964 return (error);
965 }
966
967 int
freebsd32_pwritev(struct thread * td,struct freebsd32_pwritev_args * uap)968 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
969 {
970 struct uio *auio;
971 int error;
972
973 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
974 if (error)
975 return (error);
976 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
977 free(auio, M_IOV);
978 return (error);
979 }
980
981 int
freebsd32_copyiniov(struct iovec32 * iovp32,u_int iovcnt,struct iovec ** iovp,int error)982 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
983 int error)
984 {
985 struct iovec32 iov32;
986 struct iovec *iov;
987 u_int iovlen;
988 int i;
989
990 *iovp = NULL;
991 if (iovcnt > UIO_MAXIOV)
992 return (error);
993 iovlen = iovcnt * sizeof(struct iovec);
994 iov = malloc(iovlen, M_IOV, M_WAITOK);
995 for (i = 0; i < iovcnt; i++) {
996 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
997 if (error) {
998 free(iov, M_IOV);
999 return (error);
1000 }
1001 iov[i].iov_base = PTRIN(iov32.iov_base);
1002 iov[i].iov_len = iov32.iov_len;
1003 }
1004 *iovp = iov;
1005 return (0);
1006 }
1007
1008 static int
freebsd32_copyinmsghdr(struct msghdr32 * msg32,struct msghdr * msg)1009 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
1010 {
1011 struct msghdr32 m32;
1012 int error;
1013
1014 error = copyin(msg32, &m32, sizeof(m32));
1015 if (error)
1016 return (error);
1017 msg->msg_name = PTRIN(m32.msg_name);
1018 msg->msg_namelen = m32.msg_namelen;
1019 msg->msg_iov = PTRIN(m32.msg_iov);
1020 msg->msg_iovlen = m32.msg_iovlen;
1021 msg->msg_control = PTRIN(m32.msg_control);
1022 msg->msg_controllen = m32.msg_controllen;
1023 msg->msg_flags = m32.msg_flags;
1024 return (0);
1025 }
1026
1027 static int
freebsd32_copyoutmsghdr(struct msghdr * msg,struct msghdr32 * msg32)1028 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1029 {
1030 struct msghdr32 m32;
1031 int error;
1032
1033 m32.msg_name = PTROUT(msg->msg_name);
1034 m32.msg_namelen = msg->msg_namelen;
1035 m32.msg_iov = PTROUT(msg->msg_iov);
1036 m32.msg_iovlen = msg->msg_iovlen;
1037 m32.msg_control = PTROUT(msg->msg_control);
1038 m32.msg_controllen = msg->msg_controllen;
1039 m32.msg_flags = msg->msg_flags;
1040 error = copyout(&m32, msg32, sizeof(m32));
1041 return (error);
1042 }
1043
1044 #ifndef __mips__
1045 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
1046 #else
1047 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
1048 #endif
1049 #define FREEBSD32_ALIGN(p) \
1050 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1051 #define FREEBSD32_CMSG_SPACE(l) \
1052 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1053
1054 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1055 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1056
1057 static size_t
freebsd32_cmsg_convert(const struct cmsghdr * cm,void * data,socklen_t datalen)1058 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
1059 {
1060 size_t copylen;
1061 union {
1062 struct timespec32 ts;
1063 struct timeval32 tv;
1064 struct bintime32 bt;
1065 } tmp32;
1066
1067 union {
1068 struct timespec ts;
1069 struct timeval tv;
1070 struct bintime bt;
1071 } *in;
1072
1073 in = data;
1074 copylen = 0;
1075 switch (cm->cmsg_level) {
1076 case SOL_SOCKET:
1077 switch (cm->cmsg_type) {
1078 case SCM_TIMESTAMP:
1079 TV_CP(*in, tmp32, tv);
1080 copylen = sizeof(tmp32.tv);
1081 break;
1082
1083 case SCM_BINTIME:
1084 BT_CP(*in, tmp32, bt);
1085 copylen = sizeof(tmp32.bt);
1086 break;
1087
1088 case SCM_REALTIME:
1089 case SCM_MONOTONIC:
1090 TS_CP(*in, tmp32, ts);
1091 copylen = sizeof(tmp32.ts);
1092 break;
1093
1094 default:
1095 break;
1096 }
1097
1098 default:
1099 break;
1100 }
1101
1102 if (copylen == 0)
1103 return (datalen);
1104
1105 KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1106
1107 bcopy(&tmp32, data, copylen);
1108 return (copylen);
1109 }
1110
1111 static int
freebsd32_copy_msg_out(struct msghdr * msg,struct mbuf * control)1112 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1113 {
1114 struct cmsghdr *cm;
1115 void *data;
1116 socklen_t clen, datalen, datalen_out, oldclen;
1117 int error;
1118 caddr_t ctlbuf;
1119 int len, maxlen, copylen;
1120 struct mbuf *m;
1121 error = 0;
1122
1123 len = msg->msg_controllen;
1124 maxlen = msg->msg_controllen;
1125 msg->msg_controllen = 0;
1126
1127 ctlbuf = msg->msg_control;
1128 for (m = control; m != NULL && len > 0; m = m->m_next) {
1129 cm = mtod(m, struct cmsghdr *);
1130 clen = m->m_len;
1131 while (cm != NULL) {
1132 if (sizeof(struct cmsghdr) > clen ||
1133 cm->cmsg_len > clen) {
1134 error = EINVAL;
1135 break;
1136 }
1137
1138 data = CMSG_DATA(cm);
1139 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1140 datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1141
1142 /*
1143 * Copy out the message header. Preserve the native
1144 * message size in case we need to inspect the message
1145 * contents later.
1146 */
1147 copylen = sizeof(struct cmsghdr);
1148 if (len < copylen) {
1149 msg->msg_flags |= MSG_CTRUNC;
1150 m_dispose_extcontrolm(m);
1151 goto exit;
1152 }
1153 oldclen = cm->cmsg_len;
1154 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1155 datalen_out;
1156 error = copyout(cm, ctlbuf, copylen);
1157 cm->cmsg_len = oldclen;
1158 if (error != 0)
1159 goto exit;
1160
1161 ctlbuf += FREEBSD32_ALIGN(copylen);
1162 len -= FREEBSD32_ALIGN(copylen);
1163
1164 copylen = datalen_out;
1165 if (len < copylen) {
1166 msg->msg_flags |= MSG_CTRUNC;
1167 m_dispose_extcontrolm(m);
1168 break;
1169 }
1170
1171 /* Copy out the message data. */
1172 error = copyout(data, ctlbuf, copylen);
1173 if (error)
1174 goto exit;
1175
1176 ctlbuf += FREEBSD32_ALIGN(copylen);
1177 len -= FREEBSD32_ALIGN(copylen);
1178
1179 if (CMSG_SPACE(datalen) < clen) {
1180 clen -= CMSG_SPACE(datalen);
1181 cm = (struct cmsghdr *)
1182 ((caddr_t)cm + CMSG_SPACE(datalen));
1183 } else {
1184 clen = 0;
1185 cm = NULL;
1186 }
1187
1188 msg->msg_controllen +=
1189 FREEBSD32_CMSG_SPACE(datalen_out);
1190 }
1191 }
1192 if (len == 0 && m != NULL) {
1193 msg->msg_flags |= MSG_CTRUNC;
1194 m_dispose_extcontrolm(m);
1195 }
1196
1197 exit:
1198 return (error);
1199 }
1200
1201 int
freebsd32_recvmsg(td,uap)1202 freebsd32_recvmsg(td, uap)
1203 struct thread *td;
1204 struct freebsd32_recvmsg_args /* {
1205 int s;
1206 struct msghdr32 *msg;
1207 int flags;
1208 } */ *uap;
1209 {
1210 struct msghdr msg;
1211 struct msghdr32 m32;
1212 struct iovec *uiov, *iov;
1213 struct mbuf *control = NULL;
1214 struct mbuf **controlp;
1215
1216 int error;
1217 error = copyin(uap->msg, &m32, sizeof(m32));
1218 if (error)
1219 return (error);
1220 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1221 if (error)
1222 return (error);
1223 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1224 EMSGSIZE);
1225 if (error)
1226 return (error);
1227 msg.msg_flags = uap->flags;
1228 uiov = msg.msg_iov;
1229 msg.msg_iov = iov;
1230
1231 controlp = (msg.msg_control != NULL) ? &control : NULL;
1232 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1233 if (error == 0) {
1234 msg.msg_iov = uiov;
1235
1236 if (control != NULL)
1237 error = freebsd32_copy_msg_out(&msg, control);
1238 else
1239 msg.msg_controllen = 0;
1240
1241 if (error == 0)
1242 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1243 }
1244 free(iov, M_IOV);
1245
1246 if (control != NULL) {
1247 if (error != 0)
1248 m_dispose_extcontrolm(control);
1249 m_freem(control);
1250 }
1251
1252 return (error);
1253 }
1254
1255 /*
1256 * Copy-in the array of control messages constructed using alignment
1257 * and padding suitable for a 32-bit environment and construct an
1258 * mbuf using alignment and padding suitable for a 64-bit kernel.
1259 * The alignment and padding are defined indirectly by CMSG_DATA(),
1260 * CMSG_SPACE() and CMSG_LEN().
1261 */
1262 static int
freebsd32_copyin_control(struct mbuf ** mp,caddr_t buf,u_int buflen)1263 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1264 {
1265 struct cmsghdr *cm;
1266 struct mbuf *m;
1267 void *in, *in1, *md;
1268 u_int msglen, outlen;
1269 int error;
1270
1271 if (buflen > MCLBYTES)
1272 return (EINVAL);
1273
1274 in = malloc(buflen, M_TEMP, M_WAITOK);
1275 error = copyin(buf, in, buflen);
1276 if (error != 0)
1277 goto out;
1278
1279 /*
1280 * Make a pass over the input buffer to determine the amount of space
1281 * required for 64 bit-aligned copies of the control messages.
1282 */
1283 in1 = in;
1284 outlen = 0;
1285 while (buflen > 0) {
1286 if (buflen < sizeof(*cm)) {
1287 error = EINVAL;
1288 break;
1289 }
1290 cm = (struct cmsghdr *)in1;
1291 if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) {
1292 error = EINVAL;
1293 break;
1294 }
1295 msglen = FREEBSD32_ALIGN(cm->cmsg_len);
1296 if (msglen > buflen || msglen < cm->cmsg_len) {
1297 error = EINVAL;
1298 break;
1299 }
1300 buflen -= msglen;
1301
1302 in1 = (char *)in1 + msglen;
1303 outlen += CMSG_ALIGN(sizeof(*cm)) +
1304 CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
1305 }
1306 if (error == 0 && outlen > MCLBYTES) {
1307 /*
1308 * XXXMJ This implies that the upper limit on 32-bit aligned
1309 * control messages is less than MCLBYTES, and so we are not
1310 * perfectly compatible. However, there is no platform
1311 * guarantee that mbuf clusters larger than MCLBYTES can be
1312 * allocated.
1313 */
1314 error = EINVAL;
1315 }
1316 if (error != 0)
1317 goto out;
1318
1319 m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
1320 m->m_len = outlen;
1321 md = mtod(m, void *);
1322
1323 /*
1324 * Make a second pass over input messages, copying them into the output
1325 * buffer.
1326 */
1327 in1 = in;
1328 while (outlen > 0) {
1329 /* Copy the message header and align the length field. */
1330 cm = md;
1331 memcpy(cm, in1, sizeof(*cm));
1332 msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
1333 cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
1334
1335 /* Copy the message body. */
1336 in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
1337 md = (char *)md + CMSG_ALIGN(sizeof(*cm));
1338 memcpy(md, in1, msglen);
1339 in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
1340 md = (char *)md + CMSG_ALIGN(msglen);
1341 KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
1342 ("outlen %u underflow, msglen %u", outlen, msglen));
1343 outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
1344 }
1345
1346 *mp = m;
1347 out:
1348 free(in, M_TEMP);
1349 return (error);
1350 }
1351
1352 int
freebsd32_sendmsg(struct thread * td,struct freebsd32_sendmsg_args * uap)1353 freebsd32_sendmsg(struct thread *td,
1354 struct freebsd32_sendmsg_args *uap)
1355 {
1356 struct msghdr msg;
1357 struct msghdr32 m32;
1358 struct iovec *iov;
1359 struct mbuf *control = NULL;
1360 struct sockaddr *to = NULL;
1361 int error;
1362
1363 error = copyin(uap->msg, &m32, sizeof(m32));
1364 if (error)
1365 return (error);
1366 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1367 if (error)
1368 return (error);
1369 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1370 EMSGSIZE);
1371 if (error)
1372 return (error);
1373 msg.msg_iov = iov;
1374 if (msg.msg_name != NULL) {
1375 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1376 if (error) {
1377 to = NULL;
1378 goto out;
1379 }
1380 msg.msg_name = to;
1381 }
1382
1383 if (msg.msg_control) {
1384 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1385 error = EINVAL;
1386 goto out;
1387 }
1388
1389 error = freebsd32_copyin_control(&control, msg.msg_control,
1390 msg.msg_controllen);
1391 if (error)
1392 goto out;
1393
1394 msg.msg_control = NULL;
1395 msg.msg_controllen = 0;
1396 }
1397
1398 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1399 UIO_USERSPACE);
1400
1401 out:
1402 free(iov, M_IOV);
1403 if (to)
1404 free(to, M_SONAME);
1405 return (error);
1406 }
1407
1408 int
freebsd32_recvfrom(struct thread * td,struct freebsd32_recvfrom_args * uap)1409 freebsd32_recvfrom(struct thread *td,
1410 struct freebsd32_recvfrom_args *uap)
1411 {
1412 struct msghdr msg;
1413 struct iovec aiov;
1414 int error;
1415
1416 if (uap->fromlenaddr) {
1417 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1418 sizeof(msg.msg_namelen));
1419 if (error)
1420 return (error);
1421 } else {
1422 msg.msg_namelen = 0;
1423 }
1424
1425 msg.msg_name = PTRIN(uap->from);
1426 msg.msg_iov = &aiov;
1427 msg.msg_iovlen = 1;
1428 aiov.iov_base = PTRIN(uap->buf);
1429 aiov.iov_len = uap->len;
1430 msg.msg_control = NULL;
1431 msg.msg_flags = uap->flags;
1432 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1433 if (error == 0 && uap->fromlenaddr)
1434 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1435 sizeof (msg.msg_namelen));
1436 return (error);
1437 }
1438
1439 int
freebsd32_settimeofday(struct thread * td,struct freebsd32_settimeofday_args * uap)1440 freebsd32_settimeofday(struct thread *td,
1441 struct freebsd32_settimeofday_args *uap)
1442 {
1443 struct timeval32 tv32;
1444 struct timeval tv, *tvp;
1445 struct timezone tz, *tzp;
1446 int error;
1447
1448 if (uap->tv) {
1449 error = copyin(uap->tv, &tv32, sizeof(tv32));
1450 if (error)
1451 return (error);
1452 CP(tv32, tv, tv_sec);
1453 CP(tv32, tv, tv_usec);
1454 tvp = &tv;
1455 } else
1456 tvp = NULL;
1457 if (uap->tzp) {
1458 error = copyin(uap->tzp, &tz, sizeof(tz));
1459 if (error)
1460 return (error);
1461 tzp = &tz;
1462 } else
1463 tzp = NULL;
1464 return (kern_settimeofday(td, tvp, tzp));
1465 }
1466
1467 int
freebsd32_utimes(struct thread * td,struct freebsd32_utimes_args * uap)1468 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1469 {
1470 struct timeval32 s32[2];
1471 struct timeval s[2], *sp;
1472 int error;
1473
1474 if (uap->tptr != NULL) {
1475 error = copyin(uap->tptr, s32, sizeof(s32));
1476 if (error)
1477 return (error);
1478 CP(s32[0], s[0], tv_sec);
1479 CP(s32[0], s[0], tv_usec);
1480 CP(s32[1], s[1], tv_sec);
1481 CP(s32[1], s[1], tv_usec);
1482 sp = s;
1483 } else
1484 sp = NULL;
1485 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1486 sp, UIO_SYSSPACE));
1487 }
1488
1489 int
freebsd32_lutimes(struct thread * td,struct freebsd32_lutimes_args * uap)1490 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1491 {
1492 struct timeval32 s32[2];
1493 struct timeval s[2], *sp;
1494 int error;
1495
1496 if (uap->tptr != NULL) {
1497 error = copyin(uap->tptr, s32, sizeof(s32));
1498 if (error)
1499 return (error);
1500 CP(s32[0], s[0], tv_sec);
1501 CP(s32[0], s[0], tv_usec);
1502 CP(s32[1], s[1], tv_sec);
1503 CP(s32[1], s[1], tv_usec);
1504 sp = s;
1505 } else
1506 sp = NULL;
1507 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1508 }
1509
1510 int
freebsd32_futimes(struct thread * td,struct freebsd32_futimes_args * uap)1511 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1512 {
1513 struct timeval32 s32[2];
1514 struct timeval s[2], *sp;
1515 int error;
1516
1517 if (uap->tptr != NULL) {
1518 error = copyin(uap->tptr, s32, sizeof(s32));
1519 if (error)
1520 return (error);
1521 CP(s32[0], s[0], tv_sec);
1522 CP(s32[0], s[0], tv_usec);
1523 CP(s32[1], s[1], tv_sec);
1524 CP(s32[1], s[1], tv_usec);
1525 sp = s;
1526 } else
1527 sp = NULL;
1528 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1529 }
1530
1531 int
freebsd32_futimesat(struct thread * td,struct freebsd32_futimesat_args * uap)1532 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1533 {
1534 struct timeval32 s32[2];
1535 struct timeval s[2], *sp;
1536 int error;
1537
1538 if (uap->times != NULL) {
1539 error = copyin(uap->times, s32, sizeof(s32));
1540 if (error)
1541 return (error);
1542 CP(s32[0], s[0], tv_sec);
1543 CP(s32[0], s[0], tv_usec);
1544 CP(s32[1], s[1], tv_sec);
1545 CP(s32[1], s[1], tv_usec);
1546 sp = s;
1547 } else
1548 sp = NULL;
1549 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1550 sp, UIO_SYSSPACE));
1551 }
1552
1553 int
freebsd32_futimens(struct thread * td,struct freebsd32_futimens_args * uap)1554 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1555 {
1556 struct timespec32 ts32[2];
1557 struct timespec ts[2], *tsp;
1558 int error;
1559
1560 if (uap->times != NULL) {
1561 error = copyin(uap->times, ts32, sizeof(ts32));
1562 if (error)
1563 return (error);
1564 CP(ts32[0], ts[0], tv_sec);
1565 CP(ts32[0], ts[0], tv_nsec);
1566 CP(ts32[1], ts[1], tv_sec);
1567 CP(ts32[1], ts[1], tv_nsec);
1568 tsp = ts;
1569 } else
1570 tsp = NULL;
1571 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1572 }
1573
1574 int
freebsd32_utimensat(struct thread * td,struct freebsd32_utimensat_args * uap)1575 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1576 {
1577 struct timespec32 ts32[2];
1578 struct timespec ts[2], *tsp;
1579 int error;
1580
1581 if (uap->times != NULL) {
1582 error = copyin(uap->times, ts32, sizeof(ts32));
1583 if (error)
1584 return (error);
1585 CP(ts32[0], ts[0], tv_sec);
1586 CP(ts32[0], ts[0], tv_nsec);
1587 CP(ts32[1], ts[1], tv_sec);
1588 CP(ts32[1], ts[1], tv_nsec);
1589 tsp = ts;
1590 } else
1591 tsp = NULL;
1592 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1593 tsp, UIO_SYSSPACE, uap->flag));
1594 }
1595
1596 int
freebsd32_adjtime(struct thread * td,struct freebsd32_adjtime_args * uap)1597 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1598 {
1599 struct timeval32 tv32;
1600 struct timeval delta, olddelta, *deltap;
1601 int error;
1602
1603 if (uap->delta) {
1604 error = copyin(uap->delta, &tv32, sizeof(tv32));
1605 if (error)
1606 return (error);
1607 CP(tv32, delta, tv_sec);
1608 CP(tv32, delta, tv_usec);
1609 deltap = δ
1610 } else
1611 deltap = NULL;
1612 error = kern_adjtime(td, deltap, &olddelta);
1613 if (uap->olddelta && error == 0) {
1614 CP(olddelta, tv32, tv_sec);
1615 CP(olddelta, tv32, tv_usec);
1616 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1617 }
1618 return (error);
1619 }
1620
1621 #ifdef COMPAT_FREEBSD4
1622 int
freebsd4_freebsd32_statfs(struct thread * td,struct freebsd4_freebsd32_statfs_args * uap)1623 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1624 {
1625 struct statfs32 s32;
1626 struct statfs *sp;
1627 int error;
1628
1629 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1630 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1631 if (error == 0) {
1632 copy_statfs(sp, &s32);
1633 error = copyout(&s32, uap->buf, sizeof(s32));
1634 }
1635 free(sp, M_STATFS);
1636 return (error);
1637 }
1638 #endif
1639
1640 #ifdef COMPAT_FREEBSD4
1641 int
freebsd4_freebsd32_fstatfs(struct thread * td,struct freebsd4_freebsd32_fstatfs_args * uap)1642 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1643 {
1644 struct statfs32 s32;
1645 struct statfs *sp;
1646 int error;
1647
1648 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1649 error = kern_fstatfs(td, uap->fd, sp);
1650 if (error == 0) {
1651 copy_statfs(sp, &s32);
1652 error = copyout(&s32, uap->buf, sizeof(s32));
1653 }
1654 free(sp, M_STATFS);
1655 return (error);
1656 }
1657 #endif
1658
1659 #ifdef COMPAT_FREEBSD4
1660 int
freebsd4_freebsd32_fhstatfs(struct thread * td,struct freebsd4_freebsd32_fhstatfs_args * uap)1661 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1662 {
1663 struct statfs32 s32;
1664 struct statfs *sp;
1665 fhandle_t fh;
1666 int error;
1667
1668 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1669 return (error);
1670 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1671 error = kern_fhstatfs(td, fh, sp);
1672 if (error == 0) {
1673 copy_statfs(sp, &s32);
1674 error = copyout(&s32, uap->buf, sizeof(s32));
1675 }
1676 free(sp, M_STATFS);
1677 return (error);
1678 }
1679 #endif
1680
1681 int
freebsd32_pread(struct thread * td,struct freebsd32_pread_args * uap)1682 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1683 {
1684
1685 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1686 PAIR32TO64(off_t, uap->offset)));
1687 }
1688
1689 int
freebsd32_pwrite(struct thread * td,struct freebsd32_pwrite_args * uap)1690 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1691 {
1692
1693 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1694 PAIR32TO64(off_t, uap->offset)));
1695 }
1696
1697 #ifdef COMPAT_43
1698 int
ofreebsd32_lseek(struct thread * td,struct ofreebsd32_lseek_args * uap)1699 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1700 {
1701
1702 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1703 }
1704 #endif
1705
1706 int
freebsd32_lseek(struct thread * td,struct freebsd32_lseek_args * uap)1707 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1708 {
1709 int error;
1710 off_t pos;
1711
1712 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1713 uap->whence);
1714 /* Expand the quad return into two parts for eax and edx */
1715 pos = td->td_uretoff.tdu_off;
1716 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1717 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1718 return error;
1719 }
1720
1721 int
freebsd32_truncate(struct thread * td,struct freebsd32_truncate_args * uap)1722 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1723 {
1724
1725 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1726 PAIR32TO64(off_t, uap->length)));
1727 }
1728
1729 int
freebsd32_ftruncate(struct thread * td,struct freebsd32_ftruncate_args * uap)1730 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1731 {
1732
1733 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1734 }
1735
1736 #ifdef COMPAT_43
1737 int
ofreebsd32_getdirentries(struct thread * td,struct ofreebsd32_getdirentries_args * uap)1738 ofreebsd32_getdirentries(struct thread *td,
1739 struct ofreebsd32_getdirentries_args *uap)
1740 {
1741 struct ogetdirentries_args ap;
1742 int error;
1743 long loff;
1744 int32_t loff_cut;
1745
1746 ap.fd = uap->fd;
1747 ap.buf = uap->buf;
1748 ap.count = uap->count;
1749 ap.basep = NULL;
1750 error = kern_ogetdirentries(td, &ap, &loff);
1751 if (error == 0) {
1752 loff_cut = loff;
1753 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1754 }
1755 return (error);
1756 }
1757 #endif
1758
1759 #if defined(COMPAT_FREEBSD11)
1760 int
freebsd11_freebsd32_getdirentries(struct thread * td,struct freebsd11_freebsd32_getdirentries_args * uap)1761 freebsd11_freebsd32_getdirentries(struct thread *td,
1762 struct freebsd11_freebsd32_getdirentries_args *uap)
1763 {
1764 long base;
1765 int32_t base32;
1766 int error;
1767
1768 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1769 &base, NULL);
1770 if (error)
1771 return (error);
1772 if (uap->basep != NULL) {
1773 base32 = base;
1774 error = copyout(&base32, uap->basep, sizeof(int32_t));
1775 }
1776 return (error);
1777 }
1778
1779 int
freebsd11_freebsd32_getdents(struct thread * td,struct freebsd11_freebsd32_getdents_args * uap)1780 freebsd11_freebsd32_getdents(struct thread *td,
1781 struct freebsd11_freebsd32_getdents_args *uap)
1782 {
1783 struct freebsd11_freebsd32_getdirentries_args ap;
1784
1785 ap.fd = uap->fd;
1786 ap.buf = uap->buf;
1787 ap.count = uap->count;
1788 ap.basep = NULL;
1789 return (freebsd11_freebsd32_getdirentries(td, &ap));
1790 }
1791 #endif /* COMPAT_FREEBSD11 */
1792
1793 #ifdef COMPAT_FREEBSD6
1794 /* versions with the 'int pad' argument */
1795 int
freebsd6_freebsd32_pread(struct thread * td,struct freebsd6_freebsd32_pread_args * uap)1796 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1797 {
1798
1799 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1800 PAIR32TO64(off_t, uap->offset)));
1801 }
1802
1803 int
freebsd6_freebsd32_pwrite(struct thread * td,struct freebsd6_freebsd32_pwrite_args * uap)1804 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1805 {
1806
1807 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1808 PAIR32TO64(off_t, uap->offset)));
1809 }
1810
1811 int
freebsd6_freebsd32_lseek(struct thread * td,struct freebsd6_freebsd32_lseek_args * uap)1812 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1813 {
1814 int error;
1815 off_t pos;
1816
1817 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1818 uap->whence);
1819 /* Expand the quad return into two parts for eax and edx */
1820 pos = *(off_t *)(td->td_retval);
1821 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1822 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1823 return error;
1824 }
1825
1826 int
freebsd6_freebsd32_truncate(struct thread * td,struct freebsd6_freebsd32_truncate_args * uap)1827 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1828 {
1829
1830 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1831 PAIR32TO64(off_t, uap->length)));
1832 }
1833
1834 int
freebsd6_freebsd32_ftruncate(struct thread * td,struct freebsd6_freebsd32_ftruncate_args * uap)1835 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1836 {
1837
1838 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1839 }
1840 #endif /* COMPAT_FREEBSD6 */
1841
1842 struct sf_hdtr32 {
1843 uint32_t headers;
1844 int hdr_cnt;
1845 uint32_t trailers;
1846 int trl_cnt;
1847 };
1848
1849 static int
freebsd32_do_sendfile(struct thread * td,struct freebsd32_sendfile_args * uap,int compat)1850 freebsd32_do_sendfile(struct thread *td,
1851 struct freebsd32_sendfile_args *uap, int compat)
1852 {
1853 struct sf_hdtr32 hdtr32;
1854 struct sf_hdtr hdtr;
1855 struct uio *hdr_uio, *trl_uio;
1856 struct file *fp;
1857 cap_rights_t rights;
1858 struct iovec32 *iov32;
1859 off_t offset, sbytes;
1860 int error;
1861
1862 offset = PAIR32TO64(off_t, uap->offset);
1863 if (offset < 0)
1864 return (EINVAL);
1865
1866 hdr_uio = trl_uio = NULL;
1867
1868 if (uap->hdtr != NULL) {
1869 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1870 if (error)
1871 goto out;
1872 PTRIN_CP(hdtr32, hdtr, headers);
1873 CP(hdtr32, hdtr, hdr_cnt);
1874 PTRIN_CP(hdtr32, hdtr, trailers);
1875 CP(hdtr32, hdtr, trl_cnt);
1876
1877 if (hdtr.headers != NULL) {
1878 iov32 = PTRIN(hdtr32.headers);
1879 error = freebsd32_copyinuio(iov32,
1880 hdtr32.hdr_cnt, &hdr_uio);
1881 if (error)
1882 goto out;
1883 #ifdef COMPAT_FREEBSD4
1884 /*
1885 * In FreeBSD < 5.0 the nbytes to send also included
1886 * the header. If compat is specified subtract the
1887 * header size from nbytes.
1888 */
1889 if (compat) {
1890 if (uap->nbytes > hdr_uio->uio_resid)
1891 uap->nbytes -= hdr_uio->uio_resid;
1892 else
1893 uap->nbytes = 0;
1894 }
1895 #endif
1896 }
1897 if (hdtr.trailers != NULL) {
1898 iov32 = PTRIN(hdtr32.trailers);
1899 error = freebsd32_copyinuio(iov32,
1900 hdtr32.trl_cnt, &trl_uio);
1901 if (error)
1902 goto out;
1903 }
1904 }
1905
1906 AUDIT_ARG_FD(uap->fd);
1907
1908 if ((error = fget_read(td, uap->fd,
1909 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1910 goto out;
1911
1912 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1913 uap->nbytes, &sbytes, uap->flags, td);
1914 fdrop(fp, td);
1915
1916 if (uap->sbytes != NULL)
1917 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1918
1919 out:
1920 if (hdr_uio)
1921 free(hdr_uio, M_IOV);
1922 if (trl_uio)
1923 free(trl_uio, M_IOV);
1924 return (error);
1925 }
1926
1927 #ifdef COMPAT_FREEBSD4
1928 int
freebsd4_freebsd32_sendfile(struct thread * td,struct freebsd4_freebsd32_sendfile_args * uap)1929 freebsd4_freebsd32_sendfile(struct thread *td,
1930 struct freebsd4_freebsd32_sendfile_args *uap)
1931 {
1932 return (freebsd32_do_sendfile(td,
1933 (struct freebsd32_sendfile_args *)uap, 1));
1934 }
1935 #endif
1936
1937 int
freebsd32_sendfile(struct thread * td,struct freebsd32_sendfile_args * uap)1938 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1939 {
1940
1941 return (freebsd32_do_sendfile(td, uap, 0));
1942 }
1943
1944 static void
copy_stat(struct stat * in,struct stat32 * out)1945 copy_stat(struct stat *in, struct stat32 *out)
1946 {
1947
1948 CP(*in, *out, st_dev);
1949 CP(*in, *out, st_ino);
1950 CP(*in, *out, st_mode);
1951 CP(*in, *out, st_nlink);
1952 CP(*in, *out, st_uid);
1953 CP(*in, *out, st_gid);
1954 CP(*in, *out, st_rdev);
1955 TS_CP(*in, *out, st_atim);
1956 TS_CP(*in, *out, st_mtim);
1957 TS_CP(*in, *out, st_ctim);
1958 CP(*in, *out, st_size);
1959 CP(*in, *out, st_blocks);
1960 CP(*in, *out, st_blksize);
1961 CP(*in, *out, st_flags);
1962 CP(*in, *out, st_gen);
1963 TS_CP(*in, *out, st_birthtim);
1964 out->st_padding0 = 0;
1965 out->st_padding1 = 0;
1966 #ifdef __STAT32_TIME_T_EXT
1967 out->st_atim_ext = 0;
1968 out->st_mtim_ext = 0;
1969 out->st_ctim_ext = 0;
1970 out->st_btim_ext = 0;
1971 #endif
1972 bzero(out->st_spare, sizeof(out->st_spare));
1973 }
1974
1975 #ifdef COMPAT_43
1976 static void
copy_ostat(struct stat * in,struct ostat32 * out)1977 copy_ostat(struct stat *in, struct ostat32 *out)
1978 {
1979
1980 bzero(out, sizeof(*out));
1981 CP(*in, *out, st_dev);
1982 CP(*in, *out, st_ino);
1983 CP(*in, *out, st_mode);
1984 CP(*in, *out, st_nlink);
1985 CP(*in, *out, st_uid);
1986 CP(*in, *out, st_gid);
1987 CP(*in, *out, st_rdev);
1988 out->st_size = MIN(in->st_size, INT32_MAX);
1989 TS_CP(*in, *out, st_atim);
1990 TS_CP(*in, *out, st_mtim);
1991 TS_CP(*in, *out, st_ctim);
1992 CP(*in, *out, st_blksize);
1993 CP(*in, *out, st_blocks);
1994 CP(*in, *out, st_flags);
1995 CP(*in, *out, st_gen);
1996 }
1997 #endif
1998
1999 #ifdef COMPAT_43
2000 int
ofreebsd32_stat(struct thread * td,struct ofreebsd32_stat_args * uap)2001 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
2002 {
2003 struct stat sb;
2004 struct ostat32 sb32;
2005 int error;
2006
2007 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2008 &sb, NULL);
2009 if (error)
2010 return (error);
2011 copy_ostat(&sb, &sb32);
2012 error = copyout(&sb32, uap->ub, sizeof (sb32));
2013 return (error);
2014 }
2015 #endif
2016
2017 int
freebsd32_fstat(struct thread * td,struct freebsd32_fstat_args * uap)2018 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2019 {
2020 struct stat ub;
2021 struct stat32 ub32;
2022 int error;
2023
2024 error = kern_fstat(td, uap->fd, &ub);
2025 if (error)
2026 return (error);
2027 copy_stat(&ub, &ub32);
2028 error = copyout(&ub32, uap->ub, sizeof(ub32));
2029 return (error);
2030 }
2031
2032 #ifdef COMPAT_43
2033 int
ofreebsd32_fstat(struct thread * td,struct ofreebsd32_fstat_args * uap)2034 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2035 {
2036 struct stat ub;
2037 struct ostat32 ub32;
2038 int error;
2039
2040 error = kern_fstat(td, uap->fd, &ub);
2041 if (error)
2042 return (error);
2043 copy_ostat(&ub, &ub32);
2044 error = copyout(&ub32, uap->ub, sizeof(ub32));
2045 return (error);
2046 }
2047 #endif
2048
2049 int
freebsd32_fstatat(struct thread * td,struct freebsd32_fstatat_args * uap)2050 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2051 {
2052 struct stat ub;
2053 struct stat32 ub32;
2054 int error;
2055
2056 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2057 &ub, NULL);
2058 if (error)
2059 return (error);
2060 copy_stat(&ub, &ub32);
2061 error = copyout(&ub32, uap->buf, sizeof(ub32));
2062 return (error);
2063 }
2064
2065 #ifdef COMPAT_43
2066 int
ofreebsd32_lstat(struct thread * td,struct ofreebsd32_lstat_args * uap)2067 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2068 {
2069 struct stat sb;
2070 struct ostat32 sb32;
2071 int error;
2072
2073 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2074 UIO_USERSPACE, &sb, NULL);
2075 if (error)
2076 return (error);
2077 copy_ostat(&sb, &sb32);
2078 error = copyout(&sb32, uap->ub, sizeof (sb32));
2079 return (error);
2080 }
2081 #endif
2082
2083 int
freebsd32_fhstat(struct thread * td,struct freebsd32_fhstat_args * uap)2084 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2085 {
2086 struct stat sb;
2087 struct stat32 sb32;
2088 struct fhandle fh;
2089 int error;
2090
2091 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2092 if (error != 0)
2093 return (error);
2094 error = kern_fhstat(td, fh, &sb);
2095 if (error != 0)
2096 return (error);
2097 copy_stat(&sb, &sb32);
2098 error = copyout(&sb32, uap->sb, sizeof (sb32));
2099 return (error);
2100 }
2101
2102 #if defined(COMPAT_FREEBSD11)
2103 extern int ino64_trunc_error;
2104
2105 static int
freebsd11_cvtstat32(struct stat * in,struct freebsd11_stat32 * out)2106 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2107 {
2108
2109 CP(*in, *out, st_ino);
2110 if (in->st_ino != out->st_ino) {
2111 switch (ino64_trunc_error) {
2112 default:
2113 case 0:
2114 break;
2115 case 1:
2116 return (EOVERFLOW);
2117 case 2:
2118 out->st_ino = UINT32_MAX;
2119 break;
2120 }
2121 }
2122 CP(*in, *out, st_nlink);
2123 if (in->st_nlink != out->st_nlink) {
2124 switch (ino64_trunc_error) {
2125 default:
2126 case 0:
2127 break;
2128 case 1:
2129 return (EOVERFLOW);
2130 case 2:
2131 out->st_nlink = UINT16_MAX;
2132 break;
2133 }
2134 }
2135 out->st_dev = in->st_dev;
2136 if (out->st_dev != in->st_dev) {
2137 switch (ino64_trunc_error) {
2138 default:
2139 break;
2140 case 1:
2141 return (EOVERFLOW);
2142 }
2143 }
2144 CP(*in, *out, st_mode);
2145 CP(*in, *out, st_uid);
2146 CP(*in, *out, st_gid);
2147 out->st_rdev = in->st_rdev;
2148 if (out->st_rdev != in->st_rdev) {
2149 switch (ino64_trunc_error) {
2150 default:
2151 break;
2152 case 1:
2153 return (EOVERFLOW);
2154 }
2155 }
2156 TS_CP(*in, *out, st_atim);
2157 TS_CP(*in, *out, st_mtim);
2158 TS_CP(*in, *out, st_ctim);
2159 CP(*in, *out, st_size);
2160 CP(*in, *out, st_blocks);
2161 CP(*in, *out, st_blksize);
2162 CP(*in, *out, st_flags);
2163 CP(*in, *out, st_gen);
2164 TS_CP(*in, *out, st_birthtim);
2165 out->st_lspare = 0;
2166 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2167 sizeof(*out) - offsetof(struct freebsd11_stat32,
2168 st_birthtim) - sizeof(out->st_birthtim));
2169 return (0);
2170 }
2171
2172 int
freebsd11_freebsd32_stat(struct thread * td,struct freebsd11_freebsd32_stat_args * uap)2173 freebsd11_freebsd32_stat(struct thread *td,
2174 struct freebsd11_freebsd32_stat_args *uap)
2175 {
2176 struct stat sb;
2177 struct freebsd11_stat32 sb32;
2178 int error;
2179
2180 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2181 &sb, NULL);
2182 if (error != 0)
2183 return (error);
2184 error = freebsd11_cvtstat32(&sb, &sb32);
2185 if (error == 0)
2186 error = copyout(&sb32, uap->ub, sizeof (sb32));
2187 return (error);
2188 }
2189
2190 int
freebsd11_freebsd32_fstat(struct thread * td,struct freebsd11_freebsd32_fstat_args * uap)2191 freebsd11_freebsd32_fstat(struct thread *td,
2192 struct freebsd11_freebsd32_fstat_args *uap)
2193 {
2194 struct stat sb;
2195 struct freebsd11_stat32 sb32;
2196 int error;
2197
2198 error = kern_fstat(td, uap->fd, &sb);
2199 if (error != 0)
2200 return (error);
2201 error = freebsd11_cvtstat32(&sb, &sb32);
2202 if (error == 0)
2203 error = copyout(&sb32, uap->ub, sizeof (sb32));
2204 return (error);
2205 }
2206
2207 int
freebsd11_freebsd32_fstatat(struct thread * td,struct freebsd11_freebsd32_fstatat_args * uap)2208 freebsd11_freebsd32_fstatat(struct thread *td,
2209 struct freebsd11_freebsd32_fstatat_args *uap)
2210 {
2211 struct stat sb;
2212 struct freebsd11_stat32 sb32;
2213 int error;
2214
2215 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2216 &sb, NULL);
2217 if (error != 0)
2218 return (error);
2219 error = freebsd11_cvtstat32(&sb, &sb32);
2220 if (error == 0)
2221 error = copyout(&sb32, uap->buf, sizeof (sb32));
2222 return (error);
2223 }
2224
2225 int
freebsd11_freebsd32_lstat(struct thread * td,struct freebsd11_freebsd32_lstat_args * uap)2226 freebsd11_freebsd32_lstat(struct thread *td,
2227 struct freebsd11_freebsd32_lstat_args *uap)
2228 {
2229 struct stat sb;
2230 struct freebsd11_stat32 sb32;
2231 int error;
2232
2233 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2234 UIO_USERSPACE, &sb, NULL);
2235 if (error != 0)
2236 return (error);
2237 error = freebsd11_cvtstat32(&sb, &sb32);
2238 if (error == 0)
2239 error = copyout(&sb32, uap->ub, sizeof (sb32));
2240 return (error);
2241 }
2242
2243 int
freebsd11_freebsd32_fhstat(struct thread * td,struct freebsd11_freebsd32_fhstat_args * uap)2244 freebsd11_freebsd32_fhstat(struct thread *td,
2245 struct freebsd11_freebsd32_fhstat_args *uap)
2246 {
2247 struct stat sb;
2248 struct freebsd11_stat32 sb32;
2249 struct fhandle fh;
2250 int error;
2251
2252 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2253 if (error != 0)
2254 return (error);
2255 error = kern_fhstat(td, fh, &sb);
2256 if (error != 0)
2257 return (error);
2258 error = freebsd11_cvtstat32(&sb, &sb32);
2259 if (error == 0)
2260 error = copyout(&sb32, uap->sb, sizeof (sb32));
2261 return (error);
2262 }
2263 #endif
2264
2265 int
freebsd32___sysctl(struct thread * td,struct freebsd32___sysctl_args * uap)2266 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap)
2267 {
2268 int error, name[CTL_MAXNAME];
2269 size_t j, oldlen;
2270 uint32_t tmp;
2271
2272 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2273 return (EINVAL);
2274 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2275 if (error)
2276 return (error);
2277 if (uap->oldlenp) {
2278 error = fueword32(uap->oldlenp, &tmp);
2279 oldlen = tmp;
2280 } else {
2281 oldlen = 0;
2282 }
2283 if (error != 0)
2284 return (EFAULT);
2285 error = userland_sysctl(td, name, uap->namelen,
2286 uap->old, &oldlen, 1,
2287 uap->new, uap->newlen, &j, SCTL_MASK32);
2288 if (error)
2289 return (error);
2290 if (uap->oldlenp)
2291 suword32(uap->oldlenp, j);
2292 return (0);
2293 }
2294
2295 int
freebsd32_jail(struct thread * td,struct freebsd32_jail_args * uap)2296 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2297 {
2298 uint32_t version;
2299 int error;
2300 struct jail j;
2301
2302 error = copyin(uap->jail, &version, sizeof(uint32_t));
2303 if (error)
2304 return (error);
2305
2306 switch (version) {
2307 case 0:
2308 {
2309 /* FreeBSD single IPv4 jails. */
2310 struct jail32_v0 j32_v0;
2311
2312 bzero(&j, sizeof(struct jail));
2313 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2314 if (error)
2315 return (error);
2316 CP(j32_v0, j, version);
2317 PTRIN_CP(j32_v0, j, path);
2318 PTRIN_CP(j32_v0, j, hostname);
2319 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2320 break;
2321 }
2322
2323 case 1:
2324 /*
2325 * Version 1 was used by multi-IPv4 jail implementations
2326 * that never made it into the official kernel.
2327 */
2328 return (EINVAL);
2329
2330 case 2: /* JAIL_API_VERSION */
2331 {
2332 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2333 struct jail32 j32;
2334
2335 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2336 if (error)
2337 return (error);
2338 CP(j32, j, version);
2339 PTRIN_CP(j32, j, path);
2340 PTRIN_CP(j32, j, hostname);
2341 PTRIN_CP(j32, j, jailname);
2342 CP(j32, j, ip4s);
2343 CP(j32, j, ip6s);
2344 PTRIN_CP(j32, j, ip4);
2345 PTRIN_CP(j32, j, ip6);
2346 break;
2347 }
2348
2349 default:
2350 /* Sci-Fi jails are not supported, sorry. */
2351 return (EINVAL);
2352 }
2353 return (kern_jail(td, &j));
2354 }
2355
2356 int
freebsd32_jail_set(struct thread * td,struct freebsd32_jail_set_args * uap)2357 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2358 {
2359 struct uio *auio;
2360 int error;
2361
2362 /* Check that we have an even number of iovecs. */
2363 if (uap->iovcnt & 1)
2364 return (EINVAL);
2365
2366 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2367 if (error)
2368 return (error);
2369 error = kern_jail_set(td, auio, uap->flags);
2370 free(auio, M_IOV);
2371 return (error);
2372 }
2373
2374 int
freebsd32_jail_get(struct thread * td,struct freebsd32_jail_get_args * uap)2375 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2376 {
2377 struct iovec32 iov32;
2378 struct uio *auio;
2379 int error, i;
2380
2381 /* Check that we have an even number of iovecs. */
2382 if (uap->iovcnt & 1)
2383 return (EINVAL);
2384
2385 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2386 if (error)
2387 return (error);
2388 error = kern_jail_get(td, auio, uap->flags);
2389 if (error == 0)
2390 for (i = 0; i < uap->iovcnt; i++) {
2391 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2392 CP(auio->uio_iov[i], iov32, iov_len);
2393 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2394 if (error != 0)
2395 break;
2396 }
2397 free(auio, M_IOV);
2398 return (error);
2399 }
2400
2401 int
freebsd32_sigaction(struct thread * td,struct freebsd32_sigaction_args * uap)2402 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2403 {
2404 struct sigaction32 s32;
2405 struct sigaction sa, osa, *sap;
2406 int error;
2407
2408 if (uap->act) {
2409 error = copyin(uap->act, &s32, sizeof(s32));
2410 if (error)
2411 return (error);
2412 sa.sa_handler = PTRIN(s32.sa_u);
2413 CP(s32, sa, sa_flags);
2414 CP(s32, sa, sa_mask);
2415 sap = &sa;
2416 } else
2417 sap = NULL;
2418 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2419 if (error == 0 && uap->oact != NULL) {
2420 s32.sa_u = PTROUT(osa.sa_handler);
2421 CP(osa, s32, sa_flags);
2422 CP(osa, s32, sa_mask);
2423 error = copyout(&s32, uap->oact, sizeof(s32));
2424 }
2425 return (error);
2426 }
2427
2428 #ifdef COMPAT_FREEBSD4
2429 int
freebsd4_freebsd32_sigaction(struct thread * td,struct freebsd4_freebsd32_sigaction_args * uap)2430 freebsd4_freebsd32_sigaction(struct thread *td,
2431 struct freebsd4_freebsd32_sigaction_args *uap)
2432 {
2433 struct sigaction32 s32;
2434 struct sigaction sa, osa, *sap;
2435 int error;
2436
2437 if (uap->act) {
2438 error = copyin(uap->act, &s32, sizeof(s32));
2439 if (error)
2440 return (error);
2441 sa.sa_handler = PTRIN(s32.sa_u);
2442 CP(s32, sa, sa_flags);
2443 CP(s32, sa, sa_mask);
2444 sap = &sa;
2445 } else
2446 sap = NULL;
2447 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2448 if (error == 0 && uap->oact != NULL) {
2449 s32.sa_u = PTROUT(osa.sa_handler);
2450 CP(osa, s32, sa_flags);
2451 CP(osa, s32, sa_mask);
2452 error = copyout(&s32, uap->oact, sizeof(s32));
2453 }
2454 return (error);
2455 }
2456 #endif
2457
2458 #ifdef COMPAT_43
2459 struct osigaction32 {
2460 u_int32_t sa_u;
2461 osigset_t sa_mask;
2462 int sa_flags;
2463 };
2464
2465 #define ONSIG 32
2466
2467 int
ofreebsd32_sigaction(struct thread * td,struct ofreebsd32_sigaction_args * uap)2468 ofreebsd32_sigaction(struct thread *td,
2469 struct ofreebsd32_sigaction_args *uap)
2470 {
2471 struct osigaction32 s32;
2472 struct sigaction sa, osa, *sap;
2473 int error;
2474
2475 if (uap->signum <= 0 || uap->signum >= ONSIG)
2476 return (EINVAL);
2477
2478 if (uap->nsa) {
2479 error = copyin(uap->nsa, &s32, sizeof(s32));
2480 if (error)
2481 return (error);
2482 sa.sa_handler = PTRIN(s32.sa_u);
2483 CP(s32, sa, sa_flags);
2484 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2485 sap = &sa;
2486 } else
2487 sap = NULL;
2488 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2489 if (error == 0 && uap->osa != NULL) {
2490 s32.sa_u = PTROUT(osa.sa_handler);
2491 CP(osa, s32, sa_flags);
2492 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2493 error = copyout(&s32, uap->osa, sizeof(s32));
2494 }
2495 return (error);
2496 }
2497
2498 int
ofreebsd32_sigprocmask(struct thread * td,struct ofreebsd32_sigprocmask_args * uap)2499 ofreebsd32_sigprocmask(struct thread *td,
2500 struct ofreebsd32_sigprocmask_args *uap)
2501 {
2502 sigset_t set, oset;
2503 int error;
2504
2505 OSIG2SIG(uap->mask, set);
2506 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2507 SIG2OSIG(oset, td->td_retval[0]);
2508 return (error);
2509 }
2510
2511 int
ofreebsd32_sigpending(struct thread * td,struct ofreebsd32_sigpending_args * uap)2512 ofreebsd32_sigpending(struct thread *td,
2513 struct ofreebsd32_sigpending_args *uap)
2514 {
2515 struct proc *p = td->td_proc;
2516 sigset_t siglist;
2517
2518 PROC_LOCK(p);
2519 siglist = p->p_siglist;
2520 SIGSETOR(siglist, td->td_siglist);
2521 PROC_UNLOCK(p);
2522 SIG2OSIG(siglist, td->td_retval[0]);
2523 return (0);
2524 }
2525
2526 struct sigvec32 {
2527 u_int32_t sv_handler;
2528 int sv_mask;
2529 int sv_flags;
2530 };
2531
2532 int
ofreebsd32_sigvec(struct thread * td,struct ofreebsd32_sigvec_args * uap)2533 ofreebsd32_sigvec(struct thread *td,
2534 struct ofreebsd32_sigvec_args *uap)
2535 {
2536 struct sigvec32 vec;
2537 struct sigaction sa, osa, *sap;
2538 int error;
2539
2540 if (uap->signum <= 0 || uap->signum >= ONSIG)
2541 return (EINVAL);
2542
2543 if (uap->nsv) {
2544 error = copyin(uap->nsv, &vec, sizeof(vec));
2545 if (error)
2546 return (error);
2547 sa.sa_handler = PTRIN(vec.sv_handler);
2548 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2549 sa.sa_flags = vec.sv_flags;
2550 sa.sa_flags ^= SA_RESTART;
2551 sap = &sa;
2552 } else
2553 sap = NULL;
2554 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2555 if (error == 0 && uap->osv != NULL) {
2556 vec.sv_handler = PTROUT(osa.sa_handler);
2557 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2558 vec.sv_flags = osa.sa_flags;
2559 vec.sv_flags &= ~SA_NOCLDWAIT;
2560 vec.sv_flags ^= SA_RESTART;
2561 error = copyout(&vec, uap->osv, sizeof(vec));
2562 }
2563 return (error);
2564 }
2565
2566 int
ofreebsd32_sigblock(struct thread * td,struct ofreebsd32_sigblock_args * uap)2567 ofreebsd32_sigblock(struct thread *td,
2568 struct ofreebsd32_sigblock_args *uap)
2569 {
2570 sigset_t set, oset;
2571
2572 OSIG2SIG(uap->mask, set);
2573 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2574 SIG2OSIG(oset, td->td_retval[0]);
2575 return (0);
2576 }
2577
2578 int
ofreebsd32_sigsetmask(struct thread * td,struct ofreebsd32_sigsetmask_args * uap)2579 ofreebsd32_sigsetmask(struct thread *td,
2580 struct ofreebsd32_sigsetmask_args *uap)
2581 {
2582 sigset_t set, oset;
2583
2584 OSIG2SIG(uap->mask, set);
2585 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2586 SIG2OSIG(oset, td->td_retval[0]);
2587 return (0);
2588 }
2589
2590 int
ofreebsd32_sigsuspend(struct thread * td,struct ofreebsd32_sigsuspend_args * uap)2591 ofreebsd32_sigsuspend(struct thread *td,
2592 struct ofreebsd32_sigsuspend_args *uap)
2593 {
2594 sigset_t mask;
2595
2596 OSIG2SIG(uap->mask, mask);
2597 return (kern_sigsuspend(td, mask));
2598 }
2599
2600 struct sigstack32 {
2601 u_int32_t ss_sp;
2602 int ss_onstack;
2603 };
2604
2605 int
ofreebsd32_sigstack(struct thread * td,struct ofreebsd32_sigstack_args * uap)2606 ofreebsd32_sigstack(struct thread *td,
2607 struct ofreebsd32_sigstack_args *uap)
2608 {
2609 struct sigstack32 s32;
2610 struct sigstack nss, oss;
2611 int error = 0, unss;
2612
2613 if (uap->nss != NULL) {
2614 error = copyin(uap->nss, &s32, sizeof(s32));
2615 if (error)
2616 return (error);
2617 nss.ss_sp = PTRIN(s32.ss_sp);
2618 CP(s32, nss, ss_onstack);
2619 unss = 1;
2620 } else {
2621 unss = 0;
2622 }
2623 oss.ss_sp = td->td_sigstk.ss_sp;
2624 oss.ss_onstack = sigonstack(cpu_getstack(td));
2625 if (unss) {
2626 td->td_sigstk.ss_sp = nss.ss_sp;
2627 td->td_sigstk.ss_size = 0;
2628 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2629 td->td_pflags |= TDP_ALTSTACK;
2630 }
2631 if (uap->oss != NULL) {
2632 s32.ss_sp = PTROUT(oss.ss_sp);
2633 CP(oss, s32, ss_onstack);
2634 error = copyout(&s32, uap->oss, sizeof(s32));
2635 }
2636 return (error);
2637 }
2638 #endif
2639
2640 int
freebsd32_nanosleep(struct thread * td,struct freebsd32_nanosleep_args * uap)2641 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2642 {
2643
2644 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2645 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2646 }
2647
2648 int
freebsd32_clock_nanosleep(struct thread * td,struct freebsd32_clock_nanosleep_args * uap)2649 freebsd32_clock_nanosleep(struct thread *td,
2650 struct freebsd32_clock_nanosleep_args *uap)
2651 {
2652 int error;
2653
2654 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2655 uap->rqtp, uap->rmtp);
2656 return (kern_posix_error(td, error));
2657 }
2658
2659 static int
freebsd32_user_clock_nanosleep(struct thread * td,clockid_t clock_id,int flags,const struct timespec32 * ua_rqtp,struct timespec32 * ua_rmtp)2660 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2661 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2662 {
2663 struct timespec32 rmt32, rqt32;
2664 struct timespec rmt, rqt;
2665 int error;
2666
2667 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2668 if (error)
2669 return (error);
2670
2671 CP(rqt32, rqt, tv_sec);
2672 CP(rqt32, rqt, tv_nsec);
2673
2674 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2675 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2676 return (EFAULT);
2677 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2678 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2679 int error2;
2680
2681 CP(rmt, rmt32, tv_sec);
2682 CP(rmt, rmt32, tv_nsec);
2683
2684 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2685 if (error2)
2686 error = error2;
2687 }
2688 return (error);
2689 }
2690
2691 int
freebsd32_clock_gettime(struct thread * td,struct freebsd32_clock_gettime_args * uap)2692 freebsd32_clock_gettime(struct thread *td,
2693 struct freebsd32_clock_gettime_args *uap)
2694 {
2695 struct timespec ats;
2696 struct timespec32 ats32;
2697 int error;
2698
2699 error = kern_clock_gettime(td, uap->clock_id, &ats);
2700 if (error == 0) {
2701 CP(ats, ats32, tv_sec);
2702 CP(ats, ats32, tv_nsec);
2703 error = copyout(&ats32, uap->tp, sizeof(ats32));
2704 }
2705 return (error);
2706 }
2707
2708 int
freebsd32_clock_settime(struct thread * td,struct freebsd32_clock_settime_args * uap)2709 freebsd32_clock_settime(struct thread *td,
2710 struct freebsd32_clock_settime_args *uap)
2711 {
2712 struct timespec ats;
2713 struct timespec32 ats32;
2714 int error;
2715
2716 error = copyin(uap->tp, &ats32, sizeof(ats32));
2717 if (error)
2718 return (error);
2719 CP(ats32, ats, tv_sec);
2720 CP(ats32, ats, tv_nsec);
2721
2722 return (kern_clock_settime(td, uap->clock_id, &ats));
2723 }
2724
2725 int
freebsd32_clock_getres(struct thread * td,struct freebsd32_clock_getres_args * uap)2726 freebsd32_clock_getres(struct thread *td,
2727 struct freebsd32_clock_getres_args *uap)
2728 {
2729 struct timespec ts;
2730 struct timespec32 ts32;
2731 int error;
2732
2733 if (uap->tp == NULL)
2734 return (0);
2735 error = kern_clock_getres(td, uap->clock_id, &ts);
2736 if (error == 0) {
2737 CP(ts, ts32, tv_sec);
2738 CP(ts, ts32, tv_nsec);
2739 error = copyout(&ts32, uap->tp, sizeof(ts32));
2740 }
2741 return (error);
2742 }
2743
freebsd32_ktimer_create(struct thread * td,struct freebsd32_ktimer_create_args * uap)2744 int freebsd32_ktimer_create(struct thread *td,
2745 struct freebsd32_ktimer_create_args *uap)
2746 {
2747 struct sigevent32 ev32;
2748 struct sigevent ev, *evp;
2749 int error, id;
2750
2751 if (uap->evp == NULL) {
2752 evp = NULL;
2753 } else {
2754 evp = &ev;
2755 error = copyin(uap->evp, &ev32, sizeof(ev32));
2756 if (error != 0)
2757 return (error);
2758 error = convert_sigevent32(&ev32, &ev);
2759 if (error != 0)
2760 return (error);
2761 }
2762 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2763 if (error == 0) {
2764 error = copyout(&id, uap->timerid, sizeof(int));
2765 if (error != 0)
2766 kern_ktimer_delete(td, id);
2767 }
2768 return (error);
2769 }
2770
2771 int
freebsd32_ktimer_settime(struct thread * td,struct freebsd32_ktimer_settime_args * uap)2772 freebsd32_ktimer_settime(struct thread *td,
2773 struct freebsd32_ktimer_settime_args *uap)
2774 {
2775 struct itimerspec32 val32, oval32;
2776 struct itimerspec val, oval, *ovalp;
2777 int error;
2778
2779 error = copyin(uap->value, &val32, sizeof(val32));
2780 if (error != 0)
2781 return (error);
2782 ITS_CP(val32, val);
2783 ovalp = uap->ovalue != NULL ? &oval : NULL;
2784 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2785 if (error == 0 && uap->ovalue != NULL) {
2786 ITS_CP(oval, oval32);
2787 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2788 }
2789 return (error);
2790 }
2791
2792 int
freebsd32_ktimer_gettime(struct thread * td,struct freebsd32_ktimer_gettime_args * uap)2793 freebsd32_ktimer_gettime(struct thread *td,
2794 struct freebsd32_ktimer_gettime_args *uap)
2795 {
2796 struct itimerspec32 val32;
2797 struct itimerspec val;
2798 int error;
2799
2800 error = kern_ktimer_gettime(td, uap->timerid, &val);
2801 if (error == 0) {
2802 ITS_CP(val, val32);
2803 error = copyout(&val32, uap->value, sizeof(val32));
2804 }
2805 return (error);
2806 }
2807
2808 int
freebsd32_clock_getcpuclockid2(struct thread * td,struct freebsd32_clock_getcpuclockid2_args * uap)2809 freebsd32_clock_getcpuclockid2(struct thread *td,
2810 struct freebsd32_clock_getcpuclockid2_args *uap)
2811 {
2812 clockid_t clk_id;
2813 int error;
2814
2815 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2816 uap->which, &clk_id);
2817 if (error == 0)
2818 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2819 return (error);
2820 }
2821
2822 int
freebsd32_thr_new(struct thread * td,struct freebsd32_thr_new_args * uap)2823 freebsd32_thr_new(struct thread *td,
2824 struct freebsd32_thr_new_args *uap)
2825 {
2826 struct thr_param32 param32;
2827 struct thr_param param;
2828 int error;
2829
2830 if (uap->param_size < 0 ||
2831 uap->param_size > sizeof(struct thr_param32))
2832 return (EINVAL);
2833 bzero(¶m, sizeof(struct thr_param));
2834 bzero(¶m32, sizeof(struct thr_param32));
2835 error = copyin(uap->param, ¶m32, uap->param_size);
2836 if (error != 0)
2837 return (error);
2838 param.start_func = PTRIN(param32.start_func);
2839 param.arg = PTRIN(param32.arg);
2840 param.stack_base = PTRIN(param32.stack_base);
2841 param.stack_size = param32.stack_size;
2842 param.tls_base = PTRIN(param32.tls_base);
2843 param.tls_size = param32.tls_size;
2844 param.child_tid = PTRIN(param32.child_tid);
2845 param.parent_tid = PTRIN(param32.parent_tid);
2846 param.flags = param32.flags;
2847 param.rtp = PTRIN(param32.rtp);
2848 param.spare[0] = PTRIN(param32.spare[0]);
2849 param.spare[1] = PTRIN(param32.spare[1]);
2850 param.spare[2] = PTRIN(param32.spare[2]);
2851
2852 return (kern_thr_new(td, ¶m));
2853 }
2854
2855 int
freebsd32_thr_suspend(struct thread * td,struct freebsd32_thr_suspend_args * uap)2856 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2857 {
2858 struct timespec32 ts32;
2859 struct timespec ts, *tsp;
2860 int error;
2861
2862 error = 0;
2863 tsp = NULL;
2864 if (uap->timeout != NULL) {
2865 error = copyin((const void *)uap->timeout, (void *)&ts32,
2866 sizeof(struct timespec32));
2867 if (error != 0)
2868 return (error);
2869 ts.tv_sec = ts32.tv_sec;
2870 ts.tv_nsec = ts32.tv_nsec;
2871 tsp = &ts;
2872 }
2873 return (kern_thr_suspend(td, tsp));
2874 }
2875
2876 void
siginfo_to_siginfo32(const siginfo_t * src,struct siginfo32 * dst)2877 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2878 {
2879 bzero(dst, sizeof(*dst));
2880 dst->si_signo = src->si_signo;
2881 dst->si_errno = src->si_errno;
2882 dst->si_code = src->si_code;
2883 dst->si_pid = src->si_pid;
2884 dst->si_uid = src->si_uid;
2885 dst->si_status = src->si_status;
2886 dst->si_addr = (uintptr_t)src->si_addr;
2887 dst->si_value.sival_int = src->si_value.sival_int;
2888 dst->si_timerid = src->si_timerid;
2889 dst->si_overrun = src->si_overrun;
2890 }
2891
2892 #ifndef _FREEBSD32_SYSPROTO_H_
2893 struct freebsd32_sigqueue_args {
2894 pid_t pid;
2895 int signum;
2896 /* union sigval32 */ int value;
2897 };
2898 #endif
2899 int
freebsd32_sigqueue(struct thread * td,struct freebsd32_sigqueue_args * uap)2900 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2901 {
2902 union sigval sv;
2903
2904 /*
2905 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2906 * On 64-bit little-endian ABIs, the low bits are the same.
2907 * In 64-bit big-endian ABIs, sival_int overlaps with
2908 * sival_ptr's HIGH bits. We choose to support sival_int
2909 * rather than sival_ptr in this case as it seems to be
2910 * more common.
2911 */
2912 bzero(&sv, sizeof(sv));
2913 sv.sival_int = uap->value;
2914
2915 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2916 }
2917
2918 int
freebsd32_sigtimedwait(struct thread * td,struct freebsd32_sigtimedwait_args * uap)2919 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2920 {
2921 struct timespec32 ts32;
2922 struct timespec ts;
2923 struct timespec *timeout;
2924 sigset_t set;
2925 ksiginfo_t ksi;
2926 struct siginfo32 si32;
2927 int error;
2928
2929 if (uap->timeout) {
2930 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2931 if (error)
2932 return (error);
2933 ts.tv_sec = ts32.tv_sec;
2934 ts.tv_nsec = ts32.tv_nsec;
2935 timeout = &ts;
2936 } else
2937 timeout = NULL;
2938
2939 error = copyin(uap->set, &set, sizeof(set));
2940 if (error)
2941 return (error);
2942
2943 error = kern_sigtimedwait(td, set, &ksi, timeout);
2944 if (error)
2945 return (error);
2946
2947 if (uap->info) {
2948 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2949 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2950 }
2951
2952 if (error == 0)
2953 td->td_retval[0] = ksi.ksi_signo;
2954 return (error);
2955 }
2956
2957 /*
2958 * MPSAFE
2959 */
2960 int
freebsd32_sigwaitinfo(struct thread * td,struct freebsd32_sigwaitinfo_args * uap)2961 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2962 {
2963 ksiginfo_t ksi;
2964 struct siginfo32 si32;
2965 sigset_t set;
2966 int error;
2967
2968 error = copyin(uap->set, &set, sizeof(set));
2969 if (error)
2970 return (error);
2971
2972 error = kern_sigtimedwait(td, set, &ksi, NULL);
2973 if (error)
2974 return (error);
2975
2976 if (uap->info) {
2977 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2978 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2979 }
2980 if (error == 0)
2981 td->td_retval[0] = ksi.ksi_signo;
2982 return (error);
2983 }
2984
2985 int
freebsd32_cpuset_setid(struct thread * td,struct freebsd32_cpuset_setid_args * uap)2986 freebsd32_cpuset_setid(struct thread *td,
2987 struct freebsd32_cpuset_setid_args *uap)
2988 {
2989
2990 return (kern_cpuset_setid(td, uap->which,
2991 PAIR32TO64(id_t, uap->id), uap->setid));
2992 }
2993
2994 int
freebsd32_cpuset_getid(struct thread * td,struct freebsd32_cpuset_getid_args * uap)2995 freebsd32_cpuset_getid(struct thread *td,
2996 struct freebsd32_cpuset_getid_args *uap)
2997 {
2998
2999 return (kern_cpuset_getid(td, uap->level, uap->which,
3000 PAIR32TO64(id_t, uap->id), uap->setid));
3001 }
3002
3003 int
freebsd32_cpuset_getaffinity(struct thread * td,struct freebsd32_cpuset_getaffinity_args * uap)3004 freebsd32_cpuset_getaffinity(struct thread *td,
3005 struct freebsd32_cpuset_getaffinity_args *uap)
3006 {
3007
3008 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
3009 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3010 }
3011
3012 int
freebsd32_cpuset_setaffinity(struct thread * td,struct freebsd32_cpuset_setaffinity_args * uap)3013 freebsd32_cpuset_setaffinity(struct thread *td,
3014 struct freebsd32_cpuset_setaffinity_args *uap)
3015 {
3016
3017 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
3018 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3019 }
3020
3021 int
freebsd32_cpuset_getdomain(struct thread * td,struct freebsd32_cpuset_getdomain_args * uap)3022 freebsd32_cpuset_getdomain(struct thread *td,
3023 struct freebsd32_cpuset_getdomain_args *uap)
3024 {
3025
3026 return (kern_cpuset_getdomain(td, uap->level, uap->which,
3027 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3028 }
3029
3030 int
freebsd32_cpuset_setdomain(struct thread * td,struct freebsd32_cpuset_setdomain_args * uap)3031 freebsd32_cpuset_setdomain(struct thread *td,
3032 struct freebsd32_cpuset_setdomain_args *uap)
3033 {
3034
3035 return (kern_cpuset_setdomain(td, uap->level, uap->which,
3036 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3037 }
3038
3039 int
freebsd32_nmount(struct thread * td,struct freebsd32_nmount_args * uap)3040 freebsd32_nmount(struct thread *td,
3041 struct freebsd32_nmount_args /* {
3042 struct iovec *iovp;
3043 unsigned int iovcnt;
3044 int flags;
3045 } */ *uap)
3046 {
3047 struct uio *auio;
3048 uint64_t flags;
3049 int error;
3050
3051 /*
3052 * Mount flags are now 64-bits. On 32-bit archtectures only
3053 * 32-bits are passed in, but from here on everything handles
3054 * 64-bit flags correctly.
3055 */
3056 flags = uap->flags;
3057
3058 AUDIT_ARG_FFLAGS(flags);
3059
3060 /*
3061 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
3062 * userspace to set this flag, but we must filter it out if we want
3063 * MNT_UPDATE on the root file system to work.
3064 * MNT_ROOTFS should only be set by the kernel when mounting its
3065 * root file system.
3066 */
3067 flags &= ~MNT_ROOTFS;
3068
3069 /*
3070 * check that we have an even number of iovec's
3071 * and that we have at least two options.
3072 */
3073 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3074 return (EINVAL);
3075
3076 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3077 if (error)
3078 return (error);
3079 error = vfs_donmount(td, flags, auio);
3080
3081 free(auio, M_IOV);
3082 return error;
3083 }
3084
3085 #if 0
3086 int
3087 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3088 {
3089 struct yyy32 *p32, s32;
3090 struct yyy *p = NULL, s;
3091 struct xxx_arg ap;
3092 int error;
3093
3094 if (uap->zzz) {
3095 error = copyin(uap->zzz, &s32, sizeof(s32));
3096 if (error)
3097 return (error);
3098 /* translate in */
3099 p = &s;
3100 }
3101 error = kern_xxx(td, p);
3102 if (error)
3103 return (error);
3104 if (uap->zzz) {
3105 /* translate out */
3106 error = copyout(&s32, p32, sizeof(s32));
3107 }
3108 return (error);
3109 }
3110 #endif
3111
3112 int
syscall32_module_handler(struct module * mod,int what,void * arg)3113 syscall32_module_handler(struct module *mod, int what, void *arg)
3114 {
3115
3116 return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3117 }
3118
3119 int
syscall32_helper_register(struct syscall_helper_data * sd,int flags)3120 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3121 {
3122
3123 return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3124 }
3125
3126 int
syscall32_helper_unregister(struct syscall_helper_data * sd)3127 syscall32_helper_unregister(struct syscall_helper_data *sd)
3128 {
3129
3130 return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3131 }
3132
3133 register_t *
freebsd32_copyout_strings(struct image_params * imgp)3134 freebsd32_copyout_strings(struct image_params *imgp)
3135 {
3136 int argc, envc, i;
3137 u_int32_t *vectp;
3138 char *stringp;
3139 uintptr_t destp;
3140 u_int32_t *stack_base;
3141 struct freebsd32_ps_strings *arginfo;
3142 char canary[sizeof(long) * 8];
3143 int32_t pagesizes32[MAXPAGESIZES];
3144 size_t execpath_len;
3145 int szsigcode;
3146
3147 /*
3148 * Calculate string base and vector table pointers.
3149 * Also deal with signal trampoline code for this exec type.
3150 */
3151 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3152 execpath_len = strlen(imgp->execpath) + 1;
3153 else
3154 execpath_len = 0;
3155 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3156 sv_psstrings;
3157 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3158 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3159 else
3160 szsigcode = 0;
3161 destp = (uintptr_t)arginfo;
3162
3163 /*
3164 * install sigcode
3165 */
3166 if (szsigcode != 0) {
3167 destp -= szsigcode;
3168 destp = rounddown2(destp, sizeof(uint32_t));
3169 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3170 szsigcode);
3171 }
3172
3173 /*
3174 * Copy the image path for the rtld.
3175 */
3176 if (execpath_len != 0) {
3177 destp -= execpath_len;
3178 imgp->execpathp = destp;
3179 copyout(imgp->execpath, (void *)destp, execpath_len);
3180 }
3181
3182 /*
3183 * Prepare the canary for SSP.
3184 */
3185 arc4rand(canary, sizeof(canary), 0);
3186 destp -= sizeof(canary);
3187 imgp->canary = destp;
3188 copyout(canary, (void *)destp, sizeof(canary));
3189 imgp->canarylen = sizeof(canary);
3190
3191 /*
3192 * Prepare the pagesizes array.
3193 */
3194 for (i = 0; i < MAXPAGESIZES; i++)
3195 pagesizes32[i] = (uint32_t)pagesizes[i];
3196 destp -= sizeof(pagesizes32);
3197 destp = rounddown2(destp, sizeof(uint32_t));
3198 imgp->pagesizes = destp;
3199 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3200 imgp->pagesizeslen = sizeof(pagesizes32);
3201
3202 destp -= ARG_MAX - imgp->args->stringspace;
3203 destp = rounddown2(destp, sizeof(uint32_t));
3204
3205 vectp = (uint32_t *)destp;
3206 if (imgp->sysent->sv_stackgap != NULL)
3207 imgp->sysent->sv_stackgap(imgp, (u_long *)&vectp);
3208
3209 if (imgp->auxargs) {
3210 /*
3211 * Allocate room on the stack for the ELF auxargs
3212 * array. It has up to AT_COUNT entries.
3213 */
3214 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
3215 sizeof(*vectp));
3216 }
3217
3218 /*
3219 * Allocate room for the argv[] and env vectors including the
3220 * terminating NULL pointers.
3221 */
3222 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3223
3224 /*
3225 * vectp also becomes our initial stack base
3226 */
3227 stack_base = vectp;
3228
3229 stringp = imgp->args->begin_argv;
3230 argc = imgp->args->argc;
3231 envc = imgp->args->envc;
3232 /*
3233 * Copy out strings - arguments and environment.
3234 */
3235 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3236
3237 /*
3238 * Fill in "ps_strings" struct for ps, w, etc.
3239 */
3240 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3241 suword32(&arginfo->ps_nargvstr, argc);
3242
3243 /*
3244 * Fill in argument portion of vector table.
3245 */
3246 for (; argc > 0; --argc) {
3247 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3248 while (*stringp++ != 0)
3249 destp++;
3250 destp++;
3251 }
3252
3253 /* a null vector table pointer separates the argp's from the envp's */
3254 suword32(vectp++, 0);
3255
3256 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3257 suword32(&arginfo->ps_nenvstr, envc);
3258
3259 /*
3260 * Fill in environment portion of vector table.
3261 */
3262 for (; envc > 0; --envc) {
3263 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3264 while (*stringp++ != 0)
3265 destp++;
3266 destp++;
3267 }
3268
3269 /* end of vector table is a null pointer */
3270 suword32(vectp, 0);
3271
3272 return ((register_t *)stack_base);
3273 }
3274
3275 int
freebsd32_kldstat(struct thread * td,struct freebsd32_kldstat_args * uap)3276 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3277 {
3278 struct kld_file_stat *stat;
3279 struct kld32_file_stat *stat32;
3280 int error, version;
3281
3282 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3283 != 0)
3284 return (error);
3285 if (version != sizeof(struct kld32_file_stat_1) &&
3286 version != sizeof(struct kld32_file_stat))
3287 return (EINVAL);
3288
3289 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3290 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3291 error = kern_kldstat(td, uap->fileid, stat);
3292 if (error == 0) {
3293 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3294 CP(*stat, *stat32, refs);
3295 CP(*stat, *stat32, id);
3296 PTROUT_CP(*stat, *stat32, address);
3297 CP(*stat, *stat32, size);
3298 bcopy(&stat->pathname[0], &stat32->pathname[0],
3299 sizeof(stat->pathname));
3300 stat32->version = version;
3301 error = copyout(stat32, uap->stat, version);
3302 }
3303 free(stat, M_TEMP);
3304 free(stat32, M_TEMP);
3305 return (error);
3306 }
3307
3308 int
freebsd32_posix_fallocate(struct thread * td,struct freebsd32_posix_fallocate_args * uap)3309 freebsd32_posix_fallocate(struct thread *td,
3310 struct freebsd32_posix_fallocate_args *uap)
3311 {
3312 int error;
3313
3314 error = kern_posix_fallocate(td, uap->fd,
3315 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3316 return (kern_posix_error(td, error));
3317 }
3318
3319 int
freebsd32_posix_fadvise(struct thread * td,struct freebsd32_posix_fadvise_args * uap)3320 freebsd32_posix_fadvise(struct thread *td,
3321 struct freebsd32_posix_fadvise_args *uap)
3322 {
3323 int error;
3324
3325 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3326 PAIR32TO64(off_t, uap->len), uap->advice);
3327 return (kern_posix_error(td, error));
3328 }
3329
3330 int
convert_sigevent32(struct sigevent32 * sig32,struct sigevent * sig)3331 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3332 {
3333
3334 CP(*sig32, *sig, sigev_notify);
3335 switch (sig->sigev_notify) {
3336 case SIGEV_NONE:
3337 break;
3338 case SIGEV_THREAD_ID:
3339 CP(*sig32, *sig, sigev_notify_thread_id);
3340 /* FALLTHROUGH */
3341 case SIGEV_SIGNAL:
3342 CP(*sig32, *sig, sigev_signo);
3343 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3344 break;
3345 case SIGEV_KEVENT:
3346 CP(*sig32, *sig, sigev_notify_kqueue);
3347 CP(*sig32, *sig, sigev_notify_kevent_flags);
3348 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3349 break;
3350 default:
3351 return (EINVAL);
3352 }
3353 return (0);
3354 }
3355
3356 int
freebsd32_procctl(struct thread * td,struct freebsd32_procctl_args * uap)3357 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3358 {
3359 void *data;
3360 union {
3361 struct procctl_reaper_status rs;
3362 struct procctl_reaper_pids rp;
3363 struct procctl_reaper_kill rk;
3364 } x;
3365 union {
3366 struct procctl_reaper_pids32 rp;
3367 } x32;
3368 int error, error1, flags, signum;
3369
3370 if (uap->com >= PROC_PROCCTL_MD_MIN)
3371 return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3372 uap->com, PTRIN(uap->data)));
3373
3374 switch (uap->com) {
3375 case PROC_ASLR_CTL:
3376 case PROC_SPROTECT:
3377 case PROC_STACKGAP_CTL:
3378 case PROC_TRACE_CTL:
3379 case PROC_TRAPCAP_CTL:
3380 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3381 if (error != 0)
3382 return (error);
3383 data = &flags;
3384 break;
3385 case PROC_REAP_ACQUIRE:
3386 case PROC_REAP_RELEASE:
3387 if (uap->data != NULL)
3388 return (EINVAL);
3389 data = NULL;
3390 break;
3391 case PROC_REAP_STATUS:
3392 data = &x.rs;
3393 break;
3394 case PROC_REAP_GETPIDS:
3395 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3396 if (error != 0)
3397 return (error);
3398 CP(x32.rp, x.rp, rp_count);
3399 PTRIN_CP(x32.rp, x.rp, rp_pids);
3400 data = &x.rp;
3401 break;
3402 case PROC_REAP_KILL:
3403 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3404 if (error != 0)
3405 return (error);
3406 data = &x.rk;
3407 break;
3408 case PROC_ASLR_STATUS:
3409 case PROC_STACKGAP_STATUS:
3410 case PROC_TRACE_STATUS:
3411 case PROC_TRAPCAP_STATUS:
3412 data = &flags;
3413 break;
3414 case PROC_PDEATHSIG_CTL:
3415 error = copyin(uap->data, &signum, sizeof(signum));
3416 if (error != 0)
3417 return (error);
3418 data = &signum;
3419 break;
3420 case PROC_PDEATHSIG_STATUS:
3421 data = &signum;
3422 break;
3423 default:
3424 return (EINVAL);
3425 }
3426 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3427 uap->com, data);
3428 switch (uap->com) {
3429 case PROC_REAP_STATUS:
3430 if (error == 0)
3431 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3432 break;
3433 case PROC_REAP_KILL:
3434 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3435 if (error == 0)
3436 error = error1;
3437 break;
3438 case PROC_ASLR_STATUS:
3439 case PROC_STACKGAP_STATUS:
3440 case PROC_TRACE_STATUS:
3441 case PROC_TRAPCAP_STATUS:
3442 if (error == 0)
3443 error = copyout(&flags, uap->data, sizeof(flags));
3444 break;
3445 case PROC_PDEATHSIG_STATUS:
3446 if (error == 0)
3447 error = copyout(&signum, uap->data, sizeof(signum));
3448 break;
3449 }
3450 return (error);
3451 }
3452
3453 int
freebsd32_fcntl(struct thread * td,struct freebsd32_fcntl_args * uap)3454 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3455 {
3456 long tmp;
3457
3458 switch (uap->cmd) {
3459 /*
3460 * Do unsigned conversion for arg when operation
3461 * interprets it as flags or pointer.
3462 */
3463 case F_SETLK_REMOTE:
3464 case F_SETLKW:
3465 case F_SETLK:
3466 case F_GETLK:
3467 case F_SETFD:
3468 case F_SETFL:
3469 case F_OGETLK:
3470 case F_OSETLK:
3471 case F_OSETLKW:
3472 tmp = (unsigned int)(uap->arg);
3473 break;
3474 default:
3475 tmp = uap->arg;
3476 break;
3477 }
3478 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3479 }
3480
3481 int
freebsd32_ppoll(struct thread * td,struct freebsd32_ppoll_args * uap)3482 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3483 {
3484 struct timespec32 ts32;
3485 struct timespec ts, *tsp;
3486 sigset_t set, *ssp;
3487 int error;
3488
3489 if (uap->ts != NULL) {
3490 error = copyin(uap->ts, &ts32, sizeof(ts32));
3491 if (error != 0)
3492 return (error);
3493 CP(ts32, ts, tv_sec);
3494 CP(ts32, ts, tv_nsec);
3495 tsp = &ts;
3496 } else
3497 tsp = NULL;
3498 if (uap->set != NULL) {
3499 error = copyin(uap->set, &set, sizeof(set));
3500 if (error != 0)
3501 return (error);
3502 ssp = &set;
3503 } else
3504 ssp = NULL;
3505
3506 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3507 }
3508
3509 int
freebsd32_sched_rr_get_interval(struct thread * td,struct freebsd32_sched_rr_get_interval_args * uap)3510 freebsd32_sched_rr_get_interval(struct thread *td,
3511 struct freebsd32_sched_rr_get_interval_args *uap)
3512 {
3513 struct timespec ts;
3514 struct timespec32 ts32;
3515 int error;
3516
3517 error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3518 if (error == 0) {
3519 CP(ts, ts32, tv_sec);
3520 CP(ts, ts32, tv_nsec);
3521 error = copyout(&ts32, uap->interval, sizeof(ts32));
3522 }
3523 return (error);
3524 }
3525