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