xref: /freebsd/tests/sys/audit/file-write.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*-
2  * Copyright 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 
26 #include <atf-c.h>
27 #include <fcntl.h>
28 
29 #include "utils.h"
30 
31 static struct pollfd fds[1];
32 static mode_t mode = 0777;
33 static int filedesc;
34 static off_t offlen = 0;
35 static const char *path = "fileforaudit";
36 static const char *errpath = "dirdoesnotexist/fileforaudit";
37 static const char *successreg = "fileforaudit.*return,success";
38 static const char *failurereg = "fileforaudit.*return,failure";
39 
40 
41 ATF_TC_WITH_CLEANUP(truncate_success);
42 ATF_TC_HEAD(truncate_success, tc)
43 {
44 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
45 					"truncate(2) call");
46 }
47 
48 ATF_TC_BODY(truncate_success, tc)
49 {
50 	/* File needs to exist to call truncate(2) */
51 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
52 	FILE *pipefd = setup(fds, "fw");
53 	ATF_REQUIRE_EQ(0, truncate(path, offlen));
54 	check_audit(fds, successreg, pipefd);
55 	close(filedesc);
56 }
57 
58 ATF_TC_CLEANUP(truncate_success, tc)
59 {
60 	cleanup();
61 }
62 
63 
64 ATF_TC_WITH_CLEANUP(truncate_failure);
65 ATF_TC_HEAD(truncate_failure, tc)
66 {
67 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
68 					"truncate(2) call");
69 }
70 
71 ATF_TC_BODY(truncate_failure, tc)
72 {
73 	FILE *pipefd = setup(fds, "fw");
74 	/* Failure reason: file does not exist */
75 	ATF_REQUIRE_EQ(-1, truncate(errpath, offlen));
76 	check_audit(fds, failurereg, pipefd);
77 }
78 
79 ATF_TC_CLEANUP(truncate_failure, tc)
80 {
81 	cleanup();
82 }
83 
84 
85 ATF_TC_WITH_CLEANUP(ftruncate_success);
86 ATF_TC_HEAD(ftruncate_success, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
89 					"ftruncate(2) call");
90 }
91 
92 ATF_TC_BODY(ftruncate_success, tc)
93 {
94 	const char *regex = "ftruncate.*return,success";
95 	/* Valid file descriptor needs to exist to call ftruncate(2) */
96 	ATF_REQUIRE((filedesc = open(path, O_CREAT | O_RDWR)) != -1);
97 	FILE *pipefd = setup(fds, "fw");
98 	ATF_REQUIRE_EQ(0, ftruncate(filedesc, offlen));
99 	check_audit(fds, regex, pipefd);
100 	close(filedesc);
101 }
102 
103 ATF_TC_CLEANUP(ftruncate_success, tc)
104 {
105 	cleanup();
106 }
107 
108 
109 ATF_TC_WITH_CLEANUP(ftruncate_failure);
110 ATF_TC_HEAD(ftruncate_failure, tc)
111 {
112 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
113 					"ftruncate(2) call");
114 }
115 
116 ATF_TC_BODY(ftruncate_failure, tc)
117 {
118 	const char *regex = "ftruncate.*return,failure";
119 	FILE *pipefd = setup(fds, "fw");
120 	/* Failure reason: bad file descriptor */
121 	ATF_REQUIRE_EQ(-1, ftruncate(-1, offlen));
122 	check_audit(fds, regex, pipefd);
123 }
124 
125 ATF_TC_CLEANUP(ftruncate_failure, tc)
126 {
127 	cleanup();
128 }
129 
130 
131 ATF_TP_ADD_TCS(tp)
132 {
133 	ATF_TP_ADD_TC(tp, truncate_success);
134 	ATF_TP_ADD_TC(tp, truncate_failure);
135 	ATF_TP_ADD_TC(tp, ftruncate_success);
136 	ATF_TP_ADD_TC(tp, ftruncate_failure);
137 
138 	return (atf_no_error());
139 }
140