]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/audit/file-attribute-modify.c
MFC r335261, r335275, r335284-r335285, r335294, r335318, r335320, r335703
[FreeBSD/FreeBSD.git] / tests / sys / audit / file-attribute-modify.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/extattr.h>
30 #include <sys/file.h>
31 #include <sys/stat.h>
32
33 #include <atf-c.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #include "utils.h"
38
39 static pid_t pid;
40 static uid_t uid = -1;
41 static gid_t gid = -1;
42 static int filedesc, retval;
43 static struct pollfd fds[1];
44 static mode_t mode = 0777;
45 static char extregex[80];
46 static char buff[] = "ezio";
47 static const char *auclass = "fm";
48 static const char *name = "authorname";
49 static const char *path = "fileforaudit";
50 static const char *errpath = "adirhasnoname/fileforaudit";
51 static const char *successreg = "fileforaudit.*return,success";
52 static const char *failurereg = "fileforaudit.*return,failure";
53
54
55 ATF_TC_WITH_CLEANUP(flock_success);
56 ATF_TC_HEAD(flock_success, tc)
57 {
58         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
59                                         "flock(2) call");
60 }
61
62 ATF_TC_BODY(flock_success, tc)
63 {
64         pid = getpid();
65         snprintf(extregex, sizeof(extregex), "flock.*%d.*return,success", pid);
66
67         /* File needs to exist to call flock(2) */
68         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
69         FILE *pipefd = setup(fds, auclass);
70         ATF_REQUIRE_EQ(0, flock(filedesc, LOCK_SH));
71         check_audit(fds, extregex, pipefd);
72         close(filedesc);
73 }
74
75 ATF_TC_CLEANUP(flock_success, tc)
76 {
77         cleanup();
78 }
79
80
81 ATF_TC_WITH_CLEANUP(flock_failure);
82 ATF_TC_HEAD(flock_failure, tc)
83 {
84         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
85                                         "flock(2) call");
86 }
87
88 ATF_TC_BODY(flock_failure, tc)
89 {
90         const char *regex = "flock.*return,failure : Bad file descriptor";
91         FILE *pipefd = setup(fds, auclass);
92         ATF_REQUIRE_EQ(-1, flock(-1, LOCK_SH));
93         check_audit(fds, regex, pipefd);
94 }
95
96 ATF_TC_CLEANUP(flock_failure, tc)
97 {
98         cleanup();
99 }
100
101
102 ATF_TC_WITH_CLEANUP(fcntl_success);
103 ATF_TC_HEAD(fcntl_success, tc)
104 {
105         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
106                                         "fcntl(2) call");
107 }
108
109 ATF_TC_BODY(fcntl_success, tc)
110 {
111         int flagstatus;
112         /* File needs to exist to call fcntl(2) */
113         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
114         FILE *pipefd = setup(fds, auclass);
115
116         /* Retrieve the status flags of 'filedesc' and store it in flagstatus */
117         ATF_REQUIRE((flagstatus = fcntl(filedesc, F_GETFL, 0)) != -1);
118         snprintf(extregex, sizeof(extregex),
119                         "fcntl.*return,success,%d", flagstatus);
120         check_audit(fds, extregex, pipefd);
121         close(filedesc);
122 }
123
124 ATF_TC_CLEANUP(fcntl_success, tc)
125 {
126         cleanup();
127 }
128
129
130 ATF_TC_WITH_CLEANUP(fcntl_failure);
131 ATF_TC_HEAD(fcntl_failure, tc)
132 {
133         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
134                                         "fcntl(2) call");
135 }
136
137 ATF_TC_BODY(fcntl_failure, tc)
138 {
139         const char *regex = "fcntl.*return,failure : Bad file descriptor";
140         FILE *pipefd = setup(fds, auclass);
141         ATF_REQUIRE_EQ(-1, fcntl(-1, F_GETFL, 0));
142         check_audit(fds, regex, pipefd);
143 }
144
145 ATF_TC_CLEANUP(fcntl_failure, tc)
146 {
147         cleanup();
148 }
149
150
151 ATF_TC_WITH_CLEANUP(fsync_success);
152 ATF_TC_HEAD(fsync_success, tc)
153 {
154         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
155                                         "fsync(2) call");
156 }
157
158 ATF_TC_BODY(fsync_success, tc)
159 {
160         pid = getpid();
161         snprintf(extregex, sizeof(extregex), "fsync.*%d.*return,success", pid);
162
163         /* File needs to exist to call fsync(2) */
164         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
165         FILE *pipefd = setup(fds, auclass);
166         ATF_REQUIRE_EQ(0, fsync(filedesc));
167         check_audit(fds, extregex, pipefd);
168         close(filedesc);
169 }
170
171 ATF_TC_CLEANUP(fsync_success, tc)
172 {
173         cleanup();
174 }
175
176
177 ATF_TC_WITH_CLEANUP(fsync_failure);
178 ATF_TC_HEAD(fsync_failure, tc)
179 {
180         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
181                                         "fsync(2) call");
182 }
183
184 ATF_TC_BODY(fsync_failure, tc)
185 {
186         const char *regex = "fsync.*return,failure : Bad file descriptor";
187         FILE *pipefd = setup(fds, auclass);
188         /* Failure reason: Invalid file descriptor */
189         ATF_REQUIRE_EQ(-1, fsync(-1));
190         check_audit(fds, regex, pipefd);
191 }
192
193 ATF_TC_CLEANUP(fsync_failure, tc)
194 {
195         cleanup();
196 }
197
198
199 ATF_TC_WITH_CLEANUP(chmod_success);
200 ATF_TC_HEAD(chmod_success, tc)
201 {
202         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
203                                         "chmod(2) call");
204 }
205
206 ATF_TC_BODY(chmod_success, tc)
207 {
208         /* File needs to exist to call chmod(2) */
209         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
210         FILE *pipefd = setup(fds, auclass);
211         ATF_REQUIRE_EQ(0, chmod(path, mode));
212         check_audit(fds, successreg, pipefd);
213         close(filedesc);
214 }
215
216 ATF_TC_CLEANUP(chmod_success, tc)
217 {
218         cleanup();
219 }
220
221
222 ATF_TC_WITH_CLEANUP(chmod_failure);
223 ATF_TC_HEAD(chmod_failure, tc)
224 {
225         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
226                                         "chmod(2) call");
227 }
228
229 ATF_TC_BODY(chmod_failure, tc)
230 {
231         FILE *pipefd = setup(fds, auclass);
232         /* Failure reason: file does not exist */
233         ATF_REQUIRE_EQ(-1, chmod(errpath, mode));
234         check_audit(fds, failurereg, pipefd);
235 }
236
237 ATF_TC_CLEANUP(chmod_failure, tc)
238 {
239         cleanup();
240 }
241
242
243 ATF_TC_WITH_CLEANUP(fchmod_success);
244 ATF_TC_HEAD(fchmod_success, tc)
245 {
246         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
247                                         "fchmod(2) call");
248 }
249
250 ATF_TC_BODY(fchmod_success, tc)
251 {
252         pid = getpid();
253         snprintf(extregex, sizeof(extregex), "fchmod.*%d.*return,success", pid);
254
255         /* File needs to exist to call fchmod(2) */
256         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
257         FILE *pipefd = setup(fds, auclass);
258         ATF_REQUIRE_EQ(0, fchmod(filedesc, mode));
259         check_audit(fds, extregex, pipefd);
260         close(filedesc);
261 }
262
263 ATF_TC_CLEANUP(fchmod_success, tc)
264 {
265         cleanup();
266 }
267
268
269 ATF_TC_WITH_CLEANUP(fchmod_failure);
270 ATF_TC_HEAD(fchmod_failure, tc)
271 {
272         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
273                                         "fchmod(2) call");
274 }
275
276 ATF_TC_BODY(fchmod_failure, tc)
277 {
278         const char *regex = "fchmod.*return,failure : Bad file descriptor";
279         FILE *pipefd = setup(fds, auclass);
280         /* Failure reason: Invalid file descriptor */
281         ATF_REQUIRE_EQ(-1, fchmod(-1, mode));
282         check_audit(fds, regex, pipefd);
283 }
284
285 ATF_TC_CLEANUP(fchmod_failure, tc)
286 {
287         cleanup();
288 }
289
290
291 ATF_TC_WITH_CLEANUP(lchmod_success);
292 ATF_TC_HEAD(lchmod_success, tc)
293 {
294         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
295                                         "lchmod(2) call");
296 }
297
298 ATF_TC_BODY(lchmod_success, tc)
299 {
300         /* Symbolic link needs to exist to call lchmod(2) */
301         ATF_REQUIRE_EQ(0, symlink("symlink", path));
302         FILE *pipefd = setup(fds, auclass);
303         ATF_REQUIRE_EQ(0, lchmod(path, mode));
304         check_audit(fds, successreg, pipefd);
305 }
306
307 ATF_TC_CLEANUP(lchmod_success, tc)
308 {
309         cleanup();
310 }
311
312
313 ATF_TC_WITH_CLEANUP(lchmod_failure);
314 ATF_TC_HEAD(lchmod_failure, tc)
315 {
316         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
317                                         "lchmod(2) call");
318 }
319
320 ATF_TC_BODY(lchmod_failure, tc)
321 {
322         FILE *pipefd = setup(fds, auclass);
323         /* Failure reason: file does not exist */
324         ATF_REQUIRE_EQ(-1, lchmod(errpath, mode));
325         check_audit(fds, failurereg, pipefd);
326 }
327
328 ATF_TC_CLEANUP(lchmod_failure, tc)
329 {
330         cleanup();
331 }
332
333
334 ATF_TC_WITH_CLEANUP(fchmodat_success);
335 ATF_TC_HEAD(fchmodat_success, tc)
336 {
337         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
338                                         "fchmodat(2) call");
339 }
340
341 ATF_TC_BODY(fchmodat_success, tc)
342 {
343         /* File needs to exist to call fchmodat(2) */
344         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
345         FILE *pipefd = setup(fds, auclass);
346         ATF_REQUIRE_EQ(0, fchmodat(AT_FDCWD, path, mode, 0));
347         check_audit(fds, successreg, pipefd);
348         close(filedesc);
349 }
350
351 ATF_TC_CLEANUP(fchmodat_success, tc)
352 {
353         cleanup();
354 }
355
356
357 ATF_TC_WITH_CLEANUP(fchmodat_failure);
358 ATF_TC_HEAD(fchmodat_failure, tc)
359 {
360         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
361                                         "fchmodat(2) call");
362 }
363
364 ATF_TC_BODY(fchmodat_failure, tc)
365 {
366         FILE *pipefd = setup(fds, auclass);
367         /* Failure reason: file does not exist */
368         ATF_REQUIRE_EQ(-1, fchmodat(AT_FDCWD, errpath, mode, 0));
369         check_audit(fds, failurereg, pipefd);
370 }
371
372 ATF_TC_CLEANUP(fchmodat_failure, tc)
373 {
374         cleanup();
375 }
376
377
378 ATF_TC_WITH_CLEANUP(chown_success);
379 ATF_TC_HEAD(chown_success, tc)
380 {
381         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
382                                         "chown(2) call");
383 }
384
385 ATF_TC_BODY(chown_success, tc)
386 {
387         /* File needs to exist to call chown(2) */
388         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
389         FILE *pipefd = setup(fds, auclass);
390         ATF_REQUIRE_EQ(0, chown(path, uid, gid));
391         check_audit(fds, successreg, pipefd);
392         close(filedesc);
393 }
394
395 ATF_TC_CLEANUP(chown_success, tc)
396 {
397         cleanup();
398 }
399
400
401 ATF_TC_WITH_CLEANUP(chown_failure);
402 ATF_TC_HEAD(chown_failure, tc)
403 {
404         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
405                                         "chown(2) call");
406 }
407
408 ATF_TC_BODY(chown_failure, tc)
409 {
410         FILE *pipefd = setup(fds, auclass);
411         /* Failure reason: file does not exist */
412         ATF_REQUIRE_EQ(-1, chown(errpath, uid, gid));
413         check_audit(fds, failurereg, pipefd);
414 }
415
416 ATF_TC_CLEANUP(chown_failure, tc)
417 {
418         cleanup();
419 }
420
421
422 ATF_TC_WITH_CLEANUP(fchown_success);
423 ATF_TC_HEAD(fchown_success, tc)
424 {
425         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
426                                         "fchown(2) call");
427 }
428
429 ATF_TC_BODY(fchown_success, tc)
430 {
431         pid = getpid();
432         snprintf(extregex, sizeof(extregex), "fchown.*%d.*return,success", pid);
433
434         /* File needs to exist to call fchown(2) */
435         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
436         FILE *pipefd = setup(fds, auclass);
437         ATF_REQUIRE_EQ(0, fchown(filedesc, uid, gid));
438         check_audit(fds, extregex, pipefd);
439         close(filedesc);
440 }
441
442 ATF_TC_CLEANUP(fchown_success, tc)
443 {
444         cleanup();
445 }
446
447
448 ATF_TC_WITH_CLEANUP(fchown_failure);
449 ATF_TC_HEAD(fchown_failure, tc)
450 {
451         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
452                                         "fchown(2) call");
453 }
454
455 ATF_TC_BODY(fchown_failure, tc)
456 {
457         const char *regex = "fchown.*return,failure : Bad file descriptor";
458         FILE *pipefd = setup(fds, auclass);
459         /* Failure reason: Invalid file descriptor */
460         ATF_REQUIRE_EQ(-1, fchown(-1, uid, gid));
461         check_audit(fds, regex, pipefd);
462 }
463
464 ATF_TC_CLEANUP(fchown_failure, tc)
465 {
466         cleanup();
467 }
468
469
470 ATF_TC_WITH_CLEANUP(lchown_success);
471 ATF_TC_HEAD(lchown_success, tc)
472 {
473         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
474                                         "lchown(2) call");
475 }
476
477 ATF_TC_BODY(lchown_success, tc)
478 {
479         /* Symbolic link needs to exist to call lchown(2) */
480         ATF_REQUIRE_EQ(0, symlink("symlink", path));
481         FILE *pipefd = setup(fds, auclass);
482         ATF_REQUIRE_EQ(0, lchown(path, uid, gid));
483         check_audit(fds, successreg, pipefd);
484 }
485
486 ATF_TC_CLEANUP(lchown_success, tc)
487 {
488         cleanup();
489 }
490
491
492 ATF_TC_WITH_CLEANUP(lchown_failure);
493 ATF_TC_HEAD(lchown_failure, tc)
494 {
495         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
496                                         "lchown(2) call");
497 }
498
499 ATF_TC_BODY(lchown_failure, tc)
500 {
501         FILE *pipefd = setup(fds, auclass);
502         /* Failure reason: Symbolic link does not exist */
503         ATF_REQUIRE_EQ(-1, lchown(errpath, uid, gid));
504         check_audit(fds, failurereg, pipefd);
505 }
506
507 ATF_TC_CLEANUP(lchown_failure, tc)
508 {
509         cleanup();
510 }
511
512
513 ATF_TC_WITH_CLEANUP(fchownat_success);
514 ATF_TC_HEAD(fchownat_success, tc)
515 {
516         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
517                                         "fchownat(2) call");
518 }
519
520 ATF_TC_BODY(fchownat_success, tc)
521 {
522         /* File needs to exist to call fchownat(2) */
523         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
524         FILE *pipefd = setup(fds, auclass);
525         ATF_REQUIRE_EQ(0, fchownat(AT_FDCWD, path, uid, gid, 0));
526         check_audit(fds, successreg, pipefd);
527         close(filedesc);
528 }
529
530 ATF_TC_CLEANUP(fchownat_success, tc)
531 {
532         cleanup();
533 }
534
535
536 ATF_TC_WITH_CLEANUP(fchownat_failure);
537 ATF_TC_HEAD(fchownat_failure, tc)
538 {
539         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
540                                         "fchownat(2) call");
541 }
542
543 ATF_TC_BODY(fchownat_failure, tc)
544 {
545         FILE *pipefd = setup(fds, auclass);
546         /* Failure reason: file does not exist */
547         ATF_REQUIRE_EQ(-1, fchownat(AT_FDCWD, errpath, uid, gid, 0));
548         check_audit(fds, failurereg, pipefd);
549 }
550
551 ATF_TC_CLEANUP(fchownat_failure, tc)
552 {
553         cleanup();
554 }
555
556
557 ATF_TC_WITH_CLEANUP(chflags_success);
558 ATF_TC_HEAD(chflags_success, tc)
559 {
560         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
561                                         "chflags(2) call");
562 }
563
564 ATF_TC_BODY(chflags_success, tc)
565 {
566         /* File needs to exist to call chflags(2) */
567         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
568         FILE *pipefd = setup(fds, auclass);
569         ATF_REQUIRE_EQ(0, chflags(path, UF_OFFLINE));
570         check_audit(fds, successreg, pipefd);
571         close(filedesc);
572 }
573
574 ATF_TC_CLEANUP(chflags_success, tc)
575 {
576         cleanup();
577 }
578
579
580 ATF_TC_WITH_CLEANUP(chflags_failure);
581 ATF_TC_HEAD(chflags_failure, tc)
582 {
583         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
584                                         "chflags(2) call");
585 }
586
587 ATF_TC_BODY(chflags_failure, tc)
588 {
589         FILE *pipefd = setup(fds, auclass);
590         /* Failure reason: file does not exist */
591         ATF_REQUIRE_EQ(-1, chflags(errpath, UF_OFFLINE));
592         check_audit(fds, failurereg, pipefd);
593 }
594
595 ATF_TC_CLEANUP(chflags_failure, tc)
596 {
597         cleanup();
598 }
599
600
601 ATF_TC_WITH_CLEANUP(fchflags_success);
602 ATF_TC_HEAD(fchflags_success, tc)
603 {
604         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
605                                         "fchflags(2) call");
606 }
607
608 ATF_TC_BODY(fchflags_success, tc)
609 {
610         pid = getpid();
611         snprintf(extregex, sizeof(extregex), "fchflags.*%d.*ret.*success", pid);
612         /* File needs to exist to call fchflags(2) */
613         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
614
615         FILE *pipefd = setup(fds, auclass);
616         ATF_REQUIRE_EQ(0, fchflags(filedesc, UF_OFFLINE));
617         check_audit(fds, extregex, pipefd);
618         close(filedesc);
619 }
620
621 ATF_TC_CLEANUP(fchflags_success, tc)
622 {
623         cleanup();
624 }
625
626
627 ATF_TC_WITH_CLEANUP(fchflags_failure);
628 ATF_TC_HEAD(fchflags_failure, tc)
629 {
630         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
631                                         "fchflags(2) call");
632 }
633
634 ATF_TC_BODY(fchflags_failure, tc)
635 {
636         const char *regex = "fchflags.*return,failure : Bad file descriptor";
637         FILE *pipefd = setup(fds, auclass);
638         /* Failure reason: Invalid file descriptor */
639         ATF_REQUIRE_EQ(-1, fchflags(-1, UF_OFFLINE));
640         check_audit(fds, regex, pipefd);
641 }
642
643 ATF_TC_CLEANUP(fchflags_failure, tc)
644 {
645         cleanup();
646 }
647
648
649 ATF_TC_WITH_CLEANUP(lchflags_success);
650 ATF_TC_HEAD(lchflags_success, tc)
651 {
652         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
653                                         "lchflags(2) call");
654 }
655
656 ATF_TC_BODY(lchflags_success, tc)
657 {
658         /* Symbolic link needs to exist to call lchflags(2) */
659         ATF_REQUIRE_EQ(0, symlink("symlink", path));
660         FILE *pipefd = setup(fds, auclass);
661         ATF_REQUIRE_EQ(0, lchflags(path, UF_OFFLINE));
662         check_audit(fds, successreg, pipefd);
663 }
664
665 ATF_TC_CLEANUP(lchflags_success, tc)
666 {
667         cleanup();
668 }
669
670
671 ATF_TC_WITH_CLEANUP(lchflags_failure);
672 ATF_TC_HEAD(lchflags_failure, tc)
673 {
674         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
675                                         "lchflags(2) call");
676 }
677
678 ATF_TC_BODY(lchflags_failure, tc)
679 {
680         FILE *pipefd = setup(fds, auclass);
681         /* Failure reason: Symbolic link does not exist */
682         ATF_REQUIRE_EQ(-1, lchflags(errpath, UF_OFFLINE));
683         check_audit(fds, failurereg, pipefd);
684 }
685
686 ATF_TC_CLEANUP(lchflags_failure, tc)
687 {
688         cleanup();
689 }
690
691
692 ATF_TC_WITH_CLEANUP(extattr_set_file_success);
693 ATF_TC_HEAD(extattr_set_file_success, tc)
694 {
695         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
696                                         "extattr_set_file(2) call");
697 }
698
699 ATF_TC_BODY(extattr_set_file_success, tc)
700 {
701         /* File needs to exist to call extattr_set_file(2) */
702         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
703         /* Prepare the regex to be checked in the audit record */
704         snprintf(extregex, sizeof(extregex),
705                 "extattr_set_file.*%s.*%s.*return,success", path, name);
706
707         FILE *pipefd = setup(fds, auclass);
708         ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
709                 EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
710         check_audit(fds, extregex, pipefd);
711         close(filedesc);
712 }
713
714 ATF_TC_CLEANUP(extattr_set_file_success, tc)
715 {
716         cleanup();
717 }
718
719
720 ATF_TC_WITH_CLEANUP(extattr_set_file_failure);
721 ATF_TC_HEAD(extattr_set_file_failure, tc)
722 {
723         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
724                                         "extattr_set_file(2) call");
725 }
726
727 ATF_TC_BODY(extattr_set_file_failure, tc)
728 {
729         /* Prepare the regex to be checked in the audit record */
730         snprintf(extregex, sizeof(extregex),
731                 "extattr_set_file.*%s.*%s.*failure", path, name);
732
733         FILE *pipefd = setup(fds, auclass);
734         /* Failure reason: file does not exist */
735         ATF_REQUIRE_EQ(-1, extattr_set_file(path,
736                 EXTATTR_NAMESPACE_USER, name, NULL, 0));
737         check_audit(fds, extregex, pipefd);
738 }
739
740 ATF_TC_CLEANUP(extattr_set_file_failure, tc)
741 {
742         cleanup();
743 }
744
745
746 ATF_TC_WITH_CLEANUP(extattr_set_fd_success);
747 ATF_TC_HEAD(extattr_set_fd_success, tc)
748 {
749         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
750                                         "extattr_set_fd(2) call");
751 }
752
753 ATF_TC_BODY(extattr_set_fd_success, tc)
754 {
755         /* File needs to exist to call extattr_set_fd(2) */
756         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
757
758         /* Prepare the regex to be checked in the audit record */
759         snprintf(extregex, sizeof(extregex),
760                 "extattr_set_fd.*%s.*return,success", name);
761
762         FILE *pipefd = setup(fds, auclass);
763         ATF_REQUIRE_EQ(sizeof(buff), extattr_set_fd(filedesc,
764                 EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
765         check_audit(fds, extregex, pipefd);
766         close(filedesc);
767 }
768
769 ATF_TC_CLEANUP(extattr_set_fd_success, tc)
770 {
771         cleanup();
772 }
773
774
775 ATF_TC_WITH_CLEANUP(extattr_set_fd_failure);
776 ATF_TC_HEAD(extattr_set_fd_failure, tc)
777 {
778         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
779                                         "extattr_set_fd(2) call");
780 }
781
782 ATF_TC_BODY(extattr_set_fd_failure, tc)
783 {
784         /* Prepare the regex to be checked in the audit record */
785         snprintf(extregex, sizeof(extregex),
786         "extattr_set_fd.*%s.*return,failure : Bad file descriptor", name);
787
788         FILE *pipefd = setup(fds, auclass);
789         /* Failure reason: Invalid file descriptor */
790         ATF_REQUIRE_EQ(-1, extattr_set_fd(-1,
791                 EXTATTR_NAMESPACE_USER, name, NULL, 0));
792         check_audit(fds, extregex, pipefd);
793 }
794
795 ATF_TC_CLEANUP(extattr_set_fd_failure, tc)
796 {
797         cleanup();
798 }
799
800
801 ATF_TC_WITH_CLEANUP(extattr_set_link_success);
802 ATF_TC_HEAD(extattr_set_link_success, tc)
803 {
804         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
805                                         "extattr_set_link(2) call");
806 }
807
808 ATF_TC_BODY(extattr_set_link_success, tc)
809 {
810         /* Symbolic link needs to exist to call extattr_set_link(2) */
811         ATF_REQUIRE_EQ(0, symlink("symlink", path));
812         /* Prepare the regex to be checked in the audit record */
813         snprintf(extregex, sizeof(extregex),
814                 "extattr_set_link.*%s.*%s.*return,success", path, name);
815
816         FILE *pipefd = setup(fds, auclass);
817         ATF_REQUIRE_EQ(sizeof(buff), extattr_set_link(path,
818                 EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
819
820         check_audit(fds, extregex, pipefd);
821 }
822
823 ATF_TC_CLEANUP(extattr_set_link_success, tc)
824 {
825         cleanup();
826 }
827
828
829 ATF_TC_WITH_CLEANUP(extattr_set_link_failure);
830 ATF_TC_HEAD(extattr_set_link_failure, tc)
831 {
832         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
833                                         "extattr_set_link(2) call");
834 }
835
836 ATF_TC_BODY(extattr_set_link_failure, tc)
837 {
838         /* Prepare the regex to be checked in the audit record */
839         snprintf(extregex, sizeof(extregex),
840                 "extattr_set_link.*%s.*%s.*failure", path, name);
841         FILE *pipefd = setup(fds, auclass);
842         /* Failure reason: symbolic link does not exist */
843         ATF_REQUIRE_EQ(-1, extattr_set_link(path,
844                 EXTATTR_NAMESPACE_USER, name, NULL, 0));
845         check_audit(fds, extregex, pipefd);
846 }
847
848 ATF_TC_CLEANUP(extattr_set_link_failure, tc)
849 {
850         cleanup();
851 }
852
853
854 ATF_TC_WITH_CLEANUP(extattr_delete_file_success);
855 ATF_TC_HEAD(extattr_delete_file_success, tc)
856 {
857         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
858                                         "extattr_delete_file(2) call");
859 }
860
861 ATF_TC_BODY(extattr_delete_file_success, tc)
862 {
863         /* File needs to exist to call extattr_delete_file(2) */
864         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
865         ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
866                 EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
867
868         FILE *pipefd = setup(fds, auclass);
869         ATF_REQUIRE((retval = extattr_delete_file(path,
870                 EXTATTR_NAMESPACE_USER, name)) != -1);
871         /* Prepare the regex to be checked in the audit record */
872         snprintf(extregex, sizeof(extregex),
873         "extattr_delete_file.*%s.*return,success,%d", path, retval);
874         check_audit(fds, extregex, pipefd);
875         close(filedesc);
876 }
877
878 ATF_TC_CLEANUP(extattr_delete_file_success, tc)
879 {
880         cleanup();
881 }
882
883
884 ATF_TC_WITH_CLEANUP(extattr_delete_file_failure);
885 ATF_TC_HEAD(extattr_delete_file_failure, tc)
886 {
887         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
888                                         "extattr_delete_file(2) call");
889 }
890
891 ATF_TC_BODY(extattr_delete_file_failure, tc)
892 {
893         /* Prepare the regex to be checked in the audit record */
894         snprintf(extregex, sizeof(extregex),
895                 "extattr_delete_file.*%s.*return,failure", path);
896
897         FILE *pipefd = setup(fds, auclass);
898         /* Failure reason: file does not exist */
899         ATF_REQUIRE_EQ(-1, extattr_delete_file(path,
900                 EXTATTR_NAMESPACE_USER, name));
901         check_audit(fds, extregex, pipefd);
902 }
903
904 ATF_TC_CLEANUP(extattr_delete_file_failure, tc)
905 {
906         cleanup();
907 }
908
909
910 ATF_TC_WITH_CLEANUP(extattr_delete_fd_success);
911 ATF_TC_HEAD(extattr_delete_fd_success, tc)
912 {
913         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
914                                         "extattr_delete_fd(2) call");
915 }
916
917 ATF_TC_BODY(extattr_delete_fd_success, tc)
918 {
919         /* File needs to exist to call extattr_delete_fd(2) */
920         ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
921         ATF_REQUIRE_EQ(sizeof(buff), extattr_set_file(path,
922                 EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
923
924         FILE *pipefd = setup(fds, auclass);
925         ATF_REQUIRE((retval = extattr_delete_fd(filedesc,
926                 EXTATTR_NAMESPACE_USER, name)) != -1);
927         /* Prepare the regex to be checked in the audit record */
928         snprintf(extregex, sizeof(extregex),
929                 "extattr_delete_fd.*return,success,%d", retval);
930         check_audit(fds, extregex, pipefd);
931         close(filedesc);
932 }
933
934 ATF_TC_CLEANUP(extattr_delete_fd_success, tc)
935 {
936         cleanup();
937 }
938
939
940 ATF_TC_WITH_CLEANUP(extattr_delete_fd_failure);
941 ATF_TC_HEAD(extattr_delete_fd_failure, tc)
942 {
943         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
944                                         "extattr_delete_fd(2) call");
945 }
946
947 ATF_TC_BODY(extattr_delete_fd_failure, tc)
948 {
949         /* Prepare the regex to be checked in the audit record */
950         snprintf(extregex, sizeof(extregex),
951                 "extattr_delete_fd.*return,failure : Bad file descriptor");
952
953         FILE *pipefd = setup(fds, auclass);
954         /* Failure reason: Invalid file descriptor */
955         ATF_REQUIRE_EQ(-1, extattr_delete_fd(-1, EXTATTR_NAMESPACE_USER, name));
956         check_audit(fds, extregex, pipefd);
957 }
958
959 ATF_TC_CLEANUP(extattr_delete_fd_failure, tc)
960 {
961         cleanup();
962 }
963
964
965 ATF_TC_WITH_CLEANUP(extattr_delete_link_success);
966 ATF_TC_HEAD(extattr_delete_link_success, tc)
967 {
968         atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
969                                         "extattr_delete_link(2) call");
970 }
971
972 ATF_TC_BODY(extattr_delete_link_success, tc)
973 {
974         /* Symbolic link needs to exist to call extattr_delete_link(2) */
975         ATF_REQUIRE_EQ(0, symlink("symlink", path));
976         ATF_REQUIRE_EQ(sizeof(buff), extattr_set_link(path,
977                 EXTATTR_NAMESPACE_USER, name, buff, sizeof(buff)));
978
979         FILE *pipefd = setup(fds, auclass);
980         ATF_REQUIRE((retval = extattr_delete_link(path,
981                 EXTATTR_NAMESPACE_USER, name)) != -1);
982         /* Prepare the regex to be checked in the audit record */
983         snprintf(extregex, sizeof(extregex),
984         "extattr_delete_link.*%s.*return,success,%d", path, retval);
985         check_audit(fds, extregex, pipefd);
986 }
987
988 ATF_TC_CLEANUP(extattr_delete_link_success, tc)
989 {
990         cleanup();
991 }
992
993
994 ATF_TC_WITH_CLEANUP(extattr_delete_link_failure);
995 ATF_TC_HEAD(extattr_delete_link_failure, tc)
996 {
997         atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
998                                         "extattr_delete_link(2) call");
999 }
1000
1001 ATF_TC_BODY(extattr_delete_link_failure, tc)
1002 {
1003         /* Prepare the regex to be checked in the audit record */
1004         snprintf(extregex, sizeof(extregex),
1005                 "extattr_delete_link.*%s.*failure", path);
1006         FILE *pipefd = setup(fds, auclass);
1007         /* Failure reason: symbolic link does not exist */
1008         ATF_REQUIRE_EQ(-1, extattr_delete_link(path,
1009                 EXTATTR_NAMESPACE_USER, name));
1010         check_audit(fds, extregex, pipefd);
1011 }
1012
1013 ATF_TC_CLEANUP(extattr_delete_link_failure, tc)
1014 {
1015         cleanup();
1016 }
1017
1018
1019 ATF_TP_ADD_TCS(tp)
1020 {
1021         ATF_TP_ADD_TC(tp, flock_success);
1022         ATF_TP_ADD_TC(tp, flock_failure);
1023         ATF_TP_ADD_TC(tp, fcntl_success);
1024         ATF_TP_ADD_TC(tp, fcntl_failure);
1025         ATF_TP_ADD_TC(tp, fsync_success);
1026         ATF_TP_ADD_TC(tp, fsync_failure);
1027
1028         ATF_TP_ADD_TC(tp, chmod_success);
1029         ATF_TP_ADD_TC(tp, chmod_failure);
1030         ATF_TP_ADD_TC(tp, fchmod_success);
1031         ATF_TP_ADD_TC(tp, fchmod_failure);
1032         ATF_TP_ADD_TC(tp, lchmod_success);
1033         ATF_TP_ADD_TC(tp, lchmod_failure);
1034         ATF_TP_ADD_TC(tp, fchmodat_success);
1035         ATF_TP_ADD_TC(tp, fchmodat_failure);
1036
1037         ATF_TP_ADD_TC(tp, chown_success);
1038         ATF_TP_ADD_TC(tp, chown_failure);
1039         ATF_TP_ADD_TC(tp, fchown_success);
1040         ATF_TP_ADD_TC(tp, fchown_failure);
1041         ATF_TP_ADD_TC(tp, lchown_success);
1042         ATF_TP_ADD_TC(tp, lchown_failure);
1043         ATF_TP_ADD_TC(tp, fchownat_success);
1044         ATF_TP_ADD_TC(tp, fchownat_failure);
1045
1046         ATF_TP_ADD_TC(tp, chflags_success);
1047         ATF_TP_ADD_TC(tp, chflags_failure);
1048         ATF_TP_ADD_TC(tp, fchflags_success);
1049         ATF_TP_ADD_TC(tp, fchflags_failure);
1050         ATF_TP_ADD_TC(tp, lchflags_success);
1051         ATF_TP_ADD_TC(tp, lchflags_failure);
1052
1053         ATF_TP_ADD_TC(tp, extattr_set_file_success);
1054         ATF_TP_ADD_TC(tp, extattr_set_file_failure);
1055         ATF_TP_ADD_TC(tp, extattr_set_fd_success);
1056         ATF_TP_ADD_TC(tp, extattr_set_fd_failure);
1057         ATF_TP_ADD_TC(tp, extattr_set_link_success);
1058         ATF_TP_ADD_TC(tp, extattr_set_link_failure);
1059
1060         ATF_TP_ADD_TC(tp, extattr_delete_file_success);
1061         ATF_TP_ADD_TC(tp, extattr_delete_file_failure);
1062         ATF_TP_ADD_TC(tp, extattr_delete_fd_success);
1063         ATF_TP_ADD_TC(tp, extattr_delete_fd_failure);
1064         ATF_TP_ADD_TC(tp, extattr_delete_link_success);
1065         ATF_TP_ADD_TC(tp, extattr_delete_link_failure);
1066
1067         return (atf_no_error());
1068 }