1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1989, 1990, 1993
5 * The Regents of the University of California. 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 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
32 */
33
34 #include <sys/cdefs.h>
35 #include "opt_capsicum.h"
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_ktrace.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/capsicum.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysproto.h>
47 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
49 #include <sys/proc.h>
50 #include <sys/filio.h>
51 #include <sys/jail.h>
52 #include <sys/mbuf.h>
53 #include <sys/protosw.h>
54 #include <sys/rwlock.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/syscallsubr.h>
58 #ifdef COMPAT_43
59 #include <sys/sysent.h>
60 #endif
61 #include <sys/uio.h>
62 #include <sys/un.h>
63 #include <sys/unpcb.h>
64 #ifdef KTRACE
65 #include <sys/ktrace.h>
66 #endif
67 #ifdef COMPAT_FREEBSD32
68 #include <compat/freebsd32/freebsd32_util.h>
69 #endif
70
71 #include <net/vnet.h>
72
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
75
76 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
77 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
78
79 static int accept1(struct thread *td, int s, struct sockaddr *uname,
80 socklen_t *anamelen, int flags);
81 static int sockargs(struct mbuf **, char *, socklen_t, int);
82
83 /*
84 * Convert a user file descriptor to a kernel file entry and check if required
85 * capability rights are present.
86 * If required copy of current set of capability rights is returned.
87 * A reference on the file entry is held upon returning.
88 */
89 int
getsock_cap(struct thread * td,int fd,cap_rights_t * rightsp,struct file ** fpp,struct filecaps * havecapsp)90 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
91 struct file **fpp, struct filecaps *havecapsp)
92 {
93 struct file *fp;
94 int error;
95
96 error = fget_cap(td, fd, rightsp, &fp, havecapsp);
97 if (__predict_false(error != 0))
98 return (error);
99 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
100 fdrop(fp, td);
101 if (havecapsp != NULL)
102 filecaps_free(havecapsp);
103 return (ENOTSOCK);
104 }
105 *fpp = fp;
106 return (0);
107 }
108
109 int
getsock(struct thread * td,int fd,cap_rights_t * rightsp,struct file ** fpp)110 getsock(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
111 {
112 struct file *fp;
113 int error;
114
115 error = fget_unlocked(td, fd, rightsp, &fp);
116 if (__predict_false(error != 0))
117 return (error);
118 if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
119 fdrop(fp, td);
120 return (ENOTSOCK);
121 }
122 *fpp = fp;
123 return (0);
124 }
125
126 /*
127 * System call interface to the socket abstraction.
128 */
129 #if defined(COMPAT_43)
130 #define COMPAT_OLDSOCK
131 #endif
132
133 int
sys_socket(struct thread * td,struct socket_args * uap)134 sys_socket(struct thread *td, struct socket_args *uap)
135 {
136
137 return (kern_socket(td, uap->domain, uap->type, uap->protocol));
138 }
139
140 int
kern_socket(struct thread * td,int domain,int type,int protocol)141 kern_socket(struct thread *td, int domain, int type, int protocol)
142 {
143 struct socket *so;
144 struct file *fp;
145 int fd, error, oflag, fflag;
146
147 AUDIT_ARG_SOCKET(domain, type, protocol);
148
149 oflag = 0;
150 fflag = 0;
151 if ((type & SOCK_CLOEXEC) != 0) {
152 type &= ~SOCK_CLOEXEC;
153 oflag |= O_CLOEXEC;
154 }
155 if ((type & SOCK_NONBLOCK) != 0) {
156 type &= ~SOCK_NONBLOCK;
157 fflag |= FNONBLOCK;
158 }
159
160 #ifdef MAC
161 error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
162 if (error != 0)
163 return (error);
164 #endif
165 error = falloc(td, &fp, &fd, oflag);
166 if (error != 0)
167 return (error);
168 /* An extra reference on `fp' has been held for us by falloc(). */
169 error = socreate(domain, &so, type, protocol, td->td_ucred, td);
170 if (error != 0) {
171 fdclose(td, fp, fd);
172 } else {
173 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
174 if ((fflag & FNONBLOCK) != 0)
175 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
176 td->td_retval[0] = fd;
177 }
178 fdrop(fp, td);
179 return (error);
180 }
181
182 int
sys_bind(struct thread * td,struct bind_args * uap)183 sys_bind(struct thread *td, struct bind_args *uap)
184 {
185 struct sockaddr *sa;
186 int error;
187
188 error = getsockaddr(&sa, uap->name, uap->namelen);
189 if (error == 0) {
190 error = kern_bindat(td, AT_FDCWD, uap->s, sa);
191 free(sa, M_SONAME);
192 }
193 return (error);
194 }
195
196 int
kern_bindat(struct thread * td,int dirfd,int fd,struct sockaddr * sa)197 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
198 {
199 struct socket *so;
200 struct file *fp;
201 int error;
202
203 #ifdef CAPABILITY_MODE
204 if (dirfd == AT_FDCWD) {
205 if (CAP_TRACING(td))
206 ktrcapfail(CAPFAIL_NAMEI, "AT_FDCWD");
207 if (IN_CAPABILITY_MODE(td))
208 return (ECAPMODE);
209 }
210 #endif
211
212 AUDIT_ARG_FD(fd);
213 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
214 error = getsock(td, fd, &cap_bind_rights, &fp);
215 if (error != 0)
216 return (error);
217 so = fp->f_data;
218 #ifdef KTRACE
219 if (KTRPOINT(td, KTR_STRUCT))
220 ktrsockaddr(sa);
221 #endif
222 #ifdef MAC
223 error = mac_socket_check_bind(td->td_ucred, so, sa);
224 if (error == 0) {
225 #endif
226 if (dirfd == AT_FDCWD)
227 error = sobind(so, sa, td);
228 else
229 error = sobindat(dirfd, so, sa, td);
230 #ifdef MAC
231 }
232 #endif
233 fdrop(fp, td);
234 return (error);
235 }
236
237 int
sys_bindat(struct thread * td,struct bindat_args * uap)238 sys_bindat(struct thread *td, struct bindat_args *uap)
239 {
240 struct sockaddr *sa;
241 int error;
242
243 error = getsockaddr(&sa, uap->name, uap->namelen);
244 if (error == 0) {
245 error = kern_bindat(td, uap->fd, uap->s, sa);
246 free(sa, M_SONAME);
247 }
248 return (error);
249 }
250
251 int
sys_listen(struct thread * td,struct listen_args * uap)252 sys_listen(struct thread *td, struct listen_args *uap)
253 {
254
255 return (kern_listen(td, uap->s, uap->backlog));
256 }
257
258 int
kern_listen(struct thread * td,int s,int backlog)259 kern_listen(struct thread *td, int s, int backlog)
260 {
261 struct socket *so;
262 struct file *fp;
263 int error;
264
265 AUDIT_ARG_FD(s);
266 error = getsock(td, s, &cap_listen_rights, &fp);
267 if (error == 0) {
268 so = fp->f_data;
269 #ifdef MAC
270 error = mac_socket_check_listen(td->td_ucred, so);
271 if (error == 0)
272 #endif
273 error = solisten(so, backlog, td);
274 fdrop(fp, td);
275 }
276 return (error);
277 }
278
279 /*
280 * accept1()
281 */
282 static int
accept1(struct thread * td,int s,struct sockaddr * uname,socklen_t * anamelen,int flags)283 accept1(struct thread *td, int s, struct sockaddr *uname, socklen_t *anamelen,
284 int flags)
285 {
286 struct sockaddr *name;
287 socklen_t namelen;
288 struct file *fp;
289 int error;
290
291 if (uname == NULL)
292 return (kern_accept4(td, s, NULL, NULL, flags, NULL));
293
294 error = copyin(anamelen, &namelen, sizeof (namelen));
295 if (error != 0)
296 return (error);
297
298 error = kern_accept4(td, s, &name, &namelen, flags, &fp);
299
300 if (error != 0)
301 return (error);
302
303 #ifdef COMPAT_OLDSOCK
304 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
305 (flags & ACCEPT4_COMPAT) != 0)
306 ((struct osockaddr *)name)->sa_family =
307 name->sa_family;
308 #endif
309 error = copyout(name, uname, namelen);
310 if (error == 0)
311 error = copyout(&namelen, anamelen,
312 sizeof(namelen));
313 if (error != 0)
314 fdclose(td, fp, td->td_retval[0]);
315 fdrop(fp, td);
316 free(name, M_SONAME);
317 return (error);
318 }
319
320 int
kern_accept(struct thread * td,int s,struct sockaddr ** name,socklen_t * namelen,struct file ** fp)321 kern_accept(struct thread *td, int s, struct sockaddr **name,
322 socklen_t *namelen, struct file **fp)
323 {
324 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
325 }
326
327 int
kern_accept4(struct thread * td,int s,struct sockaddr ** name,socklen_t * namelen,int flags,struct file ** fp)328 kern_accept4(struct thread *td, int s, struct sockaddr **name,
329 socklen_t *namelen, int flags, struct file **fp)
330 {
331 struct file *headfp, *nfp = NULL;
332 struct sockaddr *sa = NULL;
333 struct socket *head, *so;
334 struct filecaps fcaps;
335 u_int fflag;
336 pid_t pgid;
337 int error, fd, tmp;
338
339 if (name != NULL)
340 *name = NULL;
341
342 AUDIT_ARG_FD(s);
343 error = getsock_cap(td, s, &cap_accept_rights,
344 &headfp, &fcaps);
345 if (error != 0)
346 return (error);
347 fflag = atomic_load_int(&headfp->f_flag);
348 head = headfp->f_data;
349 if (!SOLISTENING(head)) {
350 error = EINVAL;
351 goto done;
352 }
353 #ifdef MAC
354 error = mac_socket_check_accept(td->td_ucred, head);
355 if (error != 0)
356 goto done;
357 #endif
358 error = falloc_caps(td, &nfp, &fd,
359 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
360 if (error != 0)
361 goto done;
362 SOCK_LOCK(head);
363 if (!SOLISTENING(head)) {
364 SOCK_UNLOCK(head);
365 error = EINVAL;
366 goto noconnection;
367 }
368
369 error = solisten_dequeue(head, &so, flags);
370 if (error != 0)
371 goto noconnection;
372
373 /* An extra reference on `nfp' has been held for us by falloc(). */
374 td->td_retval[0] = fd;
375
376 /* Connection has been removed from the listen queue. */
377 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
378
379 if (flags & ACCEPT4_INHERIT) {
380 pgid = fgetown(&head->so_sigio);
381 if (pgid != 0)
382 fsetown(pgid, &so->so_sigio);
383 } else {
384 fflag &= ~(FNONBLOCK | FASYNC);
385 if (flags & SOCK_NONBLOCK)
386 fflag |= FNONBLOCK;
387 }
388
389 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
390 /* Sync socket nonblocking/async state with file flags */
391 tmp = fflag & FNONBLOCK;
392 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
393 tmp = fflag & FASYNC;
394 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
395 error = soaccept(so, &sa);
396 if (error != 0)
397 goto noconnection;
398 if (sa == NULL) {
399 if (name)
400 *namelen = 0;
401 goto done;
402 }
403 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
404 if (name) {
405 /* check sa_len before it is destroyed */
406 if (*namelen > sa->sa_len)
407 *namelen = sa->sa_len;
408 #ifdef KTRACE
409 if (KTRPOINT(td, KTR_STRUCT))
410 ktrsockaddr(sa);
411 #endif
412 *name = sa;
413 sa = NULL;
414 }
415 noconnection:
416 free(sa, M_SONAME);
417
418 /*
419 * close the new descriptor, assuming someone hasn't ripped it
420 * out from under us.
421 */
422 if (error != 0)
423 fdclose(td, nfp, fd);
424
425 /*
426 * Release explicitly held references before returning. We return
427 * a reference on nfp to the caller on success if they request it.
428 */
429 done:
430 if (nfp == NULL)
431 filecaps_free(&fcaps);
432 if (fp != NULL) {
433 if (error == 0) {
434 *fp = nfp;
435 nfp = NULL;
436 } else
437 *fp = NULL;
438 }
439 if (nfp != NULL)
440 fdrop(nfp, td);
441 fdrop(headfp, td);
442 return (error);
443 }
444
445 int
sys_accept(struct thread * td,struct accept_args * uap)446 sys_accept(struct thread *td, struct accept_args *uap)
447 {
448
449 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
450 }
451
452 int
sys_accept4(struct thread * td,struct accept4_args * uap)453 sys_accept4(struct thread *td, struct accept4_args *uap)
454 {
455
456 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
457 return (EINVAL);
458
459 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
460 }
461
462 #ifdef COMPAT_OLDSOCK
463 int
oaccept(struct thread * td,struct oaccept_args * uap)464 oaccept(struct thread *td, struct oaccept_args *uap)
465 {
466
467 return (accept1(td, uap->s, uap->name, uap->anamelen,
468 ACCEPT4_INHERIT | ACCEPT4_COMPAT));
469 }
470 #endif /* COMPAT_OLDSOCK */
471
472 int
sys_connect(struct thread * td,struct connect_args * uap)473 sys_connect(struct thread *td, struct connect_args *uap)
474 {
475 struct sockaddr *sa;
476 int error;
477
478 error = getsockaddr(&sa, uap->name, uap->namelen);
479 if (error == 0) {
480 error = kern_connectat(td, AT_FDCWD, uap->s, sa);
481 free(sa, M_SONAME);
482 }
483 return (error);
484 }
485
486 int
kern_connectat(struct thread * td,int dirfd,int fd,struct sockaddr * sa)487 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
488 {
489 struct socket *so;
490 struct file *fp;
491 int error;
492
493 #ifdef CAPABILITY_MODE
494 if (dirfd == AT_FDCWD) {
495 if (CAP_TRACING(td))
496 ktrcapfail(CAPFAIL_NAMEI, "AT_FDCWD");
497 if (IN_CAPABILITY_MODE(td))
498 return (ECAPMODE);
499 }
500 #endif
501
502 AUDIT_ARG_FD(fd);
503 AUDIT_ARG_SOCKADDR(td, dirfd, sa);
504 error = getsock(td, fd, &cap_connect_rights, &fp);
505 if (error != 0)
506 return (error);
507 so = fp->f_data;
508 if (so->so_state & SS_ISCONNECTING) {
509 error = EALREADY;
510 goto done1;
511 }
512 #ifdef KTRACE
513 if (KTRPOINT(td, KTR_STRUCT))
514 ktrsockaddr(sa);
515 #endif
516 #ifdef MAC
517 error = mac_socket_check_connect(td->td_ucred, so, sa);
518 if (error != 0)
519 goto bad;
520 #endif
521 error = soconnectat(dirfd, so, sa, td);
522 if (error != 0)
523 goto bad;
524 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
525 error = EINPROGRESS;
526 goto done1;
527 }
528 SOCK_LOCK(so);
529 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
530 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
531 "connec", 0);
532 if (error != 0)
533 break;
534 }
535 if (error == 0) {
536 error = so->so_error;
537 so->so_error = 0;
538 }
539 SOCK_UNLOCK(so);
540 bad:
541 if (error == ERESTART)
542 error = EINTR;
543 done1:
544 fdrop(fp, td);
545 return (error);
546 }
547
548 int
sys_connectat(struct thread * td,struct connectat_args * uap)549 sys_connectat(struct thread *td, struct connectat_args *uap)
550 {
551 struct sockaddr *sa;
552 int error;
553
554 error = getsockaddr(&sa, uap->name, uap->namelen);
555 if (error == 0) {
556 error = kern_connectat(td, uap->fd, uap->s, sa);
557 free(sa, M_SONAME);
558 }
559 return (error);
560 }
561
562 int
kern_socketpair(struct thread * td,int domain,int type,int protocol,int * rsv)563 kern_socketpair(struct thread *td, int domain, int type, int protocol,
564 int *rsv)
565 {
566 struct file *fp1, *fp2;
567 struct socket *so1, *so2;
568 int fd, error, oflag, fflag;
569
570 AUDIT_ARG_SOCKET(domain, type, protocol);
571
572 oflag = 0;
573 fflag = 0;
574 if ((type & SOCK_CLOEXEC) != 0) {
575 type &= ~SOCK_CLOEXEC;
576 oflag |= O_CLOEXEC;
577 }
578 if ((type & SOCK_NONBLOCK) != 0) {
579 type &= ~SOCK_NONBLOCK;
580 fflag |= FNONBLOCK;
581 }
582 #ifdef MAC
583 /* We might want to have a separate check for socket pairs. */
584 error = mac_socket_check_create(td->td_ucred, domain, type,
585 protocol);
586 if (error != 0)
587 return (error);
588 #endif
589 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
590 if (error != 0)
591 return (error);
592 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
593 if (error != 0)
594 goto free1;
595 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
596 error = falloc(td, &fp1, &fd, oflag);
597 if (error != 0)
598 goto free2;
599 rsv[0] = fd;
600 fp1->f_data = so1; /* so1 already has ref count */
601 error = falloc(td, &fp2, &fd, oflag);
602 if (error != 0)
603 goto free3;
604 fp2->f_data = so2; /* so2 already has ref count */
605 rsv[1] = fd;
606 error = soconnect2(so1, so2);
607 if (error != 0)
608 goto free4;
609 if (type == SOCK_DGRAM) {
610 /*
611 * Datagram socket connection is asymmetric.
612 */
613 error = soconnect2(so2, so1);
614 if (error != 0)
615 goto free4;
616 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
617 struct unpcb *unp, *unp2;
618 unp = sotounpcb(so1);
619 unp2 = sotounpcb(so2);
620 /*
621 * No need to lock the unps, because the sockets are brand-new.
622 * No other threads can be using them yet
623 */
624 unp_copy_peercred(td, unp, unp2, unp);
625 }
626 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
627 &socketops);
628 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
629 &socketops);
630 if ((fflag & FNONBLOCK) != 0) {
631 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
632 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
633 }
634 fdrop(fp1, td);
635 fdrop(fp2, td);
636 return (0);
637 free4:
638 fdclose(td, fp2, rsv[1]);
639 fdrop(fp2, td);
640 free3:
641 fdclose(td, fp1, rsv[0]);
642 fdrop(fp1, td);
643 free2:
644 if (so2 != NULL)
645 (void)soclose(so2);
646 free1:
647 if (so1 != NULL)
648 (void)soclose(so1);
649 return (error);
650 }
651
652 int
sys_socketpair(struct thread * td,struct socketpair_args * uap)653 sys_socketpair(struct thread *td, struct socketpair_args *uap)
654 {
655 int error, sv[2];
656
657 error = kern_socketpair(td, uap->domain, uap->type,
658 uap->protocol, sv);
659 if (error != 0)
660 return (error);
661 error = copyout(sv, uap->rsv, 2 * sizeof(int));
662 if (error != 0) {
663 (void)kern_close(td, sv[0]);
664 (void)kern_close(td, sv[1]);
665 }
666 return (error);
667 }
668
669 static int
sendit(struct thread * td,int s,struct msghdr * mp,int flags)670 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
671 {
672 struct mbuf *control;
673 struct sockaddr *to;
674 int error;
675
676 if (mp->msg_name != NULL) {
677 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
678 if (error != 0) {
679 to = NULL;
680 goto bad;
681 }
682 mp->msg_name = to;
683 #ifdef CAPABILITY_MODE
684 if (CAP_TRACING(td))
685 ktrcapfail(CAPFAIL_SOCKADDR, mp->msg_name);
686 if (IN_CAPABILITY_MODE(td)) {
687 error = ECAPMODE;
688 goto bad;
689 }
690 #endif
691 } else {
692 to = NULL;
693 }
694
695 if (mp->msg_control) {
696 if (mp->msg_controllen < sizeof(struct cmsghdr)
697 #ifdef COMPAT_OLDSOCK
698 && (mp->msg_flags != MSG_COMPAT ||
699 !SV_PROC_FLAG(td->td_proc, SV_AOUT))
700 #endif
701 ) {
702 error = EINVAL;
703 goto bad;
704 }
705 error = sockargs(&control, mp->msg_control,
706 mp->msg_controllen, MT_CONTROL);
707 if (error != 0)
708 goto bad;
709 #ifdef COMPAT_OLDSOCK
710 if (mp->msg_flags == MSG_COMPAT &&
711 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
712 struct cmsghdr *cm;
713
714 M_PREPEND(control, sizeof(*cm), M_WAITOK);
715 cm = mtod(control, struct cmsghdr *);
716 cm->cmsg_len = control->m_len;
717 cm->cmsg_level = SOL_SOCKET;
718 cm->cmsg_type = SCM_RIGHTS;
719 }
720 #endif
721 } else {
722 control = NULL;
723 }
724
725 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
726
727 bad:
728 free(to, M_SONAME);
729 return (error);
730 }
731
732 int
kern_sendit(struct thread * td,int s,struct msghdr * mp,int flags,struct mbuf * control,enum uio_seg segflg)733 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
734 struct mbuf *control, enum uio_seg segflg)
735 {
736 struct file *fp;
737 struct uio auio;
738 struct iovec *iov;
739 struct socket *so;
740 cap_rights_t *rights;
741 #ifdef KTRACE
742 struct uio *ktruio = NULL;
743 #endif
744 ssize_t len;
745 int i, error;
746
747 AUDIT_ARG_FD(s);
748 rights = &cap_send_rights;
749 if (mp->msg_name != NULL) {
750 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
751 rights = &cap_send_connect_rights;
752 }
753 error = getsock(td, s, rights, &fp);
754 if (error != 0) {
755 m_freem(control);
756 return (error);
757 }
758 so = (struct socket *)fp->f_data;
759
760 #ifdef KTRACE
761 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
762 ktrsockaddr(mp->msg_name);
763 #endif
764 #ifdef MAC
765 if (mp->msg_name != NULL) {
766 error = mac_socket_check_connect(td->td_ucred, so,
767 mp->msg_name);
768 if (error != 0) {
769 m_freem(control);
770 goto bad;
771 }
772 }
773 error = mac_socket_check_send(td->td_ucred, so);
774 if (error != 0) {
775 m_freem(control);
776 goto bad;
777 }
778 #endif
779
780 auio.uio_iov = mp->msg_iov;
781 auio.uio_iovcnt = mp->msg_iovlen;
782 auio.uio_segflg = segflg;
783 auio.uio_rw = UIO_WRITE;
784 auio.uio_td = td;
785 auio.uio_offset = 0; /* XXX */
786 auio.uio_resid = 0;
787 iov = mp->msg_iov;
788 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
789 if ((auio.uio_resid += iov->iov_len) < 0) {
790 error = EINVAL;
791 m_freem(control);
792 goto bad;
793 }
794 }
795 #ifdef KTRACE
796 if (KTRPOINT(td, KTR_GENIO))
797 ktruio = cloneuio(&auio);
798 #endif
799 len = auio.uio_resid;
800 error = sousrsend(so, mp->msg_name, &auio, control, flags, NULL);
801 if (error == 0)
802 td->td_retval[0] = len - auio.uio_resid;
803 #ifdef KTRACE
804 if (ktruio != NULL) {
805 if (error == 0)
806 ktruio->uio_resid = td->td_retval[0];
807 ktrgenio(s, UIO_WRITE, ktruio, error);
808 }
809 #endif
810 bad:
811 fdrop(fp, td);
812 return (error);
813 }
814
815 int
sys_sendto(struct thread * td,struct sendto_args * uap)816 sys_sendto(struct thread *td, struct sendto_args *uap)
817 {
818 struct msghdr msg;
819 struct iovec aiov;
820
821 msg.msg_name = __DECONST(void *, uap->to);
822 msg.msg_namelen = uap->tolen;
823 msg.msg_iov = &aiov;
824 msg.msg_iovlen = 1;
825 msg.msg_control = 0;
826 #ifdef COMPAT_OLDSOCK
827 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
828 msg.msg_flags = 0;
829 #endif
830 aiov.iov_base = __DECONST(void *, uap->buf);
831 aiov.iov_len = uap->len;
832 return (sendit(td, uap->s, &msg, uap->flags));
833 }
834
835 #ifdef COMPAT_OLDSOCK
836 int
osend(struct thread * td,struct osend_args * uap)837 osend(struct thread *td, struct osend_args *uap)
838 {
839 struct msghdr msg;
840 struct iovec aiov;
841
842 msg.msg_name = 0;
843 msg.msg_namelen = 0;
844 msg.msg_iov = &aiov;
845 msg.msg_iovlen = 1;
846 aiov.iov_base = __DECONST(void *, uap->buf);
847 aiov.iov_len = uap->len;
848 msg.msg_control = 0;
849 msg.msg_flags = 0;
850 return (sendit(td, uap->s, &msg, uap->flags));
851 }
852
853 int
osendmsg(struct thread * td,struct osendmsg_args * uap)854 osendmsg(struct thread *td, struct osendmsg_args *uap)
855 {
856 struct msghdr msg;
857 struct iovec *iov;
858 int error;
859
860 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
861 if (error != 0)
862 return (error);
863 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
864 if (error != 0)
865 return (error);
866 msg.msg_iov = iov;
867 msg.msg_flags = MSG_COMPAT;
868 error = sendit(td, uap->s, &msg, uap->flags);
869 free(iov, M_IOV);
870 return (error);
871 }
872 #endif
873
874 int
sys_sendmsg(struct thread * td,struct sendmsg_args * uap)875 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
876 {
877 struct msghdr msg;
878 struct iovec *iov;
879 int error;
880
881 error = copyin(uap->msg, &msg, sizeof (msg));
882 if (error != 0)
883 return (error);
884 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
885 if (error != 0)
886 return (error);
887 msg.msg_iov = iov;
888 #ifdef COMPAT_OLDSOCK
889 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
890 msg.msg_flags = 0;
891 #endif
892 error = sendit(td, uap->s, &msg, uap->flags);
893 free(iov, M_IOV);
894 return (error);
895 }
896
897 int
kern_recvit(struct thread * td,int s,struct msghdr * mp,enum uio_seg fromseg,struct mbuf ** controlp)898 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
899 struct mbuf **controlp)
900 {
901 struct uio auio;
902 struct iovec *iov;
903 struct mbuf *control, *m;
904 caddr_t ctlbuf;
905 struct file *fp;
906 struct socket *so;
907 struct sockaddr *fromsa = NULL;
908 #ifdef KTRACE
909 struct uio *ktruio = NULL;
910 #endif
911 ssize_t len;
912 int error, i;
913
914 if (controlp != NULL)
915 *controlp = NULL;
916
917 AUDIT_ARG_FD(s);
918 error = getsock(td, s, &cap_recv_rights, &fp);
919 if (error != 0)
920 return (error);
921 so = fp->f_data;
922
923 #ifdef MAC
924 error = mac_socket_check_receive(td->td_ucred, so);
925 if (error != 0) {
926 fdrop(fp, td);
927 return (error);
928 }
929 #endif
930
931 auio.uio_iov = mp->msg_iov;
932 auio.uio_iovcnt = mp->msg_iovlen;
933 auio.uio_segflg = UIO_USERSPACE;
934 auio.uio_rw = UIO_READ;
935 auio.uio_td = td;
936 auio.uio_offset = 0; /* XXX */
937 auio.uio_resid = 0;
938 iov = mp->msg_iov;
939 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
940 if ((auio.uio_resid += iov->iov_len) < 0) {
941 fdrop(fp, td);
942 return (EINVAL);
943 }
944 }
945 #ifdef KTRACE
946 if (KTRPOINT(td, KTR_GENIO))
947 ktruio = cloneuio(&auio);
948 #endif
949 control = NULL;
950 len = auio.uio_resid;
951 error = soreceive(so, &fromsa, &auio, NULL,
952 (mp->msg_control || controlp) ? &control : NULL,
953 &mp->msg_flags);
954 if (error != 0) {
955 if (auio.uio_resid != len && (error == ERESTART ||
956 error == EINTR || error == EWOULDBLOCK))
957 error = 0;
958 }
959 if (fromsa != NULL)
960 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
961 #ifdef KTRACE
962 if (ktruio != NULL) {
963 /* MSG_TRUNC can trigger underflow of uio_resid. */
964 ktruio->uio_resid = MIN(len - auio.uio_resid, len);
965 ktrgenio(s, UIO_READ, ktruio, error);
966 }
967 #endif
968 if (error != 0)
969 goto out;
970 td->td_retval[0] = len - auio.uio_resid;
971 if (mp->msg_name) {
972 len = mp->msg_namelen;
973 if (len <= 0 || fromsa == NULL)
974 len = 0;
975 else {
976 /* save sa_len before it is destroyed by MSG_COMPAT */
977 len = MIN(len, fromsa->sa_len);
978 #ifdef COMPAT_OLDSOCK
979 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
980 SV_PROC_FLAG(td->td_proc, SV_AOUT))
981 ((struct osockaddr *)fromsa)->sa_family =
982 fromsa->sa_family;
983 #endif
984 if (fromseg == UIO_USERSPACE) {
985 error = copyout(fromsa, mp->msg_name,
986 (unsigned)len);
987 if (error != 0)
988 goto out;
989 } else
990 bcopy(fromsa, mp->msg_name, len);
991 }
992 mp->msg_namelen = len;
993 }
994 if (mp->msg_control && controlp == NULL) {
995 #ifdef COMPAT_OLDSOCK
996 /*
997 * We assume that old recvmsg calls won't receive access
998 * rights and other control info, esp. as control info
999 * is always optional and those options didn't exist in 4.3.
1000 * If we receive rights, trim the cmsghdr; anything else
1001 * is tossed.
1002 */
1003 if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
1004 SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
1005 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1006 SOL_SOCKET ||
1007 mtod(control, struct cmsghdr *)->cmsg_type !=
1008 SCM_RIGHTS) {
1009 mp->msg_controllen = 0;
1010 goto out;
1011 }
1012 control->m_len -= sizeof (struct cmsghdr);
1013 control->m_data += sizeof (struct cmsghdr);
1014 }
1015 #endif
1016 ctlbuf = mp->msg_control;
1017 len = mp->msg_controllen;
1018 mp->msg_controllen = 0;
1019 for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1020 if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1021 m->m_len)) != 0)
1022 goto out;
1023
1024 ctlbuf += m->m_len;
1025 len -= m->m_len;
1026 mp->msg_controllen += m->m_len;
1027 }
1028 if (m != NULL) {
1029 mp->msg_flags |= MSG_CTRUNC;
1030 m_dispose_extcontrolm(m);
1031 }
1032 }
1033 out:
1034 fdrop(fp, td);
1035 #ifdef KTRACE
1036 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1037 ktrsockaddr(fromsa);
1038 #endif
1039 free(fromsa, M_SONAME);
1040
1041 if (error == 0 && controlp != NULL)
1042 *controlp = control;
1043 else if (control != NULL) {
1044 if (error != 0)
1045 m_dispose_extcontrolm(control);
1046 m_freem(control);
1047 }
1048
1049 return (error);
1050 }
1051
1052 static int
recvit(struct thread * td,int s,struct msghdr * mp,void * namelenp)1053 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1054 {
1055 int error;
1056
1057 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1058 if (error != 0)
1059 return (error);
1060 if (namelenp != NULL) {
1061 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1062 #ifdef COMPAT_OLDSOCK
1063 if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1064 SV_PROC_FLAG(td->td_proc, SV_AOUT))
1065 error = 0; /* old recvfrom didn't check */
1066 #endif
1067 }
1068 return (error);
1069 }
1070
1071 static int
kern_recvfrom(struct thread * td,int s,void * buf,size_t len,int flags,struct sockaddr * from,socklen_t * fromlenaddr)1072 kern_recvfrom(struct thread *td, int s, void *buf, size_t len, int flags,
1073 struct sockaddr *from, socklen_t *fromlenaddr)
1074 {
1075 struct msghdr msg;
1076 struct iovec aiov;
1077 int error;
1078
1079 if (fromlenaddr != NULL) {
1080 error = copyin(fromlenaddr, &msg.msg_namelen,
1081 sizeof (msg.msg_namelen));
1082 if (error != 0)
1083 goto done2;
1084 } else {
1085 msg.msg_namelen = 0;
1086 }
1087 msg.msg_name = from;
1088 msg.msg_iov = &aiov;
1089 msg.msg_iovlen = 1;
1090 aiov.iov_base = buf;
1091 aiov.iov_len = len;
1092 msg.msg_control = 0;
1093 msg.msg_flags = flags;
1094 error = recvit(td, s, &msg, fromlenaddr);
1095 done2:
1096 return (error);
1097 }
1098
1099 int
sys_recvfrom(struct thread * td,struct recvfrom_args * uap)1100 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1101 {
1102 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1103 uap->flags, uap->from, uap->fromlenaddr));
1104 }
1105
1106
1107 #ifdef COMPAT_OLDSOCK
1108 int
orecvfrom(struct thread * td,struct orecvfrom_args * uap)1109 orecvfrom(struct thread *td, struct orecvfrom_args *uap)
1110 {
1111 return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1112 uap->flags | MSG_COMPAT, uap->from, uap->fromlenaddr));
1113 }
1114 #endif
1115
1116 #ifdef COMPAT_OLDSOCK
1117 int
orecv(struct thread * td,struct orecv_args * uap)1118 orecv(struct thread *td, struct orecv_args *uap)
1119 {
1120 struct msghdr msg;
1121 struct iovec aiov;
1122
1123 msg.msg_name = 0;
1124 msg.msg_namelen = 0;
1125 msg.msg_iov = &aiov;
1126 msg.msg_iovlen = 1;
1127 aiov.iov_base = uap->buf;
1128 aiov.iov_len = uap->len;
1129 msg.msg_control = 0;
1130 msg.msg_flags = uap->flags;
1131 return (recvit(td, uap->s, &msg, NULL));
1132 }
1133
1134 /*
1135 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1136 * overlays the new one, missing only the flags, and with the (old) access
1137 * rights where the control fields are now.
1138 */
1139 int
orecvmsg(struct thread * td,struct orecvmsg_args * uap)1140 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1141 {
1142 struct msghdr msg;
1143 struct iovec *iov;
1144 int error;
1145
1146 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1147 if (error != 0)
1148 return (error);
1149 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1150 if (error != 0)
1151 return (error);
1152 msg.msg_flags = uap->flags | MSG_COMPAT;
1153 msg.msg_iov = iov;
1154 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1155 if (msg.msg_controllen && error == 0)
1156 error = copyout(&msg.msg_controllen,
1157 &uap->msg->msg_accrightslen, sizeof (int));
1158 free(iov, M_IOV);
1159 return (error);
1160 }
1161 #endif
1162
1163 int
sys_recvmsg(struct thread * td,struct recvmsg_args * uap)1164 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1165 {
1166 struct msghdr msg;
1167 struct iovec *uiov, *iov;
1168 int error;
1169
1170 error = copyin(uap->msg, &msg, sizeof (msg));
1171 if (error != 0)
1172 return (error);
1173 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1174 if (error != 0)
1175 return (error);
1176 msg.msg_flags = uap->flags;
1177 #ifdef COMPAT_OLDSOCK
1178 if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1179 msg.msg_flags &= ~MSG_COMPAT;
1180 #endif
1181 uiov = msg.msg_iov;
1182 msg.msg_iov = iov;
1183 error = recvit(td, uap->s, &msg, NULL);
1184 if (error == 0) {
1185 msg.msg_iov = uiov;
1186 error = copyout(&msg, uap->msg, sizeof(msg));
1187 }
1188 free(iov, M_IOV);
1189 return (error);
1190 }
1191
1192 int
sys_shutdown(struct thread * td,struct shutdown_args * uap)1193 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1194 {
1195
1196 return (kern_shutdown(td, uap->s, uap->how));
1197 }
1198
1199 int
kern_shutdown(struct thread * td,int s,int how)1200 kern_shutdown(struct thread *td, int s, int how)
1201 {
1202 struct socket *so;
1203 struct file *fp;
1204 int error;
1205
1206 AUDIT_ARG_FD(s);
1207 error = getsock(td, s, &cap_shutdown_rights, &fp);
1208 if (error == 0) {
1209 so = fp->f_data;
1210 error = soshutdown(so, how);
1211 /*
1212 * Previous versions did not return ENOTCONN, but 0 in
1213 * case the socket was not connected. Some important
1214 * programs like syslogd up to r279016, 2015-02-19,
1215 * still depend on this behavior.
1216 */
1217 if (error == ENOTCONN &&
1218 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1219 error = 0;
1220 fdrop(fp, td);
1221 }
1222 return (error);
1223 }
1224
1225 int
sys_setsockopt(struct thread * td,struct setsockopt_args * uap)1226 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1227 {
1228
1229 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1230 uap->val, UIO_USERSPACE, uap->valsize));
1231 }
1232
1233 int
kern_setsockopt(struct thread * td,int s,int level,int name,const void * val,enum uio_seg valseg,socklen_t valsize)1234 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1235 enum uio_seg valseg, socklen_t valsize)
1236 {
1237 struct socket *so;
1238 struct file *fp;
1239 struct filecaps fcaps;
1240 struct sockopt sopt;
1241 int error;
1242
1243 if (val == NULL && valsize != 0)
1244 return (EFAULT);
1245 if ((int)valsize < 0)
1246 return (EINVAL);
1247
1248 sopt.sopt_dir = SOPT_SET;
1249 sopt.sopt_level = level;
1250 sopt.sopt_name = name;
1251 sopt.sopt_val = __DECONST(void *, val);
1252 sopt.sopt_valsize = valsize;
1253 switch (valseg) {
1254 case UIO_USERSPACE:
1255 sopt.sopt_td = td;
1256 break;
1257 case UIO_SYSSPACE:
1258 sopt.sopt_td = NULL;
1259 break;
1260 default:
1261 panic("kern_setsockopt called with bad valseg");
1262 }
1263
1264 AUDIT_ARG_FD(s);
1265 error = getsock_cap(td, s, &cap_setsockopt_rights, &fp,
1266 &fcaps);
1267 if (error == 0) {
1268 sopt.sopt_rights = &fcaps.fc_rights;
1269 so = fp->f_data;
1270 error = sosetopt(so, &sopt);
1271 fdrop(fp, td);
1272 }
1273 return(error);
1274 }
1275
1276 int
sys_getsockopt(struct thread * td,struct getsockopt_args * uap)1277 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1278 {
1279 socklen_t valsize;
1280 int error;
1281
1282 if (uap->val) {
1283 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1284 if (error != 0)
1285 return (error);
1286 }
1287
1288 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1289 uap->val, UIO_USERSPACE, &valsize);
1290
1291 if (error == 0)
1292 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1293 return (error);
1294 }
1295
1296 /*
1297 * Kernel version of getsockopt.
1298 * optval can be a userland or userspace. optlen is always a kernel pointer.
1299 */
1300 int
kern_getsockopt(struct thread * td,int s,int level,int name,void * val,enum uio_seg valseg,socklen_t * valsize)1301 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1302 enum uio_seg valseg, socklen_t *valsize)
1303 {
1304 struct socket *so;
1305 struct file *fp;
1306 struct filecaps fcaps;
1307 struct sockopt sopt;
1308 int error;
1309
1310 if (val == NULL)
1311 *valsize = 0;
1312 if ((int)*valsize < 0)
1313 return (EINVAL);
1314
1315 sopt.sopt_dir = SOPT_GET;
1316 sopt.sopt_level = level;
1317 sopt.sopt_name = name;
1318 sopt.sopt_val = val;
1319 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1320 switch (valseg) {
1321 case UIO_USERSPACE:
1322 sopt.sopt_td = td;
1323 break;
1324 case UIO_SYSSPACE:
1325 sopt.sopt_td = NULL;
1326 break;
1327 default:
1328 panic("kern_getsockopt called with bad valseg");
1329 }
1330
1331 AUDIT_ARG_FD(s);
1332 error = getsock_cap(td, s, &cap_getsockopt_rights, &fp, &fcaps);
1333 if (error == 0) {
1334 sopt.sopt_rights = &fcaps.fc_rights;
1335 so = fp->f_data;
1336 error = sogetopt(so, &sopt);
1337 *valsize = sopt.sopt_valsize;
1338 fdrop(fp, td);
1339 }
1340 return (error);
1341 }
1342
1343 static int
user_getsockname(struct thread * td,int fdes,struct sockaddr * asa,socklen_t * alen,bool compat)1344 user_getsockname(struct thread *td, int fdes, struct sockaddr *asa,
1345 socklen_t *alen, bool compat)
1346 {
1347 struct sockaddr *sa;
1348 socklen_t len;
1349 int error;
1350
1351 error = copyin(alen, &len, sizeof(len));
1352 if (error != 0)
1353 return (error);
1354
1355 error = kern_getsockname(td, fdes, &sa, &len);
1356 if (error != 0)
1357 return (error);
1358
1359 if (len != 0) {
1360 #ifdef COMPAT_OLDSOCK
1361 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1362 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1363 #endif
1364 error = copyout(sa, asa, len);
1365 }
1366 free(sa, M_SONAME);
1367 if (error == 0)
1368 error = copyout(&len, alen, sizeof(len));
1369 return (error);
1370 }
1371
1372 int
kern_getsockname(struct thread * td,int fd,struct sockaddr ** sa,socklen_t * alen)1373 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1374 socklen_t *alen)
1375 {
1376 struct socket *so;
1377 struct file *fp;
1378 socklen_t len;
1379 int error;
1380
1381 AUDIT_ARG_FD(fd);
1382 error = getsock(td, fd, &cap_getsockname_rights, &fp);
1383 if (error != 0)
1384 return (error);
1385 so = fp->f_data;
1386 *sa = NULL;
1387 CURVNET_SET(so->so_vnet);
1388 error = so->so_proto->pr_sockaddr(so, sa);
1389 CURVNET_RESTORE();
1390 if (error != 0)
1391 goto bad;
1392 if (*sa == NULL)
1393 len = 0;
1394 else
1395 len = MIN(*alen, (*sa)->sa_len);
1396 *alen = len;
1397 #ifdef KTRACE
1398 if (KTRPOINT(td, KTR_STRUCT))
1399 ktrsockaddr(*sa);
1400 #endif
1401 bad:
1402 fdrop(fp, td);
1403 if (error != 0 && *sa != NULL) {
1404 free(*sa, M_SONAME);
1405 *sa = NULL;
1406 }
1407 return (error);
1408 }
1409
1410 int
sys_getsockname(struct thread * td,struct getsockname_args * uap)1411 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1412 {
1413 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, false));
1414 }
1415
1416 #ifdef COMPAT_OLDSOCK
1417 int
ogetsockname(struct thread * td,struct ogetsockname_args * uap)1418 ogetsockname(struct thread *td, struct ogetsockname_args *uap)
1419 {
1420 return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, true));
1421 }
1422 #endif /* COMPAT_OLDSOCK */
1423
1424 static int
user_getpeername(struct thread * td,int fdes,struct sockaddr * asa,socklen_t * alen,bool compat)1425 user_getpeername(struct thread *td, int fdes, struct sockaddr *asa,
1426 socklen_t *alen, bool compat)
1427 {
1428 struct sockaddr *sa;
1429 socklen_t len;
1430 int error;
1431
1432 error = copyin(alen, &len, sizeof (len));
1433 if (error != 0)
1434 return (error);
1435
1436 error = kern_getpeername(td, fdes, &sa, &len);
1437 if (error != 0)
1438 return (error);
1439
1440 if (len != 0) {
1441 #ifdef COMPAT_OLDSOCK
1442 if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1443 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1444 #endif
1445 error = copyout(sa, asa, len);
1446 }
1447 free(sa, M_SONAME);
1448 if (error == 0)
1449 error = copyout(&len, alen, sizeof(len));
1450 return (error);
1451 }
1452
1453 int
kern_getpeername(struct thread * td,int fd,struct sockaddr ** sa,socklen_t * alen)1454 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1455 socklen_t *alen)
1456 {
1457 struct socket *so;
1458 struct file *fp;
1459 socklen_t len;
1460 int error;
1461
1462 AUDIT_ARG_FD(fd);
1463 error = getsock(td, fd, &cap_getpeername_rights, &fp);
1464 if (error != 0)
1465 return (error);
1466 so = fp->f_data;
1467 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1468 error = ENOTCONN;
1469 goto done;
1470 }
1471 *sa = NULL;
1472 CURVNET_SET(so->so_vnet);
1473 error = so->so_proto->pr_peeraddr(so, sa);
1474 CURVNET_RESTORE();
1475 if (error != 0)
1476 goto bad;
1477 if (*sa == NULL)
1478 len = 0;
1479 else
1480 len = MIN(*alen, (*sa)->sa_len);
1481 *alen = len;
1482 #ifdef KTRACE
1483 if (KTRPOINT(td, KTR_STRUCT))
1484 ktrsockaddr(*sa);
1485 #endif
1486 bad:
1487 if (error != 0 && *sa != NULL) {
1488 free(*sa, M_SONAME);
1489 *sa = NULL;
1490 }
1491 done:
1492 fdrop(fp, td);
1493 return (error);
1494 }
1495
1496 int
sys_getpeername(struct thread * td,struct getpeername_args * uap)1497 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1498 {
1499 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, false));
1500 }
1501
1502 #ifdef COMPAT_OLDSOCK
1503 int
ogetpeername(struct thread * td,struct ogetpeername_args * uap)1504 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1505 {
1506 return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, true));
1507 }
1508 #endif /* COMPAT_OLDSOCK */
1509
1510 static int
sockargs(struct mbuf ** mp,char * buf,socklen_t buflen,int type)1511 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1512 {
1513 struct sockaddr *sa;
1514 struct mbuf *m;
1515 int error;
1516
1517 if (buflen > MLEN) {
1518 #ifdef COMPAT_OLDSOCK
1519 if (type == MT_SONAME && buflen <= 112 &&
1520 SV_CURPROC_FLAG(SV_AOUT))
1521 buflen = MLEN; /* unix domain compat. hack */
1522 else
1523 #endif
1524 if (buflen > MCLBYTES)
1525 return (EMSGSIZE);
1526 }
1527 m = m_get2(buflen, M_WAITOK, type, 0);
1528 m->m_len = buflen;
1529 error = copyin(buf, mtod(m, void *), buflen);
1530 if (error != 0)
1531 (void) m_free(m);
1532 else {
1533 *mp = m;
1534 if (type == MT_SONAME) {
1535 sa = mtod(m, struct sockaddr *);
1536
1537 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1538 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1539 SV_CURPROC_FLAG(SV_AOUT))
1540 sa->sa_family = sa->sa_len;
1541 #endif
1542 sa->sa_len = buflen;
1543 }
1544 }
1545 return (error);
1546 }
1547
1548 int
getsockaddr(struct sockaddr ** namp,const struct sockaddr * uaddr,size_t len)1549 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1550 {
1551 struct sockaddr *sa;
1552 int error;
1553
1554 if (len > SOCK_MAXADDRLEN)
1555 return (ENAMETOOLONG);
1556 if (len < offsetof(struct sockaddr, sa_data[0]))
1557 return (EINVAL);
1558 sa = malloc(len, M_SONAME, M_WAITOK);
1559 error = copyin(uaddr, sa, len);
1560 if (error != 0) {
1561 free(sa, M_SONAME);
1562 } else {
1563 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1564 if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1565 SV_CURPROC_FLAG(SV_AOUT))
1566 sa->sa_family = sa->sa_len;
1567 #endif
1568 sa->sa_len = len;
1569 *namp = sa;
1570 }
1571 return (error);
1572 }
1573
1574 /*
1575 * Dispose of externalized rights from an SCM_RIGHTS message. This function
1576 * should be used in error or truncation cases to avoid leaking file descriptors
1577 * into the recipient's (the current thread's) table.
1578 */
1579 void
m_dispose_extcontrolm(struct mbuf * m)1580 m_dispose_extcontrolm(struct mbuf *m)
1581 {
1582 struct cmsghdr *cm;
1583 struct file *fp;
1584 struct thread *td;
1585 socklen_t clen, datalen;
1586 int error, fd, *fds, nfd;
1587
1588 td = curthread;
1589 for (; m != NULL; m = m->m_next) {
1590 if (m->m_type != MT_EXTCONTROL)
1591 continue;
1592 cm = mtod(m, struct cmsghdr *);
1593 clen = m->m_len;
1594 while (clen > 0) {
1595 if (clen < sizeof(*cm))
1596 panic("%s: truncated mbuf %p", __func__, m);
1597 datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1598 if (clen < datalen)
1599 panic("%s: truncated mbuf %p", __func__, m);
1600
1601 if (cm->cmsg_level == SOL_SOCKET &&
1602 cm->cmsg_type == SCM_RIGHTS) {
1603 fds = (int *)CMSG_DATA(cm);
1604 nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1605 sizeof(int);
1606
1607 while (nfd-- > 0) {
1608 fd = *fds++;
1609 error = fget(td, fd, &cap_no_rights,
1610 &fp);
1611 if (error == 0) {
1612 fdclose(td, fp, fd);
1613 fdrop(fp, td);
1614 }
1615 }
1616 }
1617 clen -= datalen;
1618 cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1619 }
1620 m_chtype(m, MT_CONTROL);
1621 }
1622 }
1623