xref: /freebsd/tests/sys/audit/file-close.c (revision e52d92164754cbfff84767d4c6eb3cc93e8c21ae)
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$
26  */
27 
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 
31 #include <atf-c.h>
32 #include <fcntl.h>
33 #include <limits.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include "utils.h"
39 
40 static pid_t pid;
41 static struct pollfd fds[1];
42 static mode_t mode = 0777;
43 static char extregex[80];
44 static struct stat statbuff;
45 static const char *auclass = "cl";
46 static const char *path = "fileforaudit";
47 static const char *errpath = "dirdoesnotexist/fileforaudit";
48 static const char *failurereg = "fileforaudit.*return,failure";
49 
50 
51 ATF_TC_WITH_CLEANUP(munmap_success);
52 ATF_TC_HEAD(munmap_success, tc)
53 {
54 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
55 					"munmap(2) call");
56 }
57 
58 ATF_TC_BODY(munmap_success, tc)
59 {
60 	pid = getpid();
61 	snprintf(extregex, sizeof(extregex), "munmap.*%d.*return,success", pid);
62 
63 	/* Allocate sample memory, to be removed by munmap(2) */
64 	char *addr = mmap(NULL, sizeof(char), PROT_READ , MAP_ANONYMOUS, -1, 0);
65 	FILE *pipefd = setup(fds, auclass);
66 	ATF_REQUIRE_EQ(0, munmap(addr, sizeof(char)));
67 	check_audit(fds, extregex, pipefd);
68 }
69 
70 ATF_TC_CLEANUP(munmap_success, tc)
71 {
72 	cleanup();
73 }
74 
75 
76 ATF_TC_WITH_CLEANUP(munmap_failure);
77 ATF_TC_HEAD(munmap_failure, tc)
78 {
79 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
80 					"munmap(2) call");
81 }
82 
83 ATF_TC_BODY(munmap_failure, tc)
84 {
85 	const char *regex = "munmap.*return,failure : Invalid argument";
86 	FILE *pipefd = setup(fds, auclass);
87 	ATF_REQUIRE_EQ(-1, munmap((void *)SIZE_MAX, -1));
88 	check_audit(fds, regex, pipefd);
89 }
90 
91 ATF_TC_CLEANUP(munmap_failure, tc)
92 {
93 	cleanup();
94 }
95 
96 
97 ATF_TC_WITH_CLEANUP(close_success);
98 ATF_TC_HEAD(close_success, tc)
99 {
100 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
101 					"close(2) call");
102 }
103 
104 ATF_TC_BODY(close_success, tc)
105 {
106 	int filedesc;
107 	/* File needs to exist to call close(2) */
108 	ATF_REQUIRE((filedesc = open(path, O_CREAT | O_RDWR, mode)) != -1);
109 	/* Call stat(2) to store the Inode number of 'path' */
110 	ATF_REQUIRE_EQ(0, stat(path, &statbuff));
111 	FILE *pipefd = setup(fds, auclass);
112 	ATF_REQUIRE_EQ(0, close(filedesc));
113 
114 	/* intmax_t to support all architectures */
115 	snprintf(extregex, sizeof(extregex), "close.*%jd.*return,succes",
116 			(intmax_t)statbuff.st_ino);
117 	check_audit(fds, extregex, pipefd);
118 }
119 
120 ATF_TC_CLEANUP(close_success, tc)
121 {
122 	cleanup();
123 }
124 
125 
126 ATF_TC_WITH_CLEANUP(close_failure);
127 ATF_TC_HEAD(close_failure, tc)
128 {
129 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
130 					"close(2) call");
131 }
132 
133 ATF_TC_BODY(close_failure, tc)
134 {
135 	const char *regex = "close.*return,failure";
136 	FILE *pipefd = setup(fds, auclass);
137 	/* Failure reason: file does not exist */
138 	ATF_REQUIRE_EQ(-1, close(-1));
139 	check_audit(fds, regex, pipefd);
140 }
141 
142 ATF_TC_CLEANUP(close_failure, tc)
143 {
144 	cleanup();
145 }
146 
147 
148 ATF_TC_WITH_CLEANUP(closefrom_success);
149 ATF_TC_HEAD(closefrom_success, tc)
150 {
151 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
152 					"closefrom(2) call");
153 }
154 
155 ATF_TC_BODY(closefrom_success, tc)
156 {
157 	const char *regex = "closefrom.*return,success";
158 	FILE *pipefd = setup(fds, auclass);
159 	/* closefrom(2) returns 'void' */
160 	closefrom(INT_MAX);
161 	check_audit(fds, regex, pipefd);
162 }
163 
164 ATF_TC_CLEANUP(closefrom_success, tc)
165 {
166 	cleanup();
167 }
168 
169 
170 ATF_TC_WITH_CLEANUP(revoke_success);
171 ATF_TC_HEAD(revoke_success, tc)
172 {
173 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
174 					"revoke(2) call");
175 }
176 
177 ATF_TC_BODY(revoke_success, tc)
178 {
179 	int filedesc;
180 	char *ptyname;
181 	pid = getpid();
182 	snprintf(extregex, sizeof(extregex), "revoke.*%d.*return,success", pid);
183 
184 	/* Obtain a pseudo terminal and get the path to slave device */
185 	ATF_REQUIRE((filedesc = posix_openpt(O_RDWR | O_NOCTTY)) != -1);
186 	ATF_REQUIRE((ptyname = ptsname(filedesc)) != NULL);
187 
188 	FILE *pipefd = setup(fds, auclass);
189 	ATF_REQUIRE_EQ(0, revoke(ptyname));
190 	check_audit(fds, extregex, pipefd);
191 
192 	/* Close the file descriptor to pseudo terminal */
193 	ATF_REQUIRE_EQ(0, close(filedesc));
194 }
195 
196 ATF_TC_CLEANUP(revoke_success, tc)
197 {
198 	cleanup();
199 }
200 
201 
202 ATF_TC_WITH_CLEANUP(revoke_failure);
203 ATF_TC_HEAD(revoke_failure, tc)
204 {
205 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
206 					"revoke(2) call");
207 }
208 
209 ATF_TC_BODY(revoke_failure, tc)
210 {
211 	FILE *pipefd = setup(fds, auclass);
212 	/* Failure reason: file does not exist */
213 	ATF_REQUIRE_EQ(-1, revoke(errpath));
214 	check_audit(fds, failurereg, pipefd);
215 }
216 
217 ATF_TC_CLEANUP(revoke_failure, tc)
218 {
219 	cleanup();
220 }
221 
222 
223 ATF_TP_ADD_TCS(tp)
224 {
225 	ATF_TP_ADD_TC(tp, munmap_success);
226 	ATF_TP_ADD_TC(tp, munmap_failure);
227 
228 	ATF_TP_ADD_TC(tp, close_success);
229 	ATF_TP_ADD_TC(tp, close_failure);
230 	ATF_TP_ADD_TC(tp, closefrom_success);
231 
232 	ATF_TP_ADD_TC(tp, revoke_success);
233 	ATF_TP_ADD_TC(tp, revoke_failure);
234 
235 	return (atf_no_error());
236 }
237