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