1 /* $NetBSD: t_msgctl.c,v 1.5 2017/01/13 20:44:45 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_msgctl.c,v 1.5 2017/01/13 20:44:45 christos Exp $");
36 #include <sys/sysctl.h>
50 #define MSG_KEY 12345689
51 #define MSG_MTYPE_1 0x41
58 static void clean(void);
65 if ((id = msgget(MSG_KEY, 0)) != -1)
66 (void)msgctl(id, IPC_RMID, 0);
69 ATF_TC_WITH_CLEANUP(msgctl_err);
70 ATF_TC_HEAD(msgctl_err, tc)
72 atf_tc_set_md_var(tc, "descr", "Test errors from msgctl(2)");
75 ATF_TC_BODY(msgctl_err, tc)
77 const int cmd[] = { IPC_STAT, IPC_SET, IPC_RMID };
78 struct msqid_ds msgds;
82 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
84 id = msgget(MSG_KEY, IPC_CREAT | 0600);
85 ATF_REQUIRE(id != -1);
88 ATF_REQUIRE_ERRNO(EINVAL, msgctl(id, INT_MAX, &msgds) == -1);
91 ATF_REQUIRE_ERRNO(EFAULT, msgctl(id, IPC_STAT, (void *)-1) == -1);
93 for (i = 0; i < __arraycount(cmd); i++) {
95 ATF_REQUIRE_ERRNO(EINVAL, msgctl(-1, cmd[i], &msgds) == -1);
98 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
101 ATF_TC_CLEANUP(msgctl_err, tc)
106 ATF_TC_WITH_CLEANUP(msgctl_perm);
107 ATF_TC_HEAD(msgctl_perm, tc)
109 atf_tc_set_md_var(tc, "descr", "Test permissions with msgctl(2)");
110 atf_tc_set_md_var(tc, "require.user", "root");
113 ATF_TC_BODY(msgctl_perm, tc)
115 struct msqid_ds msgds;
121 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
123 pw = getpwnam("nobody");
124 id = msgget(MSG_KEY, IPC_CREAT | 0600);
126 ATF_REQUIRE(id != -1);
127 ATF_REQUIRE(pw != NULL);
128 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
131 ATF_REQUIRE(pid >= 0);
135 if (setuid(pw->pw_uid) != 0)
138 msgds.msg_perm.uid = getuid();
139 msgds.msg_perm.gid = getgid();
143 if (msgctl(id, IPC_SET, &msgds) == 0)
149 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
151 if (msgctl(id, IPC_STAT, &msgds) != 0)
154 msgds.msg_qbytes = 1;
156 if (msgctl(id, IPC_SET, &msgds) == 0)
167 if (WIFEXITED(sta) == 0) {
169 if (WEXITSTATUS(sta) == EX_OSERR)
170 atf_tc_fail("system call failed");
172 if (WEXITSTATUS(sta) == EXIT_FAILURE)
173 atf_tc_fail("UID %u manipulated root's "
174 "message queue", pw->pw_uid);
177 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
180 ATF_TC_CLEANUP(msgctl_perm, tc)
185 ATF_TC_WITH_CLEANUP(msgctl_pid);
186 ATF_TC_HEAD(msgctl_pid, tc)
188 atf_tc_set_md_var(tc, "descr", "Test that PIDs are updated");
191 ATF_TC_BODY(msgctl_pid, tc)
193 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
194 struct msqid_ds msgds;
198 id = msgget(MSG_KEY, IPC_CREAT | 0600);
199 ATF_REQUIRE(id != -1);
202 ATF_REQUIRE(pid >= 0);
207 (void)msgsnd(id, &msg, sizeof(msg.buf), IPC_NOWAIT);
209 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
217 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
219 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
221 if (pid != msgds.msg_lspid)
222 atf_tc_fail("the PID of last msgsnd(2) was not updated");
225 ATF_REQUIRE(pid >= 0);
229 (void)msgrcv(id, &msg,
230 sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
237 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
239 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
241 if (pid != msgds.msg_lrpid)
242 atf_tc_fail("the PID of last msgrcv(2) was not updated");
244 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
247 ATF_TC_CLEANUP(msgctl_pid, tc)
252 ATF_TC_WITH_CLEANUP(msgctl_set);
253 ATF_TC_HEAD(msgctl_set, tc)
255 atf_tc_set_md_var(tc, "descr", "Test msgctl(2) with IPC_SET");
256 atf_tc_set_md_var(tc, "require.user", "root");
259 ATF_TC_BODY(msgctl_set, tc)
261 struct msqid_ds msgds;
265 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
267 pw = getpwnam("nobody");
268 id = msgget(MSG_KEY, IPC_CREAT | 0600);
270 ATF_REQUIRE(id != -1);
271 ATF_REQUIRE(pw != NULL);
272 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
274 msgds.msg_perm.uid = pw->pw_uid;
276 if (msgctl(id, IPC_SET, &msgds) != 0)
277 atf_tc_fail("root failed to change the UID of message queue");
279 msgds.msg_perm.uid = getuid();
280 msgds.msg_perm.gid = pw->pw_gid;
282 if (msgctl(id, IPC_SET, &msgds) != 0)
283 atf_tc_fail("root failed to change the GID of message queue");
286 * Note: setting the qbytes to zero fails even as root.
288 msgds.msg_qbytes = 1;
289 msgds.msg_perm.gid = getgid();
291 if (msgctl(id, IPC_SET, &msgds) != 0)
292 atf_tc_fail("root failed to change qbytes of message queue");
294 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
297 ATF_TC_CLEANUP(msgctl_set, tc)
302 ATF_TC_WITH_CLEANUP(msgctl_time);
303 ATF_TC_HEAD(msgctl_time, tc)
305 atf_tc_set_md_var(tc, "descr", "Test that access times are updated");
308 ATF_TC_BODY(msgctl_time, tc)
310 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
311 struct msqid_ds msgds;
315 id = msgget(MSG_KEY, IPC_CREAT | 0600);
316 ATF_REQUIRE(id != -1);
320 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
322 (void)msgsnd(id, &msg, sizeof(msg.buf), IPC_NOWAIT);
324 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
326 (void)msgctl(id, IPC_STAT, &msgds);
328 if (llabs(t - msgds.msg_stime) > 1)
329 atf_tc_fail("time of last msgsnd(2) was not updated");
331 if (msgds.msg_rtime != 0)
332 atf_tc_fail("time of last msgrcv(2) was updated incorrectly");
336 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
337 (void)msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
338 (void)msgctl(id, IPC_STAT, &msgds);
340 if (llabs(t - msgds.msg_rtime) > 1)
341 atf_tc_fail("time of last msgrcv(2) was not updated");
344 * Note: this is non-zero even after the memset(3).
346 if (msgds.msg_stime == 0)
347 atf_tc_fail("time of last msgsnd(2) was updated incorrectly");
349 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
352 ATF_TC_CLEANUP(msgctl_time, tc)
360 ATF_TP_ADD_TC(tp, msgctl_err);
361 ATF_TP_ADD_TC(tp, msgctl_perm);
362 ATF_TP_ADD_TC(tp, msgctl_pid);
363 ATF_TP_ADD_TC(tp, msgctl_set);
364 ATF_TP_ADD_TC(tp, msgctl_time);
366 return atf_no_error();