2 * Check decoding of msg_name* fields of struct msghdr array argument
3 * of sendmmsg and recvmmsg syscalls.
5 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #define IOV_MAX1 (IOV_MAX + 1)
46 # define TEST_NAME "mmsg_name"
50 print_msghdr(const struct msghdr *const msg, const int user_msg_namelen)
52 const struct sockaddr_un *const un = msg->msg_name;
53 const int offsetof_sun_path = offsetof(struct sockaddr_un, sun_path);
58 else if (user_msg_namelen < offsetof_sun_path) {
61 printf("{sa_family=AF_UNIX");
62 if (user_msg_namelen > offsetof_sun_path) {
63 int len = user_msg_namelen < (int) msg->msg_namelen ?
64 user_msg_namelen : (int) msg->msg_namelen;
65 len -= offsetof_sun_path;
66 if (len > (int) sizeof(un->sun_path))
67 len = sizeof(un->sun_path);
68 printf(", sun_path=\"%.*s\"", len, un->sun_path);
72 printf(", msg_namelen=");
73 if (user_msg_namelen != (int) msg->msg_namelen) {
74 printf("%d->", user_msg_namelen);
76 printf("%d, msg_iov=[{iov_base=\"%c\", iov_len=1}]"
77 ", msg_iovlen=1, msg_controllen=0, msg_flags=0}",
78 (int) msg->msg_namelen, *(char *) msg->msg_iov[0].iov_base);
82 test_mmsg_name(const int send_fd, const int recv_fd)
84 struct sockaddr_un *const send_addr =
85 tail_alloc(sizeof(*send_addr) * IOV_MAX1);
86 char *const send_buf = tail_alloc(sizeof(*send_buf) * IOV_MAX1);
87 struct iovec *const send_iov = tail_alloc(sizeof(*send_iov) * IOV_MAX1);
88 struct mmsghdr *const send_mh = tail_alloc(sizeof(*send_mh) * IOV_MAX1);
92 for (i = 0; i < IOV_MAX1; ++i) {
93 int sun_len = i + 1 > (int) sizeof(send_addr[i].sun_path)
94 ? (int) sizeof(send_addr[i].sun_path)
97 send_addr[i].sun_family = AF_UNIX;
98 memset(send_addr[i].sun_path, 'a' + i % 26, sun_len);
100 send_buf[i] = '0' + i % 10;
102 send_iov[i].iov_base = &send_buf[i];
103 send_iov[i].iov_len = sizeof(*send_buf);
105 send_mh[i].msg_hdr.msg_iov = &send_iov[i];
106 send_mh[i].msg_hdr.msg_iovlen = 1;
107 send_mh[i].msg_hdr.msg_name = &send_addr[i];
108 send_mh[i].msg_hdr.msg_namelen = i + 1;
109 send_mh[i].msg_hdr.msg_control = 0;
110 send_mh[i].msg_hdr.msg_controllen = 0;
111 send_mh[i].msg_hdr.msg_flags = 0;
114 rc = send_mmsg(send_fd, send_mh, IOV_MAX1, MSG_DONTWAIT);
115 int saved_errno = errno;
117 printf("sendmmsg(%d, [", send_fd);
118 for (i = 0; i < IOV_MAX1; ++i) {
123 || i >= DEFAULT_STRLEN
130 print_msghdr(&send_mh[i].msg_hdr, i + 1);
134 printf("], %u, MSG_DONTWAIT) = %d %s (%m)\n",
135 IOV_MAX1, rc, errno2name());
137 for (i = 0; i < IOV_MAX1; ++i) {
138 send_mh[i].msg_hdr.msg_name = 0;
139 send_mh[i].msg_hdr.msg_namelen = 0;
143 * When recvmmsg is called with a valid descriptor
144 * but inaccessible memory, it causes segfaults on some architectures.
145 * As in these cases we test decoding of failed recvmmsg calls,
146 * it's ok to fail recvmmsg with any reason as long as
147 * it doesn't read that inaccessible memory.
149 rc = send_mmsg(-1, &send_mh[IOV_MAX], 2, MSG_DONTWAIT);
151 printf("sendmmsg(-1, [{msg_hdr=");
152 print_msghdr(&send_mh[IOV_MAX].msg_hdr, 0);
154 printf("}, %p], %u, MSG_DONTWAIT) = %d %s (%m)\n",
155 &send_mh[IOV_MAX1], 2, rc, errno2name());
157 rc = send_mmsg(send_fd, send_mh, IOV_MAX1, MSG_DONTWAIT);
159 perror_msg_and_skip("sendmmsg");
161 printf("sendmmsg(%d, [", send_fd);
162 for (i = 0; i < IOV_MAX1; ++i) {
167 || i >= DEFAULT_STRLEN
174 print_msghdr(&send_mh[i].msg_hdr, 0);
175 printf("%s}", i < rc ? ", msg_len=1" : "");
177 printf("], %u, MSG_DONTWAIT) = %d\n", IOV_MAX1, rc);
179 struct sockaddr_un *const recv_addr =
180 tail_alloc(sizeof(*recv_addr) * IOV_MAX1);
181 char *const recv_buf = tail_alloc(sizeof(*recv_buf) * IOV_MAX1);
182 struct iovec *const recv_iov = tail_alloc(sizeof(*recv_iov) * IOV_MAX1);
183 struct mmsghdr *const recv_mh = tail_alloc(sizeof(*recv_mh) * IOV_MAX1);
185 for (i = 0; i < IOV_MAX1; ++i) {
186 recv_iov[i].iov_base = &recv_buf[i];
187 recv_iov[i].iov_len = sizeof(*recv_buf);
189 recv_mh[i].msg_hdr.msg_name = &recv_addr[i];
190 recv_mh[i].msg_hdr.msg_namelen = i;
191 recv_mh[i].msg_hdr.msg_iov = &recv_iov[i];
192 recv_mh[i].msg_hdr.msg_iovlen = 1;
193 recv_mh[i].msg_hdr.msg_control = 0;
194 recv_mh[i].msg_hdr.msg_controllen = 0;
195 recv_mh[i].msg_hdr.msg_flags = 0;
198 rc = recv_mmsg(recv_fd, recv_mh, IOV_MAX1, MSG_DONTWAIT, 0);
200 perror_msg_and_skip("recvmmsg");
202 printf("recvmmsg(%d, [", recv_fd);
203 for (i = 0; i < rc; ++i) {
207 if (i >= DEFAULT_STRLEN) {
213 print_msghdr(&recv_mh[i].msg_hdr, i);
214 printf(", msg_len=1}");
216 printf("], %u, MSG_DONTWAIT, NULL) = %d\n", IOV_MAX1, rc);
223 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
224 perror_msg_and_skip("socketpair");
226 const struct sockaddr_un un = {
227 .sun_family = AF_UNIX,
228 .sun_path = TEST_NAME "-recvmmsg.test.send.socket"
231 (void) unlink(un.sun_path);
232 if (bind(fds[1], (const void *) &un, sizeof(un)))
233 perror_msg_and_skip("bind");
234 (void) unlink(un.sun_path);
236 test_mmsg_name(fds[1], fds[0]);
238 puts("+++ exited with 0 +++");