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