1 /* $NetBSD: t_msgsnd.c,v 1.4 2017/10/08 08:31:05 kre Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_msgsnd.c,v 1.4 2017/10/08 08:31:05 kre Exp $");
33
34 #include <sys/msg.h>
35 #include <sys/stat.h>
36 #include <sys/sysctl.h>
37 #include <sys/wait.h>
38
39 #include <atf-c.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <pwd.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <time.h>
49 #include <unistd.h>
50
51 #define MSG_KEY 1234
52 #define MSG_MTYPE_1 0x41
53 #define MSG_MTYPE_2 0x42
54 #define MSG_MTYPE_3 0x43
55
56 struct msg {
57 long mtype;
58 char buf[3];
59 };
60
61 static void clean(void);
62
63 static void
clean(void)64 clean(void)
65 {
66 int id;
67
68 if ((id = msgget(MSG_KEY, 0)) != -1)
69 (void)msgctl(id, IPC_RMID, 0);
70 }
71
72 ATF_TC_WITH_CLEANUP(msgsnd_block);
ATF_TC_HEAD(msgsnd_block,tc)73 ATF_TC_HEAD(msgsnd_block, tc)
74 {
75 atf_tc_set_md_var(tc, "descr", "Test that msgsnd(2) blocks");
76 atf_tc_set_md_var(tc, "timeout", "10");
77 }
78
ATF_TC_BODY(msgsnd_block,tc)79 ATF_TC_BODY(msgsnd_block, tc)
80 {
81 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
82 int id, sta;
83 pid_t pid;
84
85 id = msgget(MSG_KEY, IPC_CREAT | 0600);
86 ATF_REQUIRE(id != -1);
87
88 pid = fork();
89 ATF_REQUIRE(pid >= 0);
90
91 if (pid == 0) {
92
93 /*
94 * Enqueue messages until some limit (e.g. the maximum
95 * number of messages in the queue or the maximum number
96 * of bytes in the queue) is reached. After this the call
97 * should block when the IPC_NOWAIT is not set.
98 */
99 for (;;) {
100
101 if (msgsnd(id, &msg, sizeof(struct msg), 0) < 0)
102 _exit(EXIT_FAILURE);
103 }
104 }
105
106 (void)sleep(2);
107 (void)kill(pid, SIGKILL);
108 (void)wait(&sta);
109
110 if (WIFEXITED(sta) != 0 || WIFSIGNALED(sta) == 0)
111 atf_tc_fail("msgsnd(2) did not block");
112
113 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
114 }
115
ATF_TC_CLEANUP(msgsnd_block,tc)116 ATF_TC_CLEANUP(msgsnd_block, tc)
117 {
118 clean();
119 }
120
121 ATF_TC_WITH_CLEANUP(msgsnd_count);
ATF_TC_HEAD(msgsnd_count,tc)122 ATF_TC_HEAD(msgsnd_count, tc)
123 {
124 atf_tc_set_md_var(tc, "descr",
125 "Test that msgsnd(2) increments the amount of "
126 "message in the queue, as given by msgctl(2)");
127 atf_tc_set_md_var(tc, "timeout", "10");
128 }
129
ATF_TC_BODY(msgsnd_count,tc)130 ATF_TC_BODY(msgsnd_count, tc)
131 {
132 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
133 struct msqid_ds ds;
134 size_t i = 0;
135 int id, rv;
136
137 id = msgget(MSG_KEY, IPC_CREAT | 0600);
138 ATF_REQUIRE(id != -1);
139
140 for (;;) {
141
142 errno = 0;
143 rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
144
145 if (rv == 0) {
146 i++;
147 continue;
148 }
149
150 if (rv == -1 && errno == EAGAIN)
151 break;
152
153 atf_tc_fail("failed to enqueue a message");
154 }
155
156 (void)memset(&ds, 0, sizeof(struct msqid_ds));
157 (void)msgctl(id, IPC_STAT, &ds);
158
159 if (ds.msg_qnum != i)
160 atf_tc_fail("incorrect message count");
161
162 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
163 }
164
ATF_TC_CLEANUP(msgsnd_count,tc)165 ATF_TC_CLEANUP(msgsnd_count, tc)
166 {
167 clean();
168 }
169
170 ATF_TC_WITH_CLEANUP(msgsnd_err);
ATF_TC_HEAD(msgsnd_err,tc)171 ATF_TC_HEAD(msgsnd_err, tc)
172 {
173 atf_tc_set_md_var(tc, "descr", "Test errors from msgsnd(2)");
174 }
175
ATF_TC_BODY(msgsnd_err,tc)176 ATF_TC_BODY(msgsnd_err, tc)
177 {
178 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
179 int id;
180
181 id = msgget(MSG_KEY, IPC_CREAT | 0600);
182 ATF_REQUIRE(id != -1);
183
184 errno = 0;
185
186 ATF_REQUIRE_ERRNO(EFAULT, msgsnd(id, (void *)-1,
187 sizeof(struct msg), IPC_NOWAIT) == -1);
188
189 errno = 0;
190
191 ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
192 sizeof(struct msg), IPC_NOWAIT) == -1);
193
194 errno = 0;
195
196 ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
197 SSIZE_MAX, IPC_NOWAIT) == -1);
198
199 errno = 0;
200 msg.mtype = 0;
201
202 ATF_REQUIRE_ERRNO(EINVAL, msgsnd(id, &msg,
203 sizeof(struct msg), IPC_NOWAIT) == -1);
204
205 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
206 }
207
ATF_TC_CLEANUP(msgsnd_err,tc)208 ATF_TC_CLEANUP(msgsnd_err, tc)
209 {
210 clean();
211 }
212
213 ATF_TC_WITH_CLEANUP(msgsnd_nonblock);
ATF_TC_HEAD(msgsnd_nonblock,tc)214 ATF_TC_HEAD(msgsnd_nonblock, tc)
215 {
216 atf_tc_set_md_var(tc, "descr", "Test msgsnd(2) with IPC_NOWAIT");
217 atf_tc_set_md_var(tc, "timeout", "10");
218 }
219
ATF_TC_BODY(msgsnd_nonblock,tc)220 ATF_TC_BODY(msgsnd_nonblock, tc)
221 {
222 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
223 int id, rv, sta;
224 pid_t pid;
225
226 id = msgget(MSG_KEY, IPC_CREAT | 0600);
227 ATF_REQUIRE(id != -1);
228
229 pid = fork();
230 ATF_REQUIRE(pid >= 0);
231
232 if (pid == 0) {
233
234 for (;;) {
235
236 errno = 0;
237 rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
238
239 if (rv == -1 && errno == EAGAIN)
240 _exit(EXIT_SUCCESS);
241 }
242 }
243
244 (void)sleep(2);
245 (void)kill(pid, SIGKILL);
246 (void)wait(&sta);
247
248 if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
249 atf_tc_fail("msgsnd(2) blocked with IPC_NOWAIT");
250
251 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
252 }
253
ATF_TC_CLEANUP(msgsnd_nonblock,tc)254 ATF_TC_CLEANUP(msgsnd_nonblock, tc)
255 {
256 clean();
257 }
258
259 ATF_TC_WITH_CLEANUP(msgsnd_perm);
ATF_TC_HEAD(msgsnd_perm,tc)260 ATF_TC_HEAD(msgsnd_perm, tc)
261 {
262 atf_tc_set_md_var(tc, "descr", "Test permissions with msgsnd(2)");
263 atf_tc_set_md_var(tc, "require.user", "root");
264 }
265
ATF_TC_BODY(msgsnd_perm,tc)266 ATF_TC_BODY(msgsnd_perm, tc)
267 {
268 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
269 struct passwd *pw;
270 int id, sta;
271 pid_t pid;
272 uid_t uid;
273
274 pw = getpwnam("nobody");
275 id = msgget(MSG_KEY, IPC_CREAT | 0600);
276
277 ATF_REQUIRE(id != -1);
278 ATF_REQUIRE(pw != NULL);
279
280 uid = pw->pw_uid;
281 ATF_REQUIRE(uid != 0);
282
283 pid = fork();
284 ATF_REQUIRE(pid >= 0);
285
286 if (pid == 0) {
287
288 /*
289 * Try to enqueue a message to the queue
290 * created by root as RW for owner only.
291 */
292 if (setuid(uid) != 0)
293 _exit(EX_OSERR);
294
295 id = msgget(MSG_KEY, 0);
296
297 if (id == -1)
298 _exit(EX_OSERR);
299
300 errno = 0;
301
302 if (msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0)
303 _exit(EXIT_FAILURE);
304
305 if (errno != EACCES)
306 _exit(EXIT_FAILURE);
307
308 _exit(EXIT_SUCCESS);
309 }
310
311 (void)wait(&sta);
312
313 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
314
315 if (errno == EX_OSERR)
316 atf_tc_fail("system call failed");
317
318 atf_tc_fail("UID %u enqueued message to root's queue", uid);
319 }
320
321 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
322 }
323
ATF_TC_CLEANUP(msgsnd_perm,tc)324 ATF_TC_CLEANUP(msgsnd_perm, tc)
325 {
326 clean();
327 }
328
329 static volatile int sig_caught;
330
331 static void
sigsys_handler(int signum)332 sigsys_handler(int signum)
333 {
334
335 sig_caught = signum;
336 }
337
338 static int
no_kernel_sysvmsg(void)339 no_kernel_sysvmsg(void)
340 {
341 int id;
342 void (*osig)(int);
343
344 sig_caught = 0;
345 osig = signal(SIGSYS, sigsys_handler);
346 id = msgget(MSG_KEY, IPC_CREAT | 0600);
347 if (sig_caught || id == -1)
348 return 1;
349
350 (void)msgctl(id, IPC_RMID, 0);
351 (void)signal(SIGSYS, osig);
352
353 return 0;
354 }
355
356 ATF_TC(msgsnd_query);
ATF_TC_HEAD(msgsnd_query,tc)357 ATF_TC_HEAD(msgsnd_query, tc)
358 {
359 atf_tc_set_md_var(tc, "descr", "Skip msgsnd_* tests - no SYSVMSG");
360 }
ATF_TC_BODY(msgsnd_query,tc)361 ATF_TC_BODY(msgsnd_query, tc)
362 {
363 atf_tc_skip("No SYSVMSG in kernel");
364 }
365
ATF_TP_ADD_TCS(tp)366 ATF_TP_ADD_TCS(tp)
367 {
368
369 if (no_kernel_sysvmsg()) {
370 ATF_TP_ADD_TC(tp, msgsnd_query);
371 } else {
372 ATF_TP_ADD_TC(tp, msgsnd_block);
373 ATF_TP_ADD_TC(tp, msgsnd_count);
374 ATF_TP_ADD_TC(tp, msgsnd_err);
375 ATF_TP_ADD_TC(tp, msgsnd_nonblock);
376 ATF_TP_ADD_TC(tp, msgsnd_perm);
377 }
378
379 return atf_no_error();
380 }
381