11f155d48SKyle Evans /*-
21f155d48SKyle Evans * SPDX-License-Identifier: BSD-2-Clause
31f155d48SKyle Evans *
41f155d48SKyle Evans * Copyright (c) 2024, Klara, Inc.
51f155d48SKyle Evans *
61f155d48SKyle Evans * Redistribution and use in source and binary forms, with or without
71f155d48SKyle Evans * modification, are permitted provided that the following conditions
81f155d48SKyle Evans * are met:
91f155d48SKyle Evans * 1. Redistributions of source code must retain the above copyright
101f155d48SKyle Evans * notice, this list of conditions and the following disclaimer.
111f155d48SKyle Evans * 2. Redistributions in binary form must reproduce the above copyright
121f155d48SKyle Evans * notice, this list of conditions and the following disclaimer in the
131f155d48SKyle Evans * documentation and/or other materials provided with the distribution.
141f155d48SKyle Evans *
151f155d48SKyle Evans * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
161f155d48SKyle Evans * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
171f155d48SKyle Evans * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
181f155d48SKyle Evans * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
191f155d48SKyle Evans * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
201f155d48SKyle Evans * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
211f155d48SKyle Evans * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
221f155d48SKyle Evans * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
231f155d48SKyle Evans * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
241f155d48SKyle Evans * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
251f155d48SKyle Evans * POSSIBILITY OF SUCH DAMAGE.
261f155d48SKyle Evans */
271f155d48SKyle Evans #ifndef _SSP_SOCKET_H_
281f155d48SKyle Evans #define _SSP_SOCKET_H_
291f155d48SKyle Evans
301f155d48SKyle Evans #include <ssp/ssp.h>
311f155d48SKyle Evans
321f155d48SKyle Evans #if __SSP_FORTIFY_LEVEL > 0
331f155d48SKyle Evans
341f155d48SKyle Evans #include <sys/_null.h>
351f155d48SKyle Evans
361f155d48SKyle Evans __BEGIN_DECLS
371f155d48SKyle Evans
381f155d48SKyle Evans __ssp_inline void
__ssp_check_msghdr(struct msghdr * hdr)391f155d48SKyle Evans __ssp_check_msghdr(struct msghdr *hdr)
401f155d48SKyle Evans {
411f155d48SKyle Evans if (__ssp_bos(hdr->msg_name) < hdr->msg_namelen)
421f155d48SKyle Evans __chk_fail();
431f155d48SKyle Evans
441f155d48SKyle Evans __ssp_check_iovec(hdr->msg_iov, hdr->msg_iovlen);
451f155d48SKyle Evans
461f155d48SKyle Evans if (__ssp_bos(hdr->msg_control) < hdr->msg_controllen)
471f155d48SKyle Evans __chk_fail();
481f155d48SKyle Evans }
491f155d48SKyle Evans
501f155d48SKyle Evans __ssp_redirect_raw_impl(int, getpeername, getpeername,
511f155d48SKyle Evans (int fdes, struct sockaddr *__restrict name, socklen_t *__restrict namelen))
521f155d48SKyle Evans {
531f155d48SKyle Evans size_t namesz = __ssp_bos(name);
541f155d48SKyle Evans
551f155d48SKyle Evans if (namesz != (size_t)-1 && namesz < *namelen)
561f155d48SKyle Evans __chk_fail();
571f155d48SKyle Evans
581f155d48SKyle Evans return (__ssp_real(getpeername)(fdes, name, namelen));
591f155d48SKyle Evans }
601f155d48SKyle Evans
611f155d48SKyle Evans __ssp_redirect_raw_impl(int, getsockname, getsockname,
621f155d48SKyle Evans (int fdes, struct sockaddr *__restrict name,
631f155d48SKyle Evans socklen_t *__restrict namelen))
641f155d48SKyle Evans {
651f155d48SKyle Evans size_t namesz = __ssp_bos(name);
661f155d48SKyle Evans
671f155d48SKyle Evans if (namesz != (size_t)-1 && namesz < *namelen)
681f155d48SKyle Evans __chk_fail();
691f155d48SKyle Evans
701f155d48SKyle Evans return (__ssp_real(getsockname)(fdes, name, namelen));
711f155d48SKyle Evans }
721f155d48SKyle Evans
731f155d48SKyle Evans __ssp_redirect(ssize_t, recv, (int __sock, void *__buf, size_t __len,
741f155d48SKyle Evans int __flags), (__sock, __buf, __len, __flags));
751f155d48SKyle Evans
761f155d48SKyle Evans __ssp_redirect_raw_impl(ssize_t, recvfrom, recvfrom,
771f155d48SKyle Evans (int s, void *buf, size_t len, int flags,
781f155d48SKyle Evans struct sockaddr *__restrict from,
791f155d48SKyle Evans socklen_t *__restrict fromlen))
801f155d48SKyle Evans {
811f155d48SKyle Evans if (__ssp_bos(buf) < len)
821f155d48SKyle Evans __chk_fail();
831f155d48SKyle Evans if (from != NULL && __ssp_bos(from) < *fromlen)
841f155d48SKyle Evans __chk_fail();
851f155d48SKyle Evans
861f155d48SKyle Evans return (__ssp_real(recvfrom)(s, buf, len, flags, from, fromlen));
871f155d48SKyle Evans }
881f155d48SKyle Evans
891f155d48SKyle Evans __ssp_redirect_raw_impl(ssize_t, recvmsg, recvmsg,
901f155d48SKyle Evans (int s, struct msghdr *hdr, int flags))
911f155d48SKyle Evans {
921f155d48SKyle Evans __ssp_check_msghdr(hdr);
931f155d48SKyle Evans return (__ssp_real(recvmsg)(s, hdr, flags));
941f155d48SKyle Evans }
951f155d48SKyle Evans
961f155d48SKyle Evans #if __BSD_VISIBLE
971f155d48SKyle Evans struct timespec;
981f155d48SKyle Evans
991f155d48SKyle Evans __ssp_redirect_raw_impl(ssize_t, recvmmsg, recvmmsg,
1001f155d48SKyle Evans (int s, struct mmsghdr *__restrict hdrvec, size_t vlen, int flags,
1011f155d48SKyle Evans const struct timespec *__restrict timeout))
1021f155d48SKyle Evans {
1031f155d48SKyle Evans const size_t vecsz = __ssp_bos(hdrvec);
104*b8730c11SKyle Evans size_t i;
1051f155d48SKyle Evans
1061f155d48SKyle Evans if (vecsz != (size_t)-1 && vecsz / sizeof(*hdrvec) < vlen)
1071f155d48SKyle Evans __chk_fail();
1081f155d48SKyle Evans
109*b8730c11SKyle Evans for (i = 0; i < vlen; i++) {
1101f155d48SKyle Evans __ssp_check_msghdr(&hdrvec[i].msg_hdr);
1111f155d48SKyle Evans }
1121f155d48SKyle Evans
1131f155d48SKyle Evans return (__ssp_real(recvmmsg)(s, hdrvec, vlen, flags, timeout));
1141f155d48SKyle Evans }
1151f155d48SKyle Evans #endif
1161f155d48SKyle Evans
1171f155d48SKyle Evans __END_DECLS
1181f155d48SKyle Evans
1191f155d48SKyle Evans #endif /* __SSP_FORTIFY_LEVEL > 0 */
1201f155d48SKyle Evans #endif /* _SSP_SOCKET_H_ */
121