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