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