1 /* $NetBSD: t_msgctl.c,v 1.4 2014/02/27 00:59:50 joerg 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.4 2014/02/27 00:59:50 joerg Exp $");
36 #include <sys/sysctl.h>
53 #define MSG_KEY 12345689
54 #define MSG_MTYPE_1 0x41
61 static void clean(void);
68 if ((id = msgget(MSG_KEY, 0)) != -1)
69 (void)msgctl(id, IPC_RMID, 0);
72 ATF_TC_WITH_CLEANUP(msgctl_err);
73 ATF_TC_HEAD(msgctl_err, tc)
75 atf_tc_set_md_var(tc, "descr", "Test errors from msgctl(2)");
78 ATF_TC_BODY(msgctl_err, tc)
80 const int cmd[] = { IPC_STAT, IPC_SET, IPC_RMID };
81 struct msqid_ds msgds;
85 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
87 id = msgget(MSG_KEY, IPC_CREAT | 0600);
88 ATF_REQUIRE(id != -1);
91 ATF_REQUIRE_ERRNO(EINVAL, msgctl(id, INT_MAX, &msgds) == -1);
94 ATF_REQUIRE_ERRNO(EFAULT, msgctl(id, IPC_STAT, (void *)-1) == -1);
96 for (i = 0; i < __arraycount(cmd); i++) {
98 ATF_REQUIRE_ERRNO(EINVAL, msgctl(-1, cmd[i], &msgds) == -1);
101 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
104 ATF_TC_CLEANUP(msgctl_err, tc)
109 ATF_TC_WITH_CLEANUP(msgctl_perm);
110 ATF_TC_HEAD(msgctl_perm, tc)
112 atf_tc_set_md_var(tc, "descr", "Test permissions with msgctl(2)");
113 atf_tc_set_md_var(tc, "require.user", "root");
116 ATF_TC_BODY(msgctl_perm, tc)
118 struct msqid_ds msgds;
124 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
126 pw = getpwnam("nobody");
127 id = msgget(MSG_KEY, IPC_CREAT | 0600);
129 ATF_REQUIRE(id != -1);
130 ATF_REQUIRE(pw != NULL);
131 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
134 ATF_REQUIRE(pid >= 0);
138 if (setuid(pw->pw_uid) != 0)
141 msgds.msg_perm.uid = getuid();
142 msgds.msg_perm.gid = getgid();
146 if (msgctl(id, IPC_SET, &msgds) == 0)
152 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
154 if (msgctl(id, IPC_STAT, &msgds) != 0)
157 msgds.msg_qbytes = 1;
159 if (msgctl(id, IPC_SET, &msgds) == 0)
170 if (WIFEXITED(sta) == 0) {
172 if (WEXITSTATUS(sta) == EX_OSERR)
173 atf_tc_fail("system call failed");
175 if (WEXITSTATUS(sta) == EXIT_FAILURE)
176 atf_tc_fail("UID %u manipulated root's "
177 "message queue", pw->pw_uid);
180 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
183 ATF_TC_CLEANUP(msgctl_perm, tc)
188 ATF_TC_WITH_CLEANUP(msgctl_pid);
189 ATF_TC_HEAD(msgctl_pid, tc)
191 atf_tc_set_md_var(tc, "descr", "Test that PIDs are updated");
194 ATF_TC_BODY(msgctl_pid, tc)
196 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
197 struct msqid_ds msgds;
201 id = msgget(MSG_KEY, IPC_CREAT | 0600);
202 ATF_REQUIRE(id != -1);
205 ATF_REQUIRE(pid >= 0);
209 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
216 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
218 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
220 if (pid != msgds.msg_lspid)
221 atf_tc_fail("the PID of last msgsnd(2) was not updated");
224 ATF_REQUIRE(pid >= 0);
228 (void)msgrcv(id, &msg,
229 sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
236 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
238 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
240 if (pid != msgds.msg_lrpid)
241 atf_tc_fail("the PID of last msgrcv(2) was not updated");
243 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
246 ATF_TC_CLEANUP(msgctl_pid, tc)
251 ATF_TC_WITH_CLEANUP(msgctl_set);
252 ATF_TC_HEAD(msgctl_set, tc)
254 atf_tc_set_md_var(tc, "descr", "Test msgctl(2) with IPC_SET");
255 atf_tc_set_md_var(tc, "require.user", "root");
258 ATF_TC_BODY(msgctl_set, tc)
260 struct msqid_ds msgds;
264 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
266 pw = getpwnam("nobody");
267 id = msgget(MSG_KEY, IPC_CREAT | 0600);
269 ATF_REQUIRE(id != -1);
270 ATF_REQUIRE(pw != NULL);
271 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
273 msgds.msg_perm.uid = pw->pw_uid;
275 if (msgctl(id, IPC_SET, &msgds) != 0)
276 atf_tc_fail("root failed to change the UID of message queue");
278 msgds.msg_perm.uid = getuid();
279 msgds.msg_perm.gid = pw->pw_gid;
281 if (msgctl(id, IPC_SET, &msgds) != 0)
282 atf_tc_fail("root failed to change the GID of message queue");
285 * Note: setting the qbytes to zero fails even as root.
287 msgds.msg_qbytes = 1;
288 msgds.msg_perm.gid = getgid();
290 if (msgctl(id, IPC_SET, &msgds) != 0)
291 atf_tc_fail("root failed to change qbytes of message queue");
293 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
296 ATF_TC_CLEANUP(msgctl_set, tc)
301 ATF_TC_WITH_CLEANUP(msgctl_time);
302 ATF_TC_HEAD(msgctl_time, tc)
304 atf_tc_set_md_var(tc, "descr", "Test that access times are updated");
307 ATF_TC_BODY(msgctl_time, tc)
309 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
310 struct msqid_ds msgds;
314 id = msgget(MSG_KEY, IPC_CREAT | 0600);
315 ATF_REQUIRE(id != -1);
319 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
320 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
321 (void)msgctl(id, IPC_STAT, &msgds);
323 if (llabs(t - msgds.msg_stime) > 1)
324 atf_tc_fail("time of last msgsnd(2) was not updated");
326 if (msgds.msg_rtime != 0)
327 atf_tc_fail("time of last msgrcv(2) was updated incorrectly");
331 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
332 (void)msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
333 (void)msgctl(id, IPC_STAT, &msgds);
335 if (llabs(t - msgds.msg_rtime) > 1)
336 atf_tc_fail("time of last msgrcv(2) was not updated");
339 * Note: this is non-zero even after the memset(3).
341 if (msgds.msg_stime == 0)
342 atf_tc_fail("time of last msgsnd(2) was updated incorrectly");
344 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
347 ATF_TC_CLEANUP(msgctl_time, tc)
355 ATF_TP_ADD_TC(tp, msgctl_err);
356 ATF_TP_ADD_TC(tp, msgctl_perm);
357 ATF_TP_ADD_TC(tp, msgctl_pid);
358 ATF_TP_ADD_TC(tp, msgctl_set);
359 ATF_TP_ADD_TC(tp, msgctl_time);
361 return atf_no_error();