]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/audit/inter-process.c
Fix 32-bit build after 335307
[FreeBSD/FreeBSD.git] / tests / sys / audit / inter-process.c
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 #include <sys/stat.h>
34
35 #include <atf-c.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 #include "utils.h"
40 #define BUFFSIZE 80
41
42 struct msgstr {
43         long int         mtype;
44         char             mtext[BUFFSIZE];
45 };
46 typedef struct msgstr msgstr_t;
47
48 static pid_t pid;
49 static int msqid, shmid;
50 static struct pollfd fds[1];
51 static struct msqid_ds msgbuff;
52 static struct shmid_ds shmbuff;
53 static char ipcregex[BUFFSIZE];
54 static const char *auclass = "ip";
55
56
57 ATF_TC_WITH_CLEANUP(msgget_success);
58 ATF_TC_HEAD(msgget_success, tc)
59 {
60         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
61                                         "msgget(2) call");
62 }
63
64 ATF_TC_BODY(msgget_success, tc)
65 {
66         FILE *pipefd = setup(fds, auclass);
67         /* Create a message queue and obtain the corresponding identifier */
68         ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
69         /* Check the presence of message queue ID in audit record */
70         snprintf(ipcregex, sizeof(ipcregex),
71                         "msgget.*return,success,%d", msqid);
72         check_audit(fds, ipcregex, pipefd);
73
74         /* Destroy the message queue with ID = msqid */
75         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
76 }
77
78 ATF_TC_CLEANUP(msgget_success, tc)
79 {
80         cleanup();
81 }
82
83
84 ATF_TC_WITH_CLEANUP(msgget_failure);
85 ATF_TC_HEAD(msgget_failure, tc)
86 {
87         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
88                                         "msgget(2) call");
89 }
90
91 ATF_TC_BODY(msgget_failure, tc)
92 {
93         const char *regex = "msgget.*return,failure.*No such file or directory";
94         FILE *pipefd = setup(fds, auclass);
95         ATF_REQUIRE_EQ(-1, msgget((key_t)(-1), 0));
96         check_audit(fds, regex, pipefd);
97 }
98
99 ATF_TC_CLEANUP(msgget_failure, tc)
100 {
101         cleanup();
102 }
103
104
105 ATF_TC_WITH_CLEANUP(msgsnd_success);
106 ATF_TC_HEAD(msgsnd_success, tc)
107 {
108         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
109                                         "msgsnd(2) call");
110 }
111
112 ATF_TC_BODY(msgsnd_success, tc)
113 {
114         /* Create a message queue and obtain the corresponding identifier */
115         ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
116
117         /* Initialize a msgstr_t structure to store message */
118         msgstr_t msg;
119         msg.mtype = 1;
120         memset(msg.mtext, 0, BUFFSIZE);
121
122         /* Check the presence of message queue ID in audit record */
123         snprintf(ipcregex, sizeof(ipcregex),
124                 "msgsnd.*Message IPC.*%d.*return,success", msqid);
125
126         FILE *pipefd = setup(fds, auclass);
127         ATF_REQUIRE_EQ(0, msgsnd(msqid, &msg, BUFFSIZE, IPC_NOWAIT));
128         check_audit(fds, ipcregex, pipefd);
129
130         /* Destroy the message queue with ID = msqid */
131         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
132 }
133
134 ATF_TC_CLEANUP(msgsnd_success, tc)
135 {
136         cleanup();
137 }
138
139
140 ATF_TC_WITH_CLEANUP(msgsnd_failure);
141 ATF_TC_HEAD(msgsnd_failure, tc)
142 {
143         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
144                                         "msgsnd(2) call");
145 }
146
147 ATF_TC_BODY(msgsnd_failure, tc)
148 {
149         const char *regex = "msgsnd.*Message IPC.*return,failure : Bad address";
150         FILE *pipefd = setup(fds, auclass);
151         ATF_REQUIRE_EQ(-1, msgsnd(-1, NULL, 0, IPC_NOWAIT));
152         check_audit(fds, regex, pipefd);
153 }
154
155 ATF_TC_CLEANUP(msgsnd_failure, tc)
156 {
157         cleanup();
158 }
159
160
161 ATF_TC_WITH_CLEANUP(msgrcv_success);
162 ATF_TC_HEAD(msgrcv_success, tc)
163 {
164         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
165                                         "msgrcv(2) call");
166 }
167
168 ATF_TC_BODY(msgrcv_success, tc)
169 {
170         ssize_t recv_bytes;
171         /* Create a message queue and obtain the corresponding identifier */
172         ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
173
174         /* Initialize two msgstr_t structures to store respective messages */
175         msgstr_t msg1, msg2;
176         msg1.mtype = 1;
177         memset(msg1.mtext, 0, BUFFSIZE);
178
179         /* Send a message to the queue with ID = msqid */
180         ATF_REQUIRE_EQ(0, msgsnd(msqid, &msg1, BUFFSIZE, IPC_NOWAIT));
181
182         FILE *pipefd = setup(fds, auclass);
183         ATF_REQUIRE((recv_bytes = msgrcv(msqid, &msg2,
184                         BUFFSIZE, 0, MSG_NOERROR | IPC_NOWAIT)) != -1);
185         /* Check the presence of queue ID and returned bytes in audit record */
186         snprintf(ipcregex, sizeof(ipcregex),
187         "msgrcv.*Message IPC,*%d.*return,success,%zd", msqid, recv_bytes);
188         check_audit(fds, ipcregex, pipefd);
189
190         /* Destroy the message queue with ID = msqid */
191         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
192 }
193
194 ATF_TC_CLEANUP(msgrcv_success, tc)
195 {
196         cleanup();
197 }
198
199
200 ATF_TC_WITH_CLEANUP(msgrcv_failure);
201 ATF_TC_HEAD(msgrcv_failure, tc)
202 {
203         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
204                                         "msgrcv(2) call");
205 }
206
207 ATF_TC_BODY(msgrcv_failure, tc)
208 {
209         const char *regex = "msgrcv.*return,failure : Invalid argument";
210         FILE *pipefd = setup(fds, auclass);
211         ATF_REQUIRE_EQ(-1, msgrcv(-1, NULL, 0, 0, MSG_NOERROR | IPC_NOWAIT));
212         check_audit(fds, regex, pipefd);
213 }
214
215 ATF_TC_CLEANUP(msgrcv_failure, tc)
216 {
217         cleanup();
218 }
219
220
221 ATF_TC_WITH_CLEANUP(msgctl_rmid_success);
222 ATF_TC_HEAD(msgctl_rmid_success, tc)
223 {
224         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
225                                         "msgctl(2) call for IPC_RMID command");
226 }
227
228 ATF_TC_BODY(msgctl_rmid_success, tc)
229 {
230         /* Create a message queue and obtain the corresponding identifier */
231         ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
232
233         FILE *pipefd = setup(fds, auclass);
234         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
235         /* Check the presence of queue ID and IPC_RMID in audit record */
236         snprintf(ipcregex, sizeof(ipcregex),
237                         "msgctl.*IPC_RMID.*%d.*return,success", msqid);
238         check_audit(fds, ipcregex, pipefd);
239 }
240
241 ATF_TC_CLEANUP(msgctl_rmid_success, tc)
242 {
243         cleanup();
244 }
245
246
247 ATF_TC_WITH_CLEANUP(msgctl_rmid_failure);
248 ATF_TC_HEAD(msgctl_rmid_failure, tc)
249 {
250         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
251                                         "msgctl(2) call for IPC_RMID command");
252 }
253
254 ATF_TC_BODY(msgctl_rmid_failure, tc)
255 {
256         const char *regex = "msgctl.*IPC_RMID.*return,failur.*Invalid argument";
257         FILE *pipefd = setup(fds, auclass);
258         ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_RMID, NULL));
259         check_audit(fds, regex, pipefd);
260 }
261
262 ATF_TC_CLEANUP(msgctl_rmid_failure, tc)
263 {
264         cleanup();
265 }
266
267
268 ATF_TC_WITH_CLEANUP(msgctl_stat_success);
269 ATF_TC_HEAD(msgctl_stat_success, tc)
270 {
271         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
272                                         "msgctl(2) call for IPC_STAT command");
273 }
274
275 ATF_TC_BODY(msgctl_stat_success, tc)
276 {
277         /* Create a message queue and obtain the corresponding identifier */
278         ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
279
280         FILE *pipefd = setup(fds, auclass);
281         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_STAT, &msgbuff));
282         /* Check the presence of queue ID and IPC_STAT in audit record */
283         snprintf(ipcregex, sizeof(ipcregex),
284                         "msgctl.*IPC_STAT.*%d.*return,success", msqid);
285         check_audit(fds, ipcregex, pipefd);
286
287         /* Destroy the message queue with ID = msqid */
288         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
289 }
290
291 ATF_TC_CLEANUP(msgctl_stat_success, tc)
292 {
293         cleanup();
294 }
295
296
297 ATF_TC_WITH_CLEANUP(msgctl_stat_failure);
298 ATF_TC_HEAD(msgctl_stat_failure, tc)
299 {
300         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
301                                         "msgctl(2) call for IPC_STAT command");
302 }
303
304 ATF_TC_BODY(msgctl_stat_failure, tc)
305 {
306         const char *regex = "msgctl.*IPC_STAT.*return,failur.*Invalid argument";
307         FILE *pipefd = setup(fds, auclass);
308         ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_STAT, &msgbuff));
309         check_audit(fds, regex, pipefd);
310 }
311
312 ATF_TC_CLEANUP(msgctl_stat_failure, tc)
313 {
314         cleanup();
315 }
316
317
318 ATF_TC_WITH_CLEANUP(msgctl_set_success);
319 ATF_TC_HEAD(msgctl_set_success, tc)
320 {
321         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
322                                         "msgctl(2) call for IPC_SET command");
323 }
324
325 ATF_TC_BODY(msgctl_set_success, tc)
326 {
327         /* Create a message queue and obtain the corresponding identifier */
328         ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
329         /* Fill up the msgbuff structure to be used with IPC_SET */
330         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_STAT, &msgbuff));
331
332         FILE *pipefd = setup(fds, auclass);
333         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_SET, &msgbuff));
334         /* Check the presence of message queue ID in audit record */
335         snprintf(ipcregex, sizeof(ipcregex),
336                         "msgctl.*IPC_SET.*%d.*return,success", msqid);
337         check_audit(fds, ipcregex, pipefd);
338
339         /* Destroy the message queue with ID = msqid */
340         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
341 }
342
343 ATF_TC_CLEANUP(msgctl_set_success, tc)
344 {
345         cleanup();
346 }
347
348
349 ATF_TC_WITH_CLEANUP(msgctl_set_failure);
350 ATF_TC_HEAD(msgctl_set_failure, tc)
351 {
352         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
353                                         "msgctl(2) call for IPC_SET command");
354 }
355
356 ATF_TC_BODY(msgctl_set_failure, tc)
357 {
358         const char *regex = "msgctl.*IPC_SET.*return,failure.*Invalid argument";
359         FILE *pipefd = setup(fds, auclass);
360         ATF_REQUIRE_EQ(-1, msgctl(-1, IPC_SET, &msgbuff));
361         check_audit(fds, regex, pipefd);
362 }
363
364 ATF_TC_CLEANUP(msgctl_set_failure, tc)
365 {
366         cleanup();
367 }
368
369
370 ATF_TC_WITH_CLEANUP(msgctl_illegal_command);
371 ATF_TC_HEAD(msgctl_illegal_command, tc)
372 {
373         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
374                                         "msgctl(2) call for illegal cmd value");
375 }
376
377 ATF_TC_BODY(msgctl_illegal_command, tc)
378 {
379         /* Create a message queue and obtain the corresponding identifier */
380         ATF_REQUIRE((msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR)) != -1);
381
382         const char *regex = "msgctl.*illegal command.*failur.*Invalid argument";
383         FILE *pipefd = setup(fds, auclass);
384         ATF_REQUIRE_EQ(-1, msgctl(msqid, -1, &msgbuff));
385         check_audit(fds, regex, pipefd);
386
387         /* Destroy the message queue with ID = msqid */
388         ATF_REQUIRE_EQ(0, msgctl(msqid, IPC_RMID, NULL));
389 }
390
391 ATF_TC_CLEANUP(msgctl_illegal_command, tc)
392 {
393         cleanup();
394 }
395
396
397 ATF_TC_WITH_CLEANUP(shmget_success);
398 ATF_TC_HEAD(shmget_success, tc)
399 {
400         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
401                                         "shmget(2) call");
402 }
403
404 ATF_TC_BODY(shmget_success, tc)
405 {
406         FILE *pipefd = setup(fds, auclass);
407         ATF_REQUIRE((shmid =
408                 shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
409         /* Check the presence of shared memory ID in audit record */
410         snprintf(ipcregex, sizeof(ipcregex), "shmget.*ret.*success,%d", shmid);
411         check_audit(fds, ipcregex, pipefd);
412
413         /* Destroy the shared memory with ID = shmid */
414         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
415 }
416
417 ATF_TC_CLEANUP(shmget_success, tc)
418 {
419         cleanup();
420 }
421
422
423 ATF_TC_WITH_CLEANUP(shmget_failure);
424 ATF_TC_HEAD(shmget_failure, tc)
425 {
426         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
427                                         "shmget(2) call");
428 }
429
430 ATF_TC_BODY(shmget_failure, tc)
431 {
432         const char *regex = "shmget.*return,failure.*No such file or directory";
433         FILE *pipefd = setup(fds, auclass);
434         ATF_REQUIRE_EQ(-1, shmget((key_t)(-1), 0, 0));
435         check_audit(fds, regex, pipefd);
436 }
437
438 ATF_TC_CLEANUP(shmget_failure, tc)
439 {
440         cleanup();
441 }
442
443
444 ATF_TC_WITH_CLEANUP(shmat_success);
445 ATF_TC_HEAD(shmat_success, tc)
446 {
447         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
448                                         "shmat(2) call");
449 }
450
451 ATF_TC_BODY(shmat_success, tc)
452 {
453         void *addr;
454         /* Create a shared memory segment and obtain the identifier */
455         ATF_REQUIRE((shmid =
456                 shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
457
458         FILE *pipefd = setup(fds, auclass);
459         ATF_REQUIRE((intptr_t)(addr = shmat(shmid, NULL, 0)) != -1);
460
461         /* Check for shared memory ID and process address in record */
462         snprintf(ipcregex, sizeof(ipcregex), "shmat.*Shared Memory "
463                         "IPC.*%d.*return,success", shmid);
464         check_audit(fds, ipcregex, pipefd);
465
466         /* Destroy the shared memory with ID = shmid */
467         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
468 }
469
470 ATF_TC_CLEANUP(shmat_success, tc)
471 {
472         cleanup();
473 }
474
475
476 ATF_TC_WITH_CLEANUP(shmat_failure);
477 ATF_TC_HEAD(shmat_failure, tc)
478 {
479         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
480                                         "shmat(2) call");
481 }
482
483 ATF_TC_BODY(shmat_failure, tc)
484 {
485         const char *regex = "shmat.*Shared Memory IPC.*return,failure";
486         FILE *pipefd = setup(fds, auclass);
487         ATF_REQUIRE_EQ(-1, (intptr_t)shmat(-1, NULL, 0));
488         check_audit(fds, regex, pipefd);
489 }
490
491 ATF_TC_CLEANUP(shmat_failure, tc)
492 {
493         cleanup();
494 }
495
496
497 ATF_TC_WITH_CLEANUP(shmdt_success);
498 ATF_TC_HEAD(shmdt_success, tc)
499 {
500         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
501                                         "shmdt(2) call");
502 }
503
504 ATF_TC_BODY(shmdt_success, tc)
505 {
506         void *addr;
507         pid = getpid();
508         snprintf(ipcregex, sizeof(ipcregex), "shmdt.*%d.*return,success", pid);
509
510         /* Create a shared memory segment and obtain the identifier */
511         ATF_REQUIRE((shmid =
512                 shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
513
514         /* Attach the shared memory to calling process's address space */
515         ATF_REQUIRE((intptr_t)(addr = shmat(shmid, NULL, 0)) != -1);
516
517         FILE *pipefd = setup(fds, auclass);
518         ATF_REQUIRE_EQ(0, shmdt(addr));
519         check_audit(fds, ipcregex, pipefd);
520
521         /* Destroy the shared memory with ID = shmid */
522         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
523 }
524
525 ATF_TC_CLEANUP(shmdt_success, tc)
526 {
527         cleanup();
528 }
529
530
531 ATF_TC_WITH_CLEANUP(shmdt_failure);
532 ATF_TC_HEAD(shmdt_failure, tc)
533 {
534         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
535                                         "shmdt(2) call");
536 }
537
538 ATF_TC_BODY(shmdt_failure, tc)
539 {
540         const char *regex = "shmdt.*return,failure : Invalid argument";
541         FILE *pipefd = setup(fds, auclass);
542         ATF_REQUIRE_EQ(-1, shmdt(NULL));
543         check_audit(fds, regex, pipefd);
544 }
545
546 ATF_TC_CLEANUP(shmdt_failure, tc)
547 {
548         cleanup();
549 }
550
551
552 ATF_TC_WITH_CLEANUP(shmctl_rmid_success);
553 ATF_TC_HEAD(shmctl_rmid_success, tc)
554 {
555         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
556                                         "shmctl(2) call for IPC_RMID command");
557 }
558
559 ATF_TC_BODY(shmctl_rmid_success, tc)
560 {
561         /* Create a shared memory segment and obtain the identifier */
562         ATF_REQUIRE((shmid =
563                 shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
564
565         FILE *pipefd = setup(fds, auclass);
566         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
567         /* Check the presence of shmid and IPC_RMID in audit record */
568         snprintf(ipcregex, sizeof(ipcregex),
569                 "shmctl.*IPC_RMID.*%d.*return,success", shmid);
570         check_audit(fds, ipcregex, pipefd);
571 }
572
573 ATF_TC_CLEANUP(shmctl_rmid_success, tc)
574 {
575         cleanup();
576 }
577
578
579 ATF_TC_WITH_CLEANUP(shmctl_rmid_failure);
580 ATF_TC_HEAD(shmctl_rmid_failure, tc)
581 {
582         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
583                                         "shmctl(2) call for IPC_RMID command");
584 }
585
586 ATF_TC_BODY(shmctl_rmid_failure, tc)
587 {
588         const char *regex = "shmctl.*IPC_RMID.*return,fail.*Invalid argument";
589         FILE *pipefd = setup(fds, auclass);
590         ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_RMID, NULL));
591         check_audit(fds, regex, pipefd);
592 }
593
594 ATF_TC_CLEANUP(shmctl_rmid_failure, tc)
595 {
596         cleanup();
597 }
598
599
600 ATF_TC_WITH_CLEANUP(shmctl_stat_success);
601 ATF_TC_HEAD(shmctl_stat_success, tc)
602 {
603         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
604                                         "shmctl(2) call for IPC_STAT command");
605 }
606
607 ATF_TC_BODY(shmctl_stat_success, tc)
608 {
609         /* Create a shared memory segment and obtain the identifier */
610         ATF_REQUIRE((shmid =
611                 shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
612
613         FILE *pipefd = setup(fds, auclass);
614         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_STAT, &shmbuff));
615         /* Check if shared memory ID and IPC_STAT are present in audit record */
616         snprintf(ipcregex, sizeof(ipcregex),
617                 "shmctl.*IPC_STAT.*%d.*return,success", shmid);
618         check_audit(fds, ipcregex, pipefd);
619
620         /* Destroy the shared memory with ID = shmid */
621         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
622 }
623
624 ATF_TC_CLEANUP(shmctl_stat_success, tc)
625 {
626         cleanup();
627 }
628
629
630 ATF_TC_WITH_CLEANUP(shmctl_stat_failure);
631 ATF_TC_HEAD(shmctl_stat_failure, tc)
632 {
633         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
634                                         "shmctl(2) call for IPC_STAT command");
635 }
636
637 ATF_TC_BODY(shmctl_stat_failure, tc)
638 {
639         const char *regex = "shmctl.*IPC_STAT.*return,fail.*Invalid argument";
640         FILE *pipefd = setup(fds, auclass);
641         ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_STAT, &shmbuff));
642         check_audit(fds, regex, pipefd);
643 }
644
645 ATF_TC_CLEANUP(shmctl_stat_failure, tc)
646 {
647         cleanup();
648 }
649
650
651 ATF_TC_WITH_CLEANUP(shmctl_set_success);
652 ATF_TC_HEAD(shmctl_set_success, tc)
653 {
654         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
655                                         "shmctl(2) call for IPC_SET command");
656 }
657
658 ATF_TC_BODY(shmctl_set_success, tc)
659 {
660         /* Create a shared memory segment and obtain the identifier */
661         ATF_REQUIRE((shmid =
662                 shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
663         /* Fill up the shmbuff structure to be used with IPC_SET */
664         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_STAT, &shmbuff));
665
666         FILE *pipefd = setup(fds, auclass);
667         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_SET, &shmbuff));
668         /* Check the presence of shared memory ID in audit record */
669         snprintf(ipcregex, sizeof(ipcregex),
670                 "shmctl.*IPC_SET.*%d.*return,success", msqid);
671         check_audit(fds, ipcregex, pipefd);
672
673         /* Destroy the shared memory with ID = shmid */
674         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
675 }
676
677 ATF_TC_CLEANUP(shmctl_set_success, tc)
678 {
679         cleanup();
680 }
681
682
683 ATF_TC_WITH_CLEANUP(shmctl_set_failure);
684 ATF_TC_HEAD(shmctl_set_failure, tc)
685 {
686         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
687                                         "shmctl(2) call for IPC_SET command");
688 }
689
690 ATF_TC_BODY(shmctl_set_failure, tc)
691 {
692         const char *regex = "shmctl.*IPC_SET.*return,failure.*Invalid argument";
693         FILE *pipefd = setup(fds, auclass);
694         ATF_REQUIRE_EQ(-1, shmctl(-1, IPC_SET, &shmbuff));
695         check_audit(fds, regex, pipefd);
696 }
697
698 ATF_TC_CLEANUP(shmctl_set_failure, tc)
699 {
700         cleanup();
701 }
702
703
704 ATF_TC_WITH_CLEANUP(shmctl_illegal_command);
705 ATF_TC_HEAD(shmctl_illegal_command, tc)
706 {
707         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
708                                         "shmctl(2) call for illegal cmd value");
709 }
710
711 ATF_TC_BODY(shmctl_illegal_command, tc)
712 {
713         /* Create a shared memory segment and obtain the identifier */
714         ATF_REQUIRE((shmid =
715                 shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR)) != -1);
716
717         const char *regex = "shmctl.*illegal command.*fail.*Invalid argument";
718         FILE *pipefd = setup(fds, auclass);
719         ATF_REQUIRE_EQ(-1, shmctl(shmid, -1, &shmbuff));
720         check_audit(fds, regex, pipefd);
721
722         /* Destroy the shared memory with ID = shmid */
723         ATF_REQUIRE_EQ(0, shmctl(shmid, IPC_RMID, NULL));
724 }
725
726 ATF_TC_CLEANUP(shmctl_illegal_command, tc)
727 {
728         cleanup();
729 }
730
731
732 ATF_TP_ADD_TCS(tp)
733 {
734         ATF_TP_ADD_TC(tp, msgget_success);
735         ATF_TP_ADD_TC(tp, msgget_failure);
736         ATF_TP_ADD_TC(tp, msgsnd_success);
737         ATF_TP_ADD_TC(tp, msgsnd_failure);
738         ATF_TP_ADD_TC(tp, msgrcv_success);
739         ATF_TP_ADD_TC(tp, msgrcv_failure);
740
741         ATF_TP_ADD_TC(tp, msgctl_rmid_success);
742         ATF_TP_ADD_TC(tp, msgctl_rmid_failure);
743         ATF_TP_ADD_TC(tp, msgctl_stat_success);
744         ATF_TP_ADD_TC(tp, msgctl_stat_failure);
745         ATF_TP_ADD_TC(tp, msgctl_set_success);
746         ATF_TP_ADD_TC(tp, msgctl_set_failure);
747         ATF_TP_ADD_TC(tp, msgctl_illegal_command);
748
749         ATF_TP_ADD_TC(tp, shmget_success);
750         ATF_TP_ADD_TC(tp, shmget_failure);
751         ATF_TP_ADD_TC(tp, shmat_success);
752         ATF_TP_ADD_TC(tp, shmat_failure);
753         ATF_TP_ADD_TC(tp, shmdt_success);
754         ATF_TP_ADD_TC(tp, shmdt_failure);
755
756         ATF_TP_ADD_TC(tp, shmctl_rmid_success);
757         ATF_TP_ADD_TC(tp, shmctl_rmid_failure);
758         ATF_TP_ADD_TC(tp, shmctl_stat_success);
759         ATF_TP_ADD_TC(tp, shmctl_stat_failure);
760         ATF_TP_ADD_TC(tp, shmctl_set_success);
761         ATF_TP_ADD_TC(tp, shmctl_set_failure);
762         ATF_TP_ADD_TC(tp, shmctl_illegal_command);
763
764         return (atf_no_error());
765 }