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