1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/*-
* Copyright 2018 Aniket Pandey
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/extattr.h>
#include <sys/ioctl.h>
#include <bsm/libbsm.h>
#include <bsm/auditd_lib.h>
#include <security/audit/audit_ioctl.h>
#include <atf-c.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "utils.h"
/*
* Checks the presence of "auditregex" in auditpipe(4) after the
* corresponding system call has been triggered.
*/
static bool
get_records(const char *auditregex, FILE *pipestream)
{
uint8_t *buff;
tokenstr_t token;
ssize_t size = 1024;
char membuff[size];
char del[] = ",";
int reclen, bytes = 0;
FILE *memstream;
/*
* Open a stream on 'membuff' (address to memory buffer) for storing
* the audit records in the default mode.'reclen' is the length of the
* available records from auditpipe which is passed to the functions
* au_fetch_tok(3) and au_print_flags_tok(3) for further use.
*/
ATF_REQUIRE((memstream = fmemopen(membuff, size, "w")) != NULL);
ATF_REQUIRE((reclen = au_read_rec(pipestream, &buff)) != -1);
/*
* Iterate through each BSM token, extracting the bits that are
* required to start processing the token sequences.
*/
while (bytes < reclen) {
if (au_fetch_tok(&token, buff + bytes, reclen - bytes) == -1) {
perror("au_read_rec");
atf_tc_fail("Incomplete Audit Record");
}
/* Print the tokens as they are obtained, in the default form */
au_print_flags_tok(memstream, &token, del, AU_OFLAG_NONE);
fputc(',', memstream);
bytes += token.len;
}
free(buff);
ATF_REQUIRE_EQ(0, fclose(memstream));
return (atf_utils_grep_string("%s", membuff, auditregex));
}
/*
* Override the system-wide audit mask settings in /etc/security/audit_control
* and set the auditpipe's maximum allowed queue length limit
*/
static void
set_preselect_mode(int filedesc, au_mask_t *fmask)
{
int qlimit_max;
int fmode = AUDITPIPE_PRESELECT_MODE_LOCAL;
/* Set local preselection mode for auditing */
if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_MODE, &fmode) < 0)
atf_tc_fail("Preselection mode: %s", strerror(errno));
/* Set local preselection flag corresponding to the audit_event */
if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_FLAGS, fmask) < 0)
atf_tc_fail("Preselection flag: %s", strerror(errno));
/* Set local preselection flag for non-attributable audit_events */
if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_NAFLAGS, fmask) < 0)
atf_tc_fail("Preselection naflag: %s", strerror(errno));
/* Query the maximum possible queue length limit for auditpipe */
if (ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlimit_max) < 0)
atf_tc_fail("Query max-limit: %s", strerror(errno));
/* Set the queue length limit as obtained from previous step */
if (ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &qlimit_max) < 0)
atf_tc_fail("Set max-qlimit: %s", strerror(errno));
/* This removes any outstanding record on the auditpipe */
if (ioctl(filedesc, AUDITPIPE_FLUSH) < 0)
atf_tc_fail("Auditpipe flush: %s", strerror(errno));
}
/*
* Get the corresponding audit_mask for class-name "name" then set the
* success and failure bits for fmask to be used as the ioctl argument
*/
static au_mask_t
get_audit_mask(const char *name)
{
au_mask_t fmask;
au_class_ent_t *class;
ATF_REQUIRE((class = getauclassnam(name)) != NULL);
fmask.am_success = class->ac_class;
fmask.am_failure = class->ac_class;
return (fmask);
}
/*
* Loop until the auditpipe returns something, check if it is what
* we want, else repeat the procedure until ppoll(2) times out.
*/
static void
check_auditpipe(struct pollfd fd[], const char *auditregex, FILE *pipestream)
{
struct timespec currtime, endtime, timeout;
/* Set the expire time for poll(2) while waiting for syscall audit */
ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &endtime));
/* Set limit to 30 seconds total and ~10s without an event. */
endtime.tv_sec += 30;
for (;;) {
/* Update the time left for auditpipe to return any event */
ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currtime));
timespecsub(&endtime, &currtime, &timeout);
timeout.tv_sec = MIN(timeout.tv_sec, 9);
if (timeout.tv_sec < 0) {
atf_tc_fail("%s not found in auditpipe within the "
"time limit", auditregex);
}
switch (ppoll(fd, 1, &timeout, NULL)) {
/* ppoll(2) returns, check if it's what we want */
case 1:
if (fd[0].revents & POLLIN) {
if (get_records(auditregex, pipestream))
return;
} else {
atf_tc_fail("Auditpipe returned an "
"unknown event %#x", fd[0].revents);
}
break;
/* poll(2) timed out */
case 0:
atf_tc_fail("%s not found in auditpipe within the "
"time limit", auditregex);
break;
/* poll(2) standard error */
case -1:
atf_tc_fail("Poll: %s", strerror(errno));
break;
default:
atf_tc_fail("Poll returned too many file descriptors");
}
}
}
/*
* Wrapper functions around static "check_auditpipe"
*/
static void
check_audit_startup(struct pollfd fd[], const char *auditrgx, FILE *pipestream){
check_auditpipe(fd, auditrgx, pipestream);
}
void
check_audit(struct pollfd fd[], const char *auditrgx, FILE *pipestream) {
check_auditpipe(fd, auditrgx, pipestream);
/* Teardown: /dev/auditpipe's instance opened for this test-suite */
ATF_REQUIRE_EQ(0, fclose(pipestream));
}
void
skip_if_extattr_not_supported(const char *path)
{
ssize_t result;
/*
* Some file systems (e.g. tmpfs) do not support extattr, so we need
* skip tests that use extattrs. To detect this we can check whether
* the extattr_list_file returns EOPNOTSUPP.
*/
result = extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0);
if (result == -1 && errno == EOPNOTSUPP) {
atf_tc_skip("File system does not support extattrs.");
}
}
static bool
is_auditd_running(void)
{
int trigger;
int err;
/*
* AUDIT_TRIGGER_INITIALIZE is a no-op message on FreeBSD and can
* therefore be used to check whether auditd has already been started.
* This is significantly cheaper than running `service auditd onestatus`
* for each test case. It is also slightly less racy since it will only
* return true once auditd() has opened the trigger file rather than
* just when the pidfile has been created.
*/
trigger = AUDIT_TRIGGER_INITIALIZE;
err = auditon(A_SENDTRIGGER, &trigger, sizeof(trigger));
if (err == 0) {
fprintf(stderr, "auditd(8) is running.\n");
return (true);
} else {
/*
* A_SENDTRIGGER returns ENODEV if auditd isn't listening,
* all other error codes indicate a fatal error.
*/
ATF_REQUIRE_MSG(errno == ENODEV,
"Unexpected error from auditon(2): %s", strerror(errno));
return (false);
}
}
FILE *
setup(struct pollfd fd[], const char *name)
{
au_mask_t fmask, nomask;
FILE *pipestream;
fmask = get_audit_mask(name);
nomask = get_audit_mask("no");
ATF_REQUIRE((fd[0].fd = open("/dev/auditpipe", O_RDONLY)) != -1);
ATF_REQUIRE((pipestream = fdopen(fd[0].fd, "r")) != NULL);
fd[0].events = POLLIN;
/*
* Disable stream buffering for read operations from /dev/auditpipe.
* Otherwise it is possible that fread(3), called via au_read_rec(3),
* can store buffered data in user-space unbeknown to ppoll(2), which
* as a result, reports that /dev/auditpipe is empty.
*/
ATF_REQUIRE_EQ(0, setvbuf(pipestream, NULL, _IONBF, 0));
/* Set local preselection audit_class as "no" for audit startup */
set_preselect_mode(fd[0].fd, &nomask);
if (!is_auditd_running()) {
fprintf(stderr, "Running audit_quick_start() for testing... ");
/*
* Previously, this test started auditd using
* `service auditd onestart`. However, there is a race condition
* there since service can return before auditd(8) has
* fully started (once the daemon parent process has forked)
* and this can cause check_audit_startup() to fail sometimes.
*
* In the CheriBSD CI this caused the first test executed by
* kyua (administrative:acct_failure) to fail every time, but
* subsequent ones would almost always succeed.
*
* To avoid this problem (and as a nice side-effect this speeds
* up the test quite a bit), we register this process as a
* "fake" auditd(8) using the audit_quick_start() function from
* libauditd.
*/
atf_utils_create_file("started_fake_auditd", "yes\n");
ATF_REQUIRE(atf_utils_file_exists("started_fake_auditd"));
ATF_REQUIRE_EQ_MSG(0, audit_quick_start(),
"Failed to start fake auditd: %m");
fprintf(stderr, "done.\n");
/* audit_quick_start() should log an audit start event. */
check_audit_startup(fd, "audit startup", pipestream);
/*
* If we exit cleanly shutdown audit_quick_start(), if not
* cleanup() will take care of it.
* This is not required, but makes it easier to run individual
* tests outside of kyua.
*/
atexit(cleanup);
}
/* Set local preselection parameters specific to "name" audit_class */
set_preselect_mode(fd[0].fd, &fmask);
return (pipestream);
}
void
cleanup(void)
{
if (atf_utils_file_exists("started_fake_auditd")) {
fprintf(stderr, "Running audit_quick_stop()... ");
if (audit_quick_stop() != 0) {
fprintf(stderr, "Failed to stop fake auditd: %m\n");
abort();
}
fprintf(stderr, "done.\n");
unlink("started_fake_auditd");
}
}
|