xref: /freebsd/include/ssp/socket.h (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2024, Klara, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef _SSP_SOCKET_H_
28 #define _SSP_SOCKET_H_
29 
30 #include <ssp/ssp.h>
31 
32 #if __SSP_FORTIFY_LEVEL > 0
33 
34 #include <sys/_null.h>
35 
36 __BEGIN_DECLS
37 
38 __ssp_inline void
39 __ssp_check_msghdr(struct msghdr *hdr)
40 {
41 	if (__ssp_bos(hdr->msg_name) < hdr->msg_namelen)
42 		__chk_fail();
43 
44 	__ssp_check_iovec(hdr->msg_iov, hdr->msg_iovlen);
45 
46 	if (__ssp_bos(hdr->msg_control) < hdr->msg_controllen)
47 		__chk_fail();
48 }
49 
50 __ssp_redirect_raw_impl(int, getpeername, getpeername,
51     (int fdes, struct sockaddr *__restrict name, socklen_t *__restrict namelen))
52 {
53 	size_t namesz = __ssp_bos(name);
54 
55 	if (namesz != (size_t)-1 && namesz < *namelen)
56 		__chk_fail();
57 
58 	return (__ssp_real(getpeername)(fdes, name, namelen));
59 }
60 
61 __ssp_redirect_raw_impl(int, getsockname, getsockname,
62     (int fdes, struct sockaddr *__restrict name,
63     socklen_t *__restrict namelen))
64 {
65 	size_t namesz = __ssp_bos(name);
66 
67 	if (namesz != (size_t)-1 && namesz < *namelen)
68 		__chk_fail();
69 
70 	return (__ssp_real(getsockname)(fdes, name, namelen));
71 }
72 
73 __ssp_redirect(ssize_t, recv, (int __sock, void *__buf, size_t __len,
74     int __flags), (__sock, __buf, __len, __flags));
75 
76 __ssp_redirect_raw_impl(ssize_t, recvfrom, recvfrom,
77     (int s, void *buf, size_t len, int flags,
78     struct sockaddr *__restrict from,
79     socklen_t *__restrict fromlen))
80 {
81 	if (__ssp_bos(buf) < len)
82 		__chk_fail();
83 	if (from != NULL && __ssp_bos(from) < *fromlen)
84 		__chk_fail();
85 
86 	return (__ssp_real(recvfrom)(s, buf, len, flags, from, fromlen));
87 }
88 
89 __ssp_redirect_raw_impl(ssize_t, recvmsg, recvmsg,
90     (int s, struct msghdr *hdr, int flags))
91 {
92 	__ssp_check_msghdr(hdr);
93 	return (__ssp_real(recvmsg)(s, hdr, flags));
94 }
95 
96 #if __BSD_VISIBLE
97 struct timespec;
98 
99 __ssp_redirect_raw_impl(ssize_t, recvmmsg, recvmmsg,
100     (int s, struct mmsghdr *__restrict hdrvec, size_t vlen, int flags,
101     const struct timespec *__restrict timeout))
102 {
103 	const size_t vecsz = __ssp_bos(hdrvec);
104 	size_t i;
105 
106 	if (vecsz != (size_t)-1 && vecsz / sizeof(*hdrvec) < vlen)
107 		__chk_fail();
108 
109 	for (i = 0; i < vlen; i++) {
110 		__ssp_check_msghdr(&hdrvec[i].msg_hdr);
111 	}
112 
113 	return (__ssp_real(recvmmsg)(s, hdrvec, vlen, flags, timeout));
114 }
115 #endif
116 
117 __END_DECLS
118 
119 #endif /* __SSP_FORTIFY_LEVEL > 0 */
120 #endif /* _SSP_SOCKET_H_ */
121