xref: /freebsd/tests/sys/audit/file-delete.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 <sys/stat.h>
27 
28 #include <atf-c.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 
32 #include "utils.h"
33 
34 static struct pollfd fds[1];
35 static mode_t mode = 0777;
36 static int filedesc;
37 static const char *path = "fileforaudit";
38 static const char *errpath = "dirdoesnotexist/fileforaudit";
39 static const char *successreg = "fileforaudit.*return,success";
40 static const char *failurereg = "fileforaudit.*return,failure";
41 
42 
43 ATF_TC_WITH_CLEANUP(rmdir_success);
44 ATF_TC_HEAD(rmdir_success, tc)
45 {
46 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
47 					"rmdir(2) call");
48 }
49 
50 ATF_TC_BODY(rmdir_success, tc)
51 {
52 	ATF_REQUIRE_EQ(0, mkdir(path, mode));
53 	FILE *pipefd = setup(fds, "fd");
54 	ATF_REQUIRE_EQ(0, rmdir(path));
55 	check_audit(fds, successreg, pipefd);
56 }
57 
58 ATF_TC_CLEANUP(rmdir_success, tc)
59 {
60 	cleanup();
61 }
62 
63 
64 ATF_TC_WITH_CLEANUP(rmdir_failure);
65 ATF_TC_HEAD(rmdir_failure, tc)
66 {
67 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
68 					"rmdir(2) call");
69 }
70 
71 ATF_TC_BODY(rmdir_failure, tc)
72 {
73 	FILE *pipefd = setup(fds, "fd");
74 	/* Failure reason: directory does not exist */
75 	ATF_REQUIRE_EQ(-1, rmdir(errpath));
76 	check_audit(fds, failurereg, pipefd);
77 }
78 
79 ATF_TC_CLEANUP(rmdir_failure, tc)
80 {
81 	cleanup();
82 }
83 
84 
85 ATF_TC_WITH_CLEANUP(rename_success);
86 ATF_TC_HEAD(rename_success, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
89 					"rename(2) call");
90 }
91 
92 ATF_TC_BODY(rename_success, tc)
93 {
94 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
95 	FILE *pipefd = setup(fds, "fd");
96 	ATF_REQUIRE_EQ(0, rename(path, "renamed"));
97 	check_audit(fds, successreg, pipefd);
98 	close(filedesc);
99 }
100 
101 ATF_TC_CLEANUP(rename_success, tc)
102 {
103 	cleanup();
104 }
105 
106 
107 ATF_TC_WITH_CLEANUP(rename_failure);
108 ATF_TC_HEAD(rename_failure, tc)
109 {
110 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
111 					"rename(2) call");
112 }
113 
114 ATF_TC_BODY(rename_failure, tc)
115 {
116 	FILE *pipefd = setup(fds, "fd");
117 	/* Failure reason: file does not exist */
118 	ATF_REQUIRE_EQ(-1, rename(path, "renamed"));
119 	check_audit(fds, failurereg, pipefd);
120 }
121 
122 ATF_TC_CLEANUP(rename_failure, tc)
123 {
124 	cleanup();
125 }
126 
127 
128 ATF_TC_WITH_CLEANUP(renameat_success);
129 ATF_TC_HEAD(renameat_success, tc)
130 {
131 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
132 					"renameat(2) call");
133 }
134 
135 ATF_TC_BODY(renameat_success, tc)
136 {
137 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
138 	FILE *pipefd = setup(fds, "fd");
139 	ATF_REQUIRE_EQ(0, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
140 	check_audit(fds, successreg, pipefd);
141 	close(filedesc);
142 }
143 
144 ATF_TC_CLEANUP(renameat_success, tc)
145 {
146 	cleanup();
147 }
148 
149 
150 ATF_TC_WITH_CLEANUP(renameat_failure);
151 ATF_TC_HEAD(renameat_failure, tc)
152 {
153 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
154 					"renameat(2) call");
155 }
156 
157 ATF_TC_BODY(renameat_failure, tc)
158 {
159 	FILE *pipefd = setup(fds, "fd");
160 	/* Failure reason: file does not exist */
161 	ATF_REQUIRE_EQ(-1, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
162 	check_audit(fds, failurereg, pipefd);
163 }
164 
165 ATF_TC_CLEANUP(renameat_failure, tc)
166 {
167 	cleanup();
168 }
169 
170 
171 ATF_TC_WITH_CLEANUP(unlink_success);
172 ATF_TC_HEAD(unlink_success, tc)
173 {
174 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
175 					"unlink(2) call");
176 }
177 
178 ATF_TC_BODY(unlink_success, tc)
179 {
180 	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
181 	FILE *pipefd = setup(fds, "fd");
182 	ATF_REQUIRE_EQ(0, unlink(path));
183 	check_audit(fds, successreg, pipefd);
184 	close(filedesc);
185 }
186 
187 ATF_TC_CLEANUP(unlink_success, tc)
188 {
189 	cleanup();
190 }
191 
192 
193 ATF_TC_WITH_CLEANUP(unlink_failure);
194 ATF_TC_HEAD(unlink_failure, tc)
195 {
196 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
197 					"unlink(2) call");
198 }
199 
200 ATF_TC_BODY(unlink_failure, tc)
201 {
202 	FILE *pipefd = setup(fds, "fd");
203 	/* Failure reason: file does not exist */
204 	ATF_REQUIRE_EQ(-1, unlink(errpath));
205 	check_audit(fds, failurereg, pipefd);
206 }
207 
208 ATF_TC_CLEANUP(unlink_failure, tc)
209 {
210 	cleanup();
211 }
212 
213 
214 ATF_TC_WITH_CLEANUP(unlinkat_success);
215 ATF_TC_HEAD(unlinkat_success, tc)
216 {
217 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
218 					"unlinkat(2) call");
219 }
220 
221 ATF_TC_BODY(unlinkat_success, tc)
222 {
223 	ATF_REQUIRE_EQ(0, mkdir(path, mode));
224 	FILE *pipefd = setup(fds, "fd");
225 	ATF_REQUIRE_EQ(0, unlinkat(AT_FDCWD, path, AT_REMOVEDIR));
226 	check_audit(fds, successreg, pipefd);
227 }
228 
229 ATF_TC_CLEANUP(unlinkat_success, tc)
230 {
231 	cleanup();
232 }
233 
234 
235 ATF_TC_WITH_CLEANUP(unlinkat_failure);
236 ATF_TC_HEAD(unlinkat_failure, tc)
237 {
238 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
239 					"unlinkat(2) call");
240 }
241 
242 ATF_TC_BODY(unlinkat_failure, tc)
243 {
244 	FILE *pipefd = setup(fds, "fd");
245 	/* Failure reason: directory does not exist */
246 	ATF_REQUIRE_EQ(-1, unlinkat(AT_FDCWD, errpath, AT_REMOVEDIR));
247 	check_audit(fds, failurereg, pipefd);
248 }
249 
250 ATF_TC_CLEANUP(unlinkat_failure, tc)
251 {
252 	cleanup();
253 }
254 
255 
256 ATF_TP_ADD_TCS(tp)
257 {
258 	ATF_TP_ADD_TC(tp, rmdir_success);
259 	ATF_TP_ADD_TC(tp, rmdir_failure);
260 
261 	ATF_TP_ADD_TC(tp, rename_success);
262 	ATF_TP_ADD_TC(tp, rename_failure);
263 	ATF_TP_ADD_TC(tp, renameat_success);
264 	ATF_TP_ADD_TC(tp, renameat_failure);
265 
266 	ATF_TP_ADD_TC(tp, unlink_success);
267 	ATF_TP_ADD_TC(tp, unlink_failure);
268 	ATF_TP_ADD_TC(tp, unlinkat_success);
269 	ATF_TP_ADD_TC(tp, unlinkat_failure);
270 
271 	return (atf_no_error());
272 }
273