xref: /freebsd/tests/sys/audit/administrative.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
1 /*-
2  * Copyright (c) 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/param.h>
29 #include <sys/mount.h>
30 #include <sys/time.h>
31 
32 #include <atf-c.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 
36 #include "utils.h"
37 
38 static pid_t pid;
39 static int filedesc;
40 static mode_t mode = 0777;
41 static struct pollfd fds[1];
42 static char adregex[60];
43 static const char *auclass = "ad";
44 static const char *path = "fileforaudit";
45 
46 
47 ATF_TC_WITH_CLEANUP(settimeofday_success);
48 ATF_TC_HEAD(settimeofday_success, tc)
49 {
50 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
51 					"settimeofday(2) call");
52 }
53 
54 ATF_TC_BODY(settimeofday_success, tc)
55 {
56 	pid = getpid();
57 	snprintf(adregex, sizeof(adregex), "settimeofday.*%d.*success", pid);
58 
59 	struct timeval tp;
60 	struct timezone tzp;
61 	ATF_REQUIRE_EQ(0, gettimeofday(&tp, &tzp));
62 
63 	FILE *pipefd = setup(fds, auclass);
64 	/* Setting the same time as obtained by gettimeofday(2) */
65 	ATF_REQUIRE_EQ(0, settimeofday(&tp, &tzp));
66 	check_audit(fds, adregex, pipefd);
67 }
68 
69 ATF_TC_CLEANUP(settimeofday_success, tc)
70 {
71 	cleanup();
72 }
73 
74 
75 ATF_TC_WITH_CLEANUP(settimeofday_failure);
76 ATF_TC_HEAD(settimeofday_failure, tc)
77 {
78 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
79 					"settimeofday(2) call");
80 }
81 
82 ATF_TC_BODY(settimeofday_failure, tc)
83 {
84 	pid = getpid();
85 	snprintf(adregex, sizeof(adregex), "settimeofday.*%d.*failure", pid);
86 
87 	struct timeval tp;
88 	struct timezone tzp;
89 	ATF_REQUIRE_EQ(0, gettimeofday(&tp, &tzp));
90 
91 	FILE *pipefd = setup(fds, auclass);
92 	tp.tv_sec = -1;
93 	/* Failure reason: Invalid value for tp.tv_sec; */
94 	ATF_REQUIRE_EQ(-1, settimeofday(&tp, &tzp));
95 	check_audit(fds, adregex, pipefd);
96 }
97 
98 ATF_TC_CLEANUP(settimeofday_failure, tc)
99 {
100 	cleanup();
101 }
102 
103 
104 ATF_TC_WITH_CLEANUP(adjtime_success);
105 ATF_TC_HEAD(adjtime_success, tc)
106 {
107 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
108 					"adjtime(2) call");
109 }
110 
111 ATF_TC_BODY(adjtime_success, tc)
112 {
113 	pid = getpid();
114 	snprintf(adregex, sizeof(adregex), "adjtime.*%d.*return,success", pid);
115 
116 	FILE *pipefd = setup(fds, auclass);
117 	/* We don't want to change the system time, hence NULL */
118 	ATF_REQUIRE_EQ(0, adjtime(NULL,NULL));
119 	check_audit(fds, adregex, pipefd);
120 }
121 
122 ATF_TC_CLEANUP(adjtime_success, tc)
123 {
124 	cleanup();
125 }
126 
127 
128 ATF_TC_WITH_CLEANUP(adjtime_failure);
129 ATF_TC_HEAD(adjtime_failure, tc)
130 {
131 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
132 					"adjtime(2) call");
133 }
134 
135 ATF_TC_BODY(adjtime_failure, tc)
136 {
137 	pid = getpid();
138 	snprintf(adregex, sizeof(adregex), "adjtime.*%d.*return,failure", pid);
139 
140 	FILE *pipefd = setup(fds, auclass);
141 	ATF_REQUIRE_EQ(-1, adjtime((struct timeval *)(-1), NULL));
142 	check_audit(fds, adregex, pipefd);
143 }
144 
145 ATF_TC_CLEANUP(adjtime_failure, tc)
146 {
147 	cleanup();
148 }
149 
150 
151 
152 ATF_TC_WITH_CLEANUP(nfs_getfh_success);
153 ATF_TC_HEAD(nfs_getfh_success, tc)
154 {
155 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
156 					"getfh(2) call");
157 }
158 
159 ATF_TC_BODY(nfs_getfh_success, tc)
160 {
161 	fhandle_t fhp;
162 	pid = getpid();
163 	snprintf(adregex, sizeof(adregex), "nfs_getfh.*%d.*ret.*success", pid);
164 
165 	/* File needs to exist to call getfh(2) */
166 	ATF_REQUIRE(filedesc = open(path, O_CREAT, mode) != -1);
167 	FILE *pipefd = setup(fds, auclass);
168 	ATF_REQUIRE_EQ(0, getfh(path, &fhp));
169 	check_audit(fds, adregex, pipefd);
170 	close(filedesc);
171 }
172 
173 ATF_TC_CLEANUP(nfs_getfh_success, tc)
174 {
175 	cleanup();
176 }
177 
178 
179 ATF_TC_WITH_CLEANUP(nfs_getfh_failure);
180 ATF_TC_HEAD(nfs_getfh_failure, tc)
181 {
182 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
183 					"getfh(2) call");
184 }
185 
186 ATF_TC_BODY(nfs_getfh_failure, tc)
187 {
188 	pid = getpid();
189 	snprintf(adregex, sizeof(adregex), "nfs_getfh.*%d.*ret.*failure", pid);
190 
191 	FILE *pipefd = setup(fds, auclass);
192 	/* Failure reason: file does not exist */
193 	ATF_REQUIRE_EQ(-1, getfh(path, NULL));
194 	check_audit(fds, adregex, pipefd);
195 }
196 
197 ATF_TC_CLEANUP(nfs_getfh_failure, tc)
198 {
199 	cleanup();
200 }
201 
202 
203 ATF_TP_ADD_TCS(tp)
204 {
205 	ATF_TP_ADD_TC(tp, settimeofday_success);
206 	ATF_TP_ADD_TC(tp, settimeofday_failure);
207 	ATF_TP_ADD_TC(tp, adjtime_success);
208 	ATF_TP_ADD_TC(tp, adjtime_failure);
209 
210 	ATF_TP_ADD_TC(tp, nfs_getfh_success);
211 	ATF_TP_ADD_TC(tp, nfs_getfh_failure);
212 
213 	return (atf_no_error());
214 }
215