• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*\
4  * Test recvmmsg() errors:
5  *
6  * - EBADF  Bad socket file descriptor
7  * - EFAULT Bad message vector address
8  * - EINVAL Bad seconds value for the timeout argument
9  * - EINVAL Bad nanoseconds value for the timeout argument
10  * - EFAULT Bad timeout address
11  */
12 
13 #define _GNU_SOURCE
14 #include "../sendmmsg/sendmmsg.h"
15 
16 static int send_sockfd;
17 static int receive_sockfd;
18 
19 #define VLEN 1
20 
21 static struct mmsghdr *msg;
22 static struct iovec *iov;
23 
24 static void *bad_addr;
25 static int bad_fd = -1;
26 
27 static struct tst_ts ts;
28 
29 struct test_case {
30 	const char *desc;
31 	int *fd;
32 	long tv_sec;
33 	long tv_nsec;
34 	int exp_errno;
35 	struct mmsghdr **msg_vec;
36 	int bad_ts_addr;
37 };
38 
39 static struct test_case tcase[] = {
40 	{
41 		.desc = "bad socket file descriptor",
42 		.fd = &bad_fd,
43 		.exp_errno = EBADF,
44 		.msg_vec = &msg,
45 	},
46 	{
47 		.desc = "bad message vector address",
48 		.fd = &receive_sockfd,
49 		.exp_errno = EFAULT,
50 		.msg_vec = (void *)&bad_addr,
51 	},
52 	{
53 		.desc = "negative seconds in timeout",
54 		.fd = &receive_sockfd,
55 		.tv_sec = -1,
56 		.tv_nsec = 0,
57 		.exp_errno = EINVAL,
58 		.msg_vec = &msg,
59 	},
60 	{
61 		.desc = "overflow in nanoseconds in timeout",
62 		.fd = &receive_sockfd,
63 		.tv_sec = 1,
64 		.tv_nsec = 1000000001,
65 		.exp_errno = EINVAL,
66 		.msg_vec = &msg,
67 	},
68 	{
69 		.desc = "bad timeout address",
70 		.fd = &receive_sockfd,
71 		.exp_errno = EFAULT,
72 		.msg_vec = &msg,
73 		.bad_ts_addr = 1,
74 	}
75 };
76 
verify_recvmmsg(unsigned int i,void * timeout)77 static void verify_recvmmsg(unsigned int i, void *timeout)
78 {
79 	struct time64_variants *tv = &variants[tst_variant];
80 	struct test_case *tc = &tcase[i];
81 
82 	ts.type = tv->ts_type;
83 	tst_ts_set_sec(&ts, tc->tv_sec);
84 	tst_ts_set_nsec(&ts, tc->tv_nsec);
85 
86 	TST_EXP_FAIL2(tv->recvmmsg(*tc->fd, *tc->msg_vec, VLEN, 0, timeout),
87 		      tc->exp_errno, "recvmmsg() %s", tc->desc);
88 }
89 
test_bad_addr(unsigned int i)90 static void test_bad_addr(unsigned int i)
91 {
92 	struct time64_variants *tv = &variants[tst_variant];
93 	void *timeout = bad_addr;
94 	pid_t pid;
95 	int status;
96 
97 	pid = SAFE_FORK();
98 	if (!pid) {
99 		verify_recvmmsg(i, timeout);
100 		exit(0);
101 	}
102 
103 	SAFE_WAITPID(pid, &status, 0);
104 
105 	if (WIFEXITED(status) && !WEXITSTATUS(status))
106 		return;
107 
108 	if (tv->ts_type == TST_LIBC_TIMESPEC &&
109 		WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
110 		tst_res(TPASS, "Child killed by expected signal");
111 		return;
112 	}
113 
114 	tst_res(TFAIL, "Child %s", tst_strstatus(status));
115 }
116 
do_test(unsigned int i)117 static void do_test(unsigned int i)
118 {
119 	struct test_case *tc = &tcase[i];
120 
121 	if (tc->bad_ts_addr)
122 		test_bad_addr(i);
123 	else
124 		verify_recvmmsg(i, tst_ts_get(&ts));
125 }
126 
setup(void)127 static void setup(void)
128 {
129 	struct sockaddr_in addr;
130 	unsigned int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
131 	struct time64_variants *tv = &variants[tst_variant];
132 
133 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
134 
135 	send_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
136 	receive_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
137 
138 	addr.sin_family = AF_INET;
139 	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
140 	addr.sin_port = port;
141 
142 	SAFE_BIND(receive_sockfd, (struct sockaddr *)&addr, sizeof(addr));
143 	SAFE_CONNECT(send_sockfd, (struct sockaddr *)&addr, sizeof(addr));
144 
145 	msg[0].msg_hdr.msg_iov = iov;
146 	msg[0].msg_hdr.msg_iovlen = 1;
147 
148 	TEST(tv->sendmmsg(send_sockfd, msg, 1, 0));
149 
150 	if (TST_RET != 1) {
151 		tst_res(TFAIL | TTERRNO, "sendmmsg() failed");
152 		return;
153 	}
154 
155 	bad_addr = tst_get_bad_addr(NULL);
156 }
157 
cleanup(void)158 static void cleanup(void)
159 {
160 	if (send_sockfd > 0)
161 		SAFE_CLOSE(send_sockfd);
162 
163 	if (receive_sockfd > 0)
164 		SAFE_CLOSE(receive_sockfd);
165 }
166 
167 static struct tst_test test = {
168 	.test = do_test,
169 	.tcnt = ARRAY_SIZE(tcase),
170 	.setup = setup,
171 	.cleanup = cleanup,
172 	.test_variants = ARRAY_SIZE(variants),
173 	.forks_child = 1,
174 	.bufs = (struct tst_buffers []) {
175 		{&iov, .iov_sizes = (int[]){1, -1}},
176 		{&msg, .size = VLEN * sizeof(*msg)},
177 		{},
178 	}
179 };
180