2 * Copyright 2018 Aniket Pandey
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
41 static struct pollfd fds[1];
42 static mode_t mode = 0777;
44 static char extregex[80];
45 static struct stat statbuff;
46 static const char *auclass = "cl";
47 static const char *path = "fileforaudit";
48 static const char *errpath = "dirdoesnotexist/fileforaudit";
49 static const char *failurereg = "fileforaudit.*return,failure";
52 ATF_TC_WITH_CLEANUP(munmap_success);
53 ATF_TC_HEAD(munmap_success, tc)
55 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
59 ATF_TC_BODY(munmap_success, tc)
62 snprintf(extregex, sizeof(extregex), "munmap.*%d.*return,success", pid);
64 /* Allocate sample memory, to be removed by munmap(2) */
65 char *addr = mmap(NULL, sizeof(char), PROT_READ , MAP_ANONYMOUS, -1, 0);
66 FILE *pipefd = setup(fds, auclass);
67 ATF_REQUIRE_EQ(0, munmap(addr, sizeof(char)));
68 check_audit(fds, extregex, pipefd);
71 ATF_TC_CLEANUP(munmap_success, tc)
77 ATF_TC_WITH_CLEANUP(munmap_failure);
78 ATF_TC_HEAD(munmap_failure, tc)
80 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
84 ATF_TC_BODY(munmap_failure, tc)
86 const char *regex = "munmap.*return,failure : Invalid argument";
87 FILE *pipefd = setup(fds, auclass);
88 ATF_REQUIRE_EQ(-1, munmap((void *)SIZE_MAX, -1));
89 check_audit(fds, regex, pipefd);
92 ATF_TC_CLEANUP(munmap_failure, tc)
98 ATF_TC_WITH_CLEANUP(close_success);
99 ATF_TC_HEAD(close_success, tc)
101 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
105 ATF_TC_BODY(close_success, tc)
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));
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);
120 ATF_TC_CLEANUP(close_success, tc)
126 ATF_TC_WITH_CLEANUP(close_failure);
127 ATF_TC_HEAD(close_failure, tc)
129 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
133 ATF_TC_BODY(close_failure, tc)
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);
142 ATF_TC_CLEANUP(close_failure, tc)
148 ATF_TC_WITH_CLEANUP(closefrom_success);
149 ATF_TC_HEAD(closefrom_success, tc)
151 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
152 "closefrom(2) call");
155 ATF_TC_BODY(closefrom_success, tc)
157 const char *regex = "closefrom.*return,success";
158 FILE *pipefd = setup(fds, auclass);
159 /* closefrom(2) returns 'void' */
161 check_audit(fds, regex, pipefd);
164 ATF_TC_CLEANUP(closefrom_success, tc)
170 ATF_TC_WITH_CLEANUP(revoke_success);
171 ATF_TC_HEAD(revoke_success, tc)
173 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
177 ATF_TC_BODY(revoke_success, tc)
181 snprintf(extregex, sizeof(extregex), "revoke.*%d.*return,success", pid);
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);
187 FILE *pipefd = setup(fds, auclass);
188 ATF_REQUIRE_EQ(0, revoke(ptyname));
189 check_audit(fds, extregex, pipefd);
193 ATF_TC_CLEANUP(revoke_success, tc)
199 ATF_TC_WITH_CLEANUP(revoke_failure);
200 ATF_TC_HEAD(revoke_failure, tc)
202 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
206 ATF_TC_BODY(revoke_failure, tc)
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);
214 ATF_TC_CLEANUP(revoke_failure, tc)
222 ATF_TP_ADD_TC(tp, munmap_success);
223 ATF_TP_ADD_TC(tp, munmap_failure);
225 ATF_TP_ADD_TC(tp, close_success);
226 ATF_TP_ADD_TC(tp, close_failure);
227 ATF_TP_ADD_TC(tp, closefrom_success);
229 ATF_TP_ADD_TC(tp, revoke_success);
230 ATF_TP_ADD_TC(tp, revoke_failure);
232 return (atf_no_error());