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>
6 * Copyright (c) 2016-2018 The strace developers.
9 * SPDX-License-Identifier: GPL-2.0-or-later
24 #define IOV_MAX1 (IOV_MAX + 1)
27 # define TEST_NAME "mmsg_name"
31 print_msghdr(const struct msghdr *const msg, const int user_msg_namelen)
33 const struct sockaddr_un *const un = msg->msg_name;
34 const int offsetof_sun_path = offsetof(struct sockaddr_un, sun_path);
39 else if (user_msg_namelen < offsetof_sun_path) {
42 printf("{sa_family=AF_UNIX");
43 if (user_msg_namelen > offsetof_sun_path) {
44 int len = user_msg_namelen < (int) msg->msg_namelen ?
45 user_msg_namelen : (int) msg->msg_namelen;
46 len -= offsetof_sun_path;
47 if (len > (int) sizeof(un->sun_path))
48 len = sizeof(un->sun_path);
49 printf(", sun_path=\"%.*s\"", len, un->sun_path);
53 printf(", msg_namelen=");
54 if (user_msg_namelen != (int) msg->msg_namelen) {
55 printf("%d->", user_msg_namelen);
57 printf("%d, msg_iov=[{iov_base=\"%c\", iov_len=1}]"
58 ", msg_iovlen=1, msg_controllen=0, msg_flags=0}",
59 (int) msg->msg_namelen, *(char *) msg->msg_iov[0].iov_base);
63 test_mmsg_name(const int send_fd, const int recv_fd)
65 struct sockaddr_un *const send_addr =
66 tail_alloc(sizeof(*send_addr) * IOV_MAX1);
67 char *const send_buf = tail_alloc(sizeof(*send_buf) * IOV_MAX1);
68 struct iovec *const send_iov = tail_alloc(sizeof(*send_iov) * IOV_MAX1);
69 struct mmsghdr *const send_mh = tail_alloc(sizeof(*send_mh) * IOV_MAX1);
73 for (i = 0; i < IOV_MAX1; ++i) {
74 int sun_len = i + 1 > (int) sizeof(send_addr[i].sun_path)
75 ? (int) sizeof(send_addr[i].sun_path)
78 send_addr[i].sun_family = AF_UNIX;
79 memset(send_addr[i].sun_path, 'a' + i % 26, sun_len);
81 send_buf[i] = '0' + i % 10;
83 send_iov[i].iov_base = &send_buf[i];
84 send_iov[i].iov_len = sizeof(*send_buf);
86 send_mh[i].msg_hdr.msg_iov = &send_iov[i];
87 send_mh[i].msg_hdr.msg_iovlen = 1;
88 send_mh[i].msg_hdr.msg_name = &send_addr[i];
89 send_mh[i].msg_hdr.msg_namelen = i + 1;
90 send_mh[i].msg_hdr.msg_control = 0;
91 send_mh[i].msg_hdr.msg_controllen = 0;
92 send_mh[i].msg_hdr.msg_flags = 0;
95 rc = send_mmsg(send_fd, send_mh, IOV_MAX1, MSG_DONTWAIT);
96 int saved_errno = errno;
98 printf("sendmmsg(%d, [", send_fd);
99 for (i = 0; i < IOV_MAX1; ++i) {
104 || i >= DEFAULT_STRLEN
111 print_msghdr(&send_mh[i].msg_hdr, i + 1);
115 printf("], %u, MSG_DONTWAIT) = %d %s (%m)\n",
116 IOV_MAX1, rc, errno2name());
118 for (i = 0; i < IOV_MAX1; ++i) {
119 send_mh[i].msg_hdr.msg_name = 0;
120 send_mh[i].msg_hdr.msg_namelen = 0;
124 * When recvmmsg is called with a valid descriptor
125 * but inaccessible memory, it causes segfaults on some architectures.
126 * As in these cases we test decoding of failed recvmmsg calls,
127 * it's ok to fail recvmmsg with any reason as long as
128 * it doesn't read that inaccessible memory.
130 rc = send_mmsg(-1, &send_mh[IOV_MAX], 2, MSG_DONTWAIT);
132 printf("sendmmsg(-1, [{msg_hdr=");
133 print_msghdr(&send_mh[IOV_MAX].msg_hdr, 0);
135 printf("}, ... /* %p */], %u, MSG_DONTWAIT) = %d %s (%m)\n",
136 &send_mh[IOV_MAX1], 2, rc, errno2name());
138 rc = send_mmsg(send_fd, send_mh, IOV_MAX1, MSG_DONTWAIT);
140 perror_msg_and_skip("sendmmsg");
142 printf("sendmmsg(%d, [", send_fd);
143 for (i = 0; i < IOV_MAX1; ++i) {
148 || i >= DEFAULT_STRLEN
155 print_msghdr(&send_mh[i].msg_hdr, 0);
156 printf("%s}", i < rc ? ", msg_len=1" : "");
158 printf("], %u, MSG_DONTWAIT) = %d\n", IOV_MAX1, rc);
160 struct sockaddr_un *const recv_addr =
161 tail_alloc(sizeof(*recv_addr) * IOV_MAX1);
162 char *const recv_buf = tail_alloc(sizeof(*recv_buf) * IOV_MAX1);
163 struct iovec *const recv_iov = tail_alloc(sizeof(*recv_iov) * IOV_MAX1);
164 struct mmsghdr *const recv_mh = tail_alloc(sizeof(*recv_mh) * IOV_MAX1);
166 for (i = 0; i < IOV_MAX1; ++i) {
167 recv_iov[i].iov_base = &recv_buf[i];
168 recv_iov[i].iov_len = sizeof(*recv_buf);
170 recv_mh[i].msg_hdr.msg_name = &recv_addr[i];
171 recv_mh[i].msg_hdr.msg_namelen = i;
172 recv_mh[i].msg_hdr.msg_iov = &recv_iov[i];
173 recv_mh[i].msg_hdr.msg_iovlen = 1;
174 recv_mh[i].msg_hdr.msg_control = 0;
175 recv_mh[i].msg_hdr.msg_controllen = 0;
176 recv_mh[i].msg_hdr.msg_flags = 0;
179 rc = recv_mmsg(recv_fd, recv_mh, IOV_MAX1, MSG_DONTWAIT, 0);
181 perror_msg_and_skip("recvmmsg");
183 printf("recvmmsg(%d, [", recv_fd);
184 for (i = 0; i < rc; ++i) {
188 if (i >= DEFAULT_STRLEN) {
194 print_msghdr(&recv_mh[i].msg_hdr, i);
195 printf(", msg_len=1}");
197 printf("], %u, MSG_DONTWAIT, NULL) = %d\n", IOV_MAX1, rc);
204 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
205 perror_msg_and_skip("socketpair");
207 const struct sockaddr_un un = {
208 .sun_family = AF_UNIX,
209 .sun_path = TEST_NAME "-recvmmsg.test.send.socket"
212 (void) unlink(un.sun_path);
213 if (bind(fds[1], (const void *) &un, sizeof(un)))
214 perror_msg_and_skip("bind");
215 (void) unlink(un.sun_path);
217 test_mmsg_name(fds[1], fds[0]);
219 puts("+++ exited with 0 +++");