xref: /freebsd/tests/sys/audit/inter-process.c (revision 964219664dcec4198441910904fb9064569d174d)
1 /*-
2  * Copyright (c) 2018 Aniket Pandey
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include <sys/types.h>
29 #include <sys/ipc.h>
30 #include <sys/mman.h>
31 #include <sys/msg.h>
32 #include <sys/shm.h>
33 #define _WANT_SEMUN
34 #include <sys/sem.h>
35 #include <sys/stat.h>
36 
37 #include <atf-c.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 
41 #include "utils.h"
42 #define BUFFSIZE 80
43 
44 struct msgstr {
45 	long int	 mtype;
46 	char		 mtext[BUFFSIZE];
47 };
48 typedef struct msgstr msgstr_t;
49 
50 
51 static pid_t pid;
52 static int msqid, shmid, semid;
53 static union semun semarg;
54 static struct pollfd fds[1];
55 static struct msqid_ds msgbuff;
56 static struct shmid_ds shmbuff;
57 static struct semid_ds sembuff;
58 static char ipcregex[BUFFSIZE];
59 static const char *auclass = "ip";
60 static unsigned short semvals[BUFFSIZE];
61 
62 
63 ATF_TC_WITH_CLEANUP(msgget_success);
64 ATF_TC_HEAD(msgget_success, tc)
65 {
66 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
67 					"msgget(2) call");
68 }
69 
70 ATF_TC_BODY(msgget_success, tc)
71 {
72 	FILE *pipefd = setup(fds, auclass);
73 	/* Create a message queue and obtain the corresponding identifier */
74 	ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
75 	/* Check the presence of message queue ID in audit record */
76 	snprintf(ipcregex, sizeof(ipcregex),
77 			"msgget.*return,success,%d", msqid);
78 	check_audit(fds, ipcregex, pipefd);
79 
80 	/* Destroy the message queue with ID = msqid */
81 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
82 }
83 
84 ATF_TC_CLEANUP(msgget_success, tc)
85 {
86 	cleanup();
87 }
88 
89 
90 ATF_TC_WITH_CLEANUP(msgget_failure);
91 ATF_TC_HEAD(msgget_failure, tc)
92 {
93 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
94 					"msgget(2) call");
95 }
96 
97 ATF_TC_BODY(msgget_failure, tc)
98 {
99 	const char *regex = "msgget.*return,failure.*No such file or directory";
100 	FILE *pipefd = setup(fds, auclass);
101 	ATF_REQUIRE_EQ(-1, msgget((key_t)(-1), 0));
102 	check_audit(fds, regex, pipefd);
103 }
104 
105 ATF_TC_CLEANUP(msgget_failure, tc)
106 {
107 	cleanup();
108 }
109 
110 
111 ATF_TC_WITH_CLEANUP(msgsnd_success);
112 ATF_TC_HEAD(msgsnd_success, tc)
113 {
114 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
115 					"msgsnd(2) call");
116 }
117 
118 ATF_TC_BODY(msgsnd_success, tc)
119 {
120 	/* Create a message queue and obtain the corresponding identifier */
121 	ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
122 
123 	/* Initialize a msgstr_t structure to store message */
124 	msgstr_t msg;
125 	msg.mtype = 1;
126 	memset(msg.mtext, 0, BUFFSIZE);
127 
128 	/* Check the presence of message queue ID in audit record */
129 	snprintf(ipcregex, sizeof(ipcregex),
130 		"msgsnd.*Message IPC.*%d.*return,success", msqid);
131 
132 	FILE *pipefd = setup(fds, auclass);
133 	ATF_REQUIRE_EQ(0, msgsnd(msqid, &msg, BUFFSIZE, IPC_NOWAIT));
134 	check_audit(fds, ipcregex, pipefd);
135 
136 	/* Destroy the message queue with ID = msqid */
137 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
138 }
139 
140 ATF_TC_CLEANUP(msgsnd_success, tc)
141 {
142 	cleanup();
143 }
144 
145 
146 ATF_TC_WITH_CLEANUP(msgsnd_failure);
147 ATF_TC_HEAD(msgsnd_failure, tc)
148 {
149 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
150 					"msgsnd(2) call");
151 }
152 
153 ATF_TC_BODY(msgsnd_failure, tc)
154 {
155 	const char *regex = "msgsnd.*Message IPC.*return,failure : Bad address";
156 	FILE *pipefd = setup(fds, auclass);
157 	ATF_REQUIRE_EQ(-1, msgsnd(-1, NULL, 0, IPC_NOWAIT));
158 	check_audit(fds, regex, pipefd);
159 }
160 
161 ATF_TC_CLEANUP(msgsnd_failure, tc)
162 {
163 	cleanup();
164 }
165 
166 
167 ATF_TC_WITH_CLEANUP(msgrcv_success);
168 ATF_TC_HEAD(msgrcv_success, tc)
169 {
170 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
171 					"msgrcv(2) call");
172 }
173 
174 ATF_TC_BODY(msgrcv_success, tc)
175 {
176 	ssize_t recv_bytes;
177 	/* Create a message queue and obtain the corresponding identifier */
178 	ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
179 
180 	/* Initialize two msgstr_t structures to store respective messages */
181 	msgstr_t msg1, msg2;
182 	msg1.mtype = 1;
183 	memset(msg1.mtext, 0, BUFFSIZE);
184 
185 	/* Send a message to the queue with ID = msqid */
186 	ATF_REQUIRE_EQ(0, msgsnd(msqid, &msg1, BUFFSIZE, IPC_NOWAIT));
187 
188 	FILE *pipefd = setup(fds, auclass);
189 	ATF_REQUIRE((recv_bytes = msgrcv(msqid, &msg2,
190 			BUFFSIZE, 0, MSG_NOERROR | IPC_NOWAIT)) != -1);
191 	/* Check the presence of queue ID and returned bytes in audit record */
192 	snprintf(ipcregex, sizeof(ipcregex),
193 	"msgrcv.*Message IPC,*%d.*return,success,%zd", msqid, recv_bytes);
194 	check_audit(fds, ipcregex, pipefd);
195 
196 	/* Destroy the message queue with ID = msqid */
197 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
198 }
199 
200 ATF_TC_CLEANUP(msgrcv_success, tc)
201 {
202 	cleanup();
203 }
204 
205 
206 ATF_TC_WITH_CLEANUP(msgrcv_failure);
207 ATF_TC_HEAD(msgrcv_failure, tc)
208 {
209 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
210 					"msgrcv(2) call");
211 }
212 
213 ATF_TC_BODY(msgrcv_failure, tc)
214 {
215 	const char *regex = "msgrcv.*return,failure : Invalid argument";
216 	FILE *pipefd = setup(fds, auclass);
217 	ATF_REQUIRE_EQ(-1, msgrcv(-1, NULL, 0, 0, MSG_NOERROR | IPC_NOWAIT));
218 	check_audit(fds, regex, pipefd);
219 }
220 
221 ATF_TC_CLEANUP(msgrcv_failure, tc)
222 {
223 	cleanup();
224 }
225 
226 
227 ATF_TC_WITH_CLEANUP(msgctl_rmid_success);
228 ATF_TC_HEAD(msgctl_rmid_success, tc)
229 {
230 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
231 					"msgctl(2) call for IPC_RMID command");
232 }
233 
234 ATF_TC_BODY(msgctl_rmid_success, tc)
235 {
236 	/* Create a message queue and obtain the corresponding identifier */
237 	ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
238 
239 	FILE *pipefd = setup(fds, auclass);
240 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
241 	/* Check the presence of queue ID and IPC_RMID in audit record */
242 	snprintf(ipcregex, sizeof(ipcregex),
243 			"msgctl.*IPC_RMID.*%d.*return,success", msqid);
244 	check_audit(fds, ipcregex, pipefd);
245 }
246 
247 ATF_TC_CLEANUP(msgctl_rmid_success, tc)
248 {
249 	cleanup();
250 }
251 
252 
253 ATF_TC_WITH_CLEANUP(msgctl_rmid_failure);
254 ATF_TC_HEAD(msgctl_rmid_failure, tc)
255 {
256 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
257 					"msgctl(2) call for IPC_RMID command");
258 }
259 
260 ATF_TC_BODY(msgctl_rmid_failure, tc)
261 {
262 	const char *regex = "msgctl.*IPC_RMID.*return,failur.*Invalid argument";
263 	FILE *pipefd = setup(fds, auclass);
264 	ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_RMID, NULL));
265 	check_audit(fds, regex, pipefd);
266 }
267 
268 ATF_TC_CLEANUP(msgctl_rmid_failure, tc)
269 {
270 	cleanup();
271 }
272 
273 
274 ATF_TC_WITH_CLEANUP(msgctl_stat_success);
275 ATF_TC_HEAD(msgctl_stat_success, tc)
276 {
277 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
278 					"msgctl(2) call for IPC_STAT command");
279 }
280 
281 ATF_TC_BODY(msgctl_stat_success, tc)
282 {
283 	/* Create a message queue and obtain the corresponding identifier */
284 	ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
285 
286 	FILE *pipefd = setup(fds, auclass);
287 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_STAT, &msgbuff));
288 	/* Check the presence of queue ID and IPC_STAT in audit record */
289 	snprintf(ipcregex, sizeof(ipcregex),
290 			"msgctl.*IPC_STAT.*%d.*return,success", msqid);
291 	check_audit(fds, ipcregex, pipefd);
292 
293 	/* Destroy the message queue with ID = msqid */
294 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
295 }
296 
297 ATF_TC_CLEANUP(msgctl_stat_success, tc)
298 {
299 	cleanup();
300 }
301 
302 
303 ATF_TC_WITH_CLEANUP(msgctl_stat_failure);
304 ATF_TC_HEAD(msgctl_stat_failure, tc)
305 {
306 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
307 					"msgctl(2) call for IPC_STAT command");
308 }
309 
310 ATF_TC_BODY(msgctl_stat_failure, tc)
311 {
312 	const char *regex = "msgctl.*IPC_STAT.*return,failur.*Invalid argument";
313 	FILE *pipefd = setup(fds, auclass);
314 	ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_STAT, &msgbuff));
315 	check_audit(fds, regex, pipefd);
316 }
317 
318 ATF_TC_CLEANUP(msgctl_stat_failure, tc)
319 {
320 	cleanup();
321 }
322 
323 
324 ATF_TC_WITH_CLEANUP(msgctl_set_success);
325 ATF_TC_HEAD(msgctl_set_success, tc)
326 {
327 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
328 					"msgctl(2) call for IPC_SET command");
329 }
330 
331 ATF_TC_BODY(msgctl_set_success, tc)
332 {
333 	/* Create a message queue and obtain the corresponding identifier */
334 	ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
335 	/* Fill up the msgbuff structure to be used with IPC_SET */
336 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_STAT, &msgbuff));
337 
338 	FILE *pipefd = setup(fds, auclass);
339 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_SET, &msgbuff));
340 	/* Check the presence of message queue ID in audit record */
341 	snprintf(ipcregex, sizeof(ipcregex),
342 			"msgctl.*IPC_SET.*%d.*return,success", msqid);
343 	check_audit(fds, ipcregex, pipefd);
344 
345 	/* Destroy the message queue with ID = msqid */
346 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
347 }
348 
349 ATF_TC_CLEANUP(msgctl_set_success, tc)
350 {
351 	cleanup();
352 }
353 
354 
355 ATF_TC_WITH_CLEANUP(msgctl_set_failure);
356 ATF_TC_HEAD(msgctl_set_failure, tc)
357 {
358 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
359 					"msgctl(2) call for IPC_SET command");
360 }
361 
362 ATF_TC_BODY(msgctl_set_failure, tc)
363 {
364 	const char *regex = "msgctl.*IPC_SET.*return,failure.*Invalid argument";
365 	FILE *pipefd = setup(fds, auclass);
366 	ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_SET, &msgbuff));
367 	check_audit(fds, regex, pipefd);
368 }
369 
370 ATF_TC_CLEANUP(msgctl_set_failure, tc)
371 {
372 	cleanup();
373 }
374 
375 
376 ATF_TC_WITH_CLEANUP(msgctl_illegal_command);
377 ATF_TC_HEAD(msgctl_illegal_command, tc)
378 {
379 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
380 					"msgctl(2) call for illegal cmd value");
381 }
382 
383 ATF_TC_BODY(msgctl_illegal_command, tc)
384 {
385 	/* Create a message queue and obtain the corresponding identifier */
386 	ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
387 
388 	const char *regex = "msgctl.*illegal command.*failur.*Invalid argument";
389 	FILE *pipefd = setup(fds, auclass);
390 	ATF_REQUIRE_EQ(-1, msgctl(msqid, -1, &msgbuff));
391 	check_audit(fds, regex, pipefd);
392 
393 	/* Destroy the message queue with ID = msqid */
394 	ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
395 }
396 
397 ATF_TC_CLEANUP(msgctl_illegal_command, tc)
398 {
399 	cleanup();
400 }
401 
402 
403 ATF_TC_WITH_CLEANUP(shmget_success);
404 ATF_TC_HEAD(shmget_success, tc)
405 {
406 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
407 					"shmget(2) call");
408 }
409 
410 ATF_TC_BODY(shmget_success, tc)
411 {
412 	FILE *pipefd = setup(fds, auclass);
413 	ATF_REQUIRE((shmid =
414 		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
415 	/* Check the presence of shared memory ID in audit record */
416 	snprintf(ipcregex, sizeof(ipcregex), "shmget.*ret.*success,%d", shmid);
417 	check_audit(fds, ipcregex, pipefd);
418 
419 	/* Destroy the shared memory with ID = shmid */
420 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
421 }
422 
423 ATF_TC_CLEANUP(shmget_success, tc)
424 {
425 	cleanup();
426 }
427 
428 
429 ATF_TC_WITH_CLEANUP(shmget_failure);
430 ATF_TC_HEAD(shmget_failure, tc)
431 {
432 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
433 					"shmget(2) call");
434 }
435 
436 ATF_TC_BODY(shmget_failure, tc)
437 {
438 	const char *regex = "shmget.*return,failure.*No such file or directory";
439 	FILE *pipefd = setup(fds, auclass);
440 	ATF_REQUIRE_EQ(-1, shmget((key_t)(-1), 0, 0));
441 	check_audit(fds, regex, pipefd);
442 }
443 
444 ATF_TC_CLEANUP(shmget_failure, tc)
445 {
446 	cleanup();
447 }
448 
449 
450 ATF_TC_WITH_CLEANUP(shmat_success);
451 ATF_TC_HEAD(shmat_success, tc)
452 {
453 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
454 					"shmat(2) call");
455 }
456 
457 ATF_TC_BODY(shmat_success, tc)
458 {
459 	void *addr;
460 	/* Create a shared memory segment and obtain the identifier */
461 	ATF_REQUIRE((shmid =
462 		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
463 
464 	FILE *pipefd = setup(fds, auclass);
465 	ATF_REQUIRE((intptr_t)(addr = shmat(shmid, NULL, 0)) != -1);
466 
467 	/* Check for shared memory ID and process address in record */
468 	snprintf(ipcregex, sizeof(ipcregex), "shmat.*Shared Memory "
469 			"IPC.*%d.*return,success", shmid);
470 	check_audit(fds, ipcregex, pipefd);
471 
472 	/* Destroy the shared memory with ID = shmid */
473 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
474 }
475 
476 ATF_TC_CLEANUP(shmat_success, tc)
477 {
478 	cleanup();
479 }
480 
481 
482 ATF_TC_WITH_CLEANUP(shmat_failure);
483 ATF_TC_HEAD(shmat_failure, tc)
484 {
485 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
486 					"shmat(2) call");
487 }
488 
489 ATF_TC_BODY(shmat_failure, tc)
490 {
491 	const char *regex = "shmat.*Shared Memory IPC.*return,failure";
492 	FILE *pipefd = setup(fds, auclass);
493 	ATF_REQUIRE_EQ(-1, (intptr_t)shmat(-1, NULL, 0));
494 	check_audit(fds, regex, pipefd);
495 }
496 
497 ATF_TC_CLEANUP(shmat_failure, tc)
498 {
499 	cleanup();
500 }
501 
502 
503 ATF_TC_WITH_CLEANUP(shmdt_success);
504 ATF_TC_HEAD(shmdt_success, tc)
505 {
506 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
507 					"shmdt(2) call");
508 }
509 
510 ATF_TC_BODY(shmdt_success, tc)
511 {
512 	void *addr;
513 	pid = getpid();
514 	snprintf(ipcregex, sizeof(ipcregex), "shmdt.*%d.*return,success", pid);
515 
516 	/* Create a shared memory segment and obtain the identifier */
517 	ATF_REQUIRE((shmid =
518 		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
519 
520 	/* Attach the shared memory to calling process's address space */
521 	ATF_REQUIRE((intptr_t)(addr = shmat(shmid, NULL, 0)) != -1);
522 
523 	FILE *pipefd = setup(fds, auclass);
524 	ATF_REQUIRE_EQ(0, shmdt(addr));
525 	check_audit(fds, ipcregex, pipefd);
526 
527 	/* Destroy the shared memory with ID = shmid */
528 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
529 }
530 
531 ATF_TC_CLEANUP(shmdt_success, tc)
532 {
533 	cleanup();
534 }
535 
536 
537 ATF_TC_WITH_CLEANUP(shmdt_failure);
538 ATF_TC_HEAD(shmdt_failure, tc)
539 {
540 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
541 					"shmdt(2) call");
542 }
543 
544 ATF_TC_BODY(shmdt_failure, tc)
545 {
546 	const char *regex = "shmdt.*return,failure : Invalid argument";
547 	FILE *pipefd = setup(fds, auclass);
548 	ATF_REQUIRE_EQ(-1, shmdt(NULL));
549 	check_audit(fds, regex, pipefd);
550 }
551 
552 ATF_TC_CLEANUP(shmdt_failure, tc)
553 {
554 	cleanup();
555 }
556 
557 
558 ATF_TC_WITH_CLEANUP(shmctl_rmid_success);
559 ATF_TC_HEAD(shmctl_rmid_success, tc)
560 {
561 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
562 					"shmctl(2) call for IPC_RMID command");
563 }
564 
565 ATF_TC_BODY(shmctl_rmid_success, tc)
566 {
567 	/* Create a shared memory segment and obtain the identifier */
568 	ATF_REQUIRE((shmid =
569 		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
570 
571 	FILE *pipefd = setup(fds, auclass);
572 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
573 	/* Check the presence of shmid and IPC_RMID in audit record */
574 	snprintf(ipcregex, sizeof(ipcregex),
575 		"shmctl.*IPC_RMID.*%d.*return,success", shmid);
576 	check_audit(fds, ipcregex, pipefd);
577 }
578 
579 ATF_TC_CLEANUP(shmctl_rmid_success, tc)
580 {
581 	cleanup();
582 }
583 
584 
585 ATF_TC_WITH_CLEANUP(shmctl_rmid_failure);
586 ATF_TC_HEAD(shmctl_rmid_failure, tc)
587 {
588 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
589 					"shmctl(2) call for IPC_RMID command");
590 }
591 
592 ATF_TC_BODY(shmctl_rmid_failure, tc)
593 {
594 	const char *regex = "shmctl.*IPC_RMID.*return,fail.*Invalid argument";
595 	FILE *pipefd = setup(fds, auclass);
596 	ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_RMID, NULL));
597 	check_audit(fds, regex, pipefd);
598 }
599 
600 ATF_TC_CLEANUP(shmctl_rmid_failure, tc)
601 {
602 	cleanup();
603 }
604 
605 
606 ATF_TC_WITH_CLEANUP(shmctl_stat_success);
607 ATF_TC_HEAD(shmctl_stat_success, tc)
608 {
609 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
610 					"shmctl(2) call for IPC_STAT command");
611 }
612 
613 ATF_TC_BODY(shmctl_stat_success, tc)
614 {
615 	/* Create a shared memory segment and obtain the identifier */
616 	ATF_REQUIRE((shmid =
617 		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
618 
619 	FILE *pipefd = setup(fds, auclass);
620 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_STAT, &shmbuff));
621 	/* Check if shared memory ID and IPC_STAT are present in audit record */
622 	snprintf(ipcregex, sizeof(ipcregex),
623 		"shmctl.*IPC_STAT.*%d.*return,success", shmid);
624 	check_audit(fds, ipcregex, pipefd);
625 
626 	/* Destroy the shared memory with ID = shmid */
627 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
628 }
629 
630 ATF_TC_CLEANUP(shmctl_stat_success, tc)
631 {
632 	cleanup();
633 }
634 
635 
636 ATF_TC_WITH_CLEANUP(shmctl_stat_failure);
637 ATF_TC_HEAD(shmctl_stat_failure, tc)
638 {
639 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
640 					"shmctl(2) call for IPC_STAT command");
641 }
642 
643 ATF_TC_BODY(shmctl_stat_failure, tc)
644 {
645 	const char *regex = "shmctl.*IPC_STAT.*return,fail.*Invalid argument";
646 	FILE *pipefd = setup(fds, auclass);
647 	ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_STAT, &shmbuff));
648 	check_audit(fds, regex, pipefd);
649 }
650 
651 ATF_TC_CLEANUP(shmctl_stat_failure, tc)
652 {
653 	cleanup();
654 }
655 
656 
657 ATF_TC_WITH_CLEANUP(shmctl_set_success);
658 ATF_TC_HEAD(shmctl_set_success, tc)
659 {
660 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
661 					"shmctl(2) call for IPC_SET command");
662 }
663 
664 ATF_TC_BODY(shmctl_set_success, tc)
665 {
666 	/* Create a shared memory segment and obtain the identifier */
667 	ATF_REQUIRE((shmid =
668 		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
669 	/* Fill up the shmbuff structure to be used with IPC_SET */
670 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_STAT, &shmbuff));
671 
672 	FILE *pipefd = setup(fds, auclass);
673 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_SET, &shmbuff));
674 	/* Check the presence of shared memory ID in audit record */
675 	snprintf(ipcregex, sizeof(ipcregex),
676 		"shmctl.*IPC_SET.*%d.*return,success", msqid);
677 	check_audit(fds, ipcregex, pipefd);
678 
679 	/* Destroy the shared memory with ID = shmid */
680 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
681 }
682 
683 ATF_TC_CLEANUP(shmctl_set_success, tc)
684 {
685 	cleanup();
686 }
687 
688 
689 ATF_TC_WITH_CLEANUP(shmctl_set_failure);
690 ATF_TC_HEAD(shmctl_set_failure, tc)
691 {
692 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
693 					"shmctl(2) call for IPC_SET command");
694 }
695 
696 ATF_TC_BODY(shmctl_set_failure, tc)
697 {
698 	const char *regex = "shmctl.*IPC_SET.*return,failure.*Invalid argument";
699 	FILE *pipefd = setup(fds, auclass);
700 	ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_SET, &shmbuff));
701 	check_audit(fds, regex, pipefd);
702 }
703 
704 ATF_TC_CLEANUP(shmctl_set_failure, tc)
705 {
706 	cleanup();
707 }
708 
709 
710 ATF_TC_WITH_CLEANUP(shmctl_illegal_command);
711 ATF_TC_HEAD(shmctl_illegal_command, tc)
712 {
713 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
714 					"shmctl(2) call for illegal cmd value");
715 }
716 
717 ATF_TC_BODY(shmctl_illegal_command, tc)
718 {
719 	/* Create a shared memory segment and obtain the identifier */
720 	ATF_REQUIRE((shmid =
721 		shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
722 
723 	const char *regex = "shmctl.*illegal command.*fail.*Invalid argument";
724 	FILE *pipefd = setup(fds, auclass);
725 	ATF_REQUIRE_EQ(-1, shmctl(shmid, -1, &shmbuff));
726 	check_audit(fds, regex, pipefd);
727 
728 	/* Destroy the shared memory with ID = shmid */
729 	ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
730 }
731 
732 ATF_TC_CLEANUP(shmctl_illegal_command, tc)
733 {
734 	cleanup();
735 }
736 
737 
738 ATF_TC_WITH_CLEANUP(semget_success);
739 ATF_TC_HEAD(semget_success, tc)
740 {
741 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
742 					"semget(2) call");
743 }
744 
745 ATF_TC_BODY(semget_success, tc)
746 {
747 	FILE *pipefd = setup(fds, auclass);
748 	ATF_REQUIRE((semid =
749 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
750 
751 	/* Check the presence of semaphore set ID in audit record */
752 	snprintf(ipcregex, sizeof(ipcregex),
753 		"semget.*return,success,%d", semid);
754 	check_audit(fds, ipcregex, pipefd);
755 
756 	/* Destroy the semaphore set with ID = semid */
757 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
758 }
759 
760 ATF_TC_CLEANUP(semget_success, tc)
761 {
762 	cleanup();
763 }
764 
765 
766 ATF_TC_WITH_CLEANUP(semget_failure);
767 ATF_TC_HEAD(semget_failure, tc)
768 {
769 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
770 					"semget(2) call");
771 }
772 
773 ATF_TC_BODY(semget_failure, tc)
774 {
775 	pid = getpid();
776 	snprintf(ipcregex, sizeof(ipcregex), "semget.*%d.*return,failure", pid);
777 
778 	FILE *pipefd = setup(fds, auclass);
779 	/* Failure reason: nsems is a negative number */
780 	ATF_REQUIRE_EQ(-1, semget(IPC_PRIVATE, -1, 0));
781 	check_audit(fds, ipcregex, pipefd);
782 }
783 
784 ATF_TC_CLEANUP(semget_failure, tc)
785 {
786 	cleanup();
787 }
788 
789 
790 ATF_TC_WITH_CLEANUP(semop_success);
791 ATF_TC_HEAD(semop_success, tc)
792 {
793 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
794 					"semop(2) call");
795 }
796 
797 ATF_TC_BODY(semop_success, tc)
798 {
799 	/* Create a semaphore set and obtain the set identifier */
800 	ATF_REQUIRE((semid =
801 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
802 
803 	/* Initialize a sembuf structure to operate on semaphore set */
804 	struct sembuf sop[1] = {{0, 1, 0}};
805 	/* Check the presence of semaphore set ID in audit record */
806 	snprintf(ipcregex, sizeof(ipcregex),
807 		"semop.*Semaphore IPC.*%d.*return,success", semid);
808 
809 	FILE *pipefd = setup(fds, auclass);
810 	ATF_REQUIRE_EQ(0, semop(semid, sop, sizeof(sop)/sizeof(struct sembuf)));
811 	check_audit(fds, ipcregex, pipefd);
812 
813 	/* Destroy the semaphore set with ID = semid */
814 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
815 }
816 
817 ATF_TC_CLEANUP(semop_success, tc)
818 {
819 	cleanup();
820 }
821 
822 
823 ATF_TC_WITH_CLEANUP(semop_failure);
824 ATF_TC_HEAD(semop_failure, tc)
825 {
826 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
827 					"semop(2) call");
828 }
829 
830 ATF_TC_BODY(semop_failure, tc)
831 {
832 	const char *regex = "semop.*0xffff.*return,failure : Invalid argument";
833 	FILE *pipefd = setup(fds, auclass);
834 	ATF_REQUIRE_EQ(-1, semop(-1, NULL, 0));
835 	check_audit(fds, regex, pipefd);
836 }
837 
838 ATF_TC_CLEANUP(semop_failure, tc)
839 {
840 	cleanup();
841 }
842 
843 
844 ATF_TC_WITH_CLEANUP(semctl_getval_success);
845 ATF_TC_HEAD(semctl_getval_success, tc)
846 {
847 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
848 					"semctl(2) call for GETVAL command");
849 }
850 
851 ATF_TC_BODY(semctl_getval_success, tc)
852 {
853 	/* Create a semaphore set and obtain the set identifier */
854 	ATF_REQUIRE((semid =
855 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
856 
857 	FILE *pipefd = setup(fds, auclass);
858 	ATF_REQUIRE_EQ(0, semctl(semid, 0, GETVAL));
859 	/* Check the presence of semaphore ID and GETVAL in audit record */
860 	snprintf(ipcregex, sizeof(ipcregex),
861 		"semctl.*GETVAL.*%d.*return,success", semid);
862 	check_audit(fds, ipcregex, pipefd);
863 
864 	/* Destroy the semaphore set with ID = semid */
865 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
866 }
867 
868 ATF_TC_CLEANUP(semctl_getval_success, tc)
869 {
870 	cleanup();
871 }
872 
873 
874 ATF_TC_WITH_CLEANUP(semctl_getval_failure);
875 ATF_TC_HEAD(semctl_getval_failure, tc)
876 {
877 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
878 					"semctl(2) call for GETVAL command");
879 }
880 
881 ATF_TC_BODY(semctl_getval_failure, tc)
882 {
883 	const char *regex = "semctl.*GETVAL.*return,failure : Invalid argument";
884 	FILE *pipefd = setup(fds, auclass);
885 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, GETVAL));
886 	check_audit(fds, regex, pipefd);
887 }
888 
889 ATF_TC_CLEANUP(semctl_getval_failure, tc)
890 {
891 	cleanup();
892 }
893 
894 
895 ATF_TC_WITH_CLEANUP(semctl_setval_success);
896 ATF_TC_HEAD(semctl_setval_success, tc)
897 {
898 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
899 					"semctl(2) call for SETVAL command");
900 }
901 
902 ATF_TC_BODY(semctl_setval_success, tc)
903 {
904 	/* Create a semaphore set and obtain the set identifier */
905 	ATF_REQUIRE((semid =
906 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
907 
908 	semarg.val = 1;
909 	FILE *pipefd = setup(fds, auclass);
910 	ATF_REQUIRE_EQ(0, semctl(semid, 0, SETVAL, semarg));
911 	/* Check the presence of semaphore ID and SETVAL in audit record */
912 	snprintf(ipcregex, sizeof(ipcregex),
913 		"semctl.*SETVAL.*%d.*return,success", semid);
914 	check_audit(fds, ipcregex, pipefd);
915 
916 	/* Destroy the semaphore set with ID = semid */
917 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
918 }
919 
920 ATF_TC_CLEANUP(semctl_setval_success, tc)
921 {
922 	cleanup();
923 }
924 
925 
926 ATF_TC_WITH_CLEANUP(semctl_setval_failure);
927 ATF_TC_HEAD(semctl_setval_failure, tc)
928 {
929 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
930 					"semctl(2) call for SETVAL command");
931 }
932 
933 ATF_TC_BODY(semctl_setval_failure, tc)
934 {
935 	const char *regex = "semctl.*SETVAL.*return,failure : Invalid argument";
936 	FILE *pipefd = setup(fds, auclass);
937 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, SETVAL, semarg));
938 	check_audit(fds, regex, pipefd);
939 }
940 
941 ATF_TC_CLEANUP(semctl_setval_failure, tc)
942 {
943 	cleanup();
944 }
945 
946 
947 ATF_TC_WITH_CLEANUP(semctl_getpid_success);
948 ATF_TC_HEAD(semctl_getpid_success, tc)
949 {
950 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
951 					"semctl(2) call for GETPID command");
952 }
953 
954 ATF_TC_BODY(semctl_getpid_success, tc)
955 {
956 	/* Create a semaphore set and obtain the set identifier */
957 	ATF_REQUIRE((semid =
958 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
959 
960 	FILE *pipefd = setup(fds, auclass);
961 	ATF_REQUIRE_EQ(0, semctl(semid, 0, GETPID));
962 	/* Check the presence of semaphore ID and GETVAL in audit record */
963 	snprintf(ipcregex, sizeof(ipcregex),
964 		"semctl.*GETPID.*%d.*return,success", semid);
965 	check_audit(fds, ipcregex, pipefd);
966 
967 	/* Destroy the semaphore set with ID = semid */
968 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
969 }
970 
971 ATF_TC_CLEANUP(semctl_getpid_success, tc)
972 {
973 	cleanup();
974 }
975 
976 
977 ATF_TC_WITH_CLEANUP(semctl_getpid_failure);
978 ATF_TC_HEAD(semctl_getpid_failure, tc)
979 {
980 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
981 					"semctl(2) call for GETPID command");
982 }
983 
984 ATF_TC_BODY(semctl_getpid_failure, tc)
985 {
986 	const char *regex = "semctl.*GETPID.*return,failure : Invalid argument";
987 	FILE *pipefd = setup(fds, auclass);
988 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, GETPID));
989 	check_audit(fds, regex, pipefd);
990 }
991 
992 ATF_TC_CLEANUP(semctl_getpid_failure, tc)
993 {
994 	cleanup();
995 }
996 
997 
998 ATF_TC_WITH_CLEANUP(semctl_getncnt_success);
999 ATF_TC_HEAD(semctl_getncnt_success, tc)
1000 {
1001 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
1002 					"semctl(2) call for GETNCNT command");
1003 }
1004 
1005 ATF_TC_BODY(semctl_getncnt_success, tc)
1006 {
1007 	/* Create a semaphore set and obtain the set identifier */
1008 	ATF_REQUIRE((semid =
1009 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1010 
1011 	FILE *pipefd = setup(fds, auclass);
1012 	ATF_REQUIRE_EQ(0, semctl(semid, 0, GETNCNT));
1013 	/* Check the presence of semaphore ID and GETNCNT in audit record */
1014 	snprintf(ipcregex, sizeof(ipcregex),
1015 		"semctl.*GETNCNT.*%d.*return,success", semid);
1016 	check_audit(fds, ipcregex, pipefd);
1017 
1018 	/* Destroy the semaphore set with ID = semid */
1019 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1020 }
1021 
1022 ATF_TC_CLEANUP(semctl_getncnt_success, tc)
1023 {
1024 	cleanup();
1025 }
1026 
1027 
1028 ATF_TC_WITH_CLEANUP(semctl_getncnt_failure);
1029 ATF_TC_HEAD(semctl_getncnt_failure, tc)
1030 {
1031 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1032 					"semctl(2) call for GETNCNT command");
1033 }
1034 
1035 ATF_TC_BODY(semctl_getncnt_failure, tc)
1036 {
1037 	const char *regex = "semctl.*GETNCNT.*return,failure.*Invalid argument";
1038 	FILE *pipefd = setup(fds, auclass);
1039 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, GETNCNT));
1040 	check_audit(fds, regex, pipefd);
1041 }
1042 
1043 ATF_TC_CLEANUP(semctl_getncnt_failure, tc)
1044 {
1045 	cleanup();
1046 }
1047 
1048 
1049 ATF_TC_WITH_CLEANUP(semctl_getzcnt_success);
1050 ATF_TC_HEAD(semctl_getzcnt_success, tc)
1051 {
1052 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
1053 					"semctl(2) call for GETZCNT command");
1054 }
1055 
1056 ATF_TC_BODY(semctl_getzcnt_success, tc)
1057 {
1058 	/* Create a semaphore set and obtain the set identifier */
1059 	ATF_REQUIRE((semid =
1060 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1061 
1062 	FILE *pipefd = setup(fds, auclass);
1063 	ATF_REQUIRE_EQ(0, semctl(semid, 0, GETZCNT));
1064 	/* Check the presence of semaphore ID and GETZCNT in audit record */
1065 	snprintf(ipcregex, sizeof(ipcregex),
1066 		"semctl.*GETZCNT.*%d.*return,success", semid);
1067 	check_audit(fds, ipcregex, pipefd);
1068 
1069 	/* Destroy the semaphore set with ID = semid */
1070 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1071 }
1072 
1073 ATF_TC_CLEANUP(semctl_getzcnt_success, tc)
1074 {
1075 	cleanup();
1076 }
1077 
1078 
1079 ATF_TC_WITH_CLEANUP(semctl_getzcnt_failure);
1080 ATF_TC_HEAD(semctl_getzcnt_failure, tc)
1081 {
1082 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1083 					"semctl(2) call for GETZCNT command");
1084 }
1085 
1086 ATF_TC_BODY(semctl_getzcnt_failure, tc)
1087 {
1088 	const char *regex = "semctl.*GETZCNT.*return,failure.*Invalid argument";
1089 	FILE *pipefd = setup(fds, auclass);
1090 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, GETZCNT));
1091 	check_audit(fds, regex, pipefd);
1092 }
1093 
1094 ATF_TC_CLEANUP(semctl_getzcnt_failure, tc)
1095 {
1096 	cleanup();
1097 }
1098 
1099 
1100 ATF_TC_WITH_CLEANUP(semctl_getall_success);
1101 ATF_TC_HEAD(semctl_getall_success, tc)
1102 {
1103 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
1104 					"semctl(2) call for GETALL command");
1105 }
1106 
1107 ATF_TC_BODY(semctl_getall_success, tc)
1108 {
1109 	/* Create a semaphore set and obtain the set identifier */
1110 	ATF_REQUIRE((semid =
1111 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1112 
1113 	semarg.array = semvals;
1114 	FILE *pipefd = setup(fds, auclass);
1115 	ATF_REQUIRE(semctl(semid, 0, GETALL, semarg) != -1);
1116 	/* Check the presence of semaphore ID and GETALL in audit record */
1117 	snprintf(ipcregex, sizeof(ipcregex),
1118 		"semctl.*GETALL.*%d.*return,success", semid);
1119 	check_audit(fds, ipcregex, pipefd);
1120 
1121 	/* Destroy the semaphore set with ID = semid */
1122 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1123 }
1124 
1125 ATF_TC_CLEANUP(semctl_getall_success, tc)
1126 {
1127 	cleanup();
1128 }
1129 
1130 
1131 ATF_TC_WITH_CLEANUP(semctl_getall_failure);
1132 ATF_TC_HEAD(semctl_getall_failure, tc)
1133 {
1134 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1135 					"semctl(2) call for GETALL command");
1136 }
1137 
1138 ATF_TC_BODY(semctl_getall_failure, tc)
1139 {
1140 	const char *regex = "semctl.*GETALL.*return,failure : Invalid argument";
1141 	FILE *pipefd = setup(fds, auclass);
1142 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, GETALL, semarg));
1143 	check_audit(fds, regex, pipefd);
1144 }
1145 
1146 ATF_TC_CLEANUP(semctl_getall_failure, tc)
1147 {
1148 	cleanup();
1149 }
1150 
1151 
1152 ATF_TC_WITH_CLEANUP(semctl_setall_success);
1153 ATF_TC_HEAD(semctl_setall_success, tc)
1154 {
1155 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
1156 					"semctl(2) call for SETALL command");
1157 }
1158 
1159 ATF_TC_BODY(semctl_setall_success, tc)
1160 {
1161 	/* Create a semaphore set and obtain the set identifier */
1162 	ATF_REQUIRE((semid =
1163 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1164 
1165 	semarg.array = semvals;
1166 	/* Initialize semvals to be used with SETALL */
1167 	ATF_REQUIRE(semctl(semid, 0, GETALL, semarg) != -1);
1168 
1169 	FILE *pipefd = setup(fds, auclass);
1170 	ATF_REQUIRE_EQ(0, semctl(semid, 0, SETALL, semarg));
1171 	/* Check the presence of semaphore ID and SETALL in audit record */
1172 	snprintf(ipcregex, sizeof(ipcregex),
1173 		"semctl.*SETALL.*%d.*return,success", semid);
1174 	check_audit(fds, ipcregex, pipefd);
1175 
1176 	/* Destroy the semaphore set with ID = semid */
1177 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1178 }
1179 
1180 ATF_TC_CLEANUP(semctl_setall_success, tc)
1181 {
1182 	cleanup();
1183 }
1184 
1185 
1186 ATF_TC_WITH_CLEANUP(semctl_setall_failure);
1187 ATF_TC_HEAD(semctl_setall_failure, tc)
1188 {
1189 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1190 					"semctl(2) call for SETALL command");
1191 }
1192 
1193 ATF_TC_BODY(semctl_setall_failure, tc)
1194 {
1195 	const char *regex = "semctl.*SETALL.*return,failure : Invalid argument";
1196 	FILE *pipefd = setup(fds, auclass);
1197 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, SETALL, semarg));
1198 	check_audit(fds, regex, pipefd);
1199 }
1200 
1201 ATF_TC_CLEANUP(semctl_setall_failure, tc)
1202 {
1203 	cleanup();
1204 }
1205 
1206 
1207 ATF_TC_WITH_CLEANUP(semctl_stat_success);
1208 ATF_TC_HEAD(semctl_stat_success, tc)
1209 {
1210 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
1211 					"semctl(2) call for IPC_STAT command");
1212 }
1213 
1214 ATF_TC_BODY(semctl_stat_success, tc)
1215 {
1216 	/* Create a semaphore set and obtain the set identifier */
1217 	ATF_REQUIRE((semid =
1218 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1219 
1220 	semarg.buf = &sembuff;
1221 	FILE *pipefd = setup(fds, auclass);
1222 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_STAT, semarg));
1223 	/* Check the presence of semaphore ID and IPC_STAT in audit record */
1224 	snprintf(ipcregex, sizeof(ipcregex),
1225 		"semctl.*IPC_STAT.*%d.*return,success", semid);
1226 	check_audit(fds, ipcregex, pipefd);
1227 
1228 	/* Destroy the semaphore set with ID = semid */
1229 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1230 }
1231 
1232 ATF_TC_CLEANUP(semctl_stat_success, tc)
1233 {
1234 	cleanup();
1235 }
1236 
1237 
1238 ATF_TC_WITH_CLEANUP(semctl_stat_failure);
1239 ATF_TC_HEAD(semctl_stat_failure, tc)
1240 {
1241 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1242 					"semctl(2) call for IPC_STAT command");
1243 }
1244 
1245 ATF_TC_BODY(semctl_stat_failure, tc)
1246 {
1247 	const char *regex = "semctl.*IPC_STAT.*return,fail.*Invalid argument";
1248 	FILE *pipefd = setup(fds, auclass);
1249 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, IPC_STAT, semarg));
1250 	check_audit(fds, regex, pipefd);
1251 }
1252 
1253 ATF_TC_CLEANUP(semctl_stat_failure, tc)
1254 {
1255 	cleanup();
1256 }
1257 
1258 
1259 ATF_TC_WITH_CLEANUP(semctl_set_success);
1260 ATF_TC_HEAD(semctl_set_success, tc)
1261 {
1262 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
1263 					"semctl(2) call for IPC_SET command");
1264 }
1265 
1266 ATF_TC_BODY(semctl_set_success, tc)
1267 {
1268 	/* Create a semaphore set and obtain the set identifier */
1269 	ATF_REQUIRE((semid =
1270 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1271 
1272 	semarg.buf = &sembuff;
1273 	/* Fill up the sembuff structure to be used with IPC_SET */
1274 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_STAT, semarg));
1275 
1276 	FILE *pipefd = setup(fds, auclass);
1277 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_SET, semarg));
1278 	/* Check the presence of semaphore ID and IPC_SET in audit record */
1279 	snprintf(ipcregex, sizeof(ipcregex),
1280 		"semctl.*IPC_SET.*%d.*return,success", semid);
1281 	check_audit(fds, ipcregex, pipefd);
1282 
1283 	/* Destroy the semaphore set with ID = semid */
1284 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1285 }
1286 
1287 ATF_TC_CLEANUP(semctl_set_success, tc)
1288 {
1289 	cleanup();
1290 }
1291 
1292 
1293 ATF_TC_WITH_CLEANUP(semctl_set_failure);
1294 ATF_TC_HEAD(semctl_set_failure, tc)
1295 {
1296 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1297 					"semctl(2) call for IPC_SET command");
1298 }
1299 
1300 ATF_TC_BODY(semctl_set_failure, tc)
1301 {
1302 	/* Create a semaphore set and obtain the set identifier */
1303 	ATF_REQUIRE((semid =
1304 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1305 
1306 	semarg.buf = &sembuff;
1307 	/* Fill up the sembuff structure to be used with IPC_SET */
1308 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_STAT, semarg));
1309 
1310 	const char *regex = "semctl.*IPC_SET.*return,failure.*Invalid argument";
1311 	FILE *pipefd = setup(fds, auclass);
1312 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, IPC_SET, semarg));
1313 	check_audit(fds, regex, pipefd);
1314 
1315 	/* Destroy the semaphore set with ID = semid */
1316 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1317 }
1318 
1319 ATF_TC_CLEANUP(semctl_set_failure, tc)
1320 {
1321 	cleanup();
1322 }
1323 
1324 
1325 ATF_TC_WITH_CLEANUP(semctl_rmid_success);
1326 ATF_TC_HEAD(semctl_rmid_success, tc)
1327 {
1328 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
1329 					"semctl(2) call for IPC_RMID command");
1330 }
1331 
1332 ATF_TC_BODY(semctl_rmid_success, tc)
1333 {
1334 	/* Create a semaphore set and obtain the set identifier */
1335 	ATF_REQUIRE((semid =
1336 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1337 
1338 	FILE *pipefd = setup(fds, auclass);
1339 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID, semarg));
1340 	/* Check the presence of semaphore ID and IPC_RMID in audit record */
1341 	snprintf(ipcregex, sizeof(ipcregex),
1342 		"semctl.*IPC_RMID.*%d.*return,success", semid);
1343 	check_audit(fds, ipcregex, pipefd);
1344 }
1345 
1346 ATF_TC_CLEANUP(semctl_rmid_success, tc)
1347 {
1348 	cleanup();
1349 }
1350 
1351 
1352 ATF_TC_WITH_CLEANUP(semctl_rmid_failure);
1353 ATF_TC_HEAD(semctl_rmid_failure, tc)
1354 {
1355 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1356 					"semctl(2) call for IPC_RMID command");
1357 }
1358 
1359 ATF_TC_BODY(semctl_rmid_failure, tc)
1360 {
1361 	const char *regex = "semctl.*IPC_RMID.*return,fail.*Invalid argument";
1362 	FILE *pipefd = setup(fds, auclass);
1363 	ATF_REQUIRE_EQ(-1, semctl(-1, 0, IPC_RMID, semarg));
1364 	check_audit(fds, regex, pipefd);
1365 }
1366 
1367 ATF_TC_CLEANUP(semctl_rmid_failure, tc)
1368 {
1369 	cleanup();
1370 }
1371 
1372 
1373 ATF_TC_WITH_CLEANUP(semctl_illegal_command);
1374 ATF_TC_HEAD(semctl_illegal_command, tc)
1375 {
1376 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
1377 					"semctl(2) call for illegal cmd value");
1378 }
1379 
1380 ATF_TC_BODY(semctl_illegal_command, tc)
1381 {
1382 	/* Create a semaphore set and obtain the set identifier */
1383 	ATF_REQUIRE((semid =
1384 		semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
1385 
1386 	const char *regex = "semctl.*illegal command.*fail.*Invalid argument";
1387 	FILE *pipefd = setup(fds, auclass);
1388 	ATF_REQUIRE_EQ(-1, semctl(semid, 0, -1));
1389 	check_audit(fds, regex, pipefd);
1390 
1391 	/* Destroy the semaphore set with ID = semid */
1392 	ATF_REQUIRE_EQ(0, semctl(semid, 0, IPC_RMID));
1393 }
1394 
1395 ATF_TC_CLEANUP(semctl_illegal_command, tc)
1396 {
1397 	cleanup();
1398 }
1399 
1400 
1401 ATF_TP_ADD_TCS(tp)
1402 {
1403 	ATF_TP_ADD_TC(tp, msgget_success);
1404 	ATF_TP_ADD_TC(tp, msgget_failure);
1405 	ATF_TP_ADD_TC(tp, msgsnd_success);
1406 	ATF_TP_ADD_TC(tp, msgsnd_failure);
1407 	ATF_TP_ADD_TC(tp, msgrcv_success);
1408 	ATF_TP_ADD_TC(tp, msgrcv_failure);
1409 
1410 	ATF_TP_ADD_TC(tp, msgctl_rmid_success);
1411 	ATF_TP_ADD_TC(tp, msgctl_rmid_failure);
1412 	ATF_TP_ADD_TC(tp, msgctl_stat_success);
1413 	ATF_TP_ADD_TC(tp, msgctl_stat_failure);
1414 	ATF_TP_ADD_TC(tp, msgctl_set_success);
1415 	ATF_TP_ADD_TC(tp, msgctl_set_failure);
1416 	ATF_TP_ADD_TC(tp, msgctl_illegal_command);
1417 
1418 	ATF_TP_ADD_TC(tp, shmget_success);
1419 	ATF_TP_ADD_TC(tp, shmget_failure);
1420 	ATF_TP_ADD_TC(tp, shmat_success);
1421 	ATF_TP_ADD_TC(tp, shmat_failure);
1422 	ATF_TP_ADD_TC(tp, shmdt_success);
1423 	ATF_TP_ADD_TC(tp, shmdt_failure);
1424 
1425 	ATF_TP_ADD_TC(tp, shmctl_rmid_success);
1426 	ATF_TP_ADD_TC(tp, shmctl_rmid_failure);
1427 	ATF_TP_ADD_TC(tp, shmctl_stat_success);
1428 	ATF_TP_ADD_TC(tp, shmctl_stat_failure);
1429 	ATF_TP_ADD_TC(tp, shmctl_set_success);
1430 	ATF_TP_ADD_TC(tp, shmctl_set_failure);
1431 	ATF_TP_ADD_TC(tp, shmctl_illegal_command);
1432 
1433 	ATF_TP_ADD_TC(tp, semget_success);
1434 	ATF_TP_ADD_TC(tp, semget_failure);
1435 	ATF_TP_ADD_TC(tp, semop_success);
1436 	ATF_TP_ADD_TC(tp, semop_failure);
1437 
1438 	ATF_TP_ADD_TC(tp, semctl_getval_success);
1439 	ATF_TP_ADD_TC(tp, semctl_getval_failure);
1440 	ATF_TP_ADD_TC(tp, semctl_setval_success);
1441 	ATF_TP_ADD_TC(tp, semctl_setval_failure);
1442 	ATF_TP_ADD_TC(tp, semctl_getpid_success);
1443 	ATF_TP_ADD_TC(tp, semctl_getpid_failure);
1444 	ATF_TP_ADD_TC(tp, semctl_getncnt_success);
1445 	ATF_TP_ADD_TC(tp, semctl_getncnt_failure);
1446 	ATF_TP_ADD_TC(tp, semctl_getzcnt_success);
1447 	ATF_TP_ADD_TC(tp, semctl_getzcnt_failure);
1448 	ATF_TP_ADD_TC(tp, semctl_getall_success);
1449 	ATF_TP_ADD_TC(tp, semctl_getall_failure);
1450 	ATF_TP_ADD_TC(tp, semctl_setall_success);
1451 	ATF_TP_ADD_TC(tp, semctl_setall_failure);
1452 	ATF_TP_ADD_TC(tp, semctl_stat_success);
1453 	ATF_TP_ADD_TC(tp, semctl_stat_failure);
1454 	ATF_TP_ADD_TC(tp, semctl_set_success);
1455 	ATF_TP_ADD_TC(tp, semctl_set_failure);
1456 	ATF_TP_ADD_TC(tp, semctl_rmid_success);
1457 	ATF_TP_ADD_TC(tp, semctl_rmid_failure);
1458 	ATF_TP_ADD_TC(tp, semctl_illegal_command);
1459 
1460 	return (atf_no_error());
1461 }
1462