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